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 <util.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
127 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
129 11b20872 2019-11-08 stsp static const struct got_error *
130 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
131 11b20872 2019-11-08 stsp int idx, short color)
133 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
134 11b20872 2019-11-08 stsp struct tog_color *tc;
135 11b20872 2019-11-08 stsp int regerr = 0;
137 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
138 11b20872 2019-11-08 stsp return NULL;
140 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
142 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
143 11b20872 2019-11-08 stsp if (tc == NULL)
144 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
145 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
146 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 11b20872 2019-11-08 stsp if (regerr) {
148 11b20872 2019-11-08 stsp static char regerr_msg[512];
149 11b20872 2019-11-08 stsp static char err_msg[512];
150 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
151 11b20872 2019-11-08 stsp sizeof(regerr_msg));
152 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 11b20872 2019-11-08 stsp regerr_msg);
154 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
156 11b20872 2019-11-08 stsp return err;
158 11b20872 2019-11-08 stsp tc->colorpair = idx;
159 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 11b20872 2019-11-08 stsp return NULL;
163 11b20872 2019-11-08 stsp static void
164 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
166 11b20872 2019-11-08 stsp struct tog_color *tc;
168 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
169 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
170 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
171 11b20872 2019-11-08 stsp regfree(&tc->regex);
176 11b20872 2019-11-08 stsp struct tog_color *
177 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
179 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
181 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
182 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
186 11b20872 2019-11-08 stsp return NULL;
190 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 11b20872 2019-11-08 stsp return COLOR_CYAN;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 11b20872 2019-11-08 stsp return COLOR_YELLOW;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 11b20872 2019-11-08 stsp return COLOR_GREEN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 91b8c405 2020-01-25 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_GREEN;
208 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 11b20872 2019-11-08 stsp return COLOR_GREEN;
210 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 11b20872 2019-11-08 stsp return COLOR_CYAN;
212 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 11b20872 2019-11-08 stsp return COLOR_YELLOW;
214 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 6458efa5 2020-11-24 stsp return COLOR_GREEN;
216 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
218 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
225 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
227 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
229 11b20872 2019-11-08 stsp if (val == NULL)
230 11b20872 2019-11-08 stsp return default_color_value(envvar);
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
233 11b20872 2019-11-08 stsp return COLOR_BLACK;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
235 11b20872 2019-11-08 stsp return COLOR_RED;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
237 11b20872 2019-11-08 stsp return COLOR_GREEN;
238 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
239 11b20872 2019-11-08 stsp return COLOR_YELLOW;
240 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
241 11b20872 2019-11-08 stsp return COLOR_BLUE;
242 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
243 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
244 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
245 11b20872 2019-11-08 stsp return COLOR_CYAN;
246 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
247 11b20872 2019-11-08 stsp return COLOR_WHITE;
248 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
251 11b20872 2019-11-08 stsp return default_color_value(envvar);
255 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
256 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
257 3dbaef42 2020-11-24 stsp const char *label1, *label2;
259 15a087fe 2019-02-21 stsp int first_displayed_line;
260 15a087fe 2019-02-21 stsp int last_displayed_line;
262 15a087fe 2019-02-21 stsp int diff_context;
263 3dbaef42 2020-11-24 stsp int ignore_whitespace;
264 64453f7e 2020-11-21 stsp int force_text_diff;
265 15a087fe 2019-02-21 stsp struct got_repository *repo;
266 78756c87 2020-11-24 stsp struct got_reflist_head refs;
267 bddb1296 2019-11-08 stsp struct tog_colors colors;
268 fe621944 2020-11-10 stsp size_t nlines;
269 f44b1f58 2020-02-02 tracey off_t *line_offsets;
270 f44b1f58 2020-02-02 tracey int matched_line;
271 f44b1f58 2020-02-02 tracey int selected_line;
273 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
274 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
277 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
280 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
281 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
282 1a76625f 2018-10-22 stsp int commits_needed;
283 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
284 1a76625f 2018-10-22 stsp struct commit_queue *commits;
285 1a76625f 2018-10-22 stsp const char *in_repo_path;
286 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
287 1a76625f 2018-10-22 stsp struct got_repository *repo;
288 1a76625f 2018-10-22 stsp int log_complete;
289 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
290 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
291 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
292 13add988 2019-10-15 stsp int *searching;
293 13add988 2019-10-15 stsp int *search_next_done;
294 13add988 2019-10-15 stsp regex_t *regex;
297 1a76625f 2018-10-22 stsp struct tog_log_view_state {
298 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
299 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
300 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
301 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
302 b01e7d3b 2018-08-04 stsp int selected;
303 b01e7d3b 2018-08-04 stsp char *in_repo_path;
304 d01904d4 2019-06-25 stsp const char *head_ref_name;
305 b672a97a 2020-01-27 stsp int log_branches;
306 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
307 78756c87 2020-11-24 stsp struct got_reflist_head refs;
308 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
309 1a76625f 2018-10-22 stsp sig_atomic_t quit;
310 1a76625f 2018-10-22 stsp pthread_t thread;
311 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
312 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
313 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
314 11b20872 2019-11-08 stsp struct tog_colors colors;
317 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
318 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
319 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
321 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
322 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
323 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
324 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
325 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
326 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
327 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
328 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
329 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
330 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
332 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
333 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
334 e9424729 2018-08-04 stsp int nlines;
336 e9424729 2018-08-04 stsp struct tog_view *view;
337 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
341 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
342 e9424729 2018-08-04 stsp const char *path;
343 e9424729 2018-08-04 stsp struct got_repository *repo;
344 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
345 e9424729 2018-08-04 stsp int *complete;
346 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
347 fc06ba56 2019-08-22 stsp void *cancel_arg;
350 e9424729 2018-08-04 stsp struct tog_blame {
352 be659d10 2020-11-18 stsp off_t filesize;
353 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
354 6fcac457 2018-11-19 stsp int nlines;
355 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
356 e9424729 2018-08-04 stsp pthread_t thread;
357 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
358 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
359 e9424729 2018-08-04 stsp const char *path;
362 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
363 7cbe629d 2018-08-04 stsp int first_displayed_line;
364 7cbe629d 2018-08-04 stsp int last_displayed_line;
365 7cbe629d 2018-08-04 stsp int selected_line;
366 7cbe629d 2018-08-04 stsp int blame_complete;
369 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
370 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
371 e5a0f69f 2018-08-18 stsp char *path;
372 7cbe629d 2018-08-04 stsp struct got_repository *repo;
373 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
374 e9424729 2018-08-04 stsp struct tog_blame blame;
375 6c4c42e0 2019-06-24 stsp int matched_line;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
379 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
380 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
381 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
382 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
383 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
384 ad80ab7b 2018-08-04 stsp int selected;
387 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
390 ad80ab7b 2018-08-04 stsp char *tree_label;
391 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
392 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
393 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
394 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
395 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
396 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
397 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
398 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
399 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
400 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
401 6458efa5 2020-11-24 stsp struct tog_colors colors;
404 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
405 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
406 6458efa5 2020-11-24 stsp struct got_reference *ref;
410 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
413 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
414 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
418 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
419 6458efa5 2020-11-24 stsp struct got_repository *repo;
420 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
421 bddb1296 2019-11-08 stsp struct tog_colors colors;
425 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
427 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
428 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
429 669b5ffa 2018-10-07 stsp * there is enough screen estate.
431 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
432 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
434 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
435 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
436 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
438 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
439 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
441 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
443 cc3c9aac 2018-08-01 stsp struct tog_view {
444 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
445 26ed57b2 2018-05-19 stsp WINDOW *window;
446 26ed57b2 2018-05-19 stsp PANEL *panel;
447 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
448 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
449 1004088d 2018-09-29 stsp int focussed;
450 669b5ffa 2018-10-07 stsp struct tog_view *parent;
451 669b5ffa 2018-10-07 stsp struct tog_view *child;
452 669b5ffa 2018-10-07 stsp int child_focussed;
454 5dc9f4bc 2018-08-04 stsp /* type-specific state */
455 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
457 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
458 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
459 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
460 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
461 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
464 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
465 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
466 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
467 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
469 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
470 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
471 60493ae3 2019-06-20 stsp int searching;
472 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
473 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
474 60493ae3 2019-06-20 stsp int search_next_done;
475 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
476 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
477 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
478 1803e47f 2019-06-22 stsp regex_t regex;
479 41605754 2020-11-12 stsp regmatch_t regmatch;
482 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
483 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
484 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
485 78756c87 2020-11-24 stsp struct got_repository *);
486 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
487 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
488 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
489 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
490 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
491 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
493 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
494 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
495 78756c87 2020-11-24 stsp const char *, const char *, int);
496 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
497 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
498 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
499 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
500 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
501 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
503 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
504 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
505 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
506 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
507 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
508 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
509 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
510 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
512 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
513 78756c87 2020-11-24 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
515 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
516 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
517 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
518 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
519 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
521 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
522 6458efa5 2020-11-24 stsp struct got_repository *);
523 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
524 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
525 6458efa5 2020-11-24 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
526 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
527 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
528 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
530 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
531 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
532 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
534 25791caa 2018-10-24 stsp static void
535 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
537 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
540 83baff54 2019-08-12 stsp static void
541 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
543 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
546 61266923 2020-01-14 stsp static void
547 61266923 2020-01-14 stsp tog_sigcont(int signo)
549 61266923 2020-01-14 stsp tog_sigcont_received = 1;
552 e5a0f69f 2018-08-18 stsp static const struct got_error *
553 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
555 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
557 669b5ffa 2018-10-07 stsp if (view->child) {
558 669b5ffa 2018-10-07 stsp view_close(view->child);
559 669b5ffa 2018-10-07 stsp view->child = NULL;
561 e5a0f69f 2018-08-18 stsp if (view->close)
562 e5a0f69f 2018-08-18 stsp err = view->close(view);
563 ea5e7bb5 2018-08-01 stsp if (view->panel)
564 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
565 ea5e7bb5 2018-08-01 stsp if (view->window)
566 ea5e7bb5 2018-08-01 stsp delwin(view->window);
567 ea5e7bb5 2018-08-01 stsp free(view);
568 e5a0f69f 2018-08-18 stsp return err;
571 ea5e7bb5 2018-08-01 stsp static struct tog_view *
572 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
573 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
575 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
577 ea5e7bb5 2018-08-01 stsp if (view == NULL)
578 ea5e7bb5 2018-08-01 stsp return NULL;
580 d6b05b5b 2018-08-04 stsp view->type = type;
581 f7d12f7e 2018-08-01 stsp view->lines = LINES;
582 f7d12f7e 2018-08-01 stsp view->cols = COLS;
583 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
584 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
585 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
586 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
587 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
588 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
589 96a765a8 2018-08-04 stsp view_close(view);
590 ea5e7bb5 2018-08-01 stsp return NULL;
592 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
593 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
594 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
595 96a765a8 2018-08-04 stsp view_close(view);
596 ea5e7bb5 2018-08-01 stsp return NULL;
599 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
600 ea5e7bb5 2018-08-01 stsp return view;
604 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
606 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
608 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
611 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
613 5c60c32a 2018-10-18 stsp static const struct got_error *
614 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
616 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
618 5c60c32a 2018-10-18 stsp view->begin_y = 0;
619 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
620 5c60c32a 2018-10-18 stsp view->nlines = LINES;
621 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
622 5c60c32a 2018-10-18 stsp view->lines = LINES;
623 5c60c32a 2018-10-18 stsp view->cols = COLS;
624 5c60c32a 2018-10-18 stsp err = view_resize(view);
626 5c60c32a 2018-10-18 stsp return err;
628 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
631 5c60c32a 2018-10-18 stsp return NULL;
634 5c60c32a 2018-10-18 stsp static const struct got_error *
635 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
637 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
639 5c60c32a 2018-10-18 stsp view->begin_x = 0;
640 5c60c32a 2018-10-18 stsp view->begin_y = 0;
641 5c60c32a 2018-10-18 stsp view->nlines = LINES;
642 5c60c32a 2018-10-18 stsp view->ncols = COLS;
643 5c60c32a 2018-10-18 stsp view->lines = LINES;
644 5c60c32a 2018-10-18 stsp view->cols = COLS;
645 5c60c32a 2018-10-18 stsp err = view_resize(view);
647 5c60c32a 2018-10-18 stsp return err;
649 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
652 5c60c32a 2018-10-18 stsp return NULL;
656 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
658 5c60c32a 2018-10-18 stsp return view->parent == NULL;
661 4d8c2215 2018-08-19 stsp static const struct got_error *
662 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
664 f7d12f7e 2018-08-01 stsp int nlines, ncols;
666 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
667 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
669 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
671 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
672 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
674 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
676 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
677 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
678 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
679 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
680 25791caa 2018-10-24 stsp wclear(view->window);
682 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
683 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
684 0cf4efb1 2018-09-29 stsp view->lines = LINES;
685 0cf4efb1 2018-09-29 stsp view->cols = COLS;
687 6e3e5d9c 2018-10-18 stsp if (view->child) {
688 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
689 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
690 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
691 5c60c32a 2018-10-18 stsp if (view->child->focussed)
692 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
694 5c60c32a 2018-10-18 stsp show_panel(view->panel);
696 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
697 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
701 5c60c32a 2018-10-18 stsp return NULL;
704 669b5ffa 2018-10-07 stsp static const struct got_error *
705 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
707 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
709 669b5ffa 2018-10-07 stsp if (view->child == NULL)
710 669b5ffa 2018-10-07 stsp return NULL;
712 669b5ffa 2018-10-07 stsp err = view_close(view->child);
713 669b5ffa 2018-10-07 stsp view->child = NULL;
714 669b5ffa 2018-10-07 stsp return err;
717 669b5ffa 2018-10-07 stsp static const struct got_error *
718 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
720 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
722 669b5ffa 2018-10-07 stsp view->child = child;
723 669b5ffa 2018-10-07 stsp child->parent = view;
724 669b5ffa 2018-10-07 stsp return err;
728 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
730 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
733 34bc9ec9 2019-02-22 stsp static void
734 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
736 25791caa 2018-10-24 stsp int cols, lines;
737 25791caa 2018-10-24 stsp struct winsize size;
739 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 25791caa 2018-10-24 stsp cols = 80; /* Default */
741 25791caa 2018-10-24 stsp lines = 24;
743 25791caa 2018-10-24 stsp cols = size.ws_col;
744 25791caa 2018-10-24 stsp lines = size.ws_row;
746 25791caa 2018-10-24 stsp resize_term(lines, cols);
749 2b49a8ae 2019-06-22 stsp static const struct got_error *
750 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
752 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
753 2b49a8ae 2019-06-22 stsp char pattern[1024];
756 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
757 2b49a8ae 2019-06-22 stsp return NULL;
759 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
762 2b49a8ae 2019-06-22 stsp nocbreak();
764 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
767 2b49a8ae 2019-06-22 stsp if (ret == ERR)
768 2b49a8ae 2019-06-22 stsp return NULL;
770 2b49a8ae 2019-06-22 stsp if (view->searching) {
771 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
772 2b49a8ae 2019-06-22 stsp view->searching = 0;
775 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 7c32bd05 2019-06-22 stsp err = view->search_start(view);
778 7c32bd05 2019-06-22 stsp regfree(&view->regex);
779 7c32bd05 2019-06-22 stsp return err;
781 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
782 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
783 2b49a8ae 2019-06-22 stsp view->search_next(view);
786 2b49a8ae 2019-06-22 stsp return NULL;
789 0cf4efb1 2018-09-29 stsp static const struct got_error *
790 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
791 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
792 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
794 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
795 669b5ffa 2018-10-07 stsp struct tog_view *v;
796 1a76625f 2018-10-22 stsp int ch, errcode;
798 e5a0f69f 2018-08-18 stsp *new = NULL;
799 e5a0f69f 2018-08-18 stsp *dead = NULL;
800 0cf4efb1 2018-09-29 stsp *focus = NULL;
802 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
803 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
808 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
809 82954512 2020-02-03 stsp if (errcode)
810 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
811 82954512 2020-02-03 stsp "pthread_mutex_unlock");
812 82954512 2020-02-03 stsp pthread_yield();
813 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
814 82954512 2020-02-03 stsp if (errcode)
815 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
816 82954512 2020-02-03 stsp "pthread_mutex_lock");
817 60493ae3 2019-06-20 stsp view->search_next(view);
818 60493ae3 2019-06-20 stsp return NULL;
821 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
822 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
823 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
824 1a76625f 2018-10-22 stsp if (errcode)
825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
827 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
828 1a76625f 2018-10-22 stsp if (errcode)
829 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
830 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
832 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
833 25791caa 2018-10-24 stsp tog_resizeterm();
834 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
835 61266923 2020-01-14 stsp tog_sigcont_received = 0;
836 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
837 25791caa 2018-10-24 stsp err = view_resize(v);
839 25791caa 2018-10-24 stsp return err;
840 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
842 25791caa 2018-10-24 stsp return err;
846 e5a0f69f 2018-08-18 stsp switch (ch) {
850 1e37a5c2 2019-05-12 jcs if (view->child) {
851 1e37a5c2 2019-05-12 jcs *focus = view->child;
852 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
853 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
854 1e37a5c2 2019-05-12 jcs *focus = view->parent;
855 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
859 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
860 1e37a5c2 2019-05-12 jcs *dead = view;
866 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
867 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
869 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
870 669b5ffa 2018-10-07 stsp *focus = view->child;
871 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
872 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
874 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
877 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
878 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
880 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
881 1e37a5c2 2019-05-12 jcs *focus = view;
882 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
883 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
885 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
889 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
890 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
893 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
896 60493ae3 2019-06-20 stsp if (view->search_start)
897 2b49a8ae 2019-06-22 stsp view_search_start(view);
899 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
903 8f4ed634 2020-03-26 stsp if (view->search_next) {
904 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
905 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 60493ae3 2019-06-20 stsp view->search_next_done = 0;
907 60493ae3 2019-06-20 stsp view->search_next(view);
909 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
912 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
916 e5a0f69f 2018-08-18 stsp return err;
920 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
922 0cf4efb1 2018-09-29 stsp PANEL *panel;
923 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
925 669b5ffa 2018-10-07 stsp if (view->parent)
926 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
928 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
929 0cf4efb1 2018-09-29 stsp if (panel == NULL)
932 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
933 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
940 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
941 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
943 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
945 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
948 669b5ffa 2018-10-07 stsp return view->focussed;
951 bcbd79e2 2018-08-19 stsp static const struct got_error *
952 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
954 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
955 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
956 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 fd823528 2018-10-22 stsp int fast_refresh = 10;
958 1a76625f 2018-10-22 stsp int done = 0, errcode;
960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
961 1a76625f 2018-10-22 stsp if (errcode)
962 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
964 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
965 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
967 a81bf10d 2018-09-29 stsp main_view = view;
968 1004088d 2018-09-29 stsp view->focussed = 1;
969 878940b7 2018-09-29 stsp err = view->show(view);
971 0cf4efb1 2018-09-29 stsp return err;
972 0cf4efb1 2018-09-29 stsp update_panels();
973 0cf4efb1 2018-09-29 stsp doupdate();
974 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
976 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
977 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
979 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
980 e4197bf9 2018-08-18 stsp view, &views);
983 e5a0f69f 2018-08-18 stsp if (dead_view) {
984 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
986 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
987 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
988 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
989 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
990 669b5ffa 2018-10-07 stsp prev = view->parent;
992 669b5ffa 2018-10-07 stsp if (dead_view->parent)
993 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
995 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
997 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
998 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
1001 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
1002 0cf4efb1 2018-09-29 stsp if (focus_view)
1003 0cf4efb1 2018-09-29 stsp view = focus_view;
1004 669b5ffa 2018-10-07 stsp else if (prev)
1005 669b5ffa 2018-10-07 stsp view = prev;
1006 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
1007 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
1008 0cf4efb1 2018-09-29 stsp tog_view_list_head);
1010 0cf4efb1 2018-09-29 stsp view = NULL;
1011 669b5ffa 2018-10-07 stsp if (view) {
1012 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
1013 669b5ffa 2018-10-07 stsp focus_view = view->child;
1015 669b5ffa 2018-10-07 stsp focus_view = view;
1019 bcbd79e2 2018-08-19 stsp if (new_view) {
1020 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1021 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1022 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1025 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1026 86c66b02 2018-10-18 stsp err = view_close(v);
1028 86c66b02 2018-10-18 stsp goto done;
1031 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1032 fed7eaa8 2018-10-24 stsp view = new_view;
1033 878940b7 2018-09-29 stsp if (focus_view == NULL)
1034 878940b7 2018-09-29 stsp focus_view = new_view;
1036 0cf4efb1 2018-09-29 stsp if (focus_view) {
1037 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
1039 0cf4efb1 2018-09-29 stsp view->focussed = 0;
1040 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
1041 0cf4efb1 2018-09-29 stsp view = focus_view;
1042 878940b7 2018-09-29 stsp if (new_view)
1043 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1044 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1045 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1047 669b5ffa 2018-10-07 stsp if (view) {
1048 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1049 758194b5 2018-10-24 stsp view->focussed = 1;
1050 758194b5 2018-10-24 stsp show_panel(view->panel);
1051 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1052 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1053 1a76625f 2018-10-22 stsp focus_view = view;
1055 669b5ffa 2018-10-07 stsp if (view->parent) {
1056 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1058 1a76625f 2018-10-22 stsp goto done;
1060 669b5ffa 2018-10-07 stsp err = view->show(view);
1062 1a76625f 2018-10-22 stsp goto done;
1063 669b5ffa 2018-10-07 stsp if (view->child) {
1064 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1066 1a76625f 2018-10-22 stsp goto done;
1068 1a76625f 2018-10-22 stsp update_panels();
1069 1a76625f 2018-10-22 stsp doupdate();
1073 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1074 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1075 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1076 e5a0f69f 2018-08-18 stsp view_close(view);
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1080 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1081 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1083 e5a0f69f 2018-08-18 stsp return err;
1086 4ed7e80c 2018-05-20 stsp __dead static void
1087 9f7d7167 2018-04-29 stsp usage_log(void)
1090 c70c5802 2018-08-01 stsp fprintf(stderr,
1091 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 9f7d7167 2018-04-29 stsp getprogname());
1096 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1097 80ddbec8 2018-04-29 stsp static const struct got_error *
1098 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 00dfcb92 2018-06-11 stsp char *vis = NULL;
1101 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1103 963b370f 2018-05-20 stsp *ws = NULL;
1104 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1105 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1106 00dfcb92 2018-06-11 stsp int vislen;
1107 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1108 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1110 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1111 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1113 00dfcb92 2018-06-11 stsp return err;
1114 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1115 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1116 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1117 a7f50699 2018-06-11 stsp goto done;
1121 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1122 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1124 a7f50699 2018-06-11 stsp goto done;
1127 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1130 00dfcb92 2018-06-11 stsp free(vis);
1131 963b370f 2018-05-20 stsp if (err) {
1132 963b370f 2018-05-20 stsp free(*ws);
1133 963b370f 2018-05-20 stsp *ws = NULL;
1134 963b370f 2018-05-20 stsp *wlen = 0;
1136 963b370f 2018-05-20 stsp return err;
1139 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 963b370f 2018-05-20 stsp static const struct got_error *
1141 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 27a741e5 2019-09-11 stsp int col_tab_align)
1144 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1145 963b370f 2018-05-20 stsp int cols = 0;
1146 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1147 963b370f 2018-05-20 stsp size_t wlen;
1150 963b370f 2018-05-20 stsp *wlinep = NULL;
1151 b700b5d6 2018-07-10 stsp *widthp = 0;
1153 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1155 963b370f 2018-05-20 stsp return err;
1158 27a741e5 2019-09-11 stsp while (i < wlen) {
1159 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1161 27a741e5 2019-09-11 stsp if (width == 0) {
1166 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1167 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1169 27a741e5 2019-09-11 stsp cols += width;
1171 27a741e5 2019-09-11 stsp } else if (width == -1) {
1172 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1173 27a741e5 2019-09-11 stsp width = TABSIZE -
1174 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1175 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1177 27a741e5 2019-09-11 stsp cols += width;
1181 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1182 963b370f 2018-05-20 stsp goto done;
1185 963b370f 2018-05-20 stsp wline[i] = L'\0';
1186 b700b5d6 2018-07-10 stsp if (widthp)
1187 b700b5d6 2018-07-10 stsp *widthp = cols;
1190 963b370f 2018-05-20 stsp free(wline);
1192 963b370f 2018-05-20 stsp *wlinep = wline;
1193 963b370f 2018-05-20 stsp return err;
1196 8b473291 2019-02-21 stsp static const struct got_error*
1197 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1200 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1201 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1203 8b473291 2019-02-21 stsp const char *name;
1205 8b473291 2019-02-21 stsp *refs_str = NULL;
1207 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1208 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1209 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1212 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1213 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1215 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1216 8b473291 2019-02-21 stsp name += 5;
1217 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1219 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1220 8b473291 2019-02-21 stsp name += 6;
1221 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1222 8b473291 2019-02-21 stsp name += 8;
1223 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1224 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1227 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1230 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1231 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1232 5d844a1e 2019-08-13 stsp if (err) {
1233 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1234 48cae60d 2020-09-22 stsp free(ref_id);
1237 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1238 5d844a1e 2019-08-13 stsp err = NULL;
1239 5d844a1e 2019-08-13 stsp tag = NULL;
1242 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1243 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1244 48cae60d 2020-09-22 stsp free(ref_id);
1246 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1247 52b5abe1 2019-08-13 stsp if (cmp != 0)
1249 8b473291 2019-02-21 stsp s = *refs_str;
1250 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1252 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1254 8b473291 2019-02-21 stsp *refs_str = NULL;
1260 8b473291 2019-02-21 stsp return err;
1263 963b370f 2018-05-20 stsp static const struct got_error *
1264 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 27a741e5 2019-09-11 stsp int col_tab_align)
1267 5813d178 2019-03-09 stsp char *smallerthan, *at;
1269 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1270 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1271 5813d178 2019-03-09 stsp author = smallerthan + 1;
1272 5813d178 2019-03-09 stsp at = strchr(author, '@');
1274 5813d178 2019-03-09 stsp *at = '\0';
1275 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 5813d178 2019-03-09 stsp static const struct got_error *
1279 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1281 8fdc79fe 2020-12-01 naddy int author_display_cols)
1283 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1284 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1285 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1287 5813d178 2019-03-09 stsp char *author = NULL;
1288 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1290 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1291 bb737323 2018-05-20 stsp int col, limit;
1292 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1293 ccb26ccd 2018-11-05 stsp struct tm tm;
1294 45d799e2 2018-12-23 stsp time_t committer_time;
1295 11b20872 2019-11-08 stsp struct tog_color *tc;
1297 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1298 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1299 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1300 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1302 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1304 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1305 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1307 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1310 11b20872 2019-11-08 stsp wattr_on(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1314 11b20872 2019-11-08 stsp wattr_off(view->window,
1315 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1316 27a741e5 2019-09-11 stsp col = limit;
1317 b39d25c7 2018-07-10 stsp if (col > avail)
1318 b39d25c7 2018-07-10 stsp goto done;
1320 6570a66d 2019-11-08 stsp if (avail >= 120) {
1321 6570a66d 2019-11-08 stsp char *id_str;
1322 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1324 6570a66d 2019-11-08 stsp goto done;
1325 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1327 11b20872 2019-11-08 stsp wattr_on(view->window,
1328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1329 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1331 11b20872 2019-11-08 stsp wattr_off(view->window,
1332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1333 6570a66d 2019-11-08 stsp free(id_str);
1335 6570a66d 2019-11-08 stsp if (col > avail)
1336 6570a66d 2019-11-08 stsp goto done;
1339 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1340 5813d178 2019-03-09 stsp if (author == NULL) {
1341 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1342 80ddbec8 2018-04-29 stsp goto done;
1344 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1346 bb737323 2018-05-20 stsp goto done;
1347 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1349 11b20872 2019-11-08 stsp wattr_on(view->window,
1350 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1351 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1353 11b20872 2019-11-08 stsp wattr_off(view->window,
1354 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1355 bb737323 2018-05-20 stsp col += author_width;
1356 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1357 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1359 bb737323 2018-05-20 stsp author_width++;
1361 9c2eaf34 2018-05-20 stsp if (col > avail)
1362 9c2eaf34 2018-05-20 stsp goto done;
1364 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1366 6d9fbc00 2018-04-29 stsp goto done;
1367 bb737323 2018-05-20 stsp logmsg = logmsg0;
1368 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1370 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1371 bb737323 2018-05-20 stsp if (newline)
1372 bb737323 2018-05-20 stsp *newline = '\0';
1373 bb737323 2018-05-20 stsp limit = avail - col;
1374 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1376 bb737323 2018-05-20 stsp goto done;
1377 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1378 bb737323 2018-05-20 stsp col += logmsg_width;
1379 27a741e5 2019-09-11 stsp while (col < avail) {
1380 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1384 80ddbec8 2018-04-29 stsp free(logmsg0);
1385 bb737323 2018-05-20 stsp free(wlogmsg);
1386 5813d178 2019-03-09 stsp free(author);
1387 bb737323 2018-05-20 stsp free(wauthor);
1388 80ddbec8 2018-04-29 stsp free(line);
1389 80ddbec8 2018-04-29 stsp return err;
1392 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1393 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1394 899d86c2 2018-05-10 stsp struct got_object_id *id)
1396 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1398 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1399 80ddbec8 2018-04-29 stsp if (entry == NULL)
1400 899d86c2 2018-05-10 stsp return NULL;
1402 899d86c2 2018-05-10 stsp entry->id = id;
1403 99db9666 2018-05-07 stsp entry->commit = commit;
1404 899d86c2 2018-05-10 stsp return entry;
1407 99db9666 2018-05-07 stsp static void
1408 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1410 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1412 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1413 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1414 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1415 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1416 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1417 99db9666 2018-05-07 stsp free(entry);
1420 99db9666 2018-05-07 stsp static void
1421 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1423 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1424 99db9666 2018-05-07 stsp pop_commit(commits);
1427 c4972b91 2018-05-07 stsp static const struct got_error *
1428 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1429 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1431 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1432 13add988 2019-10-15 stsp regmatch_t regmatch;
1433 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1435 13add988 2019-10-15 stsp *have_match = 0;
1437 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1439 13add988 2019-10-15 stsp return err;
1441 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1443 13add988 2019-10-15 stsp goto done;
1445 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1447 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1448 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1449 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1451 13add988 2019-10-15 stsp *have_match = 1;
1453 13add988 2019-10-15 stsp free(id_str);
1454 13add988 2019-10-15 stsp free(logmsg);
1455 13add988 2019-10-15 stsp return err;
1458 13add988 2019-10-15 stsp static const struct got_error *
1459 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1461 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1463 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1464 7c1452c1 2020-03-26 stsp int nqueued = 0;
1467 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1468 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1469 1a76625f 2018-10-22 stsp * while updating the display.
1471 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1472 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1474 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1475 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1476 1a76625f 2018-10-22 stsp int errcode;
1478 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1482 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1485 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1486 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1492 1a76625f 2018-10-22 stsp if (errcode) {
1493 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1494 13add988 2019-10-15 stsp "pthread_mutex_lock");
1498 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1499 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 ecb28ae0 2018-07-16 stsp nqueued++;
1501 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1503 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 7c1452c1 2020-03-26 stsp int have_match;
1505 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1508 7c1452c1 2020-03-26 stsp if (have_match)
1509 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1515 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1520 9ba79e04 2018-06-11 stsp return err;
1523 0553a4e3 2018-04-30 stsp static const struct got_error *
1524 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1526 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1527 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1528 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1529 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1530 60493ae3 2019-06-20 stsp int width;
1531 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1532 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1533 8b473291 2019-02-21 stsp char *refs_str = NULL;
1534 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1535 11b20872 2019-11-08 stsp struct tog_color *tc;
1536 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1538 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1539 e0d42f60 2018-07-22 stsp ncommits = 0;
1540 e0d42f60 2018-07-22 stsp while (entry) {
1541 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected) {
1542 8fdc79fe 2020-12-01 naddy s->selected_entry = entry;
1545 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1546 e0d42f60 2018-07-22 stsp ncommits++;
1549 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1550 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1551 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1553 ecb28ae0 2018-07-16 stsp return err;
1554 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1555 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1557 8fdc79fe 2020-12-01 naddy goto done;
1560 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1561 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1563 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1564 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1565 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1566 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1567 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1568 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1569 8f4ed634 2020-03-26 stsp goto done;
1572 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1574 f9686aa5 2020-03-27 stsp if (view->searching) {
1575 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1576 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1577 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1578 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1579 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1580 f9686aa5 2020-03-27 stsp search_str = "searching...";
1583 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1584 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1585 f9686aa5 2020-03-27 stsp search_str ? search_str :
1586 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1587 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1588 8f4ed634 2020-03-26 stsp goto done;
1592 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1593 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1594 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1595 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1597 1a76625f 2018-10-22 stsp header = NULL;
1598 1a76625f 2018-10-22 stsp goto done;
1600 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1601 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1602 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1603 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1604 1a76625f 2018-10-22 stsp header = NULL;
1605 1a76625f 2018-10-22 stsp goto done;
1607 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1609 1a76625f 2018-10-22 stsp goto done;
1611 2814baeb 2018-08-01 stsp werase(view->window);
1613 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1614 a3404814 2018-09-02 stsp wstandout(view->window);
1615 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1617 11b20872 2019-11-08 stsp wattr_on(view->window,
1618 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1619 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1621 11b20872 2019-11-08 stsp wattr_off(view->window,
1622 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1623 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1624 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1627 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1628 a3404814 2018-09-02 stsp wstandend(view->window);
1629 ecb28ae0 2018-07-16 stsp free(wline);
1630 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1631 1a76625f 2018-10-22 stsp goto done;
1633 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1634 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1635 5813d178 2019-03-09 stsp ncommits = 0;
1636 5813d178 2019-03-09 stsp while (entry) {
1637 5813d178 2019-03-09 stsp char *author;
1638 5813d178 2019-03-09 stsp wchar_t *wauthor;
1639 5813d178 2019-03-09 stsp int width;
1640 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1642 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1643 5813d178 2019-03-09 stsp if (author == NULL) {
1644 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1645 5813d178 2019-03-09 stsp goto done;
1647 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1648 27a741e5 2019-09-11 stsp date_display_cols);
1649 5813d178 2019-03-09 stsp if (author_cols < width)
1650 5813d178 2019-03-09 stsp author_cols = width;
1651 5813d178 2019-03-09 stsp free(wauthor);
1652 5813d178 2019-03-09 stsp free(author);
1653 7ca04879 2019-10-19 stsp ncommits++;
1654 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1657 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1658 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1659 867c6645 2018-07-10 stsp ncommits = 0;
1660 899d86c2 2018-05-10 stsp while (entry) {
1661 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1663 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1664 2814baeb 2018-08-01 stsp wstandout(view->window);
1665 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1666 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1667 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1668 2814baeb 2018-08-01 stsp wstandend(view->window);
1670 60493ae3 2019-06-20 stsp goto done;
1671 0553a4e3 2018-04-30 stsp ncommits++;
1672 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1673 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1676 1a57306a 2018-09-02 stsp view_vborder(view);
1678 1a76625f 2018-10-22 stsp free(id_str);
1679 8b473291 2019-02-21 stsp free(refs_str);
1680 1a76625f 2018-10-22 stsp free(ncommits_str);
1681 1a76625f 2018-10-22 stsp free(header);
1682 80ddbec8 2018-04-29 stsp return err;
1685 07b55e75 2018-05-10 stsp static void
1686 ffe38506 2020-12-01 naddy log_scroll_up(struct tog_view *view, int maxscroll)
1688 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1689 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1690 07b55e75 2018-05-10 stsp int nscrolled = 0;
1692 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1693 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1696 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1697 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1698 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1699 07b55e75 2018-05-10 stsp if (entry) {
1700 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1701 07b55e75 2018-05-10 stsp nscrolled++;
1706 aa075928 2018-05-10 stsp static const struct got_error *
1707 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1709 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1710 5e224a3e 2019-02-22 stsp int errcode;
1712 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1714 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1715 ffe38506 2020-12-01 naddy if (ta->log_complete)
1718 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1719 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1720 7aafa0d1 2019-02-22 stsp if (errcode)
1721 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1722 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1725 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1726 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1728 7c1452c1 2020-03-26 stsp if (!wait)
1731 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1732 ffe38506 2020-12-01 naddy show_log_view(view);
1733 7c1452c1 2020-03-26 stsp update_panels();
1734 7c1452c1 2020-03-26 stsp doupdate();
1736 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1737 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1738 82954512 2020-02-03 stsp if (errcode)
1739 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1740 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1742 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1743 ffe38506 2020-12-01 naddy show_log_view(view);
1744 7c1452c1 2020-03-26 stsp update_panels();
1745 7c1452c1 2020-03-26 stsp doupdate();
1748 5e224a3e 2019-02-22 stsp return NULL;
1751 5e224a3e 2019-02-22 stsp static const struct got_error *
1752 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1754 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1755 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1756 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1757 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1759 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1760 5e224a3e 2019-02-22 stsp return NULL;
1762 ffe38506 2020-12-01 naddy ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1763 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1764 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1766 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1768 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1769 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1771 5e224a3e 2019-02-22 stsp return err;
1775 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1776 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1779 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1781 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1782 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1784 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1785 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1787 dd0a52c1 2018-05-20 stsp return err;
1790 cd0acaa7 2018-05-20 stsp static const struct got_error *
1791 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1792 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1793 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1795 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1796 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1797 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1799 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1800 15a94983 2018-12-23 stsp if (diff_view == NULL)
1801 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1803 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1804 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1805 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1806 e5a0f69f 2018-08-18 stsp if (err == NULL)
1807 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1808 cd0acaa7 2018-05-20 stsp return err;
1811 80ddbec8 2018-04-29 stsp static const struct got_error *
1812 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1813 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1815 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1817 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1818 941e9f74 2019-05-21 stsp if (parent == NULL)
1819 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1821 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1822 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1823 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1824 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1825 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1826 941e9f74 2019-05-21 stsp s->tree = subtree;
1827 941e9f74 2019-05-21 stsp s->selected = 0;
1828 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1829 941e9f74 2019-05-21 stsp return NULL;
1832 941e9f74 2019-05-21 stsp static const struct got_error *
1833 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1834 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1835 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1837 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1838 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1839 941e9f74 2019-05-21 stsp const char *p;
1840 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1842 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1844 941e9f74 2019-05-21 stsp while (*p) {
1845 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1846 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1847 56e0773d 2019-11-28 stsp char *te_name;
1849 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1852 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1853 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1854 941e9f74 2019-05-21 stsp if (slash == NULL)
1855 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1857 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1858 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1859 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1862 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1863 56e0773d 2019-11-28 stsp if (te == NULL) {
1864 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1865 56e0773d 2019-11-28 stsp free(te_name);
1868 56e0773d 2019-11-28 stsp free(te_name);
1869 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1871 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1872 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1874 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1875 941e9f74 2019-05-21 stsp if (slash)
1876 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1878 941e9f74 2019-05-21 stsp subpath = strdup(path);
1879 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1880 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1884 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1889 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1890 941e9f74 2019-05-21 stsp free(tree_id);
1894 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1895 941e9f74 2019-05-21 stsp if (err) {
1896 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1899 941e9f74 2019-05-21 stsp if (slash == NULL)
1901 941e9f74 2019-05-21 stsp free(subpath);
1902 941e9f74 2019-05-21 stsp subpath = NULL;
1903 941e9f74 2019-05-21 stsp p = slash;
1906 941e9f74 2019-05-21 stsp free(subpath);
1907 1a76625f 2018-10-22 stsp return err;
1910 61266923 2020-01-14 stsp static const struct got_error *
1911 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1912 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1913 78756c87 2020-11-24 stsp struct got_repository *repo)
1915 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1916 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1917 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1918 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1920 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1921 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1923 55cccc34 2020-02-20 stsp return err;
1925 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1926 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1927 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1929 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1930 55cccc34 2020-02-20 stsp if (err) {
1931 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1932 55cccc34 2020-02-20 stsp return err;
1934 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1936 55cccc34 2020-02-20 stsp *new_view = tree_view;
1938 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1939 55cccc34 2020-02-20 stsp return NULL;
1941 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1944 55cccc34 2020-02-20 stsp static const struct got_error *
1945 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1947 61266923 2020-01-14 stsp sigset_t sigset;
1948 61266923 2020-01-14 stsp int errcode;
1950 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1951 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1953 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1954 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1955 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1956 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1957 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1959 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1960 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1961 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1963 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1964 61266923 2020-01-14 stsp if (errcode)
1965 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1967 61266923 2020-01-14 stsp return NULL;
1970 1a76625f 2018-10-22 stsp static void *
1971 1a76625f 2018-10-22 stsp log_thread(void *arg)
1973 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1974 1a76625f 2018-10-22 stsp int errcode = 0;
1975 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1976 1a76625f 2018-10-22 stsp int done = 0;
1978 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1980 61266923 2020-01-14 stsp return (void *)err;
1982 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1983 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1984 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1985 13add988 2019-10-15 stsp a->regex);
1986 1a76625f 2018-10-22 stsp if (err) {
1987 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1988 1a76625f 2018-10-22 stsp return (void *)err;
1989 1a76625f 2018-10-22 stsp err = NULL;
1991 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1992 1a76625f 2018-10-22 stsp a->commits_needed--;
1994 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1995 3abe8080 2019-04-10 stsp if (errcode) {
1996 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1997 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1999 3abe8080 2019-04-10 stsp } else if (*a->quit)
2001 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2002 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2003 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2004 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2007 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2008 7c1452c1 2020-03-26 stsp if (errcode) {
2009 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2010 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2011 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2016 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2018 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2019 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2020 7c1452c1 2020-03-26 stsp &tog_mutex);
2021 7c1452c1 2020-03-26 stsp if (errcode)
2022 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2023 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2027 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2028 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2029 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2030 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2032 3abe8080 2019-04-10 stsp a->log_complete = 1;
2033 1a76625f 2018-10-22 stsp return (void *)err;
2036 1a76625f 2018-10-22 stsp static const struct got_error *
2037 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2039 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2040 1a76625f 2018-10-22 stsp int errcode;
2042 1a76625f 2018-10-22 stsp if (s->thread) {
2043 1a76625f 2018-10-22 stsp s->quit = 1;
2044 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2045 1a76625f 2018-10-22 stsp if (errcode)
2046 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2047 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2048 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2049 1a76625f 2018-10-22 stsp if (errcode)
2050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2051 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2052 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2053 1a76625f 2018-10-22 stsp if (errcode)
2054 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2055 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2056 1a76625f 2018-10-22 stsp if (errcode)
2057 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2058 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2059 1a76625f 2018-10-22 stsp s->thread = NULL;
2062 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2063 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2064 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2067 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2068 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2069 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2072 9343a5fb 2018-06-23 stsp return err;
2075 9343a5fb 2018-06-23 stsp static const struct got_error *
2076 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2078 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2079 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2080 276b94a1 2020-11-13 naddy int errcode;
2082 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2084 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2085 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2086 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2088 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2089 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2090 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2093 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2094 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2095 1a76625f 2018-10-22 stsp free(s->start_id);
2096 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2097 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2098 1a76625f 2018-10-22 stsp return err;
2101 1a76625f 2018-10-22 stsp static const struct got_error *
2102 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2104 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2106 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2107 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2108 60493ae3 2019-06-20 stsp return NULL;
2111 60493ae3 2019-06-20 stsp static const struct got_error *
2112 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2114 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2115 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2116 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2118 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2119 f9686aa5 2020-03-27 stsp show_log_view(view);
2120 f9686aa5 2020-03-27 stsp update_panels();
2121 f9686aa5 2020-03-27 stsp doupdate();
2123 96e2b566 2019-07-08 stsp if (s->search_entry) {
2124 13add988 2019-10-15 stsp int errcode, ch;
2125 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2126 13add988 2019-10-15 stsp if (errcode)
2127 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2128 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2129 13add988 2019-10-15 stsp ch = wgetch(view->window);
2130 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2131 13add988 2019-10-15 stsp if (errcode)
2132 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2133 13add988 2019-10-15 stsp "pthread_mutex_lock");
2134 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2135 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 678cbce5 2019-07-28 stsp return NULL;
2138 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2139 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2141 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2142 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2143 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2144 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2145 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2147 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2148 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2150 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2151 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2153 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2156 60493ae3 2019-06-20 stsp while (1) {
2157 13add988 2019-10-15 stsp int have_match = 0;
2159 60493ae3 2019-06-20 stsp if (entry == NULL) {
2160 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2161 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2162 f9967bca 2020-03-27 stsp view->search_next_done =
2163 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2164 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2165 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2166 f801134a 2019-06-25 stsp return NULL;
2169 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2170 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2171 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2173 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2174 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2177 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2178 13add988 2019-10-15 stsp &view->regex);
2181 13add988 2019-10-15 stsp if (have_match) {
2182 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2183 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2187 96e2b566 2019-07-08 stsp s->search_entry = entry;
2188 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2189 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2191 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2194 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2195 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2196 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2197 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2199 60493ae3 2019-06-20 stsp return err;
2202 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2203 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2205 60493ae3 2019-06-20 stsp return err;
2210 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2212 60493ae3 2019-06-20 stsp return NULL;
2215 60493ae3 2019-06-20 stsp static const struct got_error *
2216 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2217 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2218 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2220 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2221 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2222 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2223 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2224 1a76625f 2018-10-22 stsp int errcode;
2226 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2228 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2229 f135c941 2020-02-20 stsp free(s->in_repo_path);
2230 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2231 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2232 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2235 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2236 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2237 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2239 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2241 78756c87 2020-11-24 stsp goto done;
2243 fb2756b9 2018-08-04 stsp s->repo = repo;
2244 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2245 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2246 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2247 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2248 5036bf37 2018-09-24 stsp goto done;
2250 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2252 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2253 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2254 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2257 11b20872 2019-11-08 stsp goto done;
2258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2260 11b20872 2019-11-08 stsp if (err) {
2261 11b20872 2019-11-08 stsp free_colors(&s->colors);
2262 11b20872 2019-11-08 stsp goto done;
2264 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2265 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2266 11b20872 2019-11-08 stsp if (err) {
2267 11b20872 2019-11-08 stsp free_colors(&s->colors);
2268 11b20872 2019-11-08 stsp goto done;
2272 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2273 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2274 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2275 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2276 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2278 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2280 1a76625f 2018-10-22 stsp goto done;
2281 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2282 b672a97a 2020-01-27 stsp !s->log_branches);
2284 1a76625f 2018-10-22 stsp goto done;
2285 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2286 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2288 c5b78334 2020-01-12 stsp goto done;
2290 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2291 1a76625f 2018-10-22 stsp if (errcode) {
2292 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2293 1a76625f 2018-10-22 stsp goto done;
2295 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2296 7c1452c1 2020-03-26 stsp if (errcode) {
2297 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2298 7c1452c1 2020-03-26 stsp goto done;
2301 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2302 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2303 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2304 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2305 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2306 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2307 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2308 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2309 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2310 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2311 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2312 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2313 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2316 1a76625f 2018-10-22 stsp close_log_view(view);
2317 ba4f502b 2018-08-04 stsp return err;
2320 e5a0f69f 2018-08-18 stsp static const struct got_error *
2321 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2323 f2f6d207 2020-11-24 stsp const struct got_error *err;
2324 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2326 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2327 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2328 2b380cc8 2018-10-24 stsp &s->thread_args);
2329 2b380cc8 2018-10-24 stsp if (errcode)
2330 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2331 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2332 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2334 f2f6d207 2020-11-24 stsp return err;
2338 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2341 e5a0f69f 2018-08-18 stsp static const struct got_error *
2342 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2343 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2345 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2346 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2347 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2348 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2349 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2350 669b5ffa 2018-10-07 stsp int begin_x = 0;
2351 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2353 e5a0f69f 2018-08-18 stsp switch (ch) {
2355 1e37a5c2 2019-05-12 jcs s->quit = 1;
2358 1e37a5c2 2019-05-12 jcs case KEY_UP:
2361 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2363 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2364 1e37a5c2 2019-05-12 jcs s->selected--;
2366 ffe38506 2020-12-01 naddy log_scroll_up(view, 1);
2368 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2369 a4292ac5 2019-05-12 jcs case CTRL('b'):
2370 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2372 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2373 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2374 1e37a5c2 2019-05-12 jcs s->selected = 0;
2377 ffe38506 2020-12-01 naddy log_scroll_up(view, view->nlines - 1);
2380 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2383 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2385 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2386 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2387 1e37a5c2 2019-05-12 jcs s->selected++;
2390 ffe38506 2020-12-01 naddy err = log_scroll_down(view, 1);
2392 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2393 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2394 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2395 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2396 1e37a5c2 2019-05-12 jcs if (first == NULL)
2398 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2401 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2402 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2403 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2404 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2405 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2406 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2408 1e37a5c2 2019-05-12 jcs err = NULL;
2411 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2412 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2413 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2414 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2415 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2417 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2420 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2422 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2423 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2424 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2425 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2426 78756c87 2020-11-24 stsp view, s->repo);
2429 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2430 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2432 1e37a5c2 2019-05-12 jcs return err;
2433 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2435 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2438 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2439 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2441 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2444 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2446 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2447 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2448 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2449 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2452 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2453 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2455 1e37a5c2 2019-05-12 jcs return err;
2456 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2458 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2461 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2462 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2464 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2466 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2467 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2468 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2470 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2472 74cfe85e 2020-10-20 stsp return err;
2473 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2474 74cfe85e 2020-10-20 stsp if (err) {
2475 74cfe85e 2020-10-20 stsp free(parent_path);
2476 74cfe85e 2020-10-20 stsp return err;
2478 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2479 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2480 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2481 74cfe85e 2020-10-20 stsp free(parent_path);
2482 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2484 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2485 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2486 74cfe85e 2020-10-20 stsp free(parent_path);
2488 74cfe85e 2020-10-20 stsp return err;;
2489 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2490 74cfe85e 2020-10-20 stsp *new_view = lv;
2492 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2493 74cfe85e 2020-10-20 stsp *focus_view = lv;
2496 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2497 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2499 d01904d4 2019-06-25 stsp return err;
2500 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2501 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 d01904d4 2019-06-25 stsp if (lv == NULL)
2503 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2504 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2505 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2506 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2507 ef129c5e 2019-08-03 stsp if (err) {
2508 ef129c5e 2019-08-03 stsp view_close(lv);
2509 d01904d4 2019-06-25 stsp return err;
2511 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2512 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2513 e6b23735 2019-10-04 stsp free(start_id);
2514 e6b23735 2019-10-04 stsp view_close(lv);
2515 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2517 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2518 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2519 ef129c5e 2019-08-03 stsp if (err) {
2520 ef129c5e 2019-08-03 stsp free(start_id);
2521 b672a97a 2020-01-27 stsp view_close(lv);
2522 b672a97a 2020-01-27 stsp return err;;
2524 b672a97a 2020-01-27 stsp *dead_view = view;
2525 b672a97a 2020-01-27 stsp *new_view = lv;
2528 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2529 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2531 b672a97a 2020-01-27 stsp return err;
2532 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2533 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2534 b672a97a 2020-01-27 stsp if (lv == NULL)
2535 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2536 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2537 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2538 b672a97a 2020-01-27 stsp if (err) {
2539 ef129c5e 2019-08-03 stsp view_close(lv);
2540 d01904d4 2019-06-25 stsp return err;;
2542 d01904d4 2019-06-25 stsp *dead_view = view;
2543 d01904d4 2019-06-25 stsp *new_view = lv;
2546 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2547 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2548 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2549 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2550 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2551 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2552 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2553 6458efa5 2020-11-24 stsp if (err) {
2554 6458efa5 2020-11-24 stsp view_close(ref_view);
2555 6458efa5 2020-11-24 stsp return err;
2557 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2558 6458efa5 2020-11-24 stsp err = view_close_child(view);
2560 6458efa5 2020-11-24 stsp return err;
2561 6458efa5 2020-11-24 stsp err = view_set_child(view, ref_view);
2562 6458efa5 2020-11-24 stsp if (err) {
2563 6458efa5 2020-11-24 stsp view_close(ref_view);
2566 6458efa5 2020-11-24 stsp *focus_view = ref_view;
2567 6458efa5 2020-11-24 stsp view->child_focussed = 1;
2569 6458efa5 2020-11-24 stsp *new_view = ref_view;
2575 80ddbec8 2018-04-29 stsp return err;
2578 4ed7e80c 2018-05-20 stsp static const struct got_error *
2579 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2581 c2db6724 2019-01-04 stsp const struct got_error *error;
2583 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2584 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2585 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2587 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2588 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2590 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2591 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2593 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2594 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2596 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2597 c2db6724 2019-01-04 stsp if (error != NULL)
2598 c2db6724 2019-01-04 stsp return error;
2600 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2601 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2603 c2db6724 2019-01-04 stsp return NULL;
2606 a915003a 2019-02-05 stsp static void
2607 a915003a 2019-02-05 stsp init_curses(void)
2609 a915003a 2019-02-05 stsp initscr();
2611 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2614 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2615 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2616 a915003a 2019-02-05 stsp curs_set(0);
2617 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2618 6d17833f 2019-11-08 stsp start_color();
2619 6d17833f 2019-11-08 stsp use_default_colors();
2621 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2622 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2623 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2626 c2db6724 2019-01-04 stsp static const struct got_error *
2627 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2628 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2630 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2632 f135c941 2020-02-20 stsp if (argc == 0) {
2633 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2634 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2635 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2636 f135c941 2020-02-20 stsp return NULL;
2639 f135c941 2020-02-20 stsp if (worktree) {
2640 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2643 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2645 f135c941 2020-02-20 stsp return err;
2646 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2647 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2648 bfd61697 2020-11-14 stsp p) == -1) {
2649 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2650 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2654 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2656 f135c941 2020-02-20 stsp return err;
2659 f135c941 2020-02-20 stsp static const struct got_error *
2660 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2662 80ddbec8 2018-04-29 stsp const struct got_error *error;
2663 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2664 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2665 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2666 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2667 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2668 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2669 04cc582a 2018-08-01 stsp struct tog_view *view;
2671 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2672 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2673 c2db6724 2019-01-04 stsp NULL) == -1)
2674 80ddbec8 2018-04-29 stsp err(1, "pledge");
2677 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2678 80ddbec8 2018-04-29 stsp switch (ch) {
2680 b672a97a 2020-01-27 stsp log_branches = 1;
2683 80ddbec8 2018-04-29 stsp start_commit = optarg;
2686 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2687 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2688 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2692 17020d27 2019-03-07 stsp usage_log();
2693 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2697 80ddbec8 2018-04-29 stsp argc -= optind;
2698 80ddbec8 2018-04-29 stsp argv += optind;
2700 f135c941 2020-02-20 stsp if (argc > 1)
2701 f135c941 2020-02-20 stsp usage_log();
2703 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2704 6962eb72 2020-02-20 stsp if (cwd == NULL)
2705 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2707 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2708 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2709 963f97a1 2019-03-18 stsp goto done;
2711 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2712 a1fbf39a 2019-08-11 stsp if (worktree)
2713 6962eb72 2020-02-20 stsp repo_path =
2714 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2716 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2718 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2719 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2720 963f97a1 2019-03-18 stsp goto done;
2723 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2724 80ddbec8 2018-04-29 stsp if (error != NULL)
2725 ecb28ae0 2018-07-16 stsp goto done;
2727 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2728 f135c941 2020-02-20 stsp repo, worktree);
2729 f135c941 2020-02-20 stsp if (error)
2730 f135c941 2020-02-20 stsp goto done;
2732 f135c941 2020-02-20 stsp init_curses();
2734 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2735 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2736 c02c541e 2019-03-29 stsp if (error)
2737 c02c541e 2019-03-29 stsp goto done;
2739 15a94983 2018-12-23 stsp if (start_commit == NULL)
2740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2741 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2742 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2744 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2745 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2746 80ddbec8 2018-04-29 stsp if (error != NULL)
2747 8b473291 2019-02-21 stsp goto done;
2749 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 04cc582a 2018-08-01 stsp if (view == NULL) {
2751 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2752 04cc582a 2018-08-01 stsp goto done;
2754 2fc00ff4 2019-08-31 stsp if (worktree) {
2755 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2756 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2757 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2758 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2759 2fc00ff4 2019-08-31 stsp goto done;
2762 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2763 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2764 ba4f502b 2018-08-04 stsp if (error)
2765 ba4f502b 2018-08-04 stsp goto done;
2766 2fc00ff4 2019-08-31 stsp if (worktree) {
2767 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2768 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2769 2fc00ff4 2019-08-31 stsp worktree = NULL;
2771 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2773 f135c941 2020-02-20 stsp free(in_repo_path);
2774 ecb28ae0 2018-07-16 stsp free(repo_path);
2775 ecb28ae0 2018-07-16 stsp free(cwd);
2776 899d86c2 2018-05-10 stsp free(start_id);
2777 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2779 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2780 ec142235 2019-03-07 stsp if (worktree)
2781 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2782 80ddbec8 2018-04-29 stsp return error;
2785 4ed7e80c 2018-05-20 stsp __dead static void
2786 9f7d7167 2018-04-29 stsp usage_diff(void)
2789 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2790 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2794 b304db33 2018-05-20 stsp static char *
2795 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2797 b304db33 2018-05-20 stsp char *line;
2798 b304db33 2018-05-20 stsp size_t linelen;
2799 b304db33 2018-05-20 stsp size_t lineno;
2800 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2802 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2804 b304db33 2018-05-20 stsp *len = linelen;
2805 b304db33 2018-05-20 stsp return line;
2808 6d17833f 2019-11-08 stsp static int
2809 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2810 41605754 2020-11-12 stsp regmatch_t *regmatch)
2812 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2815 f26dddb7 2019-11-08 stsp struct tog_color *
2816 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2818 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2820 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2821 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2822 6d17833f 2019-11-08 stsp return tc;
2825 6d17833f 2019-11-08 stsp return NULL;
2828 4ed7e80c 2018-05-20 stsp static const struct got_error *
2829 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2830 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2832 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2833 41605754 2020-11-12 stsp wchar_t *wline;
2834 41605754 2020-11-12 stsp int width;
2837 41605754 2020-11-12 stsp *wtotal = 0;
2839 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2840 41605754 2020-11-12 stsp if (s == NULL)
2841 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2843 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2844 41605754 2020-11-12 stsp if (err) {
2846 41605754 2020-11-12 stsp return err;
2848 41605754 2020-11-12 stsp waddwstr(window, wline);
2849 41605754 2020-11-12 stsp free(wline);
2851 41605754 2020-11-12 stsp wlimit -= width;
2852 41605754 2020-11-12 stsp *wtotal += width;
2854 41605754 2020-11-12 stsp if (wlimit > 0) {
2855 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2856 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2857 41605754 2020-11-12 stsp if (s == NULL) {
2858 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2860 41605754 2020-11-12 stsp return err;
2862 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2863 41605754 2020-11-12 stsp if (err) {
2865 41605754 2020-11-12 stsp return err;
2867 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2868 41605754 2020-11-12 stsp waddwstr(window, wline);
2869 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2870 41605754 2020-11-12 stsp free(wline);
2872 41605754 2020-11-12 stsp wlimit -= width;
2873 41605754 2020-11-12 stsp *wtotal += width;
2876 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2877 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2878 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2880 41605754 2020-11-12 stsp return err;
2881 41605754 2020-11-12 stsp waddwstr(window, wline);
2882 41605754 2020-11-12 stsp free(wline);
2883 41605754 2020-11-12 stsp *wtotal += width;
2886 41605754 2020-11-12 stsp return NULL;
2889 41605754 2020-11-12 stsp static const struct got_error *
2890 fe621944 2020-11-10 stsp draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2891 fe621944 2020-11-10 stsp off_t *line_offsets, int selected_line, int max_lines,
2892 3dbaef42 2020-11-24 stsp int *last_displayed_line, int *eof, const char *header,
2893 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2895 61e69b96 2018-05-20 stsp const struct got_error *err;
2896 fe621944 2020-11-10 stsp int nprinted = 0;
2897 b304db33 2018-05-20 stsp char *line;
2898 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2899 b304db33 2018-05-20 stsp size_t len;
2900 61e69b96 2018-05-20 stsp wchar_t *wline;
2901 e0b650dd 2018-05-20 stsp int width;
2902 fe621944 2020-11-10 stsp off_t line_offset;
2904 fe621944 2020-11-10 stsp line_offset = line_offsets[first_displayed_line - 1];
2905 fe621944 2020-11-10 stsp if (fseeko(f, line_offset, SEEK_SET) == -1)
2906 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2908 f7d12f7e 2018-08-01 stsp werase(view->window);
2910 a3404814 2018-09-02 stsp if (header) {
2911 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2912 135a2da0 2020-11-11 stsp first_displayed_line - 1 + selected_line, nlines,
2913 135a2da0 2020-11-11 stsp header) == -1)
2914 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2915 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2916 135a2da0 2020-11-11 stsp free(line);
2918 a3404814 2018-09-02 stsp return err;
2920 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2921 a3404814 2018-09-02 stsp wstandout(view->window);
2922 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2923 e54cc94a 2020-11-11 stsp free(wline);
2924 e54cc94a 2020-11-11 stsp wline = NULL;
2925 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2926 a3404814 2018-09-02 stsp wstandend(view->window);
2927 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2928 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2930 a3404814 2018-09-02 stsp if (max_lines <= 1)
2931 a3404814 2018-09-02 stsp return NULL;
2932 a3404814 2018-09-02 stsp max_lines--;
2936 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2937 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2938 26ed57b2 2018-05-19 stsp if (line == NULL) {
2943 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2945 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2946 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2947 41605754 2020-11-12 stsp if (first_displayed_line + nprinted == matched_line &&
2948 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2949 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2950 41605754 2020-11-12 stsp view->window, regmatch);
2951 41605754 2020-11-12 stsp if (err) {
2952 41605754 2020-11-12 stsp free(line);
2953 41605754 2020-11-12 stsp return err;
2956 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2957 41605754 2020-11-12 stsp if (err) {
2958 41605754 2020-11-12 stsp free(line);
2959 41605754 2020-11-12 stsp return err;
2961 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2962 41605754 2020-11-12 stsp free(wline);
2963 41605754 2020-11-12 stsp wline = NULL;
2966 6d17833f 2019-11-08 stsp wattr_off(view->window,
2967 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2968 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2969 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2970 fe621944 2020-11-10 stsp nprinted++;
2971 26ed57b2 2018-05-19 stsp free(line);
2973 fe621944 2020-11-10 stsp if (nprinted >= 1)
2974 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line + (nprinted - 1);
2976 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line;
2978 1a57306a 2018-09-02 stsp view_vborder(view);
2980 c3e9aa98 2019-05-13 jcs if (*eof) {
2981 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2982 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2983 c3e9aa98 2019-05-13 jcs nprinted++;
2986 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2988 c3e9aa98 2019-05-13 jcs return err;
2991 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2992 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2993 e54cc94a 2020-11-11 stsp free(wline);
2994 e54cc94a 2020-11-11 stsp wline = NULL;
2995 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2998 26ed57b2 2018-05-19 stsp return NULL;
3001 abd2672a 2018-12-23 stsp static char *
3002 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3004 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3005 09867e48 2019-08-13 stsp char *p, *s;
3007 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3008 09867e48 2019-08-13 stsp if (tm == NULL)
3009 09867e48 2019-08-13 stsp return NULL;
3010 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3011 09867e48 2019-08-13 stsp if (s == NULL)
3012 09867e48 2019-08-13 stsp return NULL;
3013 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3015 abd2672a 2018-12-23 stsp *p = '\0';
3019 4ed7e80c 2018-05-20 stsp static const struct got_error *
3020 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3021 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3023 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3024 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3025 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3026 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3028 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3029 0208f208 2020-05-05 stsp if (qid != NULL) {
3030 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3031 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3034 0208f208 2020-05-05 stsp return err;
3036 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3037 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3041 0208f208 2020-05-05 stsp if (tree_id1) {
3042 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3044 0208f208 2020-05-05 stsp goto done;
3047 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3048 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3050 0208f208 2020-05-05 stsp goto done;
3052 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3053 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3055 0208f208 2020-05-05 stsp if (tree1)
3056 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3057 0208f208 2020-05-05 stsp if (tree2)
3058 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3059 0208f208 2020-05-05 stsp return err;
3062 0208f208 2020-05-05 stsp static const struct got_error *
3063 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3067 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3068 fe621944 2020-11-10 stsp if (p == NULL)
3069 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3070 fe621944 2020-11-10 stsp *line_offsets = p;
3071 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3072 fe621944 2020-11-10 stsp (*nlines)++;
3073 fe621944 2020-11-10 stsp return NULL;
3076 fe621944 2020-11-10 stsp static const struct got_error *
3077 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3078 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3079 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3081 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3082 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3083 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3084 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3085 45d799e2 2018-12-23 stsp time_t committer_time;
3086 45d799e2 2018-12-23 stsp const char *author, *committer;
3087 8b473291 2019-02-21 stsp char *refs_str = NULL;
3088 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3089 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3090 fe621944 2020-11-10 stsp off_t outoff = 0;
3093 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3095 8b473291 2019-02-21 stsp if (refs) {
3096 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3098 8b473291 2019-02-21 stsp return err;
3101 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3103 abd2672a 2018-12-23 stsp return err;
3105 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3106 15a94983 2018-12-23 stsp if (err) {
3107 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3108 15a94983 2018-12-23 stsp goto done;
3111 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3113 fe621944 2020-11-10 stsp goto done;
3115 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3116 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3117 fe621944 2020-11-10 stsp if (n < 0) {
3118 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3119 abd2672a 2018-12-23 stsp goto done;
3121 fe621944 2020-11-10 stsp outoff += n;
3122 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3124 fe621944 2020-11-10 stsp goto done;
3126 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3127 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3128 fe621944 2020-11-10 stsp if (n < 0) {
3129 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3130 abd2672a 2018-12-23 stsp goto done;
3132 fe621944 2020-11-10 stsp outoff += n;
3133 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3135 fe621944 2020-11-10 stsp goto done;
3137 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3138 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3139 fe621944 2020-11-10 stsp if (datestr) {
3140 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3141 fe621944 2020-11-10 stsp if (n < 0) {
3142 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3143 fe621944 2020-11-10 stsp goto done;
3145 fe621944 2020-11-10 stsp outoff += n;
3146 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3148 fe621944 2020-11-10 stsp goto done;
3150 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3151 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3152 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3153 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3154 fe621944 2020-11-10 stsp if (n < 0) {
3155 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3156 fe621944 2020-11-10 stsp goto done;
3158 fe621944 2020-11-10 stsp outoff += n;
3159 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3161 fe621944 2020-11-10 stsp goto done;
3163 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3165 5943eee2 2019-08-13 stsp goto done;
3166 fe621944 2020-11-10 stsp s = logmsg;
3167 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3168 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3169 fe621944 2020-11-10 stsp if (n < 0) {
3170 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3171 fe621944 2020-11-10 stsp goto done;
3173 fe621944 2020-11-10 stsp outoff += n;
3174 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3176 fe621944 2020-11-10 stsp goto done;
3179 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3181 0208f208 2020-05-05 stsp goto done;
3182 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3183 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3184 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3185 fe621944 2020-11-10 stsp if (n < 0) {
3186 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3187 fe621944 2020-11-10 stsp goto done;
3189 fe621944 2020-11-10 stsp outoff += n;
3190 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3192 fe621944 2020-11-10 stsp goto done;
3193 0208f208 2020-05-05 stsp free((char *)pe->path);
3194 0208f208 2020-05-05 stsp free(pe->data);
3197 0208f208 2020-05-05 stsp fputc('\n', outfile);
3199 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3201 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3202 abd2672a 2018-12-23 stsp free(id_str);
3203 5943eee2 2019-08-13 stsp free(logmsg);
3204 8b473291 2019-02-21 stsp free(refs_str);
3205 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3206 7510f233 2020-08-09 stsp if (err) {
3207 ae6a6978 2020-08-09 stsp free(*line_offsets);
3208 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3209 ae6a6978 2020-08-09 stsp *nlines = 0;
3211 fe621944 2020-11-10 stsp return err;
3214 abd2672a 2018-12-23 stsp static const struct got_error *
3215 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3217 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3218 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3219 15a94983 2018-12-23 stsp int obj_type;
3221 fe621944 2020-11-10 stsp free(s->line_offsets);
3222 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3223 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3224 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3225 fe621944 2020-11-10 stsp s->nlines = 0;
3227 511a516b 2018-05-19 stsp f = got_opentemp();
3228 48ae06ee 2018-10-18 stsp if (f == NULL) {
3229 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3230 48ae06ee 2018-10-18 stsp goto done;
3232 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3233 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3234 fb43ecf1 2019-02-11 stsp goto done;
3238 15a94983 2018-12-23 stsp if (s->id1)
3239 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3241 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3243 15a94983 2018-12-23 stsp goto done;
3245 15a94983 2018-12-23 stsp switch (obj_type) {
3246 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3247 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3248 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3249 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3251 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3252 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3253 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3254 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3256 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3257 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3258 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3259 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3261 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3263 3ffacbe1 2020-02-02 tracey goto done;
3264 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3265 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3266 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3267 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3268 f44b1f58 2020-02-02 tracey if (err)
3269 f44b1f58 2020-02-02 tracey goto done;
3270 f44b1f58 2020-02-02 tracey } else {
3271 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3272 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3273 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3274 fe621944 2020-11-10 stsp err = write_commit_info(
3275 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3276 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3277 f44b1f58 2020-02-02 tracey if (err)
3278 f44b1f58 2020-02-02 tracey goto done;
3283 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3285 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3286 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3287 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3291 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3294 f44b1f58 2020-02-02 tracey if (err)
3295 f44b1f58 2020-02-02 tracey goto done;
3297 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3298 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3299 48ae06ee 2018-10-18 stsp return err;
3302 f5215bb9 2019-02-22 stsp static void
3303 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3305 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3306 f5215bb9 2019-02-22 stsp update_panels();
3307 f5215bb9 2019-02-22 stsp doupdate();
3310 f44b1f58 2020-02-02 tracey static const struct got_error *
3311 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3313 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3315 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3316 f44b1f58 2020-02-02 tracey return NULL;
3319 f44b1f58 2020-02-02 tracey static const struct got_error *
3320 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3322 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3323 f44b1f58 2020-02-02 tracey int lineno;
3325 f44b1f58 2020-02-02 tracey if (!view->searching) {
3326 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3327 f44b1f58 2020-02-02 tracey return NULL;
3330 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3331 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3332 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3334 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3335 f44b1f58 2020-02-02 tracey } else {
3336 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3337 f44b1f58 2020-02-02 tracey lineno = 1;
3339 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3342 f44b1f58 2020-02-02 tracey while (1) {
3343 f44b1f58 2020-02-02 tracey char *line = NULL;
3344 f44b1f58 2020-02-02 tracey off_t offset;
3345 f44b1f58 2020-02-02 tracey size_t len;
3347 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3348 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3349 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3350 f44b1f58 2020-02-02 tracey free(line);
3354 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3355 f44b1f58 2020-02-02 tracey lineno = 1;
3357 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3360 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3361 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3362 f44b1f58 2020-02-02 tracey free(line);
3363 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3365 f44b1f58 2020-02-02 tracey free(line);
3366 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3367 41605754 2020-11-12 stsp if (line &&
3368 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3369 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3370 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3371 f44b1f58 2020-02-02 tracey free(line);
3374 f44b1f58 2020-02-02 tracey free(line);
3375 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3376 f44b1f58 2020-02-02 tracey lineno++;
3378 f44b1f58 2020-02-02 tracey lineno--;
3381 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3382 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3383 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3386 f44b1f58 2020-02-02 tracey return NULL;
3389 48ae06ee 2018-10-18 stsp static const struct got_error *
3390 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3391 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3392 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3393 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3395 48ae06ee 2018-10-18 stsp const struct got_error *err;
3396 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3398 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3400 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3401 15a94983 2018-12-23 stsp int type1, type2;
3402 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3404 15a94983 2018-12-23 stsp return err;
3405 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3407 15a94983 2018-12-23 stsp return err;
3409 15a94983 2018-12-23 stsp if (type1 != type2)
3410 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3412 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3413 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3414 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3415 f44b1f58 2020-02-02 tracey s->repo = repo;
3416 f44b1f58 2020-02-02 tracey s->id1 = id1;
3417 f44b1f58 2020-02-02 tracey s->id2 = id2;
3418 3dbaef42 2020-11-24 stsp s->label1 = label1;
3419 3dbaef42 2020-11-24 stsp s->label2 = label2;
3421 15a94983 2018-12-23 stsp if (id1) {
3422 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3423 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3424 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3426 5465d566 2020-02-01 tracey s->id1 = NULL;
3428 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3429 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3430 5465d566 2020-02-01 tracey free(s->id1);
3431 5465d566 2020-02-01 tracey s->id1 = NULL;
3432 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3434 5465d566 2020-02-01 tracey s->f = NULL;
3435 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3436 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3437 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3438 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3439 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3440 5465d566 2020-02-01 tracey s->log_view = log_view;
3441 5465d566 2020-02-01 tracey s->repo = repo;
3443 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3444 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3445 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3446 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3447 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3449 6d17833f 2019-11-08 stsp return err;
3450 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3451 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3452 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3453 6d17833f 2019-11-08 stsp if (err) {
3454 5465d566 2020-02-01 tracey free_colors(&s->colors);
3455 6d17833f 2019-11-08 stsp return err;
3457 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3458 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3459 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3460 6d17833f 2019-11-08 stsp if (err) {
3461 5465d566 2020-02-01 tracey free_colors(&s->colors);
3462 6d17833f 2019-11-08 stsp return err;
3465 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3466 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3467 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3468 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3469 6d17833f 2019-11-08 stsp if (err) {
3470 5465d566 2020-02-01 tracey free_colors(&s->colors);
3471 6d17833f 2019-11-08 stsp return err;
3474 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3475 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3476 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3477 11b20872 2019-11-08 stsp if (err) {
3478 5465d566 2020-02-01 tracey free_colors(&s->colors);
3479 11b20872 2019-11-08 stsp return err;
3482 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3483 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3484 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3485 11b20872 2019-11-08 stsp if (err) {
3486 5465d566 2020-02-01 tracey free_colors(&s->colors);
3487 11b20872 2019-11-08 stsp return err;
3491 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3492 78756c87 2020-11-24 stsp if (err) {
3493 78756c87 2020-11-24 stsp free(s->id1);
3494 78756c87 2020-11-24 stsp s->id1 = NULL;
3495 78756c87 2020-11-24 stsp free(s->id2);
3496 78756c87 2020-11-24 stsp s->id2 = NULL;
3497 78756c87 2020-11-24 stsp free_colors(&s->colors);
3498 78756c87 2020-11-24 stsp return err;
3501 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3502 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3503 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3505 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3506 fe621944 2020-11-10 stsp s->nlines = 0;
3507 5465d566 2020-02-01 tracey err = create_diff(s);
3508 48ae06ee 2018-10-18 stsp if (err) {
3509 5465d566 2020-02-01 tracey free(s->id1);
3510 5465d566 2020-02-01 tracey s->id1 = NULL;
3511 5465d566 2020-02-01 tracey free(s->id2);
3512 5465d566 2020-02-01 tracey s->id2 = NULL;
3513 78756c87 2020-11-24 stsp free_colors(&s->colors);
3514 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3515 48ae06ee 2018-10-18 stsp return err;
3518 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3519 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3520 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3521 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3522 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3524 5dc9f4bc 2018-08-04 stsp return NULL;
3527 e5a0f69f 2018-08-18 stsp static const struct got_error *
3528 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3530 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3531 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3533 5465d566 2020-02-01 tracey free(s->id1);
3534 5465d566 2020-02-01 tracey s->id1 = NULL;
3535 5465d566 2020-02-01 tracey free(s->id2);
3536 5465d566 2020-02-01 tracey s->id2 = NULL;
3537 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3538 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3539 5465d566 2020-02-01 tracey free_colors(&s->colors);
3540 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3541 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3542 fe621944 2020-11-10 stsp s->nlines = 0;
3543 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3544 e5a0f69f 2018-08-18 stsp return err;
3547 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3548 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3550 a3404814 2018-09-02 stsp const struct got_error *err;
3551 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3552 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3553 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3555 a3404814 2018-09-02 stsp if (s->id1) {
3556 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3558 a3404814 2018-09-02 stsp return err;
3559 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3561 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3563 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3565 a3404814 2018-09-02 stsp return err;
3566 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3568 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3569 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3570 a3404814 2018-09-02 stsp free(id_str1);
3571 a3404814 2018-09-02 stsp free(id_str2);
3572 a3404814 2018-09-02 stsp return err;
3574 a3404814 2018-09-02 stsp free(id_str1);
3575 a3404814 2018-09-02 stsp free(id_str2);
3577 fe621944 2020-11-10 stsp return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3578 fe621944 2020-11-10 stsp s->line_offsets, s->selected_line, view->nlines,
3579 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, header, &s->colors,
3580 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
3583 15a087fe 2019-02-21 stsp static const struct got_error *
3584 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3585 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3587 d7a04538 2019-02-21 stsp const struct got_error *err;
3588 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3589 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3590 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3592 15a087fe 2019-02-21 stsp free(s->id2);
3593 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3594 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3595 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3597 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3599 d7a04538 2019-02-21 stsp return err;
3600 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3601 15a087fe 2019-02-21 stsp free(s->id1);
3602 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3603 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3604 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3605 15a087fe 2019-02-21 stsp return NULL;
3608 0cf4efb1 2018-09-29 stsp static const struct got_error *
3609 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3610 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3612 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3613 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3614 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3615 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3618 e5a0f69f 2018-08-18 stsp switch (ch) {
3621 3dbaef42 2020-11-24 stsp if (ch == 'a')
3622 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3623 3dbaef42 2020-11-24 stsp if (ch == 'w')
3624 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3625 64453f7e 2020-11-21 stsp wclear(view->window);
3626 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3627 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3628 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3629 64453f7e 2020-11-21 stsp err = create_diff(s);
3632 1e37a5c2 2019-05-12 jcs case KEY_UP:
3633 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3634 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3636 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3637 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3638 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3641 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3642 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3643 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3646 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3647 1e37a5c2 2019-05-12 jcs if (!s->eof)
3648 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3650 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3651 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3653 00ba99a7 2019-05-12 jcs if (s->eof)
3656 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3657 1e37a5c2 2019-05-12 jcs char *line;
3658 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3659 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3660 1e37a5c2 2019-05-12 jcs if (line == NULL)
3665 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3666 1e37a5c2 2019-05-12 jcs s->diff_context--;
3667 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3668 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3669 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3670 27829c9e 2020-11-21 stsp s->nlines) {
3671 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3672 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3677 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3678 1e37a5c2 2019-05-12 jcs s->diff_context++;
3679 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3680 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3685 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3687 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3688 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3689 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3690 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3693 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3698 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3702 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3703 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3705 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3706 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3710 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3712 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3714 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3715 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3716 ffe38506 2020-12-01 naddy err = trigger_log_thread(s->log_view, 1);
3720 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3725 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3726 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3729 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3733 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3734 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3736 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3737 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3743 bcbd79e2 2018-08-19 stsp return err;
3746 4ed7e80c 2018-05-20 stsp static const struct got_error *
3747 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3749 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3750 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3751 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3752 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3753 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3754 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3755 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3756 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3757 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3758 3dbaef42 2020-11-24 stsp const char *errstr;
3759 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3761 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3762 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3763 eb6600df 2019-01-04 stsp NULL) == -1)
3764 26ed57b2 2018-05-19 stsp err(1, "pledge");
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 cwd = getcwd(NULL, 0);
3805 a273ac94 2020-02-23 naddy if (cwd == NULL)
3806 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3808 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3809 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3810 a273ac94 2020-02-23 naddy goto done;
3812 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3813 a273ac94 2020-02-23 naddy if (worktree)
3814 a273ac94 2020-02-23 naddy repo_path =
3815 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3817 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3819 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3820 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3821 a273ac94 2020-02-23 naddy goto done;
3824 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3825 eb6600df 2019-01-04 stsp if (error)
3826 eb6600df 2019-01-04 stsp goto done;
3828 a273ac94 2020-02-23 naddy init_curses();
3830 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3831 26ed57b2 2018-05-19 stsp if (error)
3832 26ed57b2 2018-05-19 stsp goto done;
3834 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3835 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3836 26ed57b2 2018-05-19 stsp if (error)
3837 26ed57b2 2018-05-19 stsp goto done;
3839 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3840 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3841 26ed57b2 2018-05-19 stsp if (error)
3842 26ed57b2 2018-05-19 stsp goto done;
3844 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3845 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3846 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3847 ea5e7bb5 2018-08-01 stsp goto done;
3849 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3850 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3851 5dc9f4bc 2018-08-04 stsp if (error)
3852 5dc9f4bc 2018-08-04 stsp goto done;
3853 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3855 3dbaef42 2020-11-24 stsp free(label1);
3856 3dbaef42 2020-11-24 stsp free(label2);
3857 c02c541e 2019-03-29 stsp free(repo_path);
3858 a273ac94 2020-02-23 naddy free(cwd);
3860 921be706 2019-06-28 stsp got_repo_close(repo);
3861 a273ac94 2020-02-23 naddy if (worktree)
3862 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3863 26ed57b2 2018-05-19 stsp return error;
3866 4ed7e80c 2018-05-20 stsp __dead static void
3867 9f7d7167 2018-04-29 stsp usage_blame(void)
3870 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3871 9f7d7167 2018-04-29 stsp getprogname());
3875 84451b3e 2018-07-10 stsp struct tog_blame_line {
3876 84451b3e 2018-07-10 stsp int annotated;
3877 84451b3e 2018-07-10 stsp struct got_object_id *id;
3880 4ed7e80c 2018-05-20 stsp static const struct got_error *
3881 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3882 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3883 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3884 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3885 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3887 84451b3e 2018-07-10 stsp const struct got_error *err;
3888 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3889 84451b3e 2018-07-10 stsp char *line;
3890 84451b3e 2018-07-10 stsp size_t len;
3891 84451b3e 2018-07-10 stsp wchar_t *wline;
3892 27a741e5 2019-09-11 stsp int width;
3893 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3894 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3895 ab089a2a 2018-07-12 stsp char *id_str;
3896 11b20872 2019-11-08 stsp struct tog_color *tc;
3898 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3900 ab089a2a 2018-07-12 stsp return err;
3902 84451b3e 2018-07-10 stsp rewind(f);
3903 f7d12f7e 2018-08-01 stsp werase(view->window);
3905 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3906 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3907 ab089a2a 2018-07-12 stsp free(id_str);
3908 ab089a2a 2018-07-12 stsp return err;
3911 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3912 ab089a2a 2018-07-12 stsp free(line);
3913 2550e4c3 2018-07-13 stsp line = NULL;
3915 1cae65b4 2019-09-22 stsp return err;
3916 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3917 a3404814 2018-09-02 stsp wstandout(view->window);
3918 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3920 11b20872 2019-11-08 stsp wattr_on(view->window,
3921 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3922 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3924 11b20872 2019-11-08 stsp wattr_off(view->window,
3925 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3926 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3927 a3404814 2018-09-02 stsp wstandend(view->window);
3928 2550e4c3 2018-07-13 stsp free(wline);
3929 2550e4c3 2018-07-13 stsp wline = NULL;
3930 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3931 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3933 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3934 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3935 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3936 ab089a2a 2018-07-12 stsp free(id_str);
3937 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3939 ab089a2a 2018-07-12 stsp free(id_str);
3940 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3941 3f60a8ef 2018-07-10 stsp free(line);
3942 2550e4c3 2018-07-13 stsp line = NULL;
3944 3f60a8ef 2018-07-10 stsp return err;
3945 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3946 2550e4c3 2018-07-13 stsp free(wline);
3947 2550e4c3 2018-07-13 stsp wline = NULL;
3948 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3949 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3952 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3953 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3954 84451b3e 2018-07-10 stsp if (line == NULL) {
3958 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3959 84451b3e 2018-07-10 stsp free(line);
3963 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3964 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3966 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3967 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3968 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3969 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3970 27a741e5 2019-09-11 stsp !(view->focussed &&
3971 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3972 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3973 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3974 8d0fe45a 2019-08-12 stsp char *id_str;
3975 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3976 8d0fe45a 2019-08-12 stsp if (err) {
3977 8d0fe45a 2019-08-12 stsp free(line);
3978 8d0fe45a 2019-08-12 stsp return err;
3980 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3982 11b20872 2019-11-08 stsp wattr_on(view->window,
3983 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3984 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3986 11b20872 2019-11-08 stsp wattr_off(view->window,
3987 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3988 8d0fe45a 2019-08-12 stsp free(id_str);
3989 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3991 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3992 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3995 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3996 ee41ec32 2018-07-10 stsp prev_id = NULL;
3999 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
4000 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4001 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4003 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4004 41605754 2020-11-12 stsp width = 9;
4005 41605754 2020-11-12 stsp wline = wcsdup(L"");
4006 41605754 2020-11-12 stsp if (wline == NULL) {
4007 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4008 41605754 2020-11-12 stsp free(line);
4009 41605754 2020-11-12 stsp return err;
4011 41605754 2020-11-12 stsp } else if (*first_displayed_line + nprinted == matched_line &&
4012 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4013 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4014 41605754 2020-11-12 stsp view->window, regmatch);
4015 41605754 2020-11-12 stsp if (err) {
4016 41605754 2020-11-12 stsp free(line);
4017 41605754 2020-11-12 stsp return err;
4019 41605754 2020-11-12 stsp width += 9;
4021 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4022 41605754 2020-11-12 stsp view->ncols - 9, 9);
4023 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4024 41605754 2020-11-12 stsp free(wline);
4025 41605754 2020-11-12 stsp wline = NULL;
4026 41605754 2020-11-12 stsp width += 9;
4029 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4030 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4031 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4032 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
4033 84451b3e 2018-07-10 stsp free(line);
4035 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
4037 1a57306a 2018-09-02 stsp view_vborder(view);
4039 84451b3e 2018-07-10 stsp return NULL;
4042 84451b3e 2018-07-10 stsp static const struct got_error *
4043 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4045 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4046 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4047 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4048 1a76625f 2018-10-22 stsp int errcode;
4050 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4051 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4052 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4054 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4055 1a76625f 2018-10-22 stsp if (errcode)
4056 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4058 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4059 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4060 d68a0a7d 2018-07-10 stsp goto done;
4063 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4064 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4066 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4067 d68a0a7d 2018-07-10 stsp if (line->annotated)
4068 d68a0a7d 2018-07-10 stsp goto done;
4070 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4071 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4072 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4073 84451b3e 2018-07-10 stsp goto done;
4075 84451b3e 2018-07-10 stsp line->annotated = 1;
4077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4078 1a76625f 2018-10-22 stsp if (errcode)
4079 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4080 84451b3e 2018-07-10 stsp return err;
4083 84451b3e 2018-07-10 stsp static void *
4084 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4086 18430de3 2018-07-10 stsp const struct got_error *err;
4087 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4088 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4089 1a76625f 2018-10-22 stsp int errcode;
4091 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4093 61266923 2020-01-14 stsp return (void *)err;
4095 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4096 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4097 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4098 fc06ba56 2019-08-22 stsp err = NULL;
4100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4101 1a76625f 2018-10-22 stsp if (errcode)
4102 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4103 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4105 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4106 c9beca56 2018-07-22 stsp ta->repo = NULL;
4107 c9beca56 2018-07-22 stsp *ta->complete = 1;
4109 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4110 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4111 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4113 18430de3 2018-07-10 stsp return (void *)err;
4116 245d91c1 2018-07-12 stsp static struct got_object_id *
4117 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4118 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4120 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4122 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4123 8d0fe45a 2019-08-12 stsp return NULL;
4125 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4126 245d91c1 2018-07-12 stsp if (!line->annotated)
4127 245d91c1 2018-07-12 stsp return NULL;
4129 245d91c1 2018-07-12 stsp return line->id;
4132 b880a918 2018-07-10 stsp static const struct got_error *
4133 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4135 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4138 245d91c1 2018-07-12 stsp if (blame->thread) {
4139 1a76625f 2018-10-22 stsp int errcode;
4140 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4141 1a76625f 2018-10-22 stsp if (errcode)
4142 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4143 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4144 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4145 1a76625f 2018-10-22 stsp if (errcode)
4146 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4147 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4148 1a76625f 2018-10-22 stsp if (errcode)
4149 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4150 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4151 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4152 245d91c1 2018-07-12 stsp err = NULL;
4153 245d91c1 2018-07-12 stsp blame->thread = NULL;
4155 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4156 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4157 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4159 245d91c1 2018-07-12 stsp if (blame->f) {
4160 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4161 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4162 245d91c1 2018-07-12 stsp blame->f = NULL;
4164 57670559 2018-12-23 stsp if (blame->lines) {
4165 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4166 57670559 2018-12-23 stsp free(blame->lines[i].id);
4167 57670559 2018-12-23 stsp free(blame->lines);
4168 57670559 2018-12-23 stsp blame->lines = NULL;
4170 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4171 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4173 245d91c1 2018-07-12 stsp return err;
4176 245d91c1 2018-07-12 stsp static const struct got_error *
4177 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4179 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4180 fc06ba56 2019-08-22 stsp int *done = arg;
4181 fc06ba56 2019-08-22 stsp int errcode;
4183 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4184 fc06ba56 2019-08-22 stsp if (errcode)
4185 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4186 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4188 fc06ba56 2019-08-22 stsp if (*done)
4189 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4191 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4192 fc06ba56 2019-08-22 stsp if (errcode)
4193 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4194 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4196 fc06ba56 2019-08-22 stsp return err;
4199 fc06ba56 2019-08-22 stsp static const struct got_error *
4200 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4202 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4203 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4204 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4205 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4206 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4207 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4208 15a94983 2018-12-23 stsp int obj_type;
4210 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4211 a5388363 2020-12-01 naddy s->path);
4213 15a94983 2018-12-23 stsp return err;
4215 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4217 84451b3e 2018-07-10 stsp goto done;
4219 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4220 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4221 84451b3e 2018-07-10 stsp goto done;
4224 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4226 a70480e0 2018-06-23 stsp goto done;
4227 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4228 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4229 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4230 84451b3e 2018-07-10 stsp goto done;
4232 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4233 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4234 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4235 84451b3e 2018-07-10 stsp goto done;
4237 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4238 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4239 b02560ec 2019-08-19 stsp blame->nlines--;
4241 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4242 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4243 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4244 84451b3e 2018-07-10 stsp goto done;
4247 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4249 bd24772e 2018-07-11 stsp goto done;
4251 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4252 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4253 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4254 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4255 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4256 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4257 245d91c1 2018-07-12 stsp goto done;
4259 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4261 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4262 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4263 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4264 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4265 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4266 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4267 a5388363 2020-12-01 naddy s->blame_complete = 0;
4271 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4272 27d434c2 2018-09-15 stsp free(obj_id);
4274 245d91c1 2018-07-12 stsp stop_blame(blame);
4275 245d91c1 2018-07-12 stsp return err;
4278 245d91c1 2018-07-12 stsp static const struct got_error *
4279 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4280 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4282 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4283 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4285 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4287 c4843652 2019-08-12 stsp s->path = strdup(path);
4288 c4843652 2019-08-12 stsp if (s->path == NULL)
4289 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4291 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4292 c4843652 2019-08-12 stsp if (err) {
4293 c4843652 2019-08-12 stsp free(s->path);
4294 7cbe629d 2018-08-04 stsp return err;
4297 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4298 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4299 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4300 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4301 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4302 fb2756b9 2018-08-04 stsp s->repo = repo;
4303 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4304 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4306 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4307 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4308 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4309 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4311 11b20872 2019-11-08 stsp return err;
4314 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4315 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4316 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4317 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4318 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4320 a5388363 2020-12-01 naddy return run_blame(view);
4323 e5a0f69f 2018-08-18 stsp static const struct got_error *
4324 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4326 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4327 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4329 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4330 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4332 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4333 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4334 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4335 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4336 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4339 e5a0f69f 2018-08-18 stsp free(s->path);
4340 11b20872 2019-11-08 stsp free_colors(&s->colors);
4342 e5a0f69f 2018-08-18 stsp return err;
4345 7cbe629d 2018-08-04 stsp static const struct got_error *
4346 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4348 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4350 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4351 6c4c42e0 2019-06-24 stsp return NULL;
4354 6c4c42e0 2019-06-24 stsp static const struct got_error *
4355 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4357 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4358 6c4c42e0 2019-06-24 stsp int lineno;
4360 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4361 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4362 6c4c42e0 2019-06-24 stsp return NULL;
4365 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4366 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4367 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4369 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4371 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4372 6c4c42e0 2019-06-24 stsp lineno = 1;
4374 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4377 6c4c42e0 2019-06-24 stsp while (1) {
4378 6c4c42e0 2019-06-24 stsp char *line = NULL;
4379 6c4c42e0 2019-06-24 stsp off_t offset;
4380 6c4c42e0 2019-06-24 stsp size_t len;
4382 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4383 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4384 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4385 6c4c42e0 2019-06-24 stsp free(line);
4389 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4390 6c4c42e0 2019-06-24 stsp lineno = 1;
4392 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4395 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4396 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4397 6c4c42e0 2019-06-24 stsp free(line);
4398 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4400 6c4c42e0 2019-06-24 stsp free(line);
4401 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4402 41605754 2020-11-12 stsp if (line &&
4403 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4404 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4406 6c4c42e0 2019-06-24 stsp free(line);
4409 6c4c42e0 2019-06-24 stsp free(line);
4410 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4416 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4417 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4418 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4421 6c4c42e0 2019-06-24 stsp return NULL;
4424 6c4c42e0 2019-06-24 stsp static const struct got_error *
4425 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4427 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4428 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4429 2b380cc8 2018-10-24 stsp int errcode;
4431 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4432 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4433 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4434 2b380cc8 2018-10-24 stsp if (errcode)
4435 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4437 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4440 51fe7530 2019-08-19 stsp if (s->blame_complete)
4441 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4443 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4444 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4445 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4446 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4447 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
4449 669b5ffa 2018-10-07 stsp view_vborder(view);
4450 e5a0f69f 2018-08-18 stsp return err;
4453 e5a0f69f 2018-08-18 stsp static const struct got_error *
4454 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4455 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4457 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4458 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4459 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4460 669b5ffa 2018-10-07 stsp int begin_x = 0;
4462 e5a0f69f 2018-08-18 stsp switch (ch) {
4464 1e37a5c2 2019-05-12 jcs s->done = 1;
4467 1e37a5c2 2019-05-12 jcs case KEY_UP:
4468 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4469 1e37a5c2 2019-05-12 jcs s->selected_line--;
4470 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4471 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4472 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4474 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4475 ea025d1d 2020-02-22 naddy case CTRL('b'):
4476 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4477 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4480 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4481 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4482 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4484 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4487 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4488 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4489 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4490 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4491 1e37a5c2 2019-05-12 jcs s->selected_line++;
4492 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4493 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4494 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4497 1e37a5c2 2019-05-12 jcs case 'p': {
4498 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4499 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4500 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4501 1e37a5c2 2019-05-12 jcs if (id == NULL)
4503 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4504 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4505 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4506 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4507 1e37a5c2 2019-05-12 jcs int obj_type;
4508 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4509 1e37a5c2 2019-05-12 jcs s->repo, id);
4512 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4513 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4514 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4515 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4518 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4519 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4520 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4521 e5a0f69f 2018-08-18 stsp if (err) {
4522 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4523 1e37a5c2 2019-05-12 jcs err = NULL;
4524 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4527 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4529 1e37a5c2 2019-05-12 jcs free(blob_id);
4530 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4531 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4532 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4535 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4537 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4539 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4540 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4542 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4547 1e37a5c2 2019-05-12 jcs s->done = 1;
4548 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4549 1e37a5c2 2019-05-12 jcs s->done = 0;
4550 1e37a5c2 2019-05-12 jcs if (thread_err)
4552 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4553 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4554 a5388363 2020-12-01 naddy err = run_blame(view);
4559 1e37a5c2 2019-05-12 jcs case 'B': {
4560 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4561 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4562 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4564 1e37a5c2 2019-05-12 jcs s->done = 1;
4565 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4566 1e37a5c2 2019-05-12 jcs s->done = 0;
4567 1e37a5c2 2019-05-12 jcs if (thread_err)
4569 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4570 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4571 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4572 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4573 a5388363 2020-12-01 naddy err = run_blame(view);
4578 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4579 1e37a5c2 2019-05-12 jcs case '\r': {
4580 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4581 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4582 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4583 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4584 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4585 1e37a5c2 2019-05-12 jcs if (id == NULL)
4587 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4590 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4591 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4593 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4594 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4595 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4596 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4597 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4600 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4601 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4602 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4604 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4607 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4608 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4611 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4613 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4616 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4617 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4619 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4624 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4625 ea025d1d 2020-02-22 naddy case CTRL('f'):
4627 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4628 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4629 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4632 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4633 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4634 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4635 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4638 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4639 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4640 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4641 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4643 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4644 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4645 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4647 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4648 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4649 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4650 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4656 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4659 a70480e0 2018-06-23 stsp static const struct got_error *
4660 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4662 a70480e0 2018-06-23 stsp const struct got_error *error;
4663 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4664 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4665 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4666 0587e10c 2020-07-23 stsp char *link_target = NULL;
4667 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4668 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4670 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4672 a70480e0 2018-06-23 stsp #ifndef PROFILE
4673 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4674 8e94dd5b 2019-01-04 stsp NULL) == -1)
4675 a70480e0 2018-06-23 stsp err(1, "pledge");
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 cwd = getcwd(NULL, 0);
4702 6962eb72 2020-02-20 stsp if (cwd == NULL)
4703 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4705 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4706 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4707 f135c941 2020-02-20 stsp goto done;
4709 69069811 2018-08-02 stsp if (repo_path == NULL) {
4710 f135c941 2020-02-20 stsp if (worktree)
4711 eb41ed75 2019-02-05 stsp repo_path =
4712 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4714 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4716 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4717 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4718 f135c941 2020-02-20 stsp goto done;
4721 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4722 c02c541e 2019-03-29 stsp if (error != NULL)
4723 8e94dd5b 2019-01-04 stsp goto done;
4725 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4726 f135c941 2020-02-20 stsp worktree);
4727 c02c541e 2019-03-29 stsp if (error)
4728 92205607 2019-01-04 stsp goto done;
4730 f135c941 2020-02-20 stsp init_curses();
4732 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4733 eb41ed75 2019-02-05 stsp if (error)
4734 69069811 2018-08-02 stsp goto done;
4736 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4737 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4738 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4739 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4740 a70480e0 2018-06-23 stsp if (error != NULL)
4741 66b4983c 2018-06-23 stsp goto done;
4742 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4743 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4745 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4746 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4748 a19e88aa 2018-06-23 stsp if (error != NULL)
4749 8b473291 2019-02-21 stsp goto done;
4751 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4752 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4753 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4754 e1cd8fed 2018-08-01 stsp goto done;
4757 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4758 0587e10c 2020-07-23 stsp commit_id, repo);
4759 7cbe629d 2018-08-04 stsp if (error)
4760 7cbe629d 2018-08-04 stsp goto done;
4762 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4763 78756c87 2020-11-24 stsp commit_id, repo);
4764 0587e10c 2020-07-23 stsp if (error)
4765 0587e10c 2020-07-23 stsp goto done;
4766 12314ad4 2019-08-31 stsp if (worktree) {
4767 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4768 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4769 12314ad4 2019-08-31 stsp worktree = NULL;
4771 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4773 69069811 2018-08-02 stsp free(repo_path);
4774 f135c941 2020-02-20 stsp free(in_repo_path);
4775 0587e10c 2020-07-23 stsp free(link_target);
4776 69069811 2018-08-02 stsp free(cwd);
4777 a70480e0 2018-06-23 stsp free(commit_id);
4778 eb41ed75 2019-02-05 stsp if (worktree)
4779 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4781 a70480e0 2018-06-23 stsp got_repo_close(repo);
4782 a70480e0 2018-06-23 stsp return error;
4785 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4786 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4787 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4788 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4789 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4790 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4791 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4792 0d6c6ee3 2020-05-20 stsp int isroot, struct tog_colors *colors, struct got_repository *repo)
4794 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4795 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4796 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4797 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4798 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4800 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4802 f7d12f7e 2018-08-01 stsp werase(view->window);
4804 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4805 ffd1d5e5 2018-06-23 stsp return NULL;
4807 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4809 ffd1d5e5 2018-06-23 stsp return err;
4810 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4811 a3404814 2018-09-02 stsp wstandout(view->window);
4812 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4814 11b20872 2019-11-08 stsp wattr_on(view->window,
4815 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4816 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4818 11b20872 2019-11-08 stsp wattr_off(view->window,
4819 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4820 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4821 a3404814 2018-09-02 stsp wstandend(view->window);
4822 2550e4c3 2018-07-13 stsp free(wline);
4823 2550e4c3 2018-07-13 stsp wline = NULL;
4824 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4825 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4826 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4827 ffd1d5e5 2018-06-23 stsp return NULL;
4828 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4830 ce52c690 2018-06-23 stsp return err;
4831 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4832 2550e4c3 2018-07-13 stsp free(wline);
4833 2550e4c3 2018-07-13 stsp wline = NULL;
4834 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4835 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4836 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4837 ffd1d5e5 2018-06-23 stsp return NULL;
4838 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4839 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4840 a1eca9bb 2018-06-23 stsp return NULL;
4842 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4843 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4844 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4845 0cf4efb1 2018-09-29 stsp if (view->focussed)
4846 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4847 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4849 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4850 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4851 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4852 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4853 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4854 ffd1d5e5 2018-06-23 stsp return NULL;
4858 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4861 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4862 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4863 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4864 848d6979 2019-08-12 stsp const char *modestr = "";
4865 56e0773d 2019-11-28 stsp mode_t mode;
4867 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4868 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4870 1d13200f 2018-07-12 stsp if (show_ids) {
4871 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4872 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4874 638f9024 2019-05-13 stsp return got_error_from_errno(
4875 230a42bd 2019-05-11 jcs "got_object_id_str");
4877 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4878 63c5ca5d 2019-08-24 stsp modestr = "$";
4879 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4882 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4883 0d6c6ee3 2020-05-20 stsp te, repo);
4884 0d6c6ee3 2020-05-20 stsp if (err) {
4885 0d6c6ee3 2020-05-20 stsp free(id_str);
4886 0d6c6ee3 2020-05-20 stsp return err;
4888 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4889 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4890 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4892 848d6979 2019-08-12 stsp modestr = "@";
4894 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4895 848d6979 2019-08-12 stsp modestr = "/";
4896 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4897 848d6979 2019-08-12 stsp modestr = "*";
4898 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4899 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4900 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4901 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4902 1d13200f 2018-07-12 stsp free(id_str);
4903 0d6c6ee3 2020-05-20 stsp free(link_target);
4904 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4906 1d13200f 2018-07-12 stsp free(id_str);
4907 0d6c6ee3 2020-05-20 stsp free(link_target);
4908 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4909 ffd1d5e5 2018-06-23 stsp if (err) {
4910 ffd1d5e5 2018-06-23 stsp free(line);
4913 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4914 0cf4efb1 2018-09-29 stsp if (view->focussed)
4915 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4916 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4918 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4920 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4921 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4922 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4924 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4925 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4926 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4927 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4928 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4929 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4930 ffd1d5e5 2018-06-23 stsp free(line);
4931 2550e4c3 2018-07-13 stsp free(wline);
4932 2550e4c3 2018-07-13 stsp wline = NULL;
4934 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4935 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4936 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4940 ffd1d5e5 2018-06-23 stsp return err;
4943 ffd1d5e5 2018-06-23 stsp static void
4944 694d3271 2020-12-01 naddy tree_scroll_up(struct tog_view *view, int maxscroll)
4946 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4947 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4948 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4949 fa86c4bf 2020-11-29 stsp int i = 0;
4951 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4954 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4955 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4956 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4957 fa86c4bf 2020-11-29 stsp if (!isroot)
4958 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4961 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4962 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4966 fa86c4bf 2020-11-29 stsp static void
4967 694d3271 2020-12-01 naddy tree_scroll_down(struct tog_view *view, int maxscroll)
4969 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4970 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4971 ffd1d5e5 2018-06-23 stsp int n = 0;
4973 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4974 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4975 694d3271 2020-12-01 naddy s->first_displayed_entry);
4977 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4979 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4980 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4981 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4982 768394f3 2019-01-24 stsp if (last) {
4983 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4984 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4989 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4990 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4991 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4993 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4994 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4995 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4997 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4998 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4999 56e0773d 2019-11-28 stsp + 1 /* slash */;
5001 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5003 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5004 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5005 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5007 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5008 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5009 d9765a41 2018-06-23 stsp while (pt) {
5010 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5011 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5012 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5013 cb2ebc8a 2018-06-23 stsp goto done;
5015 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5016 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5017 cb2ebc8a 2018-06-23 stsp goto done;
5019 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5022 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5023 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5024 ce52c690 2018-06-23 stsp goto done;
5028 ce52c690 2018-06-23 stsp if (err) {
5029 ce52c690 2018-06-23 stsp free(*path);
5030 ce52c690 2018-06-23 stsp *path = NULL;
5032 ce52c690 2018-06-23 stsp return err;
5035 ce52c690 2018-06-23 stsp static const struct got_error *
5036 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5037 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5040 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5041 ce52c690 2018-06-23 stsp char *path;
5042 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5044 a0de39f3 2019-08-09 stsp *new_view = NULL;
5046 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5048 ce52c690 2018-06-23 stsp return err;
5050 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5051 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5052 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5053 83ce39e3 2019-08-12 stsp goto done;
5056 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5057 e5a0f69f 2018-08-18 stsp if (err) {
5058 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5059 fc06ba56 2019-08-22 stsp err = NULL;
5060 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5062 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5064 83ce39e3 2019-08-12 stsp free(path);
5065 69efd4c4 2018-07-18 stsp return err;
5068 69efd4c4 2018-07-18 stsp static const struct got_error *
5069 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5070 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5071 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5073 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5074 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5075 69efd4c4 2018-07-18 stsp char *path;
5077 a0de39f3 2019-08-09 stsp *new_view = NULL;
5079 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5080 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5081 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5083 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5085 69efd4c4 2018-07-18 stsp return err;
5087 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5089 e5a0f69f 2018-08-18 stsp view_close(log_view);
5091 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5092 cb2ebc8a 2018-06-23 stsp free(path);
5093 cb2ebc8a 2018-06-23 stsp return err;
5096 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5097 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5098 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5100 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5101 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5102 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5104 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5106 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5107 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5108 ffd1d5e5 2018-06-23 stsp goto done;
5110 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5111 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5112 ffd1d5e5 2018-06-23 stsp goto done;
5115 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5116 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5117 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5118 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5119 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5120 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5121 6484ec90 2018-09-29 stsp goto done;
5123 fb2756b9 2018-08-04 stsp s->repo = repo;
5125 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5127 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5128 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5129 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5130 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5132 c0b01bdb 2019-11-08 stsp goto done;
5133 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5134 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5135 c0b01bdb 2019-11-08 stsp if (err) {
5136 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5137 c0b01bdb 2019-11-08 stsp goto done;
5139 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5140 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5141 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5142 c0b01bdb 2019-11-08 stsp if (err) {
5143 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5144 c0b01bdb 2019-11-08 stsp goto done;
5147 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5148 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5149 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5150 11b20872 2019-11-08 stsp if (err) {
5151 11b20872 2019-11-08 stsp free_colors(&s->colors);
5152 11b20872 2019-11-08 stsp goto done;
5155 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5156 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5157 c0b01bdb 2019-11-08 stsp if (err) {
5158 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5159 c0b01bdb 2019-11-08 stsp goto done;
5163 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5164 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5165 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5166 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5167 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5169 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5170 6484ec90 2018-09-29 stsp if (err) {
5171 fb2756b9 2018-08-04 stsp free(s->tree_label);
5172 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5174 ad80ab7b 2018-08-04 stsp return err;
5177 e5a0f69f 2018-08-18 stsp static const struct got_error *
5178 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5180 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5182 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5183 fb2756b9 2018-08-04 stsp free(s->tree_label);
5184 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5185 6484ec90 2018-09-29 stsp free(s->commit_id);
5186 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5187 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5188 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5189 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5190 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5191 ad80ab7b 2018-08-04 stsp free(parent);
5194 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5195 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5196 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5197 7c32bd05 2019-06-22 stsp return NULL;
5200 7c32bd05 2019-06-22 stsp static const struct got_error *
5201 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5203 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5205 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5206 7c32bd05 2019-06-22 stsp return NULL;
5209 7c32bd05 2019-06-22 stsp static int
5210 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5212 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5214 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5218 7c32bd05 2019-06-22 stsp static const struct got_error *
5219 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5221 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5222 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5224 7c32bd05 2019-06-22 stsp if (!view->searching) {
5225 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5226 7c32bd05 2019-06-22 stsp return NULL;
5229 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5230 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5231 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5232 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5233 56e0773d 2019-11-28 stsp s->selected_entry);
5235 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5237 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5238 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5240 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5241 56e0773d 2019-11-28 stsp s->selected_entry);
5244 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5245 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5247 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5250 7c32bd05 2019-06-22 stsp while (1) {
5251 56e0773d 2019-11-28 stsp if (te == NULL) {
5252 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5253 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5254 ac66afb8 2019-06-24 stsp return NULL;
5256 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5257 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5259 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5262 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5263 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5264 56e0773d 2019-11-28 stsp s->matched_entry = te;
5268 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5269 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5271 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5274 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5275 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5276 7c32bd05 2019-06-22 stsp s->selected = 0;
5279 e5a0f69f 2018-08-18 stsp return NULL;
5282 ad80ab7b 2018-08-04 stsp static const struct got_error *
5283 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5285 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5286 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5287 e5a0f69f 2018-08-18 stsp char *parent_path;
5289 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5291 e5a0f69f 2018-08-18 stsp return err;
5293 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5294 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5295 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5296 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5297 0d6c6ee3 2020-05-20 stsp &s->colors, s->repo);
5298 e5a0f69f 2018-08-18 stsp free(parent_path);
5300 669b5ffa 2018-10-07 stsp view_vborder(view);
5301 e5a0f69f 2018-08-18 stsp return err;
5304 e5a0f69f 2018-08-18 stsp static const struct got_error *
5305 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5306 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5308 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5309 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5310 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5311 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5313 e5a0f69f 2018-08-18 stsp switch (ch) {
5315 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5318 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5320 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5321 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5322 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5323 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5324 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5325 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5327 1e37a5c2 2019-05-12 jcs return err;
5328 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5330 1e37a5c2 2019-05-12 jcs view_close(log_view);
5333 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5334 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5336 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5339 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5340 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5341 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5342 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5343 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5344 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5345 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5346 152c1c93 2020-11-29 stsp if (err) {
5347 152c1c93 2020-11-29 stsp view_close(ref_view);
5348 152c1c93 2020-11-29 stsp return err;
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 152c1c93 2020-11-29 stsp err = view_set_child(view, ref_view);
5355 152c1c93 2020-11-29 stsp if (err) {
5356 152c1c93 2020-11-29 stsp view_close(ref_view);
5359 152c1c93 2020-11-29 stsp *focus_view = ref_view;
5360 152c1c93 2020-11-29 stsp view->child_focussed = 1;
5362 152c1c93 2020-11-29 stsp *new_view = ref_view;
5365 1e37a5c2 2019-05-12 jcs case KEY_UP:
5366 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5367 1e37a5c2 2019-05-12 jcs s->selected--;
5370 694d3271 2020-12-01 naddy tree_scroll_up(view, 1);
5372 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5373 ea025d1d 2020-02-22 naddy case CTRL('b'):
5374 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5375 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5376 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5377 fa86c4bf 2020-11-29 stsp s->selected = 0;
5379 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5380 fa86c4bf 2020-11-29 stsp s->selected = 0;
5382 694d3271 2020-12-01 naddy tree_scroll_up(view, MAX(0, view->nlines - 3));
5385 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5386 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5387 1e37a5c2 2019-05-12 jcs s->selected++;
5390 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5392 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5394 694d3271 2020-12-01 naddy tree_scroll_down(view, 1);
5396 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5397 ea025d1d 2020-02-22 naddy case CTRL('f'):
5398 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5400 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5401 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5402 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5405 694d3271 2020-12-01 naddy tree_scroll_down(view, view->nlines - 3);
5407 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5409 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5410 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5411 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5412 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5413 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5415 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5416 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5418 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5419 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5420 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5421 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5422 1e37a5c2 2019-05-12 jcs s->selected_entry =
5423 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5424 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5425 1e37a5c2 2019-05-12 jcs free(parent);
5426 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5427 56e0773d 2019-11-28 stsp s->selected_entry))) {
5428 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5429 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5430 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5433 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5434 941e9f74 2019-05-21 stsp if (err) {
5435 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5438 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5439 56e0773d 2019-11-28 stsp s->selected_entry))) {
5440 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5441 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5442 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5444 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5445 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5446 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5449 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5450 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5452 669b5ffa 2018-10-07 stsp return err;
5453 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5454 669b5ffa 2018-10-07 stsp if (err) {
5455 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5458 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5459 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5461 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5464 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5465 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5466 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5472 ffd1d5e5 2018-06-23 stsp return err;
5475 ffd1d5e5 2018-06-23 stsp __dead static void
5476 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5479 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5480 ffd1d5e5 2018-06-23 stsp getprogname());
5484 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5485 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5487 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5488 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5489 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5490 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5491 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5492 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5493 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5494 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5496 5221c383 2018-08-01 stsp struct tog_view *view;
5498 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5499 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5500 d188b9a6 2019-01-04 stsp NULL) == -1)
5501 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5504 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5505 ffd1d5e5 2018-06-23 stsp switch (ch) {
5507 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5510 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5511 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5512 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5516 e99e2d15 2020-11-24 naddy usage_tree();
5517 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5521 ffd1d5e5 2018-06-23 stsp argc -= optind;
5522 ffd1d5e5 2018-06-23 stsp argv += optind;
5524 55cccc34 2020-02-20 stsp if (argc > 1)
5525 e99e2d15 2020-11-24 naddy usage_tree();
5527 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5528 55cccc34 2020-02-20 stsp if (cwd == NULL)
5529 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5531 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5532 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5533 55cccc34 2020-02-20 stsp goto done;
5535 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5536 55cccc34 2020-02-20 stsp if (worktree)
5537 52185f70 2019-02-05 stsp repo_path =
5538 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5540 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5542 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5543 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5544 55cccc34 2020-02-20 stsp goto done;
5547 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5548 c02c541e 2019-03-29 stsp if (error != NULL)
5549 52185f70 2019-02-05 stsp goto done;
5551 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5552 55cccc34 2020-02-20 stsp repo, worktree);
5553 55cccc34 2020-02-20 stsp if (error)
5554 55cccc34 2020-02-20 stsp goto done;
5556 55cccc34 2020-02-20 stsp init_curses();
5558 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5559 c02c541e 2019-03-29 stsp if (error)
5560 52185f70 2019-02-05 stsp goto done;
5562 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5563 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5564 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5565 55cccc34 2020-02-20 stsp if (error)
5566 ffd1d5e5 2018-06-23 stsp goto done;
5568 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5569 55cccc34 2020-02-20 stsp if (error)
5570 ffd1d5e5 2018-06-23 stsp goto done;
5572 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5573 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5574 55cccc34 2020-02-20 stsp if (error)
5575 8b473291 2019-02-21 stsp goto done;
5577 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5578 5221c383 2018-08-01 stsp if (view == NULL) {
5579 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5580 5221c383 2018-08-01 stsp goto done;
5582 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5583 ad80ab7b 2018-08-04 stsp if (error)
5584 ad80ab7b 2018-08-04 stsp goto done;
5585 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5586 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5587 55cccc34 2020-02-20 stsp in_repo_path, repo);
5588 55cccc34 2020-02-20 stsp if (error)
5589 55cccc34 2020-02-20 stsp goto done;
5592 55cccc34 2020-02-20 stsp if (worktree) {
5593 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5594 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5595 55cccc34 2020-02-20 stsp worktree = NULL;
5597 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5599 52185f70 2019-02-05 stsp free(repo_path);
5600 e4a0e26d 2020-02-20 stsp free(cwd);
5601 ffd1d5e5 2018-06-23 stsp free(commit_id);
5602 ffd1d5e5 2018-06-23 stsp if (commit)
5603 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5605 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5607 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5608 ffd1d5e5 2018-06-23 stsp return error;
5611 6458efa5 2020-11-24 stsp static const struct got_error *
5612 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5614 6458efa5 2020-11-24 stsp const struct got_error *err;
5615 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5616 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5618 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5619 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5621 6458efa5 2020-11-24 stsp return err;
5623 6458efa5 2020-11-24 stsp s->nrefs = 0;
5624 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5625 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5628 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5629 6458efa5 2020-11-24 stsp if (re == NULL)
5630 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5632 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5633 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5634 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5637 6458efa5 2020-11-24 stsp return NULL;
5641 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5643 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5645 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5646 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5647 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5650 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5653 6458efa5 2020-11-24 stsp static const struct got_error *
5654 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5656 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5657 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5659 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5660 6458efa5 2020-11-24 stsp s->repo = repo;
5662 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5663 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5664 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5666 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5668 6458efa5 2020-11-24 stsp return err;
5670 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5672 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5673 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5674 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5675 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5677 6458efa5 2020-11-24 stsp goto done;
5679 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5680 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5681 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5683 6458efa5 2020-11-24 stsp goto done;
5685 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5686 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5687 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5689 6458efa5 2020-11-24 stsp goto done;
5692 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5693 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5694 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5695 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5696 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5699 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5700 6458efa5 2020-11-24 stsp return err;
5703 6458efa5 2020-11-24 stsp static const struct got_error *
5704 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5706 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5708 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5709 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5711 6458efa5 2020-11-24 stsp return NULL;
5714 6458efa5 2020-11-24 stsp static const struct got_error *
5715 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5716 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5718 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5719 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5720 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5721 6458efa5 2020-11-24 stsp int obj_type;
5723 c42c9805 2020-11-24 stsp *commit_id = NULL;
5725 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5727 6458efa5 2020-11-24 stsp return err;
5729 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5731 6458efa5 2020-11-24 stsp goto done;
5733 6458efa5 2020-11-24 stsp switch (obj_type) {
5734 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5735 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5737 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5738 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5740 6458efa5 2020-11-24 stsp goto done;
5741 c42c9805 2020-11-24 stsp free(obj_id);
5742 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5743 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5745 6458efa5 2020-11-24 stsp goto done;
5746 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5747 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5748 c42c9805 2020-11-24 stsp goto done;
5750 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5751 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5752 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5753 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5754 c42c9805 2020-11-24 stsp goto done;
5758 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5764 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5765 c42c9805 2020-11-24 stsp if (err) {
5766 c42c9805 2020-11-24 stsp free(*commit_id);
5767 c42c9805 2020-11-24 stsp *commit_id = NULL;
5769 c42c9805 2020-11-24 stsp return err;
5772 c42c9805 2020-11-24 stsp static const struct got_error *
5773 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5774 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5776 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5777 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5778 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5780 c42c9805 2020-11-24 stsp *new_view = NULL;
5782 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5783 c42c9805 2020-11-24 stsp if (err) {
5784 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5785 c42c9805 2020-11-24 stsp return err;
5787 c42c9805 2020-11-24 stsp return NULL;
5790 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5791 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5792 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5793 6458efa5 2020-11-24 stsp goto done;
5796 6458efa5 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5799 6458efa5 2020-11-24 stsp view_close(log_view);
5801 6458efa5 2020-11-24 stsp *new_view = log_view;
5802 c42c9805 2020-11-24 stsp free(commit_id);
5803 6458efa5 2020-11-24 stsp return err;
5806 ce5b7c56 2019-07-09 stsp static void
5807 694d3271 2020-12-01 naddy ref_scroll_up(struct tog_view *view, int maxscroll)
5809 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5810 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5811 34ba6917 2020-11-30 stsp int i = 0;
5813 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5816 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5817 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5818 34ba6917 2020-11-30 stsp if (re == NULL)
5820 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5821 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5825 34ba6917 2020-11-30 stsp static void
5826 694d3271 2020-12-01 naddy ref_scroll_down(struct tog_view *view, int maxscroll)
5828 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5829 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5830 6458efa5 2020-11-24 stsp int n = 0;
5832 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5833 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5835 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5837 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5838 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5839 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5840 6458efa5 2020-11-24 stsp if (last) {
5841 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5842 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5847 6458efa5 2020-11-24 stsp static const struct got_error *
5848 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5850 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5852 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5853 6458efa5 2020-11-24 stsp return NULL;
5856 6458efa5 2020-11-24 stsp static int
5857 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5859 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5861 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5865 6458efa5 2020-11-24 stsp static const struct got_error *
5866 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5868 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5869 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5871 6458efa5 2020-11-24 stsp if (!view->searching) {
5872 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5873 6458efa5 2020-11-24 stsp return NULL;
5876 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5877 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5878 6458efa5 2020-11-24 stsp if (s->selected_entry)
5879 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5881 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5882 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5884 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5885 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5887 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5888 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5891 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5892 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5894 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5897 6458efa5 2020-11-24 stsp while (1) {
5898 6458efa5 2020-11-24 stsp if (re == NULL) {
5899 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5900 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5901 6458efa5 2020-11-24 stsp return NULL;
5903 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5904 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5906 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5909 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5910 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5911 6458efa5 2020-11-24 stsp s->matched_entry = re;
5915 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5916 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5918 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5921 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5922 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5923 6458efa5 2020-11-24 stsp s->selected = 0;
5926 6458efa5 2020-11-24 stsp return NULL;
5929 6458efa5 2020-11-24 stsp static const struct got_error *
5930 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5932 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5933 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5934 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5935 6458efa5 2020-11-24 stsp char *line = NULL;
5936 6458efa5 2020-11-24 stsp wchar_t *wline;
5937 6458efa5 2020-11-24 stsp struct tog_color *tc;
5938 6458efa5 2020-11-24 stsp int width, n;
5939 6458efa5 2020-11-24 stsp int limit = view->nlines;
5941 6458efa5 2020-11-24 stsp werase(view->window);
5943 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5945 6458efa5 2020-11-24 stsp if (limit == 0)
5946 6458efa5 2020-11-24 stsp return NULL;
5948 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5950 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5951 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5952 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5954 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5955 6458efa5 2020-11-24 stsp if (err) {
5956 6458efa5 2020-11-24 stsp free(line);
5957 6458efa5 2020-11-24 stsp return err;
5959 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5960 6458efa5 2020-11-24 stsp wstandout(view->window);
5961 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5962 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5963 6458efa5 2020-11-24 stsp wstandend(view->window);
5964 6458efa5 2020-11-24 stsp free(wline);
5965 6458efa5 2020-11-24 stsp wline = NULL;
5966 6458efa5 2020-11-24 stsp free(line);
5967 6458efa5 2020-11-24 stsp line = NULL;
5968 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5969 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5970 6458efa5 2020-11-24 stsp if (--limit <= 0)
5971 6458efa5 2020-11-24 stsp return NULL;
5974 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5975 6458efa5 2020-11-24 stsp char *line = NULL;
5977 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5978 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5979 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5980 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5981 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5982 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5983 6458efa5 2020-11-24 stsp struct got_object_id *id;
5984 6458efa5 2020-11-24 stsp char *id_str;
5985 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5987 6458efa5 2020-11-24 stsp return err;
5988 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5989 6458efa5 2020-11-24 stsp if (err) {
5991 6458efa5 2020-11-24 stsp return err;
5993 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5994 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5995 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5997 6458efa5 2020-11-24 stsp free(id_str);
5998 6458efa5 2020-11-24 stsp return err;
6001 6458efa5 2020-11-24 stsp free(id_str);
6003 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6004 6458efa5 2020-11-24 stsp if (line == NULL)
6005 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6008 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6009 6458efa5 2020-11-24 stsp if (err) {
6010 6458efa5 2020-11-24 stsp free(line);
6011 6458efa5 2020-11-24 stsp return err;
6013 6458efa5 2020-11-24 stsp if (n == s->selected) {
6014 6458efa5 2020-11-24 stsp if (view->focussed)
6015 6458efa5 2020-11-24 stsp wstandout(view->window);
6016 6458efa5 2020-11-24 stsp s->selected_entry = re;
6018 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6020 6458efa5 2020-11-24 stsp wattr_on(view->window,
6021 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6022 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6024 6458efa5 2020-11-24 stsp wattr_off(view->window,
6025 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6026 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6027 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6028 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6029 6458efa5 2020-11-24 stsp wstandend(view->window);
6030 6458efa5 2020-11-24 stsp free(line);
6031 6458efa5 2020-11-24 stsp free(wline);
6032 6458efa5 2020-11-24 stsp wline = NULL;
6034 6458efa5 2020-11-24 stsp s->ndisplayed++;
6035 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6038 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6041 6458efa5 2020-11-24 stsp view_vborder(view);
6042 6458efa5 2020-11-24 stsp return err;
6045 6458efa5 2020-11-24 stsp static const struct got_error *
6046 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6047 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6049 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6050 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6051 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6052 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6054 c42c9805 2020-11-24 stsp *new_view = NULL;
6056 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6057 c42c9805 2020-11-24 stsp if (err) {
6058 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6059 c42c9805 2020-11-24 stsp return err;
6061 c42c9805 2020-11-24 stsp return NULL;
6064 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6066 c42c9805 2020-11-24 stsp goto done;
6068 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6070 c42c9805 2020-11-24 stsp goto done;
6072 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6073 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6074 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6075 c42c9805 2020-11-24 stsp goto done;
6078 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6080 c42c9805 2020-11-24 stsp goto done;
6082 c42c9805 2020-11-24 stsp *new_view = tree_view;
6084 c42c9805 2020-11-24 stsp free(commit_id);
6085 c42c9805 2020-11-24 stsp free(tree_id);
6086 c42c9805 2020-11-24 stsp if (err) {
6088 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6090 c42c9805 2020-11-24 stsp return err;
6092 c42c9805 2020-11-24 stsp static const struct got_error *
6093 6458efa5 2020-11-24 stsp input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6094 6458efa5 2020-11-24 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
6096 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6097 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6098 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6099 34ba6917 2020-11-30 stsp int begin_x = 0;
6101 6458efa5 2020-11-24 stsp switch (ch) {
6103 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6105 6458efa5 2020-11-24 stsp case KEY_ENTER:
6106 6458efa5 2020-11-24 stsp case '\r':
6107 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6109 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6110 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6111 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6113 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6114 6458efa5 2020-11-24 stsp err = view_close_child(view);
6116 6458efa5 2020-11-24 stsp return err;
6117 6458efa5 2020-11-24 stsp err = view_set_child(view, log_view);
6118 6458efa5 2020-11-24 stsp if (err) {
6119 6458efa5 2020-11-24 stsp view_close(log_view);
6122 6458efa5 2020-11-24 stsp *focus_view = log_view;
6123 6458efa5 2020-11-24 stsp view->child_focussed = 1;
6125 6458efa5 2020-11-24 stsp *new_view = log_view;
6128 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6130 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6131 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6132 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6134 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
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 c42c9805 2020-11-24 stsp err = view_set_child(view, tree_view);
6141 c42c9805 2020-11-24 stsp if (err) {
6142 c42c9805 2020-11-24 stsp view_close(tree_view);
6145 c42c9805 2020-11-24 stsp *focus_view = tree_view;
6146 c42c9805 2020-11-24 stsp view->child_focussed = 1;
6148 c42c9805 2020-11-24 stsp *new_view = tree_view;
6151 6458efa5 2020-11-24 stsp case KEY_UP:
6152 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6153 6458efa5 2020-11-24 stsp s->selected--;
6156 694d3271 2020-12-01 naddy ref_scroll_up(view, 1);
6158 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6159 6458efa5 2020-11-24 stsp case CTRL('b'):
6160 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6161 34ba6917 2020-11-30 stsp s->selected = 0;
6162 694d3271 2020-12-01 naddy ref_scroll_up(view, MAX(0, view->nlines - 1));
6165 6458efa5 2020-11-24 stsp case KEY_DOWN:
6166 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6167 6458efa5 2020-11-24 stsp s->selected++;
6170 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6171 6458efa5 2020-11-24 stsp /* can't scroll any further */
6173 694d3271 2020-12-01 naddy ref_scroll_down(view, 1);
6175 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6176 6458efa5 2020-11-24 stsp case CTRL('f'):
6177 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6178 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6179 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6180 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6183 694d3271 2020-12-01 naddy ref_scroll_down(view, view->nlines - 1);
6185 6458efa5 2020-11-24 stsp case CTRL('l'):
6186 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6187 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6189 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6190 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6191 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6197 6458efa5 2020-11-24 stsp return err;
6200 6458efa5 2020-11-24 stsp __dead static void
6201 6458efa5 2020-11-24 stsp usage_ref(void)
6204 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6205 6458efa5 2020-11-24 stsp getprogname());
6209 6458efa5 2020-11-24 stsp static const struct got_error *
6210 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6212 6458efa5 2020-11-24 stsp const struct got_error *error;
6213 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6214 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6215 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6217 6458efa5 2020-11-24 stsp struct tog_view *view;
6219 6458efa5 2020-11-24 stsp #ifndef PROFILE
6220 6458efa5 2020-11-24 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6221 6458efa5 2020-11-24 stsp NULL) == -1)
6222 6458efa5 2020-11-24 stsp err(1, "pledge");
6225 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6226 6458efa5 2020-11-24 stsp switch (ch) {
6228 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6229 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6230 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6234 e99e2d15 2020-11-24 naddy usage_ref();
6235 6458efa5 2020-11-24 stsp /* NOTREACHED */
6239 6458efa5 2020-11-24 stsp argc -= optind;
6240 6458efa5 2020-11-24 stsp argv += optind;
6242 6458efa5 2020-11-24 stsp if (argc > 1)
6243 e99e2d15 2020-11-24 naddy usage_ref();
6245 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6246 6458efa5 2020-11-24 stsp if (cwd == NULL)
6247 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6249 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6250 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6251 6458efa5 2020-11-24 stsp goto done;
6253 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6254 6458efa5 2020-11-24 stsp if (worktree)
6255 6458efa5 2020-11-24 stsp repo_path =
6256 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6258 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6260 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6261 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6262 6458efa5 2020-11-24 stsp goto done;
6265 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6266 6458efa5 2020-11-24 stsp if (error != NULL)
6267 6458efa5 2020-11-24 stsp goto done;
6269 6458efa5 2020-11-24 stsp init_curses();
6271 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6272 6458efa5 2020-11-24 stsp if (error)
6273 6458efa5 2020-11-24 stsp goto done;
6275 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6276 6458efa5 2020-11-24 stsp if (view == NULL) {
6277 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6278 6458efa5 2020-11-24 stsp goto done;
6281 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6282 6458efa5 2020-11-24 stsp if (error)
6283 6458efa5 2020-11-24 stsp goto done;
6285 6458efa5 2020-11-24 stsp if (worktree) {
6286 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6287 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6288 6458efa5 2020-11-24 stsp worktree = NULL;
6290 6458efa5 2020-11-24 stsp error = view_loop(view);
6292 6458efa5 2020-11-24 stsp free(repo_path);
6293 6458efa5 2020-11-24 stsp free(cwd);
6295 6458efa5 2020-11-24 stsp got_repo_close(repo);
6296 6458efa5 2020-11-24 stsp return error;
6299 6458efa5 2020-11-24 stsp static void
6300 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6304 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6305 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6306 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6307 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6309 6879ba42 2020-10-01 naddy fputc('\n', fp);
6312 4ed7e80c 2018-05-20 stsp __dead static void
6313 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6315 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6317 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6318 6879ba42 2020-10-01 naddy getprogname());
6319 6879ba42 2020-10-01 naddy if (hflag) {
6320 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6321 6879ba42 2020-10-01 naddy list_commands(fp);
6323 6879ba42 2020-10-01 naddy exit(status);
6326 c2301be8 2018-04-30 stsp static char **
6327 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6329 ee85c5e8 2020-02-29 stsp va_list ap;
6330 c2301be8 2018-04-30 stsp char **argv;
6333 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6335 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6336 c2301be8 2018-04-30 stsp if (argv == NULL)
6337 c2301be8 2018-04-30 stsp err(1, "calloc");
6338 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6339 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6340 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6341 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6344 ee85c5e8 2020-02-29 stsp va_end(ap);
6345 c2301be8 2018-04-30 stsp return argv;
6349 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6350 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6351 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6352 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6353 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6355 ee85c5e8 2020-02-29 stsp static const struct got_error *
6356 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6358 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6359 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6360 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6361 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6362 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6363 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6364 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6366 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6367 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6368 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6370 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6371 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6372 ee85c5e8 2020-02-29 stsp goto done;
6374 ee85c5e8 2020-02-29 stsp if (worktree)
6375 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6377 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6378 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6379 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6380 ee85c5e8 2020-02-29 stsp goto done;
6383 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6384 ee85c5e8 2020-02-29 stsp if (error != NULL)
6385 ee85c5e8 2020-02-29 stsp goto done;
6387 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6388 ee85c5e8 2020-02-29 stsp repo, worktree);
6389 ee85c5e8 2020-02-29 stsp if (error)
6390 ee85c5e8 2020-02-29 stsp goto done;
6392 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6393 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6394 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6395 ee85c5e8 2020-02-29 stsp if (error)
6396 ee85c5e8 2020-02-29 stsp goto done;
6398 ee85c5e8 2020-02-29 stsp if (worktree) {
6399 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6400 ee85c5e8 2020-02-29 stsp worktree = NULL;
6403 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6404 ee85c5e8 2020-02-29 stsp if (error) {
6405 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6406 ee85c5e8 2020-02-29 stsp goto done;
6407 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6408 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6409 6879ba42 2020-10-01 naddy usage(1, 1);
6410 ee85c5e8 2020-02-29 stsp /* not reached */
6413 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6414 ee85c5e8 2020-02-29 stsp repo = NULL;
6416 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6417 ee85c5e8 2020-02-29 stsp if (error)
6418 ee85c5e8 2020-02-29 stsp goto done;
6420 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6422 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6423 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6426 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6427 ee85c5e8 2020-02-29 stsp if (worktree)
6428 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6430 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6431 ee85c5e8 2020-02-29 stsp free(commit_id);
6432 ee85c5e8 2020-02-29 stsp free(cwd);
6433 ee85c5e8 2020-02-29 stsp free(repo_path);
6434 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6435 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6437 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6438 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6439 ee85c5e8 2020-02-29 stsp free(cmd_argv);
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 c2301be8 2018-04-30 stsp if (argc == 0) {
6483 f29d3e89 2018-06-23 stsp if (hflag)
6484 6879ba42 2020-10-01 naddy usage(hflag, 0);
6485 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6486 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6488 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6492 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6493 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6494 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6495 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6496 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6502 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6503 ee85c5e8 2020-02-29 stsp if (argc != 1)
6504 6879ba42 2020-10-01 naddy usage(0, 1);
6505 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6506 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6508 ee85c5e8 2020-02-29 stsp if (hflag)
6509 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6511 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6515 b46c1e04 2020-09-20 naddy putchar('\n');
6516 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6518 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6519 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6520 a2f4a359 2020-02-28 stsp free(cmd_argv);
6523 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6524 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);