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;
453 5dc9f4bc 2018-08-04 stsp /* type-specific state */
454 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
456 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
457 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
458 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
459 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
460 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
463 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
464 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
465 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
466 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
468 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
469 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
470 60493ae3 2019-06-20 stsp int searching;
471 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
472 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
473 60493ae3 2019-06-20 stsp int search_next_done;
474 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
475 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
476 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
477 1803e47f 2019-06-22 stsp regex_t regex;
478 41605754 2020-11-12 stsp regmatch_t regmatch;
481 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
482 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
483 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
484 78756c87 2020-11-24 stsp struct got_repository *);
485 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
486 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
487 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
488 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
489 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
490 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
492 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
493 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
494 78756c87 2020-11-24 stsp const char *, const char *, int);
495 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
496 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
497 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
498 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
499 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
500 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
502 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
503 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
504 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
505 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
506 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
507 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
508 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
509 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
511 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
512 78756c87 2020-11-24 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
513 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
514 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
515 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
516 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
517 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
518 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
520 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
521 6458efa5 2020-11-24 stsp struct got_repository *);
522 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
523 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
524 6458efa5 2020-11-24 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
525 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
526 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
527 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
529 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
530 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
531 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
533 25791caa 2018-10-24 stsp static void
534 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
536 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
539 83baff54 2019-08-12 stsp static void
540 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
542 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
545 61266923 2020-01-14 stsp static void
546 61266923 2020-01-14 stsp tog_sigcont(int signo)
548 61266923 2020-01-14 stsp tog_sigcont_received = 1;
551 e5a0f69f 2018-08-18 stsp static const struct got_error *
552 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
554 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
556 669b5ffa 2018-10-07 stsp if (view->child) {
557 669b5ffa 2018-10-07 stsp view_close(view->child);
558 669b5ffa 2018-10-07 stsp view->child = NULL;
560 e5a0f69f 2018-08-18 stsp if (view->close)
561 e5a0f69f 2018-08-18 stsp err = view->close(view);
562 ea5e7bb5 2018-08-01 stsp if (view->panel)
563 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
564 ea5e7bb5 2018-08-01 stsp if (view->window)
565 ea5e7bb5 2018-08-01 stsp delwin(view->window);
566 ea5e7bb5 2018-08-01 stsp free(view);
567 e5a0f69f 2018-08-18 stsp return err;
570 ea5e7bb5 2018-08-01 stsp static struct tog_view *
571 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
572 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
574 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
576 ea5e7bb5 2018-08-01 stsp if (view == NULL)
577 ea5e7bb5 2018-08-01 stsp return NULL;
579 d6b05b5b 2018-08-04 stsp view->type = type;
580 f7d12f7e 2018-08-01 stsp view->lines = LINES;
581 f7d12f7e 2018-08-01 stsp view->cols = COLS;
582 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
583 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
584 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
585 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
586 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
587 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
588 96a765a8 2018-08-04 stsp view_close(view);
589 ea5e7bb5 2018-08-01 stsp return NULL;
591 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
592 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
593 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
594 96a765a8 2018-08-04 stsp view_close(view);
595 ea5e7bb5 2018-08-01 stsp return NULL;
598 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
599 ea5e7bb5 2018-08-01 stsp return view;
603 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
605 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
607 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
610 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
612 5c60c32a 2018-10-18 stsp static const struct got_error *
613 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
615 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
617 5c60c32a 2018-10-18 stsp view->begin_y = 0;
618 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
619 5c60c32a 2018-10-18 stsp view->nlines = LINES;
620 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
621 5c60c32a 2018-10-18 stsp view->lines = LINES;
622 5c60c32a 2018-10-18 stsp view->cols = COLS;
623 5c60c32a 2018-10-18 stsp err = view_resize(view);
625 5c60c32a 2018-10-18 stsp return err;
627 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
628 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
630 5c60c32a 2018-10-18 stsp return NULL;
633 5c60c32a 2018-10-18 stsp static const struct got_error *
634 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
636 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
638 5c60c32a 2018-10-18 stsp view->begin_x = 0;
639 5c60c32a 2018-10-18 stsp view->begin_y = 0;
640 5c60c32a 2018-10-18 stsp view->nlines = LINES;
641 5c60c32a 2018-10-18 stsp view->ncols = COLS;
642 5c60c32a 2018-10-18 stsp view->lines = LINES;
643 5c60c32a 2018-10-18 stsp view->cols = COLS;
644 5c60c32a 2018-10-18 stsp err = view_resize(view);
646 5c60c32a 2018-10-18 stsp return err;
648 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
649 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
651 5c60c32a 2018-10-18 stsp return NULL;
655 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
657 5c60c32a 2018-10-18 stsp return view->parent == NULL;
660 4d8c2215 2018-08-19 stsp static const struct got_error *
661 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
663 f7d12f7e 2018-08-01 stsp int nlines, ncols;
665 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
666 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
668 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
670 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
671 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
673 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
675 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
676 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
677 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
678 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
679 25791caa 2018-10-24 stsp wclear(view->window);
681 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
682 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
683 0cf4efb1 2018-09-29 stsp view->lines = LINES;
684 0cf4efb1 2018-09-29 stsp view->cols = COLS;
686 6e3e5d9c 2018-10-18 stsp if (view->child) {
687 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
688 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
689 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
690 5c60c32a 2018-10-18 stsp if (view->child->focussed)
691 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
693 5c60c32a 2018-10-18 stsp show_panel(view->panel);
695 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
696 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
700 5c60c32a 2018-10-18 stsp return NULL;
703 669b5ffa 2018-10-07 stsp static const struct got_error *
704 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
706 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
708 669b5ffa 2018-10-07 stsp if (view->child == NULL)
709 669b5ffa 2018-10-07 stsp return NULL;
711 669b5ffa 2018-10-07 stsp err = view_close(view->child);
712 669b5ffa 2018-10-07 stsp view->child = NULL;
713 669b5ffa 2018-10-07 stsp return err;
716 669b5ffa 2018-10-07 stsp static const struct got_error *
717 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
719 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
721 669b5ffa 2018-10-07 stsp view->child = child;
722 669b5ffa 2018-10-07 stsp child->parent = view;
723 669b5ffa 2018-10-07 stsp return err;
727 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
729 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
732 34bc9ec9 2019-02-22 stsp static void
733 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
735 25791caa 2018-10-24 stsp int cols, lines;
736 25791caa 2018-10-24 stsp struct winsize size;
738 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
739 25791caa 2018-10-24 stsp cols = 80; /* Default */
740 25791caa 2018-10-24 stsp lines = 24;
742 25791caa 2018-10-24 stsp cols = size.ws_col;
743 25791caa 2018-10-24 stsp lines = size.ws_row;
745 25791caa 2018-10-24 stsp resize_term(lines, cols);
748 2b49a8ae 2019-06-22 stsp static const struct got_error *
749 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
751 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
752 2b49a8ae 2019-06-22 stsp char pattern[1024];
755 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
756 2b49a8ae 2019-06-22 stsp return NULL;
758 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
759 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
761 2b49a8ae 2019-06-22 stsp nocbreak();
763 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
766 2b49a8ae 2019-06-22 stsp if (ret == ERR)
767 2b49a8ae 2019-06-22 stsp return NULL;
769 2b49a8ae 2019-06-22 stsp if (view->searching) {
770 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
771 2b49a8ae 2019-06-22 stsp view->searching = 0;
774 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
775 7c32bd05 2019-06-22 stsp err = view->search_start(view);
777 7c32bd05 2019-06-22 stsp regfree(&view->regex);
778 7c32bd05 2019-06-22 stsp return err;
780 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
781 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
782 2b49a8ae 2019-06-22 stsp view->search_next(view);
785 2b49a8ae 2019-06-22 stsp return NULL;
788 0cf4efb1 2018-09-29 stsp static const struct got_error *
789 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
790 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
791 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
793 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
794 669b5ffa 2018-10-07 stsp struct tog_view *v;
795 1a76625f 2018-10-22 stsp int ch, errcode;
797 e5a0f69f 2018-08-18 stsp *new = NULL;
798 e5a0f69f 2018-08-18 stsp *dead = NULL;
799 0cf4efb1 2018-09-29 stsp *focus = NULL;
801 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
802 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
803 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
804 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
806 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
807 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
808 82954512 2020-02-03 stsp if (errcode)
809 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
810 82954512 2020-02-03 stsp "pthread_mutex_unlock");
811 82954512 2020-02-03 stsp pthread_yield();
812 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
813 82954512 2020-02-03 stsp if (errcode)
814 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
815 82954512 2020-02-03 stsp "pthread_mutex_lock");
816 60493ae3 2019-06-20 stsp view->search_next(view);
817 60493ae3 2019-06-20 stsp return NULL;
820 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
821 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
822 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
823 1a76625f 2018-10-22 stsp if (errcode)
824 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
825 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
826 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
827 1a76625f 2018-10-22 stsp if (errcode)
828 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
829 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
831 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
832 25791caa 2018-10-24 stsp tog_resizeterm();
833 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
834 61266923 2020-01-14 stsp tog_sigcont_received = 0;
835 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
836 25791caa 2018-10-24 stsp err = view_resize(v);
838 25791caa 2018-10-24 stsp return err;
839 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
841 25791caa 2018-10-24 stsp return err;
845 e5a0f69f 2018-08-18 stsp switch (ch) {
849 1e37a5c2 2019-05-12 jcs if (view->child) {
850 1e37a5c2 2019-05-12 jcs *focus = view->child;
851 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
852 1e37a5c2 2019-05-12 jcs *focus = view->parent;
856 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
857 1e37a5c2 2019-05-12 jcs *dead = view;
863 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
864 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
866 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
867 669b5ffa 2018-10-07 stsp *focus = view->child;
868 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
870 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
873 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
874 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
876 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
877 1e37a5c2 2019-05-12 jcs *focus = view;
878 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
880 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
884 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
885 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
888 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
891 60493ae3 2019-06-20 stsp if (view->search_start)
892 2b49a8ae 2019-06-22 stsp view_search_start(view);
894 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
898 8f4ed634 2020-03-26 stsp if (view->search_next) {
899 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
900 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
901 60493ae3 2019-06-20 stsp view->search_next_done = 0;
902 60493ae3 2019-06-20 stsp view->search_next(view);
904 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
907 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
911 e5a0f69f 2018-08-18 stsp return err;
915 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
917 0cf4efb1 2018-09-29 stsp PANEL *panel;
918 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
920 669b5ffa 2018-10-07 stsp if (view->parent)
921 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
923 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
924 0cf4efb1 2018-09-29 stsp if (panel == NULL)
927 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
928 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
929 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
933 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
935 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
936 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
938 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
940 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
943 669b5ffa 2018-10-07 stsp return view->focussed;
946 bcbd79e2 2018-08-19 stsp static const struct got_error *
947 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
949 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
950 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
951 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
952 fd823528 2018-10-22 stsp int fast_refresh = 10;
953 1a76625f 2018-10-22 stsp int done = 0, errcode;
955 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
956 1a76625f 2018-10-22 stsp if (errcode)
957 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
959 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
960 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
962 a81bf10d 2018-09-29 stsp main_view = view;
963 1004088d 2018-09-29 stsp view->focussed = 1;
964 878940b7 2018-09-29 stsp err = view->show(view);
966 0cf4efb1 2018-09-29 stsp return err;
967 0cf4efb1 2018-09-29 stsp update_panels();
968 0cf4efb1 2018-09-29 stsp doupdate();
969 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
970 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
971 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
972 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
974 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
975 e4197bf9 2018-08-18 stsp view, &views);
978 e5a0f69f 2018-08-18 stsp if (dead_view) {
979 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
981 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
982 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
983 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
984 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
985 669b5ffa 2018-10-07 stsp prev = view->parent;
987 669b5ffa 2018-10-07 stsp if (dead_view->parent)
988 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
990 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
992 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
993 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
996 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
997 0cf4efb1 2018-09-29 stsp if (focus_view)
998 0cf4efb1 2018-09-29 stsp view = focus_view;
999 669b5ffa 2018-10-07 stsp else if (prev)
1000 669b5ffa 2018-10-07 stsp view = prev;
1001 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
1002 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
1003 0cf4efb1 2018-09-29 stsp tog_view_list_head);
1005 0cf4efb1 2018-09-29 stsp view = NULL;
1006 669b5ffa 2018-10-07 stsp if (view) {
1007 acdafe9c 2020-12-03 stsp if (view->child &&
1008 acdafe9c 2020-12-03 stsp view->child->focussed)
1009 669b5ffa 2018-10-07 stsp focus_view = view->child;
1011 669b5ffa 2018-10-07 stsp focus_view = view;
1015 bcbd79e2 2018-08-19 stsp if (new_view) {
1016 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1017 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1018 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1019 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1021 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1022 86c66b02 2018-10-18 stsp err = view_close(v);
1024 86c66b02 2018-10-18 stsp goto done;
1027 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1028 fed7eaa8 2018-10-24 stsp view = new_view;
1029 878940b7 2018-09-29 stsp if (focus_view == NULL)
1030 878940b7 2018-09-29 stsp focus_view = new_view;
1032 0cf4efb1 2018-09-29 stsp if (focus_view) {
1033 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
1035 0cf4efb1 2018-09-29 stsp view->focussed = 0;
1036 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
1037 0cf4efb1 2018-09-29 stsp view = focus_view;
1038 878940b7 2018-09-29 stsp if (new_view)
1039 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1040 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1041 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1043 669b5ffa 2018-10-07 stsp if (view) {
1044 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1045 758194b5 2018-10-24 stsp view->focussed = 1;
1046 758194b5 2018-10-24 stsp show_panel(view->panel);
1047 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1048 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1049 1a76625f 2018-10-22 stsp focus_view = view;
1051 669b5ffa 2018-10-07 stsp if (view->parent) {
1052 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1054 1a76625f 2018-10-22 stsp goto done;
1056 669b5ffa 2018-10-07 stsp err = view->show(view);
1058 1a76625f 2018-10-22 stsp goto done;
1059 669b5ffa 2018-10-07 stsp if (view->child) {
1060 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1062 1a76625f 2018-10-22 stsp goto done;
1064 1a76625f 2018-10-22 stsp update_panels();
1065 1a76625f 2018-10-22 stsp doupdate();
1069 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1070 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1071 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1072 e5a0f69f 2018-08-18 stsp view_close(view);
1075 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1076 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1077 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1079 e5a0f69f 2018-08-18 stsp return err;
1082 4ed7e80c 2018-05-20 stsp __dead static void
1083 9f7d7167 2018-04-29 stsp usage_log(void)
1086 c70c5802 2018-08-01 stsp fprintf(stderr,
1087 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1088 9f7d7167 2018-04-29 stsp getprogname());
1092 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1093 80ddbec8 2018-04-29 stsp static const struct got_error *
1094 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1096 00dfcb92 2018-06-11 stsp char *vis = NULL;
1097 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1099 963b370f 2018-05-20 stsp *ws = NULL;
1100 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1101 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1102 00dfcb92 2018-06-11 stsp int vislen;
1103 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1104 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1106 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1107 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1109 00dfcb92 2018-06-11 stsp return err;
1110 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1111 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1112 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1113 a7f50699 2018-06-11 stsp goto done;
1117 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1118 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1119 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1120 a7f50699 2018-06-11 stsp goto done;
1123 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1124 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1126 00dfcb92 2018-06-11 stsp free(vis);
1127 963b370f 2018-05-20 stsp if (err) {
1128 963b370f 2018-05-20 stsp free(*ws);
1129 963b370f 2018-05-20 stsp *ws = NULL;
1130 963b370f 2018-05-20 stsp *wlen = 0;
1132 963b370f 2018-05-20 stsp return err;
1135 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1136 963b370f 2018-05-20 stsp static const struct got_error *
1137 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1138 27a741e5 2019-09-11 stsp int col_tab_align)
1140 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1141 963b370f 2018-05-20 stsp int cols = 0;
1142 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1143 963b370f 2018-05-20 stsp size_t wlen;
1146 963b370f 2018-05-20 stsp *wlinep = NULL;
1147 b700b5d6 2018-07-10 stsp *widthp = 0;
1149 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1151 963b370f 2018-05-20 stsp return err;
1154 27a741e5 2019-09-11 stsp while (i < wlen) {
1155 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1157 27a741e5 2019-09-11 stsp if (width == 0) {
1162 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1163 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1165 27a741e5 2019-09-11 stsp cols += width;
1167 27a741e5 2019-09-11 stsp } else if (width == -1) {
1168 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1169 27a741e5 2019-09-11 stsp width = TABSIZE -
1170 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1171 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1173 27a741e5 2019-09-11 stsp cols += width;
1177 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1178 963b370f 2018-05-20 stsp goto done;
1181 963b370f 2018-05-20 stsp wline[i] = L'\0';
1182 b700b5d6 2018-07-10 stsp if (widthp)
1183 b700b5d6 2018-07-10 stsp *widthp = cols;
1186 963b370f 2018-05-20 stsp free(wline);
1188 963b370f 2018-05-20 stsp *wlinep = wline;
1189 963b370f 2018-05-20 stsp return err;
1192 8b473291 2019-02-21 stsp static const struct got_error*
1193 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1194 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1196 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1197 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1199 8b473291 2019-02-21 stsp const char *name;
1201 8b473291 2019-02-21 stsp *refs_str = NULL;
1203 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1204 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1205 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1208 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1209 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1211 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1212 8b473291 2019-02-21 stsp name += 5;
1213 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1215 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1216 8b473291 2019-02-21 stsp name += 6;
1217 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1218 8b473291 2019-02-21 stsp name += 8;
1219 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1220 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1223 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1226 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1227 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1228 5d844a1e 2019-08-13 stsp if (err) {
1229 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1230 48cae60d 2020-09-22 stsp free(ref_id);
1233 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1234 5d844a1e 2019-08-13 stsp err = NULL;
1235 5d844a1e 2019-08-13 stsp tag = NULL;
1238 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1239 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1240 48cae60d 2020-09-22 stsp free(ref_id);
1242 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1243 52b5abe1 2019-08-13 stsp if (cmp != 0)
1245 8b473291 2019-02-21 stsp s = *refs_str;
1246 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1247 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1250 8b473291 2019-02-21 stsp *refs_str = NULL;
1256 8b473291 2019-02-21 stsp return err;
1259 963b370f 2018-05-20 stsp static const struct got_error *
1260 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1261 27a741e5 2019-09-11 stsp int col_tab_align)
1263 5813d178 2019-03-09 stsp char *smallerthan, *at;
1265 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1266 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1267 5813d178 2019-03-09 stsp author = smallerthan + 1;
1268 5813d178 2019-03-09 stsp at = strchr(author, '@');
1270 5813d178 2019-03-09 stsp *at = '\0';
1271 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1274 5813d178 2019-03-09 stsp static const struct got_error *
1275 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1276 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1277 8fdc79fe 2020-12-01 naddy int author_display_cols)
1279 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1280 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1281 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1282 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1283 5813d178 2019-03-09 stsp char *author = NULL;
1284 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1285 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1286 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1287 bb737323 2018-05-20 stsp int col, limit;
1288 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1289 ccb26ccd 2018-11-05 stsp struct tm tm;
1290 45d799e2 2018-12-23 stsp time_t committer_time;
1291 11b20872 2019-11-08 stsp struct tog_color *tc;
1293 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1294 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1295 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1296 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1297 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1298 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1300 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1301 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1303 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1304 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1306 11b20872 2019-11-08 stsp wattr_on(view->window,
1307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1308 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1310 11b20872 2019-11-08 stsp wattr_off(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 27a741e5 2019-09-11 stsp col = limit;
1313 b39d25c7 2018-07-10 stsp if (col > avail)
1314 b39d25c7 2018-07-10 stsp goto done;
1316 6570a66d 2019-11-08 stsp if (avail >= 120) {
1317 6570a66d 2019-11-08 stsp char *id_str;
1318 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1320 6570a66d 2019-11-08 stsp goto done;
1321 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1323 11b20872 2019-11-08 stsp wattr_on(view->window,
1324 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1325 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1327 11b20872 2019-11-08 stsp wattr_off(view->window,
1328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1329 6570a66d 2019-11-08 stsp free(id_str);
1331 6570a66d 2019-11-08 stsp if (col > avail)
1332 6570a66d 2019-11-08 stsp goto done;
1335 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1336 5813d178 2019-03-09 stsp if (author == NULL) {
1337 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1338 80ddbec8 2018-04-29 stsp goto done;
1340 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1342 bb737323 2018-05-20 stsp goto done;
1343 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1345 11b20872 2019-11-08 stsp wattr_on(view->window,
1346 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1347 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1349 11b20872 2019-11-08 stsp wattr_off(view->window,
1350 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1351 bb737323 2018-05-20 stsp col += author_width;
1352 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1353 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1355 bb737323 2018-05-20 stsp author_width++;
1357 9c2eaf34 2018-05-20 stsp if (col > avail)
1358 9c2eaf34 2018-05-20 stsp goto done;
1360 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1362 6d9fbc00 2018-04-29 stsp goto done;
1363 bb737323 2018-05-20 stsp logmsg = logmsg0;
1364 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1366 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1367 bb737323 2018-05-20 stsp if (newline)
1368 bb737323 2018-05-20 stsp *newline = '\0';
1369 bb737323 2018-05-20 stsp limit = avail - col;
1370 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1372 bb737323 2018-05-20 stsp goto done;
1373 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1374 bb737323 2018-05-20 stsp col += logmsg_width;
1375 27a741e5 2019-09-11 stsp while (col < avail) {
1376 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1380 80ddbec8 2018-04-29 stsp free(logmsg0);
1381 bb737323 2018-05-20 stsp free(wlogmsg);
1382 5813d178 2019-03-09 stsp free(author);
1383 bb737323 2018-05-20 stsp free(wauthor);
1384 80ddbec8 2018-04-29 stsp free(line);
1385 80ddbec8 2018-04-29 stsp return err;
1388 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1389 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1390 899d86c2 2018-05-10 stsp struct got_object_id *id)
1392 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1394 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1395 80ddbec8 2018-04-29 stsp if (entry == NULL)
1396 899d86c2 2018-05-10 stsp return NULL;
1398 899d86c2 2018-05-10 stsp entry->id = id;
1399 99db9666 2018-05-07 stsp entry->commit = commit;
1400 899d86c2 2018-05-10 stsp return entry;
1403 99db9666 2018-05-07 stsp static void
1404 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1406 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1408 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1409 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1410 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1411 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1412 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1413 99db9666 2018-05-07 stsp free(entry);
1416 99db9666 2018-05-07 stsp static void
1417 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1419 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1420 99db9666 2018-05-07 stsp pop_commit(commits);
1423 c4972b91 2018-05-07 stsp static const struct got_error *
1424 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1425 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1427 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1428 13add988 2019-10-15 stsp regmatch_t regmatch;
1429 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1431 13add988 2019-10-15 stsp *have_match = 0;
1433 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1435 13add988 2019-10-15 stsp return err;
1437 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1439 13add988 2019-10-15 stsp goto done;
1441 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1442 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1443 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1444 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1445 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1446 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1447 13add988 2019-10-15 stsp *have_match = 1;
1449 13add988 2019-10-15 stsp free(id_str);
1450 13add988 2019-10-15 stsp free(logmsg);
1451 13add988 2019-10-15 stsp return err;
1454 13add988 2019-10-15 stsp static const struct got_error *
1455 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1456 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1457 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1459 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1460 7c1452c1 2020-03-26 stsp int nqueued = 0;
1463 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1464 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1465 1a76625f 2018-10-22 stsp * while updating the display.
1467 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1468 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1469 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1470 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1471 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1472 1a76625f 2018-10-22 stsp int errcode;
1474 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1475 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1478 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1481 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1482 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1483 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1487 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1488 1a76625f 2018-10-22 stsp if (errcode) {
1489 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1490 13add988 2019-10-15 stsp "pthread_mutex_lock");
1494 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1495 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1496 ecb28ae0 2018-07-16 stsp nqueued++;
1497 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1499 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1500 7c1452c1 2020-03-26 stsp int have_match;
1501 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1504 7c1452c1 2020-03-26 stsp if (have_match)
1505 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1508 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1509 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1510 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1511 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1516 9ba79e04 2018-06-11 stsp return err;
1519 0553a4e3 2018-04-30 stsp static const struct got_error *
1520 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1522 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1523 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1524 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1525 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1526 60493ae3 2019-06-20 stsp int width;
1527 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1528 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1529 8b473291 2019-02-21 stsp char *refs_str = NULL;
1530 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1531 11b20872 2019-11-08 stsp struct tog_color *tc;
1532 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1534 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1535 e0d42f60 2018-07-22 stsp ncommits = 0;
1536 e0d42f60 2018-07-22 stsp while (entry) {
1537 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected) {
1538 8fdc79fe 2020-12-01 naddy s->selected_entry = entry;
1541 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1542 e0d42f60 2018-07-22 stsp ncommits++;
1545 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1546 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1547 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1549 ecb28ae0 2018-07-16 stsp return err;
1550 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1551 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1553 8fdc79fe 2020-12-01 naddy goto done;
1556 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1557 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1559 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1560 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1561 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1562 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1563 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1564 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1565 8f4ed634 2020-03-26 stsp goto done;
1568 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1570 f9686aa5 2020-03-27 stsp if (view->searching) {
1571 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1572 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1573 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1574 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1575 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1576 f9686aa5 2020-03-27 stsp search_str = "searching...";
1579 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1580 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1581 f9686aa5 2020-03-27 stsp search_str ? search_str :
1582 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1583 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1584 8f4ed634 2020-03-26 stsp goto done;
1588 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1589 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1590 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1591 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1592 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1593 1a76625f 2018-10-22 stsp header = NULL;
1594 1a76625f 2018-10-22 stsp goto done;
1596 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1597 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1598 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1599 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1600 1a76625f 2018-10-22 stsp header = NULL;
1601 1a76625f 2018-10-22 stsp goto done;
1603 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1605 1a76625f 2018-10-22 stsp goto done;
1607 2814baeb 2018-08-01 stsp werase(view->window);
1609 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1610 a3404814 2018-09-02 stsp wstandout(view->window);
1611 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1613 11b20872 2019-11-08 stsp wattr_on(view->window,
1614 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1615 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1617 11b20872 2019-11-08 stsp wattr_off(view->window,
1618 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1619 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1620 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1623 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1624 a3404814 2018-09-02 stsp wstandend(view->window);
1625 ecb28ae0 2018-07-16 stsp free(wline);
1626 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1627 1a76625f 2018-10-22 stsp goto done;
1629 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1630 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1631 5813d178 2019-03-09 stsp ncommits = 0;
1632 5813d178 2019-03-09 stsp while (entry) {
1633 5813d178 2019-03-09 stsp char *author;
1634 5813d178 2019-03-09 stsp wchar_t *wauthor;
1635 5813d178 2019-03-09 stsp int width;
1636 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1638 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1639 5813d178 2019-03-09 stsp if (author == NULL) {
1640 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1641 5813d178 2019-03-09 stsp goto done;
1643 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1644 27a741e5 2019-09-11 stsp date_display_cols);
1645 5813d178 2019-03-09 stsp if (author_cols < width)
1646 5813d178 2019-03-09 stsp author_cols = width;
1647 5813d178 2019-03-09 stsp free(wauthor);
1648 5813d178 2019-03-09 stsp free(author);
1649 7ca04879 2019-10-19 stsp ncommits++;
1650 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1653 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1654 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1655 867c6645 2018-07-10 stsp ncommits = 0;
1656 899d86c2 2018-05-10 stsp while (entry) {
1657 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1659 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1660 2814baeb 2018-08-01 stsp wstandout(view->window);
1661 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1662 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1663 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1664 2814baeb 2018-08-01 stsp wstandend(view->window);
1666 60493ae3 2019-06-20 stsp goto done;
1667 0553a4e3 2018-04-30 stsp ncommits++;
1668 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1669 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1672 1a57306a 2018-09-02 stsp view_vborder(view);
1674 1a76625f 2018-10-22 stsp free(id_str);
1675 8b473291 2019-02-21 stsp free(refs_str);
1676 1a76625f 2018-10-22 stsp free(ncommits_str);
1677 1a76625f 2018-10-22 stsp free(header);
1678 80ddbec8 2018-04-29 stsp return err;
1681 07b55e75 2018-05-10 stsp static void
1682 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1684 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1685 07b55e75 2018-05-10 stsp int nscrolled = 0;
1687 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1688 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1691 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1692 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1693 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1694 07b55e75 2018-05-10 stsp if (entry) {
1695 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1696 07b55e75 2018-05-10 stsp nscrolled++;
1701 aa075928 2018-05-10 stsp static const struct got_error *
1702 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1704 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1705 5e224a3e 2019-02-22 stsp int errcode;
1707 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1709 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1710 ffe38506 2020-12-01 naddy if (ta->log_complete)
1713 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1714 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1715 7aafa0d1 2019-02-22 stsp if (errcode)
1716 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1717 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1720 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1721 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1723 7c1452c1 2020-03-26 stsp if (!wait)
1726 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1727 ffe38506 2020-12-01 naddy show_log_view(view);
1728 7c1452c1 2020-03-26 stsp update_panels();
1729 7c1452c1 2020-03-26 stsp doupdate();
1731 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1732 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1733 82954512 2020-02-03 stsp if (errcode)
1734 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1735 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1737 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1738 ffe38506 2020-12-01 naddy show_log_view(view);
1739 7c1452c1 2020-03-26 stsp update_panels();
1740 7c1452c1 2020-03-26 stsp doupdate();
1743 5e224a3e 2019-02-22 stsp return NULL;
1746 5e224a3e 2019-02-22 stsp static const struct got_error *
1747 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1749 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1750 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1751 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1752 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1754 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1755 5e224a3e 2019-02-22 stsp return NULL;
1757 ffe38506 2020-12-01 naddy ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1758 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1759 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1761 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1763 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1764 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1766 5e224a3e 2019-02-22 stsp return err;
1770 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1771 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1774 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1776 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1777 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1779 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1780 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1782 dd0a52c1 2018-05-20 stsp return err;
1785 cd0acaa7 2018-05-20 stsp static const struct got_error *
1786 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1787 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1788 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1790 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1791 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1792 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1794 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1795 15a94983 2018-12-23 stsp if (diff_view == NULL)
1796 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1798 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1799 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1800 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1801 e5a0f69f 2018-08-18 stsp if (err == NULL)
1802 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1803 cd0acaa7 2018-05-20 stsp return err;
1806 80ddbec8 2018-04-29 stsp static const struct got_error *
1807 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1808 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1810 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1812 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1813 941e9f74 2019-05-21 stsp if (parent == NULL)
1814 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1816 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1817 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1818 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1819 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1820 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1821 941e9f74 2019-05-21 stsp s->tree = subtree;
1822 941e9f74 2019-05-21 stsp s->selected = 0;
1823 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1824 941e9f74 2019-05-21 stsp return NULL;
1827 941e9f74 2019-05-21 stsp static const struct got_error *
1828 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1829 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1831 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1832 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1833 941e9f74 2019-05-21 stsp const char *p;
1834 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1836 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1838 941e9f74 2019-05-21 stsp while (*p) {
1839 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1840 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1841 56e0773d 2019-11-28 stsp char *te_name;
1843 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1846 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1847 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1848 941e9f74 2019-05-21 stsp if (slash == NULL)
1849 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1851 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1852 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1853 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1856 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1857 56e0773d 2019-11-28 stsp if (te == NULL) {
1858 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1859 56e0773d 2019-11-28 stsp free(te_name);
1862 56e0773d 2019-11-28 stsp free(te_name);
1863 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1865 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1866 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1868 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1869 941e9f74 2019-05-21 stsp if (slash)
1870 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1872 941e9f74 2019-05-21 stsp subpath = strdup(path);
1873 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1874 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1878 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1883 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1884 941e9f74 2019-05-21 stsp free(tree_id);
1888 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1889 941e9f74 2019-05-21 stsp if (err) {
1890 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1893 941e9f74 2019-05-21 stsp if (slash == NULL)
1895 941e9f74 2019-05-21 stsp free(subpath);
1896 941e9f74 2019-05-21 stsp subpath = NULL;
1897 941e9f74 2019-05-21 stsp p = slash;
1900 941e9f74 2019-05-21 stsp free(subpath);
1901 1a76625f 2018-10-22 stsp return err;
1904 61266923 2020-01-14 stsp static const struct got_error *
1905 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1906 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1907 78756c87 2020-11-24 stsp struct got_repository *repo)
1909 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1910 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1911 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1912 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1914 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1915 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1917 55cccc34 2020-02-20 stsp return err;
1919 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1920 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1921 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1923 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1924 55cccc34 2020-02-20 stsp if (err) {
1925 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1926 55cccc34 2020-02-20 stsp return err;
1928 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1930 55cccc34 2020-02-20 stsp *new_view = tree_view;
1932 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1933 55cccc34 2020-02-20 stsp return NULL;
1935 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1938 55cccc34 2020-02-20 stsp static const struct got_error *
1939 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1941 61266923 2020-01-14 stsp sigset_t sigset;
1942 61266923 2020-01-14 stsp int errcode;
1944 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1945 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1947 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1948 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1949 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1950 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1951 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1953 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1954 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1955 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1957 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1958 61266923 2020-01-14 stsp if (errcode)
1959 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1961 61266923 2020-01-14 stsp return NULL;
1964 1a76625f 2018-10-22 stsp static void *
1965 1a76625f 2018-10-22 stsp log_thread(void *arg)
1967 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1968 1a76625f 2018-10-22 stsp int errcode = 0;
1969 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1970 1a76625f 2018-10-22 stsp int done = 0;
1972 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1974 61266923 2020-01-14 stsp return (void *)err;
1976 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1977 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1978 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1979 13add988 2019-10-15 stsp a->regex);
1980 1a76625f 2018-10-22 stsp if (err) {
1981 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1982 1a76625f 2018-10-22 stsp return (void *)err;
1983 1a76625f 2018-10-22 stsp err = NULL;
1985 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1986 1a76625f 2018-10-22 stsp a->commits_needed--;
1988 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1989 3abe8080 2019-04-10 stsp if (errcode) {
1990 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1991 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1993 3abe8080 2019-04-10 stsp } else if (*a->quit)
1995 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1996 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1997 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1998 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2001 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2002 7c1452c1 2020-03-26 stsp if (errcode) {
2003 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2004 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2005 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2010 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2012 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2013 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2014 7c1452c1 2020-03-26 stsp &tog_mutex);
2015 7c1452c1 2020-03-26 stsp if (errcode)
2016 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2017 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2022 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2023 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2024 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2026 3abe8080 2019-04-10 stsp a->log_complete = 1;
2027 1a76625f 2018-10-22 stsp return (void *)err;
2030 1a76625f 2018-10-22 stsp static const struct got_error *
2031 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2033 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2034 1a76625f 2018-10-22 stsp int errcode;
2036 1a76625f 2018-10-22 stsp if (s->thread) {
2037 1a76625f 2018-10-22 stsp s->quit = 1;
2038 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2039 1a76625f 2018-10-22 stsp if (errcode)
2040 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2041 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2042 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2043 1a76625f 2018-10-22 stsp if (errcode)
2044 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2045 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2046 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2047 1a76625f 2018-10-22 stsp if (errcode)
2048 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2050 1a76625f 2018-10-22 stsp if (errcode)
2051 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2052 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2053 1a76625f 2018-10-22 stsp s->thread = NULL;
2056 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2057 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2058 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2061 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2062 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2063 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2066 9343a5fb 2018-06-23 stsp return err;
2069 9343a5fb 2018-06-23 stsp static const struct got_error *
2070 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2073 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2074 276b94a1 2020-11-13 naddy int errcode;
2076 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2078 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2079 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2080 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2082 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2083 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2084 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2086 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2087 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2088 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2089 1a76625f 2018-10-22 stsp free(s->start_id);
2090 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2091 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2092 1a76625f 2018-10-22 stsp return err;
2095 1a76625f 2018-10-22 stsp static const struct got_error *
2096 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2098 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2100 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2101 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2102 60493ae3 2019-06-20 stsp return NULL;
2105 60493ae3 2019-06-20 stsp static const struct got_error *
2106 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2108 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2109 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2110 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2112 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2113 f9686aa5 2020-03-27 stsp show_log_view(view);
2114 f9686aa5 2020-03-27 stsp update_panels();
2115 f9686aa5 2020-03-27 stsp doupdate();
2117 96e2b566 2019-07-08 stsp if (s->search_entry) {
2118 13add988 2019-10-15 stsp int errcode, ch;
2119 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2120 13add988 2019-10-15 stsp if (errcode)
2121 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2122 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2123 13add988 2019-10-15 stsp ch = wgetch(view->window);
2124 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2125 13add988 2019-10-15 stsp if (errcode)
2126 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2127 13add988 2019-10-15 stsp "pthread_mutex_lock");
2128 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2129 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2130 678cbce5 2019-07-28 stsp return NULL;
2132 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2133 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2135 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2136 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2137 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2138 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2139 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2141 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2142 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2144 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2145 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2147 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2150 60493ae3 2019-06-20 stsp while (1) {
2151 13add988 2019-10-15 stsp int have_match = 0;
2153 60493ae3 2019-06-20 stsp if (entry == NULL) {
2154 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2155 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2156 f9967bca 2020-03-27 stsp view->search_next_done =
2157 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2158 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2159 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2160 f801134a 2019-06-25 stsp return NULL;
2163 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2164 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2165 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2167 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2168 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2171 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2172 13add988 2019-10-15 stsp &view->regex);
2175 13add988 2019-10-15 stsp if (have_match) {
2176 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2177 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2181 96e2b566 2019-07-08 stsp s->search_entry = entry;
2182 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2183 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2185 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2188 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2189 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2190 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2191 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2193 60493ae3 2019-06-20 stsp return err;
2196 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2197 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2199 60493ae3 2019-06-20 stsp return err;
2204 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2206 60493ae3 2019-06-20 stsp return NULL;
2209 60493ae3 2019-06-20 stsp static const struct got_error *
2210 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2211 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2212 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2214 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2215 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2216 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2217 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2218 1a76625f 2018-10-22 stsp int errcode;
2220 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2222 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2223 f135c941 2020-02-20 stsp free(s->in_repo_path);
2224 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2225 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2226 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2229 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2230 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2231 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2233 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2235 78756c87 2020-11-24 stsp goto done;
2237 fb2756b9 2018-08-04 stsp s->repo = repo;
2238 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2239 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2240 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2241 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2242 5036bf37 2018-09-24 stsp goto done;
2244 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2246 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2247 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2248 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2251 11b20872 2019-11-08 stsp goto done;
2252 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2253 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2254 11b20872 2019-11-08 stsp if (err) {
2255 11b20872 2019-11-08 stsp free_colors(&s->colors);
2256 11b20872 2019-11-08 stsp goto done;
2258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
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;
2266 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2267 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2268 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2269 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2270 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2272 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2274 1a76625f 2018-10-22 stsp goto done;
2275 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2276 b672a97a 2020-01-27 stsp !s->log_branches);
2278 1a76625f 2018-10-22 stsp goto done;
2279 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2280 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2282 c5b78334 2020-01-12 stsp goto done;
2284 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2285 1a76625f 2018-10-22 stsp if (errcode) {
2286 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2287 1a76625f 2018-10-22 stsp goto done;
2289 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2290 7c1452c1 2020-03-26 stsp if (errcode) {
2291 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2292 7c1452c1 2020-03-26 stsp goto done;
2295 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2296 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2297 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2298 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2299 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2300 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2301 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2302 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2303 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2304 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2305 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2306 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2307 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2310 1a76625f 2018-10-22 stsp close_log_view(view);
2311 ba4f502b 2018-08-04 stsp return err;
2314 e5a0f69f 2018-08-18 stsp static const struct got_error *
2315 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2317 f2f6d207 2020-11-24 stsp const struct got_error *err;
2318 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2320 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2321 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2322 2b380cc8 2018-10-24 stsp &s->thread_args);
2323 2b380cc8 2018-10-24 stsp if (errcode)
2324 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2325 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2326 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2328 f2f6d207 2020-11-24 stsp return err;
2332 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2335 e5a0f69f 2018-08-18 stsp static const struct got_error *
2336 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2337 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2339 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2340 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2341 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2342 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2343 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2344 669b5ffa 2018-10-07 stsp int begin_x = 0;
2345 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2347 e5a0f69f 2018-08-18 stsp switch (ch) {
2349 1e37a5c2 2019-05-12 jcs s->quit = 1;
2352 1e37a5c2 2019-05-12 jcs case KEY_UP:
2355 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2357 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2358 1e37a5c2 2019-05-12 jcs s->selected--;
2360 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2362 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2363 a4292ac5 2019-05-12 jcs case CTRL('b'):
2364 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2366 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2367 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2368 1e37a5c2 2019-05-12 jcs s->selected = 0;
2371 3e135950 2020-12-01 naddy log_scroll_up(s, view->nlines - 1);
2374 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2377 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2379 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2380 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2381 1e37a5c2 2019-05-12 jcs s->selected++;
2384 ffe38506 2020-12-01 naddy err = log_scroll_down(view, 1);
2386 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2387 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2388 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2389 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2390 1e37a5c2 2019-05-12 jcs if (first == NULL)
2392 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2395 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2396 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2397 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2398 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2399 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2400 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2402 1e37a5c2 2019-05-12 jcs err = NULL;
2405 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2406 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2407 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2408 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2409 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2410 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2411 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2412 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2413 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2414 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 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;
2440 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2443 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2445 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2446 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2447 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2448 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2451 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2452 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2454 1e37a5c2 2019-05-12 jcs return err;
2455 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2457 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2460 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2462 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2464 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2465 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2466 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2468 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2470 74cfe85e 2020-10-20 stsp return err;
2471 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2472 74cfe85e 2020-10-20 stsp if (err) {
2473 74cfe85e 2020-10-20 stsp free(parent_path);
2474 74cfe85e 2020-10-20 stsp return err;
2476 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2477 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2478 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2479 74cfe85e 2020-10-20 stsp free(parent_path);
2480 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2482 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2483 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2484 74cfe85e 2020-10-20 stsp free(parent_path);
2486 74cfe85e 2020-10-20 stsp return err;;
2487 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2488 74cfe85e 2020-10-20 stsp *new_view = lv;
2490 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2491 74cfe85e 2020-10-20 stsp *focus_view = lv;
2494 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2495 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2497 d01904d4 2019-06-25 stsp return err;
2498 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2499 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2500 d01904d4 2019-06-25 stsp if (lv == NULL)
2501 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2502 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2503 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2504 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2505 ef129c5e 2019-08-03 stsp if (err) {
2506 ef129c5e 2019-08-03 stsp view_close(lv);
2507 d01904d4 2019-06-25 stsp return err;
2509 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2510 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2511 e6b23735 2019-10-04 stsp free(start_id);
2512 e6b23735 2019-10-04 stsp view_close(lv);
2513 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2515 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2516 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2517 ef129c5e 2019-08-03 stsp if (err) {
2518 ef129c5e 2019-08-03 stsp free(start_id);
2519 b672a97a 2020-01-27 stsp view_close(lv);
2520 b672a97a 2020-01-27 stsp return err;;
2522 b672a97a 2020-01-27 stsp *dead_view = view;
2523 b672a97a 2020-01-27 stsp *new_view = lv;
2526 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2527 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2529 b672a97a 2020-01-27 stsp return err;
2530 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2531 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2532 b672a97a 2020-01-27 stsp if (lv == NULL)
2533 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2534 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2535 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2536 b672a97a 2020-01-27 stsp if (err) {
2537 ef129c5e 2019-08-03 stsp view_close(lv);
2538 d01904d4 2019-06-25 stsp return err;;
2540 d01904d4 2019-06-25 stsp *dead_view = view;
2541 d01904d4 2019-06-25 stsp *new_view = lv;
2544 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2545 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2546 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2547 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2548 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2549 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2550 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2551 6458efa5 2020-11-24 stsp if (err) {
2552 6458efa5 2020-11-24 stsp view_close(ref_view);
2553 6458efa5 2020-11-24 stsp return err;
2555 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2556 6458efa5 2020-11-24 stsp err = view_close_child(view);
2558 6458efa5 2020-11-24 stsp return err;
2559 6458efa5 2020-11-24 stsp err = view_set_child(view, ref_view);
2560 6458efa5 2020-11-24 stsp if (err) {
2561 6458efa5 2020-11-24 stsp view_close(ref_view);
2564 6458efa5 2020-11-24 stsp *focus_view = ref_view;
2566 6458efa5 2020-11-24 stsp *new_view = ref_view;
2572 80ddbec8 2018-04-29 stsp return err;
2575 4ed7e80c 2018-05-20 stsp static const struct got_error *
2576 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2578 c2db6724 2019-01-04 stsp const struct got_error *error;
2580 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2581 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2582 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2584 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2585 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2587 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2588 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2590 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2591 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2593 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2594 c2db6724 2019-01-04 stsp if (error != NULL)
2595 c2db6724 2019-01-04 stsp return error;
2597 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2598 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2600 c2db6724 2019-01-04 stsp return NULL;
2603 a915003a 2019-02-05 stsp static void
2604 a915003a 2019-02-05 stsp init_curses(void)
2606 a915003a 2019-02-05 stsp initscr();
2608 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2611 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2612 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2613 a915003a 2019-02-05 stsp curs_set(0);
2614 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2615 6d17833f 2019-11-08 stsp start_color();
2616 6d17833f 2019-11-08 stsp use_default_colors();
2618 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2619 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2620 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2623 c2db6724 2019-01-04 stsp static const struct got_error *
2624 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2625 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2627 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2629 f135c941 2020-02-20 stsp if (argc == 0) {
2630 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2631 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2632 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2633 f135c941 2020-02-20 stsp return NULL;
2636 f135c941 2020-02-20 stsp if (worktree) {
2637 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2640 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2642 f135c941 2020-02-20 stsp return err;
2643 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2644 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2645 bfd61697 2020-11-14 stsp p) == -1) {
2646 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2647 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2651 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2653 f135c941 2020-02-20 stsp return err;
2656 f135c941 2020-02-20 stsp static const struct got_error *
2657 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2659 80ddbec8 2018-04-29 stsp const struct got_error *error;
2660 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2661 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2662 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2663 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2664 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2665 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2666 04cc582a 2018-08-01 stsp struct tog_view *view;
2668 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2669 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2670 c2db6724 2019-01-04 stsp NULL) == -1)
2671 80ddbec8 2018-04-29 stsp err(1, "pledge");
2674 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2675 80ddbec8 2018-04-29 stsp switch (ch) {
2677 b672a97a 2020-01-27 stsp log_branches = 1;
2680 80ddbec8 2018-04-29 stsp start_commit = optarg;
2683 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2684 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2685 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2689 17020d27 2019-03-07 stsp usage_log();
2690 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2694 80ddbec8 2018-04-29 stsp argc -= optind;
2695 80ddbec8 2018-04-29 stsp argv += optind;
2697 f135c941 2020-02-20 stsp if (argc > 1)
2698 f135c941 2020-02-20 stsp usage_log();
2700 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2701 6962eb72 2020-02-20 stsp if (cwd == NULL)
2702 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2704 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2705 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2706 963f97a1 2019-03-18 stsp goto done;
2708 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2709 a1fbf39a 2019-08-11 stsp if (worktree)
2710 6962eb72 2020-02-20 stsp repo_path =
2711 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2713 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2715 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2716 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2717 963f97a1 2019-03-18 stsp goto done;
2720 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2721 80ddbec8 2018-04-29 stsp if (error != NULL)
2722 ecb28ae0 2018-07-16 stsp goto done;
2724 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2725 f135c941 2020-02-20 stsp repo, worktree);
2726 f135c941 2020-02-20 stsp if (error)
2727 f135c941 2020-02-20 stsp goto done;
2729 f135c941 2020-02-20 stsp init_curses();
2731 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2732 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2733 c02c541e 2019-03-29 stsp if (error)
2734 c02c541e 2019-03-29 stsp goto done;
2736 15a94983 2018-12-23 stsp if (start_commit == NULL)
2737 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2738 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2739 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2741 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2742 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 80ddbec8 2018-04-29 stsp if (error != NULL)
2744 8b473291 2019-02-21 stsp goto done;
2746 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2747 04cc582a 2018-08-01 stsp if (view == NULL) {
2748 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2749 04cc582a 2018-08-01 stsp goto done;
2751 2fc00ff4 2019-08-31 stsp if (worktree) {
2752 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2753 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2754 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2755 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2756 2fc00ff4 2019-08-31 stsp goto done;
2759 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2760 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2761 ba4f502b 2018-08-04 stsp if (error)
2762 ba4f502b 2018-08-04 stsp goto done;
2763 2fc00ff4 2019-08-31 stsp if (worktree) {
2764 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2765 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2766 2fc00ff4 2019-08-31 stsp worktree = NULL;
2768 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2770 f135c941 2020-02-20 stsp free(in_repo_path);
2771 ecb28ae0 2018-07-16 stsp free(repo_path);
2772 ecb28ae0 2018-07-16 stsp free(cwd);
2773 899d86c2 2018-05-10 stsp free(start_id);
2774 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2776 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2777 ec142235 2019-03-07 stsp if (worktree)
2778 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2779 80ddbec8 2018-04-29 stsp return error;
2782 4ed7e80c 2018-05-20 stsp __dead static void
2783 9f7d7167 2018-04-29 stsp usage_diff(void)
2786 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2787 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2791 b304db33 2018-05-20 stsp static char *
2792 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2794 b304db33 2018-05-20 stsp char *line;
2795 b304db33 2018-05-20 stsp size_t linelen;
2796 b304db33 2018-05-20 stsp size_t lineno;
2797 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2799 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2801 b304db33 2018-05-20 stsp *len = linelen;
2802 b304db33 2018-05-20 stsp return line;
2805 6d17833f 2019-11-08 stsp static int
2806 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2807 41605754 2020-11-12 stsp regmatch_t *regmatch)
2809 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2812 f26dddb7 2019-11-08 stsp struct tog_color *
2813 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2815 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2817 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2818 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2819 6d17833f 2019-11-08 stsp return tc;
2822 6d17833f 2019-11-08 stsp return NULL;
2825 4ed7e80c 2018-05-20 stsp static const struct got_error *
2826 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2827 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2829 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2830 41605754 2020-11-12 stsp wchar_t *wline;
2831 41605754 2020-11-12 stsp int width;
2834 41605754 2020-11-12 stsp *wtotal = 0;
2836 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2837 41605754 2020-11-12 stsp if (s == NULL)
2838 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2840 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2841 41605754 2020-11-12 stsp if (err) {
2843 41605754 2020-11-12 stsp return err;
2845 41605754 2020-11-12 stsp waddwstr(window, wline);
2846 41605754 2020-11-12 stsp free(wline);
2848 41605754 2020-11-12 stsp wlimit -= width;
2849 41605754 2020-11-12 stsp *wtotal += width;
2851 41605754 2020-11-12 stsp if (wlimit > 0) {
2852 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2853 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2854 41605754 2020-11-12 stsp if (s == NULL) {
2855 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2857 41605754 2020-11-12 stsp return err;
2859 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2860 41605754 2020-11-12 stsp if (err) {
2862 41605754 2020-11-12 stsp return err;
2864 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2865 41605754 2020-11-12 stsp waddwstr(window, wline);
2866 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2867 41605754 2020-11-12 stsp free(wline);
2869 41605754 2020-11-12 stsp wlimit -= width;
2870 41605754 2020-11-12 stsp *wtotal += width;
2873 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2874 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2875 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2877 41605754 2020-11-12 stsp return err;
2878 41605754 2020-11-12 stsp waddwstr(window, wline);
2879 41605754 2020-11-12 stsp free(wline);
2880 41605754 2020-11-12 stsp *wtotal += width;
2883 41605754 2020-11-12 stsp return NULL;
2886 41605754 2020-11-12 stsp static const struct got_error *
2887 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2889 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2890 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2891 61e69b96 2018-05-20 stsp const struct got_error *err;
2892 fe621944 2020-11-10 stsp int nprinted = 0;
2893 b304db33 2018-05-20 stsp char *line;
2894 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2895 b304db33 2018-05-20 stsp size_t len;
2896 61e69b96 2018-05-20 stsp wchar_t *wline;
2897 e0b650dd 2018-05-20 stsp int width;
2898 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2899 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2900 fe621944 2020-11-10 stsp off_t line_offset;
2902 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2903 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2904 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2906 f7d12f7e 2018-08-01 stsp werase(view->window);
2908 a3404814 2018-09-02 stsp if (header) {
2909 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2910 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2911 135a2da0 2020-11-11 stsp header) == -1)
2912 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2913 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2914 135a2da0 2020-11-11 stsp free(line);
2916 a3404814 2018-09-02 stsp return err;
2918 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2919 a3404814 2018-09-02 stsp wstandout(view->window);
2920 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2921 e54cc94a 2020-11-11 stsp free(wline);
2922 e54cc94a 2020-11-11 stsp wline = NULL;
2923 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2924 a3404814 2018-09-02 stsp wstandend(view->window);
2925 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2926 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2928 a3404814 2018-09-02 stsp if (max_lines <= 1)
2929 a3404814 2018-09-02 stsp return NULL;
2930 a3404814 2018-09-02 stsp max_lines--;
2933 89f1a395 2020-12-01 naddy s->eof = 0;
2934 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2935 89f1a395 2020-12-01 naddy line = parse_next_line(s->f, &len);
2936 26ed57b2 2018-05-19 stsp if (line == NULL) {
2937 89f1a395 2020-12-01 naddy s->eof = 1;
2941 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2943 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2944 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2945 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2946 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2948 41605754 2020-11-12 stsp view->window, regmatch);
2949 41605754 2020-11-12 stsp if (err) {
2950 41605754 2020-11-12 stsp free(line);
2951 41605754 2020-11-12 stsp return err;
2954 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2955 41605754 2020-11-12 stsp if (err) {
2956 41605754 2020-11-12 stsp free(line);
2957 41605754 2020-11-12 stsp return err;
2959 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2960 41605754 2020-11-12 stsp free(wline);
2961 41605754 2020-11-12 stsp wline = NULL;
2964 6d17833f 2019-11-08 stsp wattr_off(view->window,
2965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2966 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2967 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2968 fe621944 2020-11-10 stsp nprinted++;
2969 26ed57b2 2018-05-19 stsp free(line);
2971 fe621944 2020-11-10 stsp if (nprinted >= 1)
2972 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2973 89f1a395 2020-12-01 naddy (nprinted - 1);
2975 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2977 1a57306a 2018-09-02 stsp view_vborder(view);
2979 89f1a395 2020-12-01 naddy if (s->eof) {
2980 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2981 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2982 c3e9aa98 2019-05-13 jcs nprinted++;
2985 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 c3e9aa98 2019-05-13 jcs return err;
2990 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2991 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2992 e54cc94a 2020-11-11 stsp free(wline);
2993 e54cc94a 2020-11-11 stsp wline = NULL;
2994 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2997 26ed57b2 2018-05-19 stsp return NULL;
3000 abd2672a 2018-12-23 stsp static char *
3001 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3003 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3004 09867e48 2019-08-13 stsp char *p, *s;
3006 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3007 09867e48 2019-08-13 stsp if (tm == NULL)
3008 09867e48 2019-08-13 stsp return NULL;
3009 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3010 09867e48 2019-08-13 stsp if (s == NULL)
3011 09867e48 2019-08-13 stsp return NULL;
3012 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3014 abd2672a 2018-12-23 stsp *p = '\0';
3018 4ed7e80c 2018-05-20 stsp static const struct got_error *
3019 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3020 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3022 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3023 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3027 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3028 0208f208 2020-05-05 stsp if (qid != NULL) {
3029 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3030 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3033 0208f208 2020-05-05 stsp return err;
3035 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3040 0208f208 2020-05-05 stsp if (tree_id1) {
3041 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 0208f208 2020-05-05 stsp goto done;
3046 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3047 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 0208f208 2020-05-05 stsp goto done;
3051 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3052 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3054 0208f208 2020-05-05 stsp if (tree1)
3055 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3056 0208f208 2020-05-05 stsp if (tree2)
3057 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3058 0208f208 2020-05-05 stsp return err;
3061 0208f208 2020-05-05 stsp static const struct got_error *
3062 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3066 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3067 fe621944 2020-11-10 stsp if (p == NULL)
3068 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3069 fe621944 2020-11-10 stsp *line_offsets = p;
3070 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3071 fe621944 2020-11-10 stsp (*nlines)++;
3072 fe621944 2020-11-10 stsp return NULL;
3075 fe621944 2020-11-10 stsp static const struct got_error *
3076 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3077 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3080 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3081 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3082 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3083 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 45d799e2 2018-12-23 stsp time_t committer_time;
3085 45d799e2 2018-12-23 stsp const char *author, *committer;
3086 8b473291 2019-02-21 stsp char *refs_str = NULL;
3087 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3088 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3089 fe621944 2020-11-10 stsp off_t outoff = 0;
3092 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3094 8b473291 2019-02-21 stsp if (refs) {
3095 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 8b473291 2019-02-21 stsp return err;
3100 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3102 abd2672a 2018-12-23 stsp return err;
3104 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3105 15a94983 2018-12-23 stsp if (err) {
3106 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3107 15a94983 2018-12-23 stsp goto done;
3110 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3112 fe621944 2020-11-10 stsp goto done;
3114 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3116 fe621944 2020-11-10 stsp if (n < 0) {
3117 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3118 abd2672a 2018-12-23 stsp goto done;
3120 fe621944 2020-11-10 stsp outoff += n;
3121 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3123 fe621944 2020-11-10 stsp goto done;
3125 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3126 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3127 fe621944 2020-11-10 stsp if (n < 0) {
3128 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3129 abd2672a 2018-12-23 stsp goto done;
3131 fe621944 2020-11-10 stsp outoff += n;
3132 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3134 fe621944 2020-11-10 stsp goto done;
3136 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3137 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3138 fe621944 2020-11-10 stsp if (datestr) {
3139 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3140 fe621944 2020-11-10 stsp if (n < 0) {
3141 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3142 fe621944 2020-11-10 stsp goto done;
3144 fe621944 2020-11-10 stsp outoff += n;
3145 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3147 fe621944 2020-11-10 stsp goto done;
3149 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3150 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3151 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3152 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3153 fe621944 2020-11-10 stsp if (n < 0) {
3154 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3155 fe621944 2020-11-10 stsp goto done;
3157 fe621944 2020-11-10 stsp outoff += n;
3158 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3160 fe621944 2020-11-10 stsp goto done;
3162 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3164 5943eee2 2019-08-13 stsp goto done;
3165 fe621944 2020-11-10 stsp s = logmsg;
3166 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3167 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3168 fe621944 2020-11-10 stsp if (n < 0) {
3169 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3170 fe621944 2020-11-10 stsp goto done;
3172 fe621944 2020-11-10 stsp outoff += n;
3173 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3175 fe621944 2020-11-10 stsp goto done;
3178 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3180 0208f208 2020-05-05 stsp goto done;
3181 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3183 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3184 fe621944 2020-11-10 stsp if (n < 0) {
3185 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3186 fe621944 2020-11-10 stsp goto done;
3188 fe621944 2020-11-10 stsp outoff += n;
3189 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3191 fe621944 2020-11-10 stsp goto done;
3192 0208f208 2020-05-05 stsp free((char *)pe->path);
3193 0208f208 2020-05-05 stsp free(pe->data);
3196 0208f208 2020-05-05 stsp fputc('\n', outfile);
3198 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3200 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3201 abd2672a 2018-12-23 stsp free(id_str);
3202 5943eee2 2019-08-13 stsp free(logmsg);
3203 8b473291 2019-02-21 stsp free(refs_str);
3204 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3205 7510f233 2020-08-09 stsp if (err) {
3206 ae6a6978 2020-08-09 stsp free(*line_offsets);
3207 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3208 ae6a6978 2020-08-09 stsp *nlines = 0;
3210 fe621944 2020-11-10 stsp return err;
3213 abd2672a 2018-12-23 stsp static const struct got_error *
3214 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3216 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3217 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3218 15a94983 2018-12-23 stsp int obj_type;
3220 fe621944 2020-11-10 stsp free(s->line_offsets);
3221 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3222 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3223 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3224 fe621944 2020-11-10 stsp s->nlines = 0;
3226 511a516b 2018-05-19 stsp f = got_opentemp();
3227 48ae06ee 2018-10-18 stsp if (f == NULL) {
3228 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3229 48ae06ee 2018-10-18 stsp goto done;
3231 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3232 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3233 fb43ecf1 2019-02-11 stsp goto done;
3237 15a94983 2018-12-23 stsp if (s->id1)
3238 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 15a94983 2018-12-23 stsp goto done;
3244 15a94983 2018-12-23 stsp switch (obj_type) {
3245 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3246 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3251 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3253 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3256 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3257 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3258 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3260 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3262 3ffacbe1 2020-02-02 tracey goto done;
3263 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3264 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3265 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3266 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3267 f44b1f58 2020-02-02 tracey if (err)
3268 f44b1f58 2020-02-02 tracey goto done;
3269 f44b1f58 2020-02-02 tracey } else {
3270 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3271 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 fe621944 2020-11-10 stsp err = write_commit_info(
3274 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3275 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3276 f44b1f58 2020-02-02 tracey if (err)
3277 f44b1f58 2020-02-02 tracey goto done;
3282 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3284 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3290 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3293 f44b1f58 2020-02-02 tracey if (err)
3294 f44b1f58 2020-02-02 tracey goto done;
3296 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3297 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3298 48ae06ee 2018-10-18 stsp return err;
3301 f5215bb9 2019-02-22 stsp static void
3302 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3304 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3305 f5215bb9 2019-02-22 stsp update_panels();
3306 f5215bb9 2019-02-22 stsp doupdate();
3309 f44b1f58 2020-02-02 tracey static const struct got_error *
3310 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3312 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3314 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3315 f44b1f58 2020-02-02 tracey return NULL;
3318 f44b1f58 2020-02-02 tracey static const struct got_error *
3319 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3321 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3322 f44b1f58 2020-02-02 tracey int lineno;
3324 f44b1f58 2020-02-02 tracey if (!view->searching) {
3325 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 f44b1f58 2020-02-02 tracey return NULL;
3329 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3330 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3331 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3333 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3334 f44b1f58 2020-02-02 tracey } else {
3335 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3336 f44b1f58 2020-02-02 tracey lineno = 1;
3338 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3341 f44b1f58 2020-02-02 tracey while (1) {
3342 f44b1f58 2020-02-02 tracey char *line = NULL;
3343 f44b1f58 2020-02-02 tracey off_t offset;
3344 f44b1f58 2020-02-02 tracey size_t len;
3346 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3347 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3348 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3349 f44b1f58 2020-02-02 tracey free(line);
3353 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3354 f44b1f58 2020-02-02 tracey lineno = 1;
3356 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3359 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3360 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3361 f44b1f58 2020-02-02 tracey free(line);
3362 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3364 f44b1f58 2020-02-02 tracey free(line);
3365 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3366 41605754 2020-11-12 stsp if (line &&
3367 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3368 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3369 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3370 f44b1f58 2020-02-02 tracey free(line);
3373 f44b1f58 2020-02-02 tracey free(line);
3374 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3375 f44b1f58 2020-02-02 tracey lineno++;
3377 f44b1f58 2020-02-02 tracey lineno--;
3380 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3381 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3382 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3385 f44b1f58 2020-02-02 tracey return NULL;
3388 48ae06ee 2018-10-18 stsp static const struct got_error *
3389 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3390 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3391 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3392 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3394 48ae06ee 2018-10-18 stsp const struct got_error *err;
3395 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3397 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3399 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3400 15a94983 2018-12-23 stsp int type1, type2;
3401 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3403 15a94983 2018-12-23 stsp return err;
3404 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3406 15a94983 2018-12-23 stsp return err;
3408 15a94983 2018-12-23 stsp if (type1 != type2)
3409 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3411 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3412 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3413 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3414 f44b1f58 2020-02-02 tracey s->repo = repo;
3415 f44b1f58 2020-02-02 tracey s->id1 = id1;
3416 f44b1f58 2020-02-02 tracey s->id2 = id2;
3417 3dbaef42 2020-11-24 stsp s->label1 = label1;
3418 3dbaef42 2020-11-24 stsp s->label2 = label2;
3420 15a94983 2018-12-23 stsp if (id1) {
3421 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3422 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3423 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3425 5465d566 2020-02-01 tracey s->id1 = NULL;
3427 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3428 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3429 5465d566 2020-02-01 tracey free(s->id1);
3430 5465d566 2020-02-01 tracey s->id1 = NULL;
3431 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3433 5465d566 2020-02-01 tracey s->f = NULL;
3434 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3435 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3436 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3437 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3438 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3439 5465d566 2020-02-01 tracey s->log_view = log_view;
3440 5465d566 2020-02-01 tracey s->repo = repo;
3442 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3443 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3444 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3445 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3448 6d17833f 2019-11-08 stsp return err;
3449 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3450 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3451 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3452 6d17833f 2019-11-08 stsp if (err) {
3453 5465d566 2020-02-01 tracey free_colors(&s->colors);
3454 6d17833f 2019-11-08 stsp return err;
3456 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3457 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3458 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3459 6d17833f 2019-11-08 stsp if (err) {
3460 5465d566 2020-02-01 tracey free_colors(&s->colors);
3461 6d17833f 2019-11-08 stsp return err;
3464 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3465 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3466 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3467 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3468 6d17833f 2019-11-08 stsp if (err) {
3469 5465d566 2020-02-01 tracey free_colors(&s->colors);
3470 6d17833f 2019-11-08 stsp return err;
3473 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3474 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3475 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3476 11b20872 2019-11-08 stsp if (err) {
3477 5465d566 2020-02-01 tracey free_colors(&s->colors);
3478 11b20872 2019-11-08 stsp return err;
3481 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3482 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3483 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3484 11b20872 2019-11-08 stsp if (err) {
3485 5465d566 2020-02-01 tracey free_colors(&s->colors);
3486 11b20872 2019-11-08 stsp return err;
3490 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3491 78756c87 2020-11-24 stsp if (err) {
3492 78756c87 2020-11-24 stsp free(s->id1);
3493 78756c87 2020-11-24 stsp s->id1 = NULL;
3494 78756c87 2020-11-24 stsp free(s->id2);
3495 78756c87 2020-11-24 stsp s->id2 = NULL;
3496 78756c87 2020-11-24 stsp free_colors(&s->colors);
3497 78756c87 2020-11-24 stsp return err;
3500 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3501 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3502 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3504 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3505 fe621944 2020-11-10 stsp s->nlines = 0;
3506 5465d566 2020-02-01 tracey err = create_diff(s);
3507 48ae06ee 2018-10-18 stsp if (err) {
3508 5465d566 2020-02-01 tracey free(s->id1);
3509 5465d566 2020-02-01 tracey s->id1 = NULL;
3510 5465d566 2020-02-01 tracey free(s->id2);
3511 5465d566 2020-02-01 tracey s->id2 = NULL;
3512 78756c87 2020-11-24 stsp free_colors(&s->colors);
3513 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3514 48ae06ee 2018-10-18 stsp return err;
3517 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3518 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3519 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3520 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3521 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3523 5dc9f4bc 2018-08-04 stsp return NULL;
3526 e5a0f69f 2018-08-18 stsp static const struct got_error *
3527 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3530 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3532 5465d566 2020-02-01 tracey free(s->id1);
3533 5465d566 2020-02-01 tracey s->id1 = NULL;
3534 5465d566 2020-02-01 tracey free(s->id2);
3535 5465d566 2020-02-01 tracey s->id2 = NULL;
3536 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3537 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3538 5465d566 2020-02-01 tracey free_colors(&s->colors);
3539 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3540 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3541 fe621944 2020-11-10 stsp s->nlines = 0;
3542 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3543 e5a0f69f 2018-08-18 stsp return err;
3546 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3547 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3549 a3404814 2018-09-02 stsp const struct got_error *err;
3550 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3551 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3552 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3554 a3404814 2018-09-02 stsp if (s->id1) {
3555 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3557 a3404814 2018-09-02 stsp return err;
3558 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3560 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3562 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3564 a3404814 2018-09-02 stsp return err;
3565 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3567 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3568 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3569 a3404814 2018-09-02 stsp free(id_str1);
3570 a3404814 2018-09-02 stsp free(id_str2);
3571 a3404814 2018-09-02 stsp return err;
3573 a3404814 2018-09-02 stsp free(id_str1);
3574 a3404814 2018-09-02 stsp free(id_str2);
3576 89f1a395 2020-12-01 naddy return draw_file(view, header);
3579 15a087fe 2019-02-21 stsp static const struct got_error *
3580 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3581 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3583 d7a04538 2019-02-21 stsp const struct got_error *err;
3584 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3585 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3586 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3588 15a087fe 2019-02-21 stsp free(s->id2);
3589 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3590 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3591 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3593 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3595 d7a04538 2019-02-21 stsp return err;
3596 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3597 15a087fe 2019-02-21 stsp free(s->id1);
3598 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3599 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3600 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3601 15a087fe 2019-02-21 stsp return NULL;
3604 0cf4efb1 2018-09-29 stsp static const struct got_error *
3605 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3606 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3608 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3609 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3610 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3611 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3614 e5a0f69f 2018-08-18 stsp switch (ch) {
3617 3dbaef42 2020-11-24 stsp if (ch == 'a')
3618 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3619 3dbaef42 2020-11-24 stsp if (ch == 'w')
3620 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3621 64453f7e 2020-11-21 stsp wclear(view->window);
3622 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3623 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3624 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3625 64453f7e 2020-11-21 stsp err = create_diff(s);
3628 1e37a5c2 2019-05-12 jcs case KEY_UP:
3629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3630 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3632 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3633 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3634 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3637 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3638 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3639 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3642 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3643 1e37a5c2 2019-05-12 jcs if (!s->eof)
3644 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3646 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3647 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3649 00ba99a7 2019-05-12 jcs if (s->eof)
3652 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3653 1e37a5c2 2019-05-12 jcs char *line;
3654 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3655 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3656 1e37a5c2 2019-05-12 jcs if (line == NULL)
3661 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3662 1e37a5c2 2019-05-12 jcs s->diff_context--;
3663 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3664 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3665 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3666 27829c9e 2020-11-21 stsp s->nlines) {
3667 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3668 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3673 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3674 1e37a5c2 2019-05-12 jcs s->diff_context++;
3675 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3676 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3681 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3683 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3684 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3685 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3686 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3689 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3694 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3698 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3699 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3701 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3702 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3706 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3708 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3710 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3711 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3712 ffe38506 2020-12-01 naddy err = trigger_log_thread(s->log_view, 1);
3716 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3721 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3722 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3725 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3729 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3730 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3732 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3733 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3739 bcbd79e2 2018-08-19 stsp return err;
3742 4ed7e80c 2018-05-20 stsp static const struct got_error *
3743 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3745 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3746 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3747 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3748 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3749 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3750 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3751 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3752 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3753 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3754 3dbaef42 2020-11-24 stsp const char *errstr;
3755 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3757 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3758 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3759 eb6600df 2019-01-04 stsp NULL) == -1)
3760 26ed57b2 2018-05-19 stsp err(1, "pledge");
3762 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3763 26ed57b2 2018-05-19 stsp switch (ch) {
3765 64453f7e 2020-11-21 stsp force_text_diff = 1;
3768 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3770 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3771 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3773 09b5bff8 2020-02-23 naddy case 'r':
3774 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3775 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3776 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3778 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3781 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3784 17020d27 2019-03-07 stsp usage_diff();
3785 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3789 26ed57b2 2018-05-19 stsp argc -= optind;
3790 26ed57b2 2018-05-19 stsp argv += optind;
3792 26ed57b2 2018-05-19 stsp if (argc == 0) {
3793 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3794 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3795 15a94983 2018-12-23 stsp id_str1 = argv[0];
3796 15a94983 2018-12-23 stsp id_str2 = argv[1];
3798 26ed57b2 2018-05-19 stsp usage_diff();
3800 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3801 a273ac94 2020-02-23 naddy if (cwd == NULL)
3802 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3804 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3805 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3806 a273ac94 2020-02-23 naddy goto done;
3808 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3809 a273ac94 2020-02-23 naddy if (worktree)
3810 a273ac94 2020-02-23 naddy repo_path =
3811 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3813 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3815 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3816 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3817 a273ac94 2020-02-23 naddy goto done;
3820 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3821 eb6600df 2019-01-04 stsp if (error)
3822 eb6600df 2019-01-04 stsp goto done;
3824 a273ac94 2020-02-23 naddy init_curses();
3826 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3827 26ed57b2 2018-05-19 stsp if (error)
3828 26ed57b2 2018-05-19 stsp goto done;
3830 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3831 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3832 26ed57b2 2018-05-19 stsp if (error)
3833 26ed57b2 2018-05-19 stsp goto done;
3835 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3836 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3837 26ed57b2 2018-05-19 stsp if (error)
3838 26ed57b2 2018-05-19 stsp goto done;
3840 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3841 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3842 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3843 ea5e7bb5 2018-08-01 stsp goto done;
3845 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3846 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3847 5dc9f4bc 2018-08-04 stsp if (error)
3848 5dc9f4bc 2018-08-04 stsp goto done;
3849 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3851 3dbaef42 2020-11-24 stsp free(label1);
3852 3dbaef42 2020-11-24 stsp free(label2);
3853 c02c541e 2019-03-29 stsp free(repo_path);
3854 a273ac94 2020-02-23 naddy free(cwd);
3856 921be706 2019-06-28 stsp got_repo_close(repo);
3857 a273ac94 2020-02-23 naddy if (worktree)
3858 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3859 26ed57b2 2018-05-19 stsp return error;
3862 4ed7e80c 2018-05-20 stsp __dead static void
3863 9f7d7167 2018-04-29 stsp usage_blame(void)
3866 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3867 9f7d7167 2018-04-29 stsp getprogname());
3871 84451b3e 2018-07-10 stsp struct tog_blame_line {
3872 84451b3e 2018-07-10 stsp int annotated;
3873 84451b3e 2018-07-10 stsp struct got_object_id *id;
3876 4ed7e80c 2018-05-20 stsp static const struct got_error *
3877 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3879 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3880 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3881 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3882 84451b3e 2018-07-10 stsp const struct got_error *err;
3883 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3884 84451b3e 2018-07-10 stsp char *line;
3885 84451b3e 2018-07-10 stsp size_t len;
3886 84451b3e 2018-07-10 stsp wchar_t *wline;
3887 27a741e5 2019-09-11 stsp int width;
3888 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3889 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3890 ab089a2a 2018-07-12 stsp char *id_str;
3891 11b20872 2019-11-08 stsp struct tog_color *tc;
3893 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3895 ab089a2a 2018-07-12 stsp return err;
3897 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3898 f7d12f7e 2018-08-01 stsp werase(view->window);
3900 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3902 ab089a2a 2018-07-12 stsp free(id_str);
3903 ab089a2a 2018-07-12 stsp return err;
3906 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3907 ab089a2a 2018-07-12 stsp free(line);
3908 2550e4c3 2018-07-13 stsp line = NULL;
3910 1cae65b4 2019-09-22 stsp return err;
3911 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3912 a3404814 2018-09-02 stsp wstandout(view->window);
3913 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3915 11b20872 2019-11-08 stsp wattr_on(view->window,
3916 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3917 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3919 11b20872 2019-11-08 stsp wattr_off(view->window,
3920 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3921 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3922 a3404814 2018-09-02 stsp wstandend(view->window);
3923 2550e4c3 2018-07-13 stsp free(wline);
3924 2550e4c3 2018-07-13 stsp wline = NULL;
3925 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3926 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3928 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3929 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3930 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3931 ab089a2a 2018-07-12 stsp free(id_str);
3932 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3934 ab089a2a 2018-07-12 stsp free(id_str);
3935 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3936 3f60a8ef 2018-07-10 stsp free(line);
3937 2550e4c3 2018-07-13 stsp line = NULL;
3939 3f60a8ef 2018-07-10 stsp return err;
3940 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3941 2550e4c3 2018-07-13 stsp free(wline);
3942 2550e4c3 2018-07-13 stsp wline = NULL;
3943 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3944 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3946 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3947 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3948 4f7c3e5e 2020-12-01 naddy line = parse_next_line(blame->f, &len);
3949 84451b3e 2018-07-10 stsp if (line == NULL) {
3950 4f7c3e5e 2020-12-01 naddy s->eof = 1;
3953 4f7c3e5e 2020-12-01 naddy if (++lineno < s->first_displayed_line) {
3954 84451b3e 2018-07-10 stsp free(line);
3958 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3959 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3961 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3962 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3963 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3964 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3965 27a741e5 2019-09-11 stsp !(view->focussed &&
3966 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3967 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3968 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3969 8d0fe45a 2019-08-12 stsp char *id_str;
3970 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3971 8d0fe45a 2019-08-12 stsp if (err) {
3972 8d0fe45a 2019-08-12 stsp free(line);
3973 8d0fe45a 2019-08-12 stsp return err;
3975 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3977 11b20872 2019-11-08 stsp wattr_on(view->window,
3978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3979 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3981 11b20872 2019-11-08 stsp wattr_off(view->window,
3982 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3983 8d0fe45a 2019-08-12 stsp free(id_str);
3984 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3986 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3987 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3990 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3991 ee41ec32 2018-07-10 stsp prev_id = NULL;
3994 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3995 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3996 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3998 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3999 41605754 2020-11-12 stsp width = 9;
4000 41605754 2020-11-12 stsp wline = wcsdup(L"");
4001 41605754 2020-11-12 stsp if (wline == NULL) {
4002 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4003 41605754 2020-11-12 stsp free(line);
4004 41605754 2020-11-12 stsp return err;
4006 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4007 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4008 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4009 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4010 41605754 2020-11-12 stsp view->window, regmatch);
4011 41605754 2020-11-12 stsp if (err) {
4012 41605754 2020-11-12 stsp free(line);
4013 41605754 2020-11-12 stsp return err;
4015 41605754 2020-11-12 stsp width += 9;
4017 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4018 41605754 2020-11-12 stsp view->ncols - 9, 9);
4019 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4020 41605754 2020-11-12 stsp free(wline);
4021 41605754 2020-11-12 stsp wline = NULL;
4022 41605754 2020-11-12 stsp width += 9;
4025 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4026 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4027 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4028 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4029 84451b3e 2018-07-10 stsp free(line);
4031 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4033 1a57306a 2018-09-02 stsp view_vborder(view);
4035 84451b3e 2018-07-10 stsp return NULL;
4038 84451b3e 2018-07-10 stsp static const struct got_error *
4039 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4041 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4042 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4043 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4044 1a76625f 2018-10-22 stsp int errcode;
4046 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4047 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4048 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4050 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4051 1a76625f 2018-10-22 stsp if (errcode)
4052 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4054 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4055 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4056 d68a0a7d 2018-07-10 stsp goto done;
4059 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4060 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4062 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4063 d68a0a7d 2018-07-10 stsp if (line->annotated)
4064 d68a0a7d 2018-07-10 stsp goto done;
4066 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4067 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4068 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4069 84451b3e 2018-07-10 stsp goto done;
4071 84451b3e 2018-07-10 stsp line->annotated = 1;
4073 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4074 1a76625f 2018-10-22 stsp if (errcode)
4075 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4076 84451b3e 2018-07-10 stsp return err;
4079 84451b3e 2018-07-10 stsp static void *
4080 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4082 18430de3 2018-07-10 stsp const struct got_error *err;
4083 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4084 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4085 1a76625f 2018-10-22 stsp int errcode;
4087 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4089 61266923 2020-01-14 stsp return (void *)err;
4091 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4092 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4093 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4094 fc06ba56 2019-08-22 stsp err = NULL;
4096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4097 1a76625f 2018-10-22 stsp if (errcode)
4098 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4101 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4102 c9beca56 2018-07-22 stsp ta->repo = NULL;
4103 c9beca56 2018-07-22 stsp *ta->complete = 1;
4105 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4106 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4107 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4109 18430de3 2018-07-10 stsp return (void *)err;
4112 245d91c1 2018-07-12 stsp static struct got_object_id *
4113 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4114 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4116 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4118 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4119 8d0fe45a 2019-08-12 stsp return NULL;
4121 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4122 245d91c1 2018-07-12 stsp if (!line->annotated)
4123 245d91c1 2018-07-12 stsp return NULL;
4125 245d91c1 2018-07-12 stsp return line->id;
4128 b880a918 2018-07-10 stsp static const struct got_error *
4129 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4131 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4134 245d91c1 2018-07-12 stsp if (blame->thread) {
4135 1a76625f 2018-10-22 stsp int errcode;
4136 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4137 1a76625f 2018-10-22 stsp if (errcode)
4138 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4139 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4140 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4141 1a76625f 2018-10-22 stsp if (errcode)
4142 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4143 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4144 1a76625f 2018-10-22 stsp if (errcode)
4145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4146 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4147 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4148 245d91c1 2018-07-12 stsp err = NULL;
4149 245d91c1 2018-07-12 stsp blame->thread = NULL;
4151 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4152 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4153 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4155 245d91c1 2018-07-12 stsp if (blame->f) {
4156 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4157 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4158 245d91c1 2018-07-12 stsp blame->f = NULL;
4160 57670559 2018-12-23 stsp if (blame->lines) {
4161 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4162 57670559 2018-12-23 stsp free(blame->lines[i].id);
4163 57670559 2018-12-23 stsp free(blame->lines);
4164 57670559 2018-12-23 stsp blame->lines = NULL;
4166 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4167 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4169 245d91c1 2018-07-12 stsp return err;
4172 245d91c1 2018-07-12 stsp static const struct got_error *
4173 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4175 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4176 fc06ba56 2019-08-22 stsp int *done = arg;
4177 fc06ba56 2019-08-22 stsp int errcode;
4179 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4180 fc06ba56 2019-08-22 stsp if (errcode)
4181 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4182 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4184 fc06ba56 2019-08-22 stsp if (*done)
4185 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4187 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4188 fc06ba56 2019-08-22 stsp if (errcode)
4189 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4190 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4192 fc06ba56 2019-08-22 stsp return err;
4195 fc06ba56 2019-08-22 stsp static const struct got_error *
4196 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4198 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4199 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4200 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4201 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4202 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4203 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4204 15a94983 2018-12-23 stsp int obj_type;
4206 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4207 a5388363 2020-12-01 naddy s->path);
4209 15a94983 2018-12-23 stsp return err;
4211 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4213 84451b3e 2018-07-10 stsp goto done;
4215 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4216 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4217 84451b3e 2018-07-10 stsp goto done;
4220 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4222 a70480e0 2018-06-23 stsp goto done;
4223 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4224 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4225 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4226 84451b3e 2018-07-10 stsp goto done;
4228 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4229 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4230 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4231 84451b3e 2018-07-10 stsp goto done;
4233 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4234 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4235 b02560ec 2019-08-19 stsp blame->nlines--;
4237 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4238 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4239 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4240 84451b3e 2018-07-10 stsp goto done;
4243 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4245 bd24772e 2018-07-11 stsp goto done;
4247 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4248 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4249 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4250 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4251 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4253 245d91c1 2018-07-12 stsp goto done;
4255 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4257 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4258 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4259 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4260 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4261 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4262 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4263 a5388363 2020-12-01 naddy s->blame_complete = 0;
4267 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4268 27d434c2 2018-09-15 stsp free(obj_id);
4270 245d91c1 2018-07-12 stsp stop_blame(blame);
4271 245d91c1 2018-07-12 stsp return err;
4274 245d91c1 2018-07-12 stsp static const struct got_error *
4275 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4276 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4278 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4279 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4281 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4283 c4843652 2019-08-12 stsp s->path = strdup(path);
4284 c4843652 2019-08-12 stsp if (s->path == NULL)
4285 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4287 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4288 c4843652 2019-08-12 stsp if (err) {
4289 c4843652 2019-08-12 stsp free(s->path);
4290 7cbe629d 2018-08-04 stsp return err;
4293 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4294 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4295 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4296 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4297 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4298 fb2756b9 2018-08-04 stsp s->repo = repo;
4299 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4300 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4302 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4303 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4304 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4305 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4307 11b20872 2019-11-08 stsp return err;
4310 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4311 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4312 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4313 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4314 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4316 a5388363 2020-12-01 naddy return run_blame(view);
4319 e5a0f69f 2018-08-18 stsp static const struct got_error *
4320 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4322 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4323 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4325 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4326 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4328 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4329 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4330 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4331 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4332 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4335 e5a0f69f 2018-08-18 stsp free(s->path);
4336 11b20872 2019-11-08 stsp free_colors(&s->colors);
4338 e5a0f69f 2018-08-18 stsp return err;
4341 7cbe629d 2018-08-04 stsp static const struct got_error *
4342 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4344 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4346 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4347 6c4c42e0 2019-06-24 stsp return NULL;
4350 6c4c42e0 2019-06-24 stsp static const struct got_error *
4351 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4353 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4354 6c4c42e0 2019-06-24 stsp int lineno;
4356 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4357 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4358 6c4c42e0 2019-06-24 stsp return NULL;
4361 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4362 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4363 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4365 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4367 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4368 6c4c42e0 2019-06-24 stsp lineno = 1;
4370 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4373 6c4c42e0 2019-06-24 stsp while (1) {
4374 6c4c42e0 2019-06-24 stsp char *line = NULL;
4375 6c4c42e0 2019-06-24 stsp off_t offset;
4376 6c4c42e0 2019-06-24 stsp size_t len;
4378 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4379 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4380 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 6c4c42e0 2019-06-24 stsp free(line);
4385 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4386 6c4c42e0 2019-06-24 stsp lineno = 1;
4388 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4391 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4392 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4393 6c4c42e0 2019-06-24 stsp free(line);
4394 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4396 6c4c42e0 2019-06-24 stsp free(line);
4397 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4398 41605754 2020-11-12 stsp if (line &&
4399 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4400 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4402 6c4c42e0 2019-06-24 stsp free(line);
4405 6c4c42e0 2019-06-24 stsp free(line);
4406 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4412 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4413 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4414 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4417 6c4c42e0 2019-06-24 stsp return NULL;
4420 6c4c42e0 2019-06-24 stsp static const struct got_error *
4421 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4423 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4424 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4425 2b380cc8 2018-10-24 stsp int errcode;
4427 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4428 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4429 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4430 2b380cc8 2018-10-24 stsp if (errcode)
4431 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4433 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4436 51fe7530 2019-08-19 stsp if (s->blame_complete)
4437 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4439 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4441 669b5ffa 2018-10-07 stsp view_vborder(view);
4442 e5a0f69f 2018-08-18 stsp return err;
4445 e5a0f69f 2018-08-18 stsp static const struct got_error *
4446 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4447 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4449 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4450 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4451 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4452 669b5ffa 2018-10-07 stsp int begin_x = 0;
4454 e5a0f69f 2018-08-18 stsp switch (ch) {
4456 1e37a5c2 2019-05-12 jcs s->done = 1;
4459 1e37a5c2 2019-05-12 jcs case KEY_UP:
4460 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4461 1e37a5c2 2019-05-12 jcs s->selected_line--;
4462 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4463 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4464 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4466 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4467 ea025d1d 2020-02-22 naddy case CTRL('b'):
4468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4469 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4472 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4473 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4474 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4476 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4479 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4480 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4481 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4482 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4483 1e37a5c2 2019-05-12 jcs s->selected_line++;
4484 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4485 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4486 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4489 1e37a5c2 2019-05-12 jcs case 'p': {
4490 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4491 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4492 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4493 1e37a5c2 2019-05-12 jcs if (id == NULL)
4495 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4496 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4497 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4498 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4499 1e37a5c2 2019-05-12 jcs int obj_type;
4500 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4501 1e37a5c2 2019-05-12 jcs s->repo, id);
4504 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4505 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4506 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4507 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4510 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4511 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4512 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4513 e5a0f69f 2018-08-18 stsp if (err) {
4514 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4515 1e37a5c2 2019-05-12 jcs err = NULL;
4516 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4519 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4521 1e37a5c2 2019-05-12 jcs free(blob_id);
4522 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4523 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4524 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4527 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4529 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4531 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4532 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4534 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4539 1e37a5c2 2019-05-12 jcs s->done = 1;
4540 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4541 1e37a5c2 2019-05-12 jcs s->done = 0;
4542 1e37a5c2 2019-05-12 jcs if (thread_err)
4544 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4545 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4546 a5388363 2020-12-01 naddy err = run_blame(view);
4551 1e37a5c2 2019-05-12 jcs case 'B': {
4552 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4553 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4554 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4556 1e37a5c2 2019-05-12 jcs s->done = 1;
4557 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4558 1e37a5c2 2019-05-12 jcs s->done = 0;
4559 1e37a5c2 2019-05-12 jcs if (thread_err)
4561 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4562 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4563 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4564 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4565 a5388363 2020-12-01 naddy err = run_blame(view);
4570 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4571 1e37a5c2 2019-05-12 jcs case '\r': {
4572 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4573 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4574 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4575 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4576 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4577 1e37a5c2 2019-05-12 jcs if (id == NULL)
4579 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4582 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4583 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4584 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4585 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4586 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4587 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4588 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4589 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4592 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4593 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4594 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4596 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4599 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4600 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4603 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4605 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4608 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4610 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4615 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4616 ea025d1d 2020-02-22 naddy case CTRL('f'):
4618 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4619 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4620 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4623 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4624 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4625 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4626 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4629 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4630 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4631 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4632 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4635 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4636 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4638 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4639 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4640 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4641 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4647 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4650 a70480e0 2018-06-23 stsp static const struct got_error *
4651 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4653 a70480e0 2018-06-23 stsp const struct got_error *error;
4654 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4655 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4656 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4657 0587e10c 2020-07-23 stsp char *link_target = NULL;
4658 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4659 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4661 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4663 a70480e0 2018-06-23 stsp #ifndef PROFILE
4664 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4665 8e94dd5b 2019-01-04 stsp NULL) == -1)
4666 a70480e0 2018-06-23 stsp err(1, "pledge");
4669 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4670 a70480e0 2018-06-23 stsp switch (ch) {
4672 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4675 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4676 69069811 2018-08-02 stsp if (repo_path == NULL)
4677 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4681 17020d27 2019-03-07 stsp usage_blame();
4682 a70480e0 2018-06-23 stsp /* NOTREACHED */
4686 a70480e0 2018-06-23 stsp argc -= optind;
4687 a70480e0 2018-06-23 stsp argv += optind;
4689 f135c941 2020-02-20 stsp if (argc != 1)
4690 a70480e0 2018-06-23 stsp usage_blame();
4692 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4693 6962eb72 2020-02-20 stsp if (cwd == NULL)
4694 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4696 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4697 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4698 f135c941 2020-02-20 stsp goto done;
4700 69069811 2018-08-02 stsp if (repo_path == NULL) {
4701 f135c941 2020-02-20 stsp if (worktree)
4702 eb41ed75 2019-02-05 stsp repo_path =
4703 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4705 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4707 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4708 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4709 f135c941 2020-02-20 stsp goto done;
4712 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4713 c02c541e 2019-03-29 stsp if (error != NULL)
4714 8e94dd5b 2019-01-04 stsp goto done;
4716 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4717 f135c941 2020-02-20 stsp worktree);
4718 c02c541e 2019-03-29 stsp if (error)
4719 92205607 2019-01-04 stsp goto done;
4721 f135c941 2020-02-20 stsp init_curses();
4723 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4724 eb41ed75 2019-02-05 stsp if (error)
4725 69069811 2018-08-02 stsp goto done;
4727 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4728 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4729 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4730 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4731 a70480e0 2018-06-23 stsp if (error != NULL)
4732 66b4983c 2018-06-23 stsp goto done;
4733 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4734 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4736 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4737 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4739 a19e88aa 2018-06-23 stsp if (error != NULL)
4740 8b473291 2019-02-21 stsp goto done;
4742 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4743 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4744 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4745 e1cd8fed 2018-08-01 stsp goto done;
4748 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4749 0587e10c 2020-07-23 stsp commit_id, repo);
4750 7cbe629d 2018-08-04 stsp if (error)
4751 7cbe629d 2018-08-04 stsp goto done;
4753 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4754 78756c87 2020-11-24 stsp commit_id, repo);
4755 0587e10c 2020-07-23 stsp if (error)
4756 0587e10c 2020-07-23 stsp goto done;
4757 12314ad4 2019-08-31 stsp if (worktree) {
4758 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4759 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4760 12314ad4 2019-08-31 stsp worktree = NULL;
4762 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4764 69069811 2018-08-02 stsp free(repo_path);
4765 f135c941 2020-02-20 stsp free(in_repo_path);
4766 0587e10c 2020-07-23 stsp free(link_target);
4767 69069811 2018-08-02 stsp free(cwd);
4768 a70480e0 2018-06-23 stsp free(commit_id);
4769 eb41ed75 2019-02-05 stsp if (worktree)
4770 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4772 a70480e0 2018-06-23 stsp got_repo_close(repo);
4773 a70480e0 2018-06-23 stsp return error;
4776 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4777 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4779 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4780 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4781 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4782 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4783 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4784 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4785 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4787 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4789 f7d12f7e 2018-08-01 stsp werase(view->window);
4791 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4792 ffd1d5e5 2018-06-23 stsp return NULL;
4794 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4796 ffd1d5e5 2018-06-23 stsp return err;
4797 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4798 a3404814 2018-09-02 stsp wstandout(view->window);
4799 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4801 11b20872 2019-11-08 stsp wattr_on(view->window,
4802 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4805 11b20872 2019-11-08 stsp wattr_off(view->window,
4806 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4807 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4808 a3404814 2018-09-02 stsp wstandend(view->window);
4809 2550e4c3 2018-07-13 stsp free(wline);
4810 2550e4c3 2018-07-13 stsp wline = NULL;
4811 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4812 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4813 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4814 ffd1d5e5 2018-06-23 stsp return NULL;
4815 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4817 ce52c690 2018-06-23 stsp return err;
4818 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4819 2550e4c3 2018-07-13 stsp free(wline);
4820 2550e4c3 2018-07-13 stsp wline = NULL;
4821 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4822 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4823 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4824 ffd1d5e5 2018-06-23 stsp return NULL;
4825 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4826 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4827 a1eca9bb 2018-06-23 stsp return NULL;
4829 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4830 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4831 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4832 0cf4efb1 2018-09-29 stsp if (view->focussed)
4833 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4834 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4836 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4837 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4838 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4839 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4840 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4841 ffd1d5e5 2018-06-23 stsp return NULL;
4845 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4848 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4849 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4850 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4851 848d6979 2019-08-12 stsp const char *modestr = "";
4852 56e0773d 2019-11-28 stsp mode_t mode;
4854 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4855 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4857 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4858 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4859 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4861 638f9024 2019-05-13 stsp return got_error_from_errno(
4862 230a42bd 2019-05-11 jcs "got_object_id_str");
4864 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4865 63c5ca5d 2019-08-24 stsp modestr = "$";
4866 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4869 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4870 d86d3b18 2020-12-01 naddy te, s->repo);
4871 0d6c6ee3 2020-05-20 stsp if (err) {
4872 0d6c6ee3 2020-05-20 stsp free(id_str);
4873 0d6c6ee3 2020-05-20 stsp return err;
4875 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4876 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4877 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4879 848d6979 2019-08-12 stsp modestr = "@";
4881 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4882 848d6979 2019-08-12 stsp modestr = "/";
4883 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4884 848d6979 2019-08-12 stsp modestr = "*";
4885 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4886 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4887 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4888 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4889 1d13200f 2018-07-12 stsp free(id_str);
4890 0d6c6ee3 2020-05-20 stsp free(link_target);
4891 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4893 1d13200f 2018-07-12 stsp free(id_str);
4894 0d6c6ee3 2020-05-20 stsp free(link_target);
4895 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4896 ffd1d5e5 2018-06-23 stsp if (err) {
4897 ffd1d5e5 2018-06-23 stsp free(line);
4900 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4901 0cf4efb1 2018-09-29 stsp if (view->focussed)
4902 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4903 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4905 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4907 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4908 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4909 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4911 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4912 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4913 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4914 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4915 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4916 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4917 ffd1d5e5 2018-06-23 stsp free(line);
4918 2550e4c3 2018-07-13 stsp free(wline);
4919 2550e4c3 2018-07-13 stsp wline = NULL;
4921 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4922 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4923 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4927 ffd1d5e5 2018-06-23 stsp return err;
4930 ffd1d5e5 2018-06-23 stsp static void
4931 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4933 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4934 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4935 fa86c4bf 2020-11-29 stsp int i = 0;
4937 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4940 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4941 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4942 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4943 fa86c4bf 2020-11-29 stsp if (!isroot)
4944 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4947 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4948 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4952 fa86c4bf 2020-11-29 stsp static void
4953 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4955 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4956 ffd1d5e5 2018-06-23 stsp int n = 0;
4958 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4959 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4960 694d3271 2020-12-01 naddy s->first_displayed_entry);
4962 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4964 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4965 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4966 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4967 768394f3 2019-01-24 stsp if (last) {
4968 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4969 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4975 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4976 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4978 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4979 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4980 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4982 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4983 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4984 56e0773d 2019-11-28 stsp + 1 /* slash */;
4986 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4988 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4989 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4990 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4992 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4993 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4994 d9765a41 2018-06-23 stsp while (pt) {
4995 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4996 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4997 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4998 cb2ebc8a 2018-06-23 stsp goto done;
5000 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5001 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5002 cb2ebc8a 2018-06-23 stsp goto done;
5004 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5007 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5008 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5009 ce52c690 2018-06-23 stsp goto done;
5013 ce52c690 2018-06-23 stsp if (err) {
5014 ce52c690 2018-06-23 stsp free(*path);
5015 ce52c690 2018-06-23 stsp *path = NULL;
5017 ce52c690 2018-06-23 stsp return err;
5020 ce52c690 2018-06-23 stsp static const struct got_error *
5021 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5022 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5023 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5025 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5026 ce52c690 2018-06-23 stsp char *path;
5027 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5029 a0de39f3 2019-08-09 stsp *new_view = NULL;
5031 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5033 ce52c690 2018-06-23 stsp return err;
5035 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5036 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5037 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5038 83ce39e3 2019-08-12 stsp goto done;
5041 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5042 e5a0f69f 2018-08-18 stsp if (err) {
5043 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5044 fc06ba56 2019-08-22 stsp err = NULL;
5045 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5047 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5049 83ce39e3 2019-08-12 stsp free(path);
5050 69efd4c4 2018-07-18 stsp return err;
5053 69efd4c4 2018-07-18 stsp static const struct got_error *
5054 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5055 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5056 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5058 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5059 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5060 69efd4c4 2018-07-18 stsp char *path;
5062 a0de39f3 2019-08-09 stsp *new_view = NULL;
5064 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5065 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5066 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5068 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5070 69efd4c4 2018-07-18 stsp return err;
5072 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5074 e5a0f69f 2018-08-18 stsp view_close(log_view);
5076 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5077 cb2ebc8a 2018-06-23 stsp free(path);
5078 cb2ebc8a 2018-06-23 stsp return err;
5081 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5082 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5083 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5085 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5086 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5087 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5089 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5091 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5092 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5093 ffd1d5e5 2018-06-23 stsp goto done;
5095 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5096 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5097 ffd1d5e5 2018-06-23 stsp goto done;
5100 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5101 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5102 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5103 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5104 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5105 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5106 6484ec90 2018-09-29 stsp goto done;
5108 fb2756b9 2018-08-04 stsp s->repo = repo;
5110 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5112 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5113 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5114 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5115 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5117 c0b01bdb 2019-11-08 stsp goto done;
5118 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5119 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5120 c0b01bdb 2019-11-08 stsp if (err) {
5121 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5122 c0b01bdb 2019-11-08 stsp goto done;
5124 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5125 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5126 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5127 c0b01bdb 2019-11-08 stsp if (err) {
5128 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5129 c0b01bdb 2019-11-08 stsp goto done;
5132 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5133 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5134 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5135 11b20872 2019-11-08 stsp if (err) {
5136 11b20872 2019-11-08 stsp free_colors(&s->colors);
5137 11b20872 2019-11-08 stsp goto done;
5140 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5141 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
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;
5148 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5149 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5150 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5151 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5152 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5154 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5155 6484ec90 2018-09-29 stsp if (err) {
5156 fb2756b9 2018-08-04 stsp free(s->tree_label);
5157 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5159 ad80ab7b 2018-08-04 stsp return err;
5162 e5a0f69f 2018-08-18 stsp static const struct got_error *
5163 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5165 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5167 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5168 fb2756b9 2018-08-04 stsp free(s->tree_label);
5169 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5170 6484ec90 2018-09-29 stsp free(s->commit_id);
5171 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5172 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5173 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5174 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5175 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5176 ad80ab7b 2018-08-04 stsp free(parent);
5179 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5180 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5181 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5182 7c32bd05 2019-06-22 stsp return NULL;
5185 7c32bd05 2019-06-22 stsp static const struct got_error *
5186 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5188 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5190 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5191 7c32bd05 2019-06-22 stsp return NULL;
5194 7c32bd05 2019-06-22 stsp static int
5195 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5199 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5203 7c32bd05 2019-06-22 stsp static const struct got_error *
5204 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5206 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5207 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5209 7c32bd05 2019-06-22 stsp if (!view->searching) {
5210 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5211 7c32bd05 2019-06-22 stsp return NULL;
5214 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5215 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5216 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5217 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5218 56e0773d 2019-11-28 stsp s->selected_entry);
5220 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5222 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5223 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5226 56e0773d 2019-11-28 stsp s->selected_entry);
5229 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5230 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5232 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5235 7c32bd05 2019-06-22 stsp while (1) {
5236 56e0773d 2019-11-28 stsp if (te == NULL) {
5237 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5238 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 ac66afb8 2019-06-24 stsp return NULL;
5241 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5244 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5247 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5248 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 56e0773d 2019-11-28 stsp s->matched_entry = te;
5253 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5254 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5256 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5259 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5260 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5261 7c32bd05 2019-06-22 stsp s->selected = 0;
5264 e5a0f69f 2018-08-18 stsp return NULL;
5267 ad80ab7b 2018-08-04 stsp static const struct got_error *
5268 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5270 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5271 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5272 e5a0f69f 2018-08-18 stsp char *parent_path;
5274 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5276 e5a0f69f 2018-08-18 stsp return err;
5278 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5279 e5a0f69f 2018-08-18 stsp free(parent_path);
5281 669b5ffa 2018-10-07 stsp view_vborder(view);
5282 e5a0f69f 2018-08-18 stsp return err;
5285 e5a0f69f 2018-08-18 stsp static const struct got_error *
5286 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5287 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5289 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5290 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5291 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5292 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5294 e5a0f69f 2018-08-18 stsp switch (ch) {
5296 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5299 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5301 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5302 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5303 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5304 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5305 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5306 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5308 1e37a5c2 2019-05-12 jcs return err;
5309 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5311 1e37a5c2 2019-05-12 jcs view_close(log_view);
5314 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5316 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5319 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5320 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5321 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5322 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5323 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5324 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5325 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5326 152c1c93 2020-11-29 stsp if (err) {
5327 152c1c93 2020-11-29 stsp view_close(ref_view);
5328 152c1c93 2020-11-29 stsp return err;
5330 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5331 152c1c93 2020-11-29 stsp err = view_close_child(view);
5333 152c1c93 2020-11-29 stsp return err;
5334 152c1c93 2020-11-29 stsp err = view_set_child(view, ref_view);
5335 152c1c93 2020-11-29 stsp if (err) {
5336 152c1c93 2020-11-29 stsp view_close(ref_view);
5339 152c1c93 2020-11-29 stsp *focus_view = ref_view;
5341 152c1c93 2020-11-29 stsp *new_view = ref_view;
5344 1e37a5c2 2019-05-12 jcs case KEY_UP:
5345 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5346 1e37a5c2 2019-05-12 jcs s->selected--;
5349 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5351 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5352 ea025d1d 2020-02-22 naddy case CTRL('b'):
5353 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5354 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5355 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5356 fa86c4bf 2020-11-29 stsp s->selected = 0;
5358 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5359 fa86c4bf 2020-11-29 stsp s->selected = 0;
5361 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5364 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5365 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5366 1e37a5c2 2019-05-12 jcs s->selected++;
5369 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5371 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5373 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5375 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5376 ea025d1d 2020-02-22 naddy case CTRL('f'):
5377 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5379 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5380 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5381 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5384 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5386 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5388 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5389 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5390 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5391 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5392 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5394 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5395 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5397 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5398 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5399 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5400 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5401 1e37a5c2 2019-05-12 jcs s->selected_entry =
5402 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5403 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5404 1e37a5c2 2019-05-12 jcs free(parent);
5405 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5406 56e0773d 2019-11-28 stsp s->selected_entry))) {
5407 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5408 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5409 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5412 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5413 941e9f74 2019-05-21 stsp if (err) {
5414 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5417 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5418 56e0773d 2019-11-28 stsp s->selected_entry))) {
5419 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5420 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5421 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5423 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5424 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5425 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5428 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5429 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5431 669b5ffa 2018-10-07 stsp return err;
5432 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5433 669b5ffa 2018-10-07 stsp if (err) {
5434 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5437 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5439 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5442 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5443 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5444 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5450 ffd1d5e5 2018-06-23 stsp return err;
5453 ffd1d5e5 2018-06-23 stsp __dead static void
5454 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5457 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5458 ffd1d5e5 2018-06-23 stsp getprogname());
5462 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5463 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5465 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5466 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5467 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5468 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5469 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5470 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5471 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5472 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5474 5221c383 2018-08-01 stsp struct tog_view *view;
5476 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5477 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5478 d188b9a6 2019-01-04 stsp NULL) == -1)
5479 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5482 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5483 ffd1d5e5 2018-06-23 stsp switch (ch) {
5485 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5488 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5489 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5490 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5494 e99e2d15 2020-11-24 naddy usage_tree();
5495 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5499 ffd1d5e5 2018-06-23 stsp argc -= optind;
5500 ffd1d5e5 2018-06-23 stsp argv += optind;
5502 55cccc34 2020-02-20 stsp if (argc > 1)
5503 e99e2d15 2020-11-24 naddy usage_tree();
5505 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5506 55cccc34 2020-02-20 stsp if (cwd == NULL)
5507 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5509 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5510 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5511 55cccc34 2020-02-20 stsp goto done;
5513 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5514 55cccc34 2020-02-20 stsp if (worktree)
5515 52185f70 2019-02-05 stsp repo_path =
5516 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5518 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5520 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5521 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5522 55cccc34 2020-02-20 stsp goto done;
5525 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5526 c02c541e 2019-03-29 stsp if (error != NULL)
5527 52185f70 2019-02-05 stsp goto done;
5529 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5530 55cccc34 2020-02-20 stsp repo, worktree);
5531 55cccc34 2020-02-20 stsp if (error)
5532 55cccc34 2020-02-20 stsp goto done;
5534 55cccc34 2020-02-20 stsp init_curses();
5536 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5537 c02c541e 2019-03-29 stsp if (error)
5538 52185f70 2019-02-05 stsp goto done;
5540 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5541 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5542 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5543 55cccc34 2020-02-20 stsp if (error)
5544 ffd1d5e5 2018-06-23 stsp goto done;
5546 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5547 55cccc34 2020-02-20 stsp if (error)
5548 ffd1d5e5 2018-06-23 stsp goto done;
5550 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5551 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5552 55cccc34 2020-02-20 stsp if (error)
5553 8b473291 2019-02-21 stsp goto done;
5555 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5556 5221c383 2018-08-01 stsp if (view == NULL) {
5557 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5558 5221c383 2018-08-01 stsp goto done;
5560 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5561 ad80ab7b 2018-08-04 stsp if (error)
5562 ad80ab7b 2018-08-04 stsp goto done;
5563 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5564 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5565 d91faf3b 2020-12-01 naddy in_repo_path);
5566 55cccc34 2020-02-20 stsp if (error)
5567 55cccc34 2020-02-20 stsp goto done;
5570 55cccc34 2020-02-20 stsp if (worktree) {
5571 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5572 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5573 55cccc34 2020-02-20 stsp worktree = NULL;
5575 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5577 52185f70 2019-02-05 stsp free(repo_path);
5578 e4a0e26d 2020-02-20 stsp free(cwd);
5579 ffd1d5e5 2018-06-23 stsp free(commit_id);
5580 ffd1d5e5 2018-06-23 stsp if (commit)
5581 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5583 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5585 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5586 ffd1d5e5 2018-06-23 stsp return error;
5589 6458efa5 2020-11-24 stsp static const struct got_error *
5590 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5592 6458efa5 2020-11-24 stsp const struct got_error *err;
5593 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5594 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5596 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5597 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5599 6458efa5 2020-11-24 stsp return err;
5601 6458efa5 2020-11-24 stsp s->nrefs = 0;
5602 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5603 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5606 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5607 6458efa5 2020-11-24 stsp if (re == NULL)
5608 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5610 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5611 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5612 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5615 6458efa5 2020-11-24 stsp return NULL;
5619 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5621 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5623 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5624 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5625 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5628 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5631 6458efa5 2020-11-24 stsp static const struct got_error *
5632 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5634 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5635 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5637 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5638 6458efa5 2020-11-24 stsp s->repo = repo;
5640 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5641 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5642 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5644 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5646 6458efa5 2020-11-24 stsp return err;
5648 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5650 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5651 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5652 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5653 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5655 6458efa5 2020-11-24 stsp goto done;
5657 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5658 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5659 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5661 6458efa5 2020-11-24 stsp goto done;
5663 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5664 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5665 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5667 6458efa5 2020-11-24 stsp goto done;
5670 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5671 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5672 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5673 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5674 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5677 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5678 6458efa5 2020-11-24 stsp return err;
5681 6458efa5 2020-11-24 stsp static const struct got_error *
5682 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5684 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5686 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5687 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5689 6458efa5 2020-11-24 stsp return NULL;
5692 6458efa5 2020-11-24 stsp static const struct got_error *
5693 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5694 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5696 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5697 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5698 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5699 6458efa5 2020-11-24 stsp int obj_type;
5701 c42c9805 2020-11-24 stsp *commit_id = NULL;
5703 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5705 6458efa5 2020-11-24 stsp return err;
5707 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5709 6458efa5 2020-11-24 stsp goto done;
5711 6458efa5 2020-11-24 stsp switch (obj_type) {
5712 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5713 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5715 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5716 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5718 6458efa5 2020-11-24 stsp goto done;
5719 c42c9805 2020-11-24 stsp free(obj_id);
5720 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5721 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5723 6458efa5 2020-11-24 stsp goto done;
5724 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5725 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5726 c42c9805 2020-11-24 stsp goto done;
5728 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5729 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5730 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5731 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5732 c42c9805 2020-11-24 stsp goto done;
5736 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5742 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5743 c42c9805 2020-11-24 stsp if (err) {
5744 c42c9805 2020-11-24 stsp free(*commit_id);
5745 c42c9805 2020-11-24 stsp *commit_id = NULL;
5747 c42c9805 2020-11-24 stsp return err;
5750 c42c9805 2020-11-24 stsp static const struct got_error *
5751 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5752 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5754 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5755 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5756 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5758 c42c9805 2020-11-24 stsp *new_view = NULL;
5760 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5761 c42c9805 2020-11-24 stsp if (err) {
5762 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5763 c42c9805 2020-11-24 stsp return err;
5765 c42c9805 2020-11-24 stsp return NULL;
5768 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5769 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5770 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5771 6458efa5 2020-11-24 stsp goto done;
5774 6458efa5 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5777 6458efa5 2020-11-24 stsp view_close(log_view);
5779 6458efa5 2020-11-24 stsp *new_view = log_view;
5780 c42c9805 2020-11-24 stsp free(commit_id);
5781 6458efa5 2020-11-24 stsp return err;
5784 ce5b7c56 2019-07-09 stsp static void
5785 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5787 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5788 34ba6917 2020-11-30 stsp int i = 0;
5790 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5793 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5794 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5795 34ba6917 2020-11-30 stsp if (re == NULL)
5797 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5798 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5802 34ba6917 2020-11-30 stsp static void
5803 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5805 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5806 6458efa5 2020-11-24 stsp int n = 0;
5808 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5809 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5811 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5813 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5814 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5815 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5816 6458efa5 2020-11-24 stsp if (last) {
5817 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5818 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5823 6458efa5 2020-11-24 stsp static const struct got_error *
5824 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5826 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5828 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5829 6458efa5 2020-11-24 stsp return NULL;
5832 6458efa5 2020-11-24 stsp static int
5833 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5835 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5837 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5841 6458efa5 2020-11-24 stsp static const struct got_error *
5842 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5844 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5845 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5847 6458efa5 2020-11-24 stsp if (!view->searching) {
5848 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 6458efa5 2020-11-24 stsp return NULL;
5852 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5853 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5854 6458efa5 2020-11-24 stsp if (s->selected_entry)
5855 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5857 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5858 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5860 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5861 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5863 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5864 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5867 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5868 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5870 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5873 6458efa5 2020-11-24 stsp while (1) {
5874 6458efa5 2020-11-24 stsp if (re == NULL) {
5875 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5876 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 6458efa5 2020-11-24 stsp return NULL;
5879 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5880 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5882 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5885 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5886 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5887 6458efa5 2020-11-24 stsp s->matched_entry = re;
5891 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5892 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5894 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5897 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5898 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5899 6458efa5 2020-11-24 stsp s->selected = 0;
5902 6458efa5 2020-11-24 stsp return NULL;
5905 6458efa5 2020-11-24 stsp static const struct got_error *
5906 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5908 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5909 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5910 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5911 6458efa5 2020-11-24 stsp char *line = NULL;
5912 6458efa5 2020-11-24 stsp wchar_t *wline;
5913 6458efa5 2020-11-24 stsp struct tog_color *tc;
5914 6458efa5 2020-11-24 stsp int width, n;
5915 6458efa5 2020-11-24 stsp int limit = view->nlines;
5917 6458efa5 2020-11-24 stsp werase(view->window);
5919 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5921 6458efa5 2020-11-24 stsp if (limit == 0)
5922 6458efa5 2020-11-24 stsp return NULL;
5924 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5926 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5927 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5928 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5930 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5931 6458efa5 2020-11-24 stsp if (err) {
5932 6458efa5 2020-11-24 stsp free(line);
5933 6458efa5 2020-11-24 stsp return err;
5935 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5936 6458efa5 2020-11-24 stsp wstandout(view->window);
5937 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5938 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5939 6458efa5 2020-11-24 stsp wstandend(view->window);
5940 6458efa5 2020-11-24 stsp free(wline);
5941 6458efa5 2020-11-24 stsp wline = NULL;
5942 6458efa5 2020-11-24 stsp free(line);
5943 6458efa5 2020-11-24 stsp line = NULL;
5944 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5945 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5946 6458efa5 2020-11-24 stsp if (--limit <= 0)
5947 6458efa5 2020-11-24 stsp return NULL;
5950 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5951 6458efa5 2020-11-24 stsp char *line = NULL;
5953 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5954 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5955 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5956 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5957 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5958 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5959 6458efa5 2020-11-24 stsp struct got_object_id *id;
5960 6458efa5 2020-11-24 stsp char *id_str;
5961 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5963 6458efa5 2020-11-24 stsp return err;
5964 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5965 6458efa5 2020-11-24 stsp if (err) {
5967 6458efa5 2020-11-24 stsp return err;
5969 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5970 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5971 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5973 6458efa5 2020-11-24 stsp free(id_str);
5974 6458efa5 2020-11-24 stsp return err;
5977 6458efa5 2020-11-24 stsp free(id_str);
5979 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
5980 6458efa5 2020-11-24 stsp if (line == NULL)
5981 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
5984 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5985 6458efa5 2020-11-24 stsp if (err) {
5986 6458efa5 2020-11-24 stsp free(line);
5987 6458efa5 2020-11-24 stsp return err;
5989 6458efa5 2020-11-24 stsp if (n == s->selected) {
5990 6458efa5 2020-11-24 stsp if (view->focussed)
5991 6458efa5 2020-11-24 stsp wstandout(view->window);
5992 6458efa5 2020-11-24 stsp s->selected_entry = re;
5994 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
5996 6458efa5 2020-11-24 stsp wattr_on(view->window,
5997 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5998 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6000 6458efa5 2020-11-24 stsp wattr_off(view->window,
6001 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6002 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6003 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6004 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6005 6458efa5 2020-11-24 stsp wstandend(view->window);
6006 6458efa5 2020-11-24 stsp free(line);
6007 6458efa5 2020-11-24 stsp free(wline);
6008 6458efa5 2020-11-24 stsp wline = NULL;
6010 6458efa5 2020-11-24 stsp s->ndisplayed++;
6011 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6014 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6017 6458efa5 2020-11-24 stsp view_vborder(view);
6018 6458efa5 2020-11-24 stsp return err;
6021 6458efa5 2020-11-24 stsp static const struct got_error *
6022 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6023 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6025 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6026 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6027 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6028 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6030 c42c9805 2020-11-24 stsp *new_view = NULL;
6032 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6033 c42c9805 2020-11-24 stsp if (err) {
6034 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6035 c42c9805 2020-11-24 stsp return err;
6037 c42c9805 2020-11-24 stsp return NULL;
6040 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6042 c42c9805 2020-11-24 stsp goto done;
6044 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6046 c42c9805 2020-11-24 stsp goto done;
6048 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6049 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6050 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6051 c42c9805 2020-11-24 stsp goto done;
6054 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6056 c42c9805 2020-11-24 stsp goto done;
6058 c42c9805 2020-11-24 stsp *new_view = tree_view;
6060 c42c9805 2020-11-24 stsp free(commit_id);
6061 c42c9805 2020-11-24 stsp free(tree_id);
6062 c42c9805 2020-11-24 stsp if (err) {
6064 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6066 c42c9805 2020-11-24 stsp return err;
6068 c42c9805 2020-11-24 stsp static const struct got_error *
6069 6458efa5 2020-11-24 stsp input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6070 6458efa5 2020-11-24 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
6072 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6073 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6074 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6075 34ba6917 2020-11-30 stsp int begin_x = 0;
6077 6458efa5 2020-11-24 stsp switch (ch) {
6079 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6081 6458efa5 2020-11-24 stsp case KEY_ENTER:
6082 6458efa5 2020-11-24 stsp case '\r':
6083 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6085 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6086 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6087 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6089 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6090 6458efa5 2020-11-24 stsp err = view_close_child(view);
6092 6458efa5 2020-11-24 stsp return err;
6093 6458efa5 2020-11-24 stsp err = view_set_child(view, log_view);
6094 6458efa5 2020-11-24 stsp if (err) {
6095 6458efa5 2020-11-24 stsp view_close(log_view);
6098 6458efa5 2020-11-24 stsp *focus_view = log_view;
6100 6458efa5 2020-11-24 stsp *new_view = log_view;
6103 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6105 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6106 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6107 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6109 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6111 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6112 c42c9805 2020-11-24 stsp err = view_close_child(view);
6114 c42c9805 2020-11-24 stsp return err;
6115 c42c9805 2020-11-24 stsp err = view_set_child(view, tree_view);
6116 c42c9805 2020-11-24 stsp if (err) {
6117 c42c9805 2020-11-24 stsp view_close(tree_view);
6120 c42c9805 2020-11-24 stsp *focus_view = tree_view;
6122 c42c9805 2020-11-24 stsp *new_view = tree_view;
6125 6458efa5 2020-11-24 stsp case KEY_UP:
6126 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6127 6458efa5 2020-11-24 stsp s->selected--;
6130 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6132 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6133 6458efa5 2020-11-24 stsp case CTRL('b'):
6134 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6135 34ba6917 2020-11-30 stsp s->selected = 0;
6136 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6139 6458efa5 2020-11-24 stsp case KEY_DOWN:
6140 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6141 6458efa5 2020-11-24 stsp s->selected++;
6144 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6145 6458efa5 2020-11-24 stsp /* can't scroll any further */
6147 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6149 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6150 6458efa5 2020-11-24 stsp case CTRL('f'):
6151 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6152 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6153 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6154 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6157 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6159 6458efa5 2020-11-24 stsp case CTRL('l'):
6160 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6161 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6163 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6164 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6165 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6171 6458efa5 2020-11-24 stsp return err;
6174 6458efa5 2020-11-24 stsp __dead static void
6175 6458efa5 2020-11-24 stsp usage_ref(void)
6178 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6179 6458efa5 2020-11-24 stsp getprogname());
6183 6458efa5 2020-11-24 stsp static const struct got_error *
6184 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6186 6458efa5 2020-11-24 stsp const struct got_error *error;
6187 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6188 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6189 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6191 6458efa5 2020-11-24 stsp struct tog_view *view;
6193 6458efa5 2020-11-24 stsp #ifndef PROFILE
6194 6458efa5 2020-11-24 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6195 6458efa5 2020-11-24 stsp NULL) == -1)
6196 6458efa5 2020-11-24 stsp err(1, "pledge");
6199 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6200 6458efa5 2020-11-24 stsp switch (ch) {
6202 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6203 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6204 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6208 e99e2d15 2020-11-24 naddy usage_ref();
6209 6458efa5 2020-11-24 stsp /* NOTREACHED */
6213 6458efa5 2020-11-24 stsp argc -= optind;
6214 6458efa5 2020-11-24 stsp argv += optind;
6216 6458efa5 2020-11-24 stsp if (argc > 1)
6217 e99e2d15 2020-11-24 naddy usage_ref();
6219 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6220 6458efa5 2020-11-24 stsp if (cwd == NULL)
6221 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6223 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6224 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6225 6458efa5 2020-11-24 stsp goto done;
6227 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6228 6458efa5 2020-11-24 stsp if (worktree)
6229 6458efa5 2020-11-24 stsp repo_path =
6230 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6232 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6234 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6235 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6236 6458efa5 2020-11-24 stsp goto done;
6239 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6240 6458efa5 2020-11-24 stsp if (error != NULL)
6241 6458efa5 2020-11-24 stsp goto done;
6243 6458efa5 2020-11-24 stsp init_curses();
6245 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6246 6458efa5 2020-11-24 stsp if (error)
6247 6458efa5 2020-11-24 stsp goto done;
6249 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6250 6458efa5 2020-11-24 stsp if (view == NULL) {
6251 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6252 6458efa5 2020-11-24 stsp goto done;
6255 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6256 6458efa5 2020-11-24 stsp if (error)
6257 6458efa5 2020-11-24 stsp goto done;
6259 6458efa5 2020-11-24 stsp if (worktree) {
6260 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6261 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6262 6458efa5 2020-11-24 stsp worktree = NULL;
6264 6458efa5 2020-11-24 stsp error = view_loop(view);
6266 6458efa5 2020-11-24 stsp free(repo_path);
6267 6458efa5 2020-11-24 stsp free(cwd);
6269 6458efa5 2020-11-24 stsp got_repo_close(repo);
6270 6458efa5 2020-11-24 stsp return error;
6273 6458efa5 2020-11-24 stsp static void
6274 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6278 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6279 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6280 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6281 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6283 6879ba42 2020-10-01 naddy fputc('\n', fp);
6286 4ed7e80c 2018-05-20 stsp __dead static void
6287 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6289 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6291 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6292 6879ba42 2020-10-01 naddy getprogname());
6293 6879ba42 2020-10-01 naddy if (hflag) {
6294 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6295 6879ba42 2020-10-01 naddy list_commands(fp);
6297 6879ba42 2020-10-01 naddy exit(status);
6300 c2301be8 2018-04-30 stsp static char **
6301 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6303 ee85c5e8 2020-02-29 stsp va_list ap;
6304 c2301be8 2018-04-30 stsp char **argv;
6307 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6309 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6310 c2301be8 2018-04-30 stsp if (argv == NULL)
6311 c2301be8 2018-04-30 stsp err(1, "calloc");
6312 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6313 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6314 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6315 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6318 ee85c5e8 2020-02-29 stsp va_end(ap);
6319 c2301be8 2018-04-30 stsp return argv;
6323 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6324 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6325 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6326 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6327 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6329 ee85c5e8 2020-02-29 stsp static const struct got_error *
6330 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6332 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6333 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6334 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6335 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6336 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6337 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6338 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6340 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6341 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6342 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6344 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6345 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6346 ee85c5e8 2020-02-29 stsp goto done;
6348 ee85c5e8 2020-02-29 stsp if (worktree)
6349 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6351 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6352 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6353 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6354 ee85c5e8 2020-02-29 stsp goto done;
6357 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6358 ee85c5e8 2020-02-29 stsp if (error != NULL)
6359 ee85c5e8 2020-02-29 stsp goto done;
6361 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6362 ee85c5e8 2020-02-29 stsp repo, worktree);
6363 ee85c5e8 2020-02-29 stsp if (error)
6364 ee85c5e8 2020-02-29 stsp goto done;
6366 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6367 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6368 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6369 ee85c5e8 2020-02-29 stsp if (error)
6370 ee85c5e8 2020-02-29 stsp goto done;
6372 ee85c5e8 2020-02-29 stsp if (worktree) {
6373 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6374 ee85c5e8 2020-02-29 stsp worktree = NULL;
6377 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6378 ee85c5e8 2020-02-29 stsp if (error) {
6379 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6380 ee85c5e8 2020-02-29 stsp goto done;
6381 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6382 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6383 6879ba42 2020-10-01 naddy usage(1, 1);
6384 ee85c5e8 2020-02-29 stsp /* not reached */
6387 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6388 ee85c5e8 2020-02-29 stsp repo = NULL;
6390 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6391 ee85c5e8 2020-02-29 stsp if (error)
6392 ee85c5e8 2020-02-29 stsp goto done;
6394 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6396 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6397 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6400 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6401 ee85c5e8 2020-02-29 stsp if (worktree)
6402 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6404 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6405 ee85c5e8 2020-02-29 stsp free(commit_id);
6406 ee85c5e8 2020-02-29 stsp free(cwd);
6407 ee85c5e8 2020-02-29 stsp free(repo_path);
6408 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6409 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6411 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6412 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6413 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6415 ee85c5e8 2020-02-29 stsp return error;
6419 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6421 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6422 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6423 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6424 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6425 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6426 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6427 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6430 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6432 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6433 9f7d7167 2018-04-29 stsp switch (ch) {
6435 9f7d7167 2018-04-29 stsp hflag = 1;
6438 53ccebc2 2019-07-30 stsp Vflag = 1;
6441 6879ba42 2020-10-01 naddy usage(hflag, 1);
6442 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6446 9f7d7167 2018-04-29 stsp argc -= optind;
6447 9f7d7167 2018-04-29 stsp argv += optind;
6448 9814e6a3 2020-09-27 naddy optind = 1;
6449 c2301be8 2018-04-30 stsp optreset = 1;
6451 53ccebc2 2019-07-30 stsp if (Vflag) {
6452 53ccebc2 2019-07-30 stsp got_version_print_str();
6453 6879ba42 2020-10-01 naddy return 0;
6456 c2301be8 2018-04-30 stsp if (argc == 0) {
6457 f29d3e89 2018-06-23 stsp if (hflag)
6458 6879ba42 2020-10-01 naddy usage(hflag, 0);
6459 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6460 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6462 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6466 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6467 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6468 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6469 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6470 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6476 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6477 ee85c5e8 2020-02-29 stsp if (argc != 1)
6478 6879ba42 2020-10-01 naddy usage(0, 1);
6479 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6480 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6482 ee85c5e8 2020-02-29 stsp if (hflag)
6483 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6485 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6489 b46c1e04 2020-09-20 naddy putchar('\n');
6490 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6492 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6493 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6494 a2f4a359 2020-02-28 stsp free(cmd_argv);
6497 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6498 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);