2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <util.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
110 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
111 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
112 ba4f502b 2018-08-04 stsp struct got_object_id *id;
113 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
116 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 ba4f502b 2018-08-04 stsp struct commit_queue {
118 ba4f502b 2018-08-04 stsp int ncommits;
119 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
122 f26dddb7 2019-11-08 stsp struct tog_color {
123 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
124 6d17833f 2019-11-08 stsp regex_t regex;
125 6d17833f 2019-11-08 stsp short colorpair;
127 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
129 11b20872 2019-11-08 stsp static const struct got_error *
130 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
131 11b20872 2019-11-08 stsp int idx, short color)
133 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
134 11b20872 2019-11-08 stsp struct tog_color *tc;
135 11b20872 2019-11-08 stsp int regerr = 0;
137 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
138 11b20872 2019-11-08 stsp return NULL;
140 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
142 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
143 11b20872 2019-11-08 stsp if (tc == NULL)
144 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
145 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
146 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
147 11b20872 2019-11-08 stsp if (regerr) {
148 11b20872 2019-11-08 stsp static char regerr_msg[512];
149 11b20872 2019-11-08 stsp static char err_msg[512];
150 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
151 11b20872 2019-11-08 stsp sizeof(regerr_msg));
152 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
153 11b20872 2019-11-08 stsp regerr_msg);
154 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
156 11b20872 2019-11-08 stsp return err;
158 11b20872 2019-11-08 stsp tc->colorpair = idx;
159 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
160 11b20872 2019-11-08 stsp return NULL;
163 11b20872 2019-11-08 stsp static void
164 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
166 11b20872 2019-11-08 stsp struct tog_color *tc;
168 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
169 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
170 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
171 11b20872 2019-11-08 stsp regfree(&tc->regex);
176 11b20872 2019-11-08 stsp struct tog_color *
177 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
179 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
181 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
182 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
186 11b20872 2019-11-08 stsp return NULL;
190 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
193 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
195 11b20872 2019-11-08 stsp return COLOR_CYAN;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
197 11b20872 2019-11-08 stsp return COLOR_YELLOW;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
199 11b20872 2019-11-08 stsp return COLOR_GREEN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
203 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
205 91b8c405 2020-01-25 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_GREEN;
208 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
209 11b20872 2019-11-08 stsp return COLOR_GREEN;
210 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
211 11b20872 2019-11-08 stsp return COLOR_CYAN;
212 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 11b20872 2019-11-08 stsp return COLOR_YELLOW;
214 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
215 6458efa5 2020-11-24 stsp return COLOR_GREEN;
216 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
217 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
218 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
219 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
225 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
227 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
229 11b20872 2019-11-08 stsp if (val == NULL)
230 11b20872 2019-11-08 stsp return default_color_value(envvar);
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
233 11b20872 2019-11-08 stsp return COLOR_BLACK;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
235 11b20872 2019-11-08 stsp return COLOR_RED;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
237 11b20872 2019-11-08 stsp return COLOR_GREEN;
238 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
239 11b20872 2019-11-08 stsp return COLOR_YELLOW;
240 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
241 11b20872 2019-11-08 stsp return COLOR_BLUE;
242 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
243 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
244 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
245 11b20872 2019-11-08 stsp return COLOR_CYAN;
246 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
247 11b20872 2019-11-08 stsp return COLOR_WHITE;
248 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
251 11b20872 2019-11-08 stsp return default_color_value(envvar);
255 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
256 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
257 3dbaef42 2020-11-24 stsp const char *label1, *label2;
259 15a087fe 2019-02-21 stsp int first_displayed_line;
260 15a087fe 2019-02-21 stsp int last_displayed_line;
262 15a087fe 2019-02-21 stsp int diff_context;
263 3dbaef42 2020-11-24 stsp int ignore_whitespace;
264 64453f7e 2020-11-21 stsp int force_text_diff;
265 15a087fe 2019-02-21 stsp struct got_repository *repo;
266 78756c87 2020-11-24 stsp struct got_reflist_head refs;
267 bddb1296 2019-11-08 stsp struct tog_colors colors;
268 fe621944 2020-11-10 stsp size_t nlines;
269 f44b1f58 2020-02-02 tracey off_t *line_offsets;
270 f44b1f58 2020-02-02 tracey int matched_line;
271 f44b1f58 2020-02-02 tracey int selected_line;
273 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
274 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
277 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
279 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
280 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
281 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
282 1a76625f 2018-10-22 stsp int commits_needed;
283 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
284 1a76625f 2018-10-22 stsp struct commit_queue *commits;
285 1a76625f 2018-10-22 stsp const char *in_repo_path;
286 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
287 1a76625f 2018-10-22 stsp struct got_repository *repo;
288 1a76625f 2018-10-22 stsp int log_complete;
289 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
290 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
291 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
292 13add988 2019-10-15 stsp int *searching;
293 13add988 2019-10-15 stsp int *search_next_done;
294 13add988 2019-10-15 stsp regex_t *regex;
297 1a76625f 2018-10-22 stsp struct tog_log_view_state {
298 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
299 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
300 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
301 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
302 b01e7d3b 2018-08-04 stsp int selected;
303 b01e7d3b 2018-08-04 stsp char *in_repo_path;
304 d01904d4 2019-06-25 stsp const char *head_ref_name;
305 b672a97a 2020-01-27 stsp int log_branches;
306 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
307 78756c87 2020-11-24 stsp struct got_reflist_head refs;
308 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
309 1a76625f 2018-10-22 stsp sig_atomic_t quit;
310 1a76625f 2018-10-22 stsp pthread_t thread;
311 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
312 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
313 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
314 11b20872 2019-11-08 stsp struct tog_colors colors;
317 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
318 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
319 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
321 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
322 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
323 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
324 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
325 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
326 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
327 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
328 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
329 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
330 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
332 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
333 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
334 e9424729 2018-08-04 stsp int nlines;
336 e9424729 2018-08-04 stsp struct tog_view *view;
337 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
341 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
342 e9424729 2018-08-04 stsp const char *path;
343 e9424729 2018-08-04 stsp struct got_repository *repo;
344 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
345 e9424729 2018-08-04 stsp int *complete;
346 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
347 fc06ba56 2019-08-22 stsp void *cancel_arg;
350 e9424729 2018-08-04 stsp struct tog_blame {
352 be659d10 2020-11-18 stsp off_t filesize;
353 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
354 6fcac457 2018-11-19 stsp int nlines;
355 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
356 e9424729 2018-08-04 stsp pthread_t thread;
357 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
358 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
359 e9424729 2018-08-04 stsp const char *path;
362 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
363 7cbe629d 2018-08-04 stsp int first_displayed_line;
364 7cbe629d 2018-08-04 stsp int last_displayed_line;
365 7cbe629d 2018-08-04 stsp int selected_line;
366 7cbe629d 2018-08-04 stsp int blame_complete;
369 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
370 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
371 e5a0f69f 2018-08-18 stsp char *path;
372 7cbe629d 2018-08-04 stsp struct got_repository *repo;
373 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
374 e9424729 2018-08-04 stsp struct tog_blame blame;
375 6c4c42e0 2019-06-24 stsp int matched_line;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
379 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
380 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
381 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
382 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
383 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
384 ad80ab7b 2018-08-04 stsp int selected;
387 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
389 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
390 ad80ab7b 2018-08-04 stsp char *tree_label;
391 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
392 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
393 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
394 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
395 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
396 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
397 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
398 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
399 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
400 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
401 6458efa5 2020-11-24 stsp struct tog_colors colors;
404 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
405 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
406 6458efa5 2020-11-24 stsp struct got_reference *ref;
410 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
413 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
414 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
418 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
419 6458efa5 2020-11-24 stsp struct got_repository *repo;
420 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
421 bddb1296 2019-11-08 stsp struct tog_colors colors;
425 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
427 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
428 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
429 669b5ffa 2018-10-07 stsp * there is enough screen estate.
431 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
432 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
434 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
435 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
436 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
438 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
439 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
441 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
443 cc3c9aac 2018-08-01 stsp struct tog_view {
444 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
445 26ed57b2 2018-05-19 stsp WINDOW *window;
446 26ed57b2 2018-05-19 stsp PANEL *panel;
447 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
448 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
449 1004088d 2018-09-29 stsp int focussed;
450 669b5ffa 2018-10-07 stsp struct tog_view *parent;
451 669b5ffa 2018-10-07 stsp struct tog_view *child;
452 669b5ffa 2018-10-07 stsp int child_focussed;
454 5dc9f4bc 2018-08-04 stsp /* type-specific state */
455 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
457 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
458 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
459 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
460 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
461 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
464 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
465 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
466 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
467 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
469 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
470 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
471 60493ae3 2019-06-20 stsp int searching;
472 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
473 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
474 60493ae3 2019-06-20 stsp int search_next_done;
475 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
476 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
477 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
478 1803e47f 2019-06-22 stsp regex_t regex;
479 41605754 2020-11-12 stsp regmatch_t regmatch;
482 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
483 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
484 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
485 78756c87 2020-11-24 stsp struct got_repository *);
486 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
487 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
488 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
489 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
490 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
491 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
493 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
494 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
495 78756c87 2020-11-24 stsp const char *, const char *, int);
496 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
497 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
498 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
499 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
500 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
501 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
503 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
504 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
505 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
506 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
507 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
508 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
509 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
510 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
512 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
513 78756c87 2020-11-24 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
514 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
515 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
516 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
517 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
518 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
519 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
521 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
522 6458efa5 2020-11-24 stsp struct got_repository *);
523 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
524 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
525 6458efa5 2020-11-24 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
526 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
527 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
528 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
530 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
531 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
532 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
534 25791caa 2018-10-24 stsp static void
535 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
537 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
540 83baff54 2019-08-12 stsp static void
541 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
543 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
546 61266923 2020-01-14 stsp static void
547 61266923 2020-01-14 stsp tog_sigcont(int signo)
549 61266923 2020-01-14 stsp tog_sigcont_received = 1;
552 e5a0f69f 2018-08-18 stsp static const struct got_error *
553 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
555 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
557 669b5ffa 2018-10-07 stsp if (view->child) {
558 669b5ffa 2018-10-07 stsp view_close(view->child);
559 669b5ffa 2018-10-07 stsp view->child = NULL;
561 e5a0f69f 2018-08-18 stsp if (view->close)
562 e5a0f69f 2018-08-18 stsp err = view->close(view);
563 ea5e7bb5 2018-08-01 stsp if (view->panel)
564 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
565 ea5e7bb5 2018-08-01 stsp if (view->window)
566 ea5e7bb5 2018-08-01 stsp delwin(view->window);
567 ea5e7bb5 2018-08-01 stsp free(view);
568 e5a0f69f 2018-08-18 stsp return err;
571 ea5e7bb5 2018-08-01 stsp static struct tog_view *
572 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
573 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
575 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
577 ea5e7bb5 2018-08-01 stsp if (view == NULL)
578 ea5e7bb5 2018-08-01 stsp return NULL;
580 d6b05b5b 2018-08-04 stsp view->type = type;
581 f7d12f7e 2018-08-01 stsp view->lines = LINES;
582 f7d12f7e 2018-08-01 stsp view->cols = COLS;
583 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
584 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
585 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
586 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
587 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
588 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
589 96a765a8 2018-08-04 stsp view_close(view);
590 ea5e7bb5 2018-08-01 stsp return NULL;
592 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
593 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
594 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
595 96a765a8 2018-08-04 stsp view_close(view);
596 ea5e7bb5 2018-08-01 stsp return NULL;
599 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
600 ea5e7bb5 2018-08-01 stsp return view;
604 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
606 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
608 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
611 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
613 5c60c32a 2018-10-18 stsp static const struct got_error *
614 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
616 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
618 5c60c32a 2018-10-18 stsp view->begin_y = 0;
619 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
620 5c60c32a 2018-10-18 stsp view->nlines = LINES;
621 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
622 5c60c32a 2018-10-18 stsp view->lines = LINES;
623 5c60c32a 2018-10-18 stsp view->cols = COLS;
624 5c60c32a 2018-10-18 stsp err = view_resize(view);
626 5c60c32a 2018-10-18 stsp return err;
628 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
629 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
631 5c60c32a 2018-10-18 stsp return NULL;
634 5c60c32a 2018-10-18 stsp static const struct got_error *
635 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
637 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
639 5c60c32a 2018-10-18 stsp view->begin_x = 0;
640 5c60c32a 2018-10-18 stsp view->begin_y = 0;
641 5c60c32a 2018-10-18 stsp view->nlines = LINES;
642 5c60c32a 2018-10-18 stsp view->ncols = COLS;
643 5c60c32a 2018-10-18 stsp view->lines = LINES;
644 5c60c32a 2018-10-18 stsp view->cols = COLS;
645 5c60c32a 2018-10-18 stsp err = view_resize(view);
647 5c60c32a 2018-10-18 stsp return err;
649 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
650 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
652 5c60c32a 2018-10-18 stsp return NULL;
656 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
658 5c60c32a 2018-10-18 stsp return view->parent == NULL;
661 4d8c2215 2018-08-19 stsp static const struct got_error *
662 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
664 f7d12f7e 2018-08-01 stsp int nlines, ncols;
666 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
667 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
669 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
671 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
672 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
674 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
676 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
677 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
678 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
679 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
680 25791caa 2018-10-24 stsp wclear(view->window);
682 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
683 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
684 0cf4efb1 2018-09-29 stsp view->lines = LINES;
685 0cf4efb1 2018-09-29 stsp view->cols = COLS;
687 6e3e5d9c 2018-10-18 stsp if (view->child) {
688 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
689 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
690 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
691 5c60c32a 2018-10-18 stsp if (view->child->focussed)
692 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
694 5c60c32a 2018-10-18 stsp show_panel(view->panel);
696 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
697 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
701 5c60c32a 2018-10-18 stsp return NULL;
704 669b5ffa 2018-10-07 stsp static const struct got_error *
705 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
707 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
709 669b5ffa 2018-10-07 stsp if (view->child == NULL)
710 669b5ffa 2018-10-07 stsp return NULL;
712 669b5ffa 2018-10-07 stsp err = view_close(view->child);
713 669b5ffa 2018-10-07 stsp view->child = NULL;
714 669b5ffa 2018-10-07 stsp return err;
717 669b5ffa 2018-10-07 stsp static const struct got_error *
718 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
720 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
722 669b5ffa 2018-10-07 stsp view->child = child;
723 669b5ffa 2018-10-07 stsp child->parent = view;
724 669b5ffa 2018-10-07 stsp return err;
728 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
730 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
733 34bc9ec9 2019-02-22 stsp static void
734 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
736 25791caa 2018-10-24 stsp int cols, lines;
737 25791caa 2018-10-24 stsp struct winsize size;
739 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
740 25791caa 2018-10-24 stsp cols = 80; /* Default */
741 25791caa 2018-10-24 stsp lines = 24;
743 25791caa 2018-10-24 stsp cols = size.ws_col;
744 25791caa 2018-10-24 stsp lines = size.ws_row;
746 25791caa 2018-10-24 stsp resize_term(lines, cols);
749 2b49a8ae 2019-06-22 stsp static const struct got_error *
750 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
752 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
753 2b49a8ae 2019-06-22 stsp char pattern[1024];
756 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
757 2b49a8ae 2019-06-22 stsp return NULL;
759 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
760 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
762 2b49a8ae 2019-06-22 stsp nocbreak();
764 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
767 2b49a8ae 2019-06-22 stsp if (ret == ERR)
768 2b49a8ae 2019-06-22 stsp return NULL;
770 2b49a8ae 2019-06-22 stsp if (view->searching) {
771 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
772 2b49a8ae 2019-06-22 stsp view->searching = 0;
775 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
776 7c32bd05 2019-06-22 stsp err = view->search_start(view);
778 7c32bd05 2019-06-22 stsp regfree(&view->regex);
779 7c32bd05 2019-06-22 stsp return err;
781 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
782 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
783 2b49a8ae 2019-06-22 stsp view->search_next(view);
786 2b49a8ae 2019-06-22 stsp return NULL;
789 0cf4efb1 2018-09-29 stsp static const struct got_error *
790 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
791 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
792 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
794 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
795 669b5ffa 2018-10-07 stsp struct tog_view *v;
796 1a76625f 2018-10-22 stsp int ch, errcode;
798 e5a0f69f 2018-08-18 stsp *new = NULL;
799 e5a0f69f 2018-08-18 stsp *dead = NULL;
800 0cf4efb1 2018-09-29 stsp *focus = NULL;
802 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
803 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
804 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
805 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
807 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
808 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
809 82954512 2020-02-03 stsp if (errcode)
810 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
811 82954512 2020-02-03 stsp "pthread_mutex_unlock");
812 82954512 2020-02-03 stsp pthread_yield();
813 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
814 82954512 2020-02-03 stsp if (errcode)
815 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
816 82954512 2020-02-03 stsp "pthread_mutex_lock");
817 60493ae3 2019-06-20 stsp view->search_next(view);
818 60493ae3 2019-06-20 stsp return NULL;
821 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
822 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
823 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
824 1a76625f 2018-10-22 stsp if (errcode)
825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
826 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
827 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
828 1a76625f 2018-10-22 stsp if (errcode)
829 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
830 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
832 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
833 25791caa 2018-10-24 stsp tog_resizeterm();
834 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
835 61266923 2020-01-14 stsp tog_sigcont_received = 0;
836 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
837 25791caa 2018-10-24 stsp err = view_resize(v);
839 25791caa 2018-10-24 stsp return err;
840 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
842 25791caa 2018-10-24 stsp return err;
846 e5a0f69f 2018-08-18 stsp switch (ch) {
850 1e37a5c2 2019-05-12 jcs if (view->child) {
851 1e37a5c2 2019-05-12 jcs *focus = view->child;
852 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
853 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
854 1e37a5c2 2019-05-12 jcs *focus = view->parent;
855 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
859 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
860 1e37a5c2 2019-05-12 jcs *dead = view;
866 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
867 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
869 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
870 669b5ffa 2018-10-07 stsp *focus = view->child;
871 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
872 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
874 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
877 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
878 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
880 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
881 1e37a5c2 2019-05-12 jcs *focus = view;
882 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
883 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
885 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
889 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
890 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
893 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
896 60493ae3 2019-06-20 stsp if (view->search_start)
897 2b49a8ae 2019-06-22 stsp view_search_start(view);
899 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
903 8f4ed634 2020-03-26 stsp if (view->search_next) {
904 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
905 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
906 60493ae3 2019-06-20 stsp view->search_next_done = 0;
907 60493ae3 2019-06-20 stsp view->search_next(view);
909 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
912 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
916 e5a0f69f 2018-08-18 stsp return err;
920 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
922 0cf4efb1 2018-09-29 stsp PANEL *panel;
923 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
925 669b5ffa 2018-10-07 stsp if (view->parent)
926 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
928 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
929 0cf4efb1 2018-09-29 stsp if (panel == NULL)
932 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
933 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
934 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
938 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
940 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
941 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
943 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
945 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
948 669b5ffa 2018-10-07 stsp return view->focussed;
951 bcbd79e2 2018-08-19 stsp static const struct got_error *
952 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
954 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
955 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
956 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
957 fd823528 2018-10-22 stsp int fast_refresh = 10;
958 1a76625f 2018-10-22 stsp int done = 0, errcode;
960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
961 1a76625f 2018-10-22 stsp if (errcode)
962 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
964 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
965 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
967 a81bf10d 2018-09-29 stsp main_view = view;
968 1004088d 2018-09-29 stsp view->focussed = 1;
969 878940b7 2018-09-29 stsp err = view->show(view);
971 0cf4efb1 2018-09-29 stsp return err;
972 0cf4efb1 2018-09-29 stsp update_panels();
973 0cf4efb1 2018-09-29 stsp doupdate();
974 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
975 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
976 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
977 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
979 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
980 e4197bf9 2018-08-18 stsp view, &views);
983 e5a0f69f 2018-08-18 stsp if (dead_view) {
984 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
986 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
987 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
988 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
989 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
990 669b5ffa 2018-10-07 stsp prev = view->parent;
992 669b5ffa 2018-10-07 stsp if (dead_view->parent)
993 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
995 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
997 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
998 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
1001 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
1002 0cf4efb1 2018-09-29 stsp if (focus_view)
1003 0cf4efb1 2018-09-29 stsp view = focus_view;
1004 669b5ffa 2018-10-07 stsp else if (prev)
1005 669b5ffa 2018-10-07 stsp view = prev;
1006 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
1007 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
1008 0cf4efb1 2018-09-29 stsp tog_view_list_head);
1010 0cf4efb1 2018-09-29 stsp view = NULL;
1011 669b5ffa 2018-10-07 stsp if (view) {
1012 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
1013 669b5ffa 2018-10-07 stsp focus_view = view->child;
1015 669b5ffa 2018-10-07 stsp focus_view = view;
1019 bcbd79e2 2018-08-19 stsp if (new_view) {
1020 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1021 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1022 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1023 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1025 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1026 86c66b02 2018-10-18 stsp err = view_close(v);
1028 86c66b02 2018-10-18 stsp goto done;
1031 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1032 fed7eaa8 2018-10-24 stsp view = new_view;
1033 878940b7 2018-09-29 stsp if (focus_view == NULL)
1034 878940b7 2018-09-29 stsp focus_view = new_view;
1036 0cf4efb1 2018-09-29 stsp if (focus_view) {
1037 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
1039 0cf4efb1 2018-09-29 stsp view->focussed = 0;
1040 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
1041 0cf4efb1 2018-09-29 stsp view = focus_view;
1042 878940b7 2018-09-29 stsp if (new_view)
1043 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1044 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1045 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1047 669b5ffa 2018-10-07 stsp if (view) {
1048 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1049 758194b5 2018-10-24 stsp view->focussed = 1;
1050 758194b5 2018-10-24 stsp show_panel(view->panel);
1051 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1052 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1053 1a76625f 2018-10-22 stsp focus_view = view;
1055 669b5ffa 2018-10-07 stsp if (view->parent) {
1056 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1058 1a76625f 2018-10-22 stsp goto done;
1060 669b5ffa 2018-10-07 stsp err = view->show(view);
1062 1a76625f 2018-10-22 stsp goto done;
1063 669b5ffa 2018-10-07 stsp if (view->child) {
1064 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1066 1a76625f 2018-10-22 stsp goto done;
1068 1a76625f 2018-10-22 stsp update_panels();
1069 1a76625f 2018-10-22 stsp doupdate();
1073 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1074 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1075 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1076 e5a0f69f 2018-08-18 stsp view_close(view);
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1080 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1081 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1083 e5a0f69f 2018-08-18 stsp return err;
1086 4ed7e80c 2018-05-20 stsp __dead static void
1087 9f7d7167 2018-04-29 stsp usage_log(void)
1090 c70c5802 2018-08-01 stsp fprintf(stderr,
1091 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1092 9f7d7167 2018-04-29 stsp getprogname());
1096 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1097 80ddbec8 2018-04-29 stsp static const struct got_error *
1098 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1100 00dfcb92 2018-06-11 stsp char *vis = NULL;
1101 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1103 963b370f 2018-05-20 stsp *ws = NULL;
1104 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1105 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1106 00dfcb92 2018-06-11 stsp int vislen;
1107 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1108 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1110 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1111 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1113 00dfcb92 2018-06-11 stsp return err;
1114 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1115 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1116 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1117 a7f50699 2018-06-11 stsp goto done;
1121 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1122 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1124 a7f50699 2018-06-11 stsp goto done;
1127 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1128 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1130 00dfcb92 2018-06-11 stsp free(vis);
1131 963b370f 2018-05-20 stsp if (err) {
1132 963b370f 2018-05-20 stsp free(*ws);
1133 963b370f 2018-05-20 stsp *ws = NULL;
1134 963b370f 2018-05-20 stsp *wlen = 0;
1136 963b370f 2018-05-20 stsp return err;
1139 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1140 963b370f 2018-05-20 stsp static const struct got_error *
1141 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1142 27a741e5 2019-09-11 stsp int col_tab_align)
1144 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1145 963b370f 2018-05-20 stsp int cols = 0;
1146 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1147 963b370f 2018-05-20 stsp size_t wlen;
1150 963b370f 2018-05-20 stsp *wlinep = NULL;
1151 b700b5d6 2018-07-10 stsp *widthp = 0;
1153 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1155 963b370f 2018-05-20 stsp return err;
1158 27a741e5 2019-09-11 stsp while (i < wlen) {
1159 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1161 27a741e5 2019-09-11 stsp if (width == 0) {
1166 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1167 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1169 27a741e5 2019-09-11 stsp cols += width;
1171 27a741e5 2019-09-11 stsp } else if (width == -1) {
1172 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1173 27a741e5 2019-09-11 stsp width = TABSIZE -
1174 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1175 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1177 27a741e5 2019-09-11 stsp cols += width;
1181 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1182 963b370f 2018-05-20 stsp goto done;
1185 963b370f 2018-05-20 stsp wline[i] = L'\0';
1186 b700b5d6 2018-07-10 stsp if (widthp)
1187 b700b5d6 2018-07-10 stsp *widthp = cols;
1190 963b370f 2018-05-20 stsp free(wline);
1192 963b370f 2018-05-20 stsp *wlinep = wline;
1193 963b370f 2018-05-20 stsp return err;
1196 8b473291 2019-02-21 stsp static const struct got_error*
1197 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1198 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1200 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1201 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1203 8b473291 2019-02-21 stsp const char *name;
1205 8b473291 2019-02-21 stsp *refs_str = NULL;
1207 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1208 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1209 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1212 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1213 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1215 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1216 8b473291 2019-02-21 stsp name += 5;
1217 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1219 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1220 8b473291 2019-02-21 stsp name += 6;
1221 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1222 8b473291 2019-02-21 stsp name += 8;
1223 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1224 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1227 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1230 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1231 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1232 5d844a1e 2019-08-13 stsp if (err) {
1233 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1234 48cae60d 2020-09-22 stsp free(ref_id);
1237 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1238 5d844a1e 2019-08-13 stsp err = NULL;
1239 5d844a1e 2019-08-13 stsp tag = NULL;
1242 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1243 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1244 48cae60d 2020-09-22 stsp free(ref_id);
1246 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1247 52b5abe1 2019-08-13 stsp if (cmp != 0)
1249 8b473291 2019-02-21 stsp s = *refs_str;
1250 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1251 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1252 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1254 8b473291 2019-02-21 stsp *refs_str = NULL;
1260 8b473291 2019-02-21 stsp return err;
1263 963b370f 2018-05-20 stsp static const struct got_error *
1264 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1265 27a741e5 2019-09-11 stsp int col_tab_align)
1267 5813d178 2019-03-09 stsp char *smallerthan, *at;
1269 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1270 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1271 5813d178 2019-03-09 stsp author = smallerthan + 1;
1272 5813d178 2019-03-09 stsp at = strchr(author, '@');
1274 5813d178 2019-03-09 stsp *at = '\0';
1275 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1278 5813d178 2019-03-09 stsp static const struct got_error *
1279 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1280 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1281 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1282 11b20872 2019-11-08 stsp struct tog_colors *colors)
1284 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1285 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1286 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1287 5813d178 2019-03-09 stsp char *author = NULL;
1288 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1289 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1290 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1291 bb737323 2018-05-20 stsp int col, limit;
1292 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1293 ccb26ccd 2018-11-05 stsp struct tm tm;
1294 45d799e2 2018-12-23 stsp time_t committer_time;
1295 11b20872 2019-11-08 stsp struct tog_color *tc;
1297 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1298 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1299 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1300 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1301 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1302 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1304 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1305 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1307 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1308 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1310 11b20872 2019-11-08 stsp wattr_on(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1314 11b20872 2019-11-08 stsp wattr_off(view->window,
1315 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1316 27a741e5 2019-09-11 stsp col = limit;
1317 b39d25c7 2018-07-10 stsp if (col > avail)
1318 b39d25c7 2018-07-10 stsp goto done;
1320 6570a66d 2019-11-08 stsp if (avail >= 120) {
1321 6570a66d 2019-11-08 stsp char *id_str;
1322 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1324 6570a66d 2019-11-08 stsp goto done;
1325 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1327 11b20872 2019-11-08 stsp wattr_on(view->window,
1328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1329 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1331 11b20872 2019-11-08 stsp wattr_off(view->window,
1332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1333 6570a66d 2019-11-08 stsp free(id_str);
1335 6570a66d 2019-11-08 stsp if (col > avail)
1336 6570a66d 2019-11-08 stsp goto done;
1339 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1340 5813d178 2019-03-09 stsp if (author == NULL) {
1341 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1342 80ddbec8 2018-04-29 stsp goto done;
1344 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1346 bb737323 2018-05-20 stsp goto done;
1347 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1349 11b20872 2019-11-08 stsp wattr_on(view->window,
1350 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1351 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1353 11b20872 2019-11-08 stsp wattr_off(view->window,
1354 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1355 bb737323 2018-05-20 stsp col += author_width;
1356 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1357 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1359 bb737323 2018-05-20 stsp author_width++;
1361 9c2eaf34 2018-05-20 stsp if (col > avail)
1362 9c2eaf34 2018-05-20 stsp goto done;
1364 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1366 6d9fbc00 2018-04-29 stsp goto done;
1367 bb737323 2018-05-20 stsp logmsg = logmsg0;
1368 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1370 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1371 bb737323 2018-05-20 stsp if (newline)
1372 bb737323 2018-05-20 stsp *newline = '\0';
1373 bb737323 2018-05-20 stsp limit = avail - col;
1374 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1376 bb737323 2018-05-20 stsp goto done;
1377 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1378 bb737323 2018-05-20 stsp col += logmsg_width;
1379 27a741e5 2019-09-11 stsp while (col < avail) {
1380 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1384 80ddbec8 2018-04-29 stsp free(logmsg0);
1385 bb737323 2018-05-20 stsp free(wlogmsg);
1386 5813d178 2019-03-09 stsp free(author);
1387 bb737323 2018-05-20 stsp free(wauthor);
1388 80ddbec8 2018-04-29 stsp free(line);
1389 80ddbec8 2018-04-29 stsp return err;
1392 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1393 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1394 899d86c2 2018-05-10 stsp struct got_object_id *id)
1396 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1398 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1399 80ddbec8 2018-04-29 stsp if (entry == NULL)
1400 899d86c2 2018-05-10 stsp return NULL;
1402 899d86c2 2018-05-10 stsp entry->id = id;
1403 99db9666 2018-05-07 stsp entry->commit = commit;
1404 899d86c2 2018-05-10 stsp return entry;
1407 99db9666 2018-05-07 stsp static void
1408 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1410 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1412 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1413 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1414 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1415 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1416 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1417 99db9666 2018-05-07 stsp free(entry);
1420 99db9666 2018-05-07 stsp static void
1421 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1423 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1424 99db9666 2018-05-07 stsp pop_commit(commits);
1427 c4972b91 2018-05-07 stsp static const struct got_error *
1428 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1429 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1431 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1432 13add988 2019-10-15 stsp regmatch_t regmatch;
1433 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1435 13add988 2019-10-15 stsp *have_match = 0;
1437 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1439 13add988 2019-10-15 stsp return err;
1441 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1443 13add988 2019-10-15 stsp goto done;
1445 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1446 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1447 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1448 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1449 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1450 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1451 13add988 2019-10-15 stsp *have_match = 1;
1453 13add988 2019-10-15 stsp free(id_str);
1454 13add988 2019-10-15 stsp free(logmsg);
1455 13add988 2019-10-15 stsp return err;
1458 13add988 2019-10-15 stsp static const struct got_error *
1459 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1460 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1461 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1463 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1464 7c1452c1 2020-03-26 stsp int nqueued = 0;
1467 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1468 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1469 1a76625f 2018-10-22 stsp * while updating the display.
1471 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1472 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1473 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1474 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1475 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1476 1a76625f 2018-10-22 stsp int errcode;
1478 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1479 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1482 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1485 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1486 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1492 1a76625f 2018-10-22 stsp if (errcode) {
1493 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1494 13add988 2019-10-15 stsp "pthread_mutex_lock");
1498 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1499 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1500 ecb28ae0 2018-07-16 stsp nqueued++;
1501 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1503 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1504 7c1452c1 2020-03-26 stsp int have_match;
1505 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1508 7c1452c1 2020-03-26 stsp if (have_match)
1509 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1515 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1520 9ba79e04 2018-06-11 stsp return err;
1523 0553a4e3 2018-04-30 stsp static const struct got_error *
1524 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1525 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1526 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1527 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1528 11b20872 2019-11-08 stsp struct tog_colors *colors)
1530 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1531 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1532 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1533 60493ae3 2019-06-20 stsp int width;
1534 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1535 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1536 8b473291 2019-02-21 stsp char *refs_str = NULL;
1537 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1538 11b20872 2019-11-08 stsp struct tog_color *tc;
1539 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1541 e0d42f60 2018-07-22 stsp entry = first;
1542 e0d42f60 2018-07-22 stsp ncommits = 0;
1543 e0d42f60 2018-07-22 stsp while (entry) {
1544 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1545 e0d42f60 2018-07-22 stsp *selected = entry;
1548 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1549 e0d42f60 2018-07-22 stsp ncommits++;
1552 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1553 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1555 ecb28ae0 2018-07-16 stsp return err;
1556 8b473291 2019-02-21 stsp if (refs) {
1557 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1560 8b473291 2019-02-21 stsp goto done;
1564 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1565 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1567 8f4ed634 2020-03-26 stsp if (commits_needed > 0) {
1568 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1569 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1570 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1571 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1572 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1573 8f4ed634 2020-03-26 stsp goto done;
1576 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1578 f9686aa5 2020-03-27 stsp if (view->searching) {
1579 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1580 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1581 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1582 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1583 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1584 f9686aa5 2020-03-27 stsp search_str = "searching...";
1587 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1588 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1589 f9686aa5 2020-03-27 stsp search_str ? search_str :
1590 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1591 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1592 8f4ed634 2020-03-26 stsp goto done;
1596 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1597 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1598 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1599 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1601 1a76625f 2018-10-22 stsp header = NULL;
1602 1a76625f 2018-10-22 stsp goto done;
1604 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1605 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1606 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1607 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1608 1a76625f 2018-10-22 stsp header = NULL;
1609 1a76625f 2018-10-22 stsp goto done;
1611 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1613 1a76625f 2018-10-22 stsp goto done;
1615 2814baeb 2018-08-01 stsp werase(view->window);
1617 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1618 a3404814 2018-09-02 stsp wstandout(view->window);
1619 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1621 11b20872 2019-11-08 stsp wattr_on(view->window,
1622 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1623 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1625 11b20872 2019-11-08 stsp wattr_off(view->window,
1626 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1627 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1628 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1631 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1632 a3404814 2018-09-02 stsp wstandend(view->window);
1633 ecb28ae0 2018-07-16 stsp free(wline);
1634 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1635 1a76625f 2018-10-22 stsp goto done;
1637 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1638 899d86c2 2018-05-10 stsp entry = first;
1639 5813d178 2019-03-09 stsp ncommits = 0;
1640 5813d178 2019-03-09 stsp while (entry) {
1641 5813d178 2019-03-09 stsp char *author;
1642 5813d178 2019-03-09 stsp wchar_t *wauthor;
1643 5813d178 2019-03-09 stsp int width;
1644 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1646 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1647 5813d178 2019-03-09 stsp if (author == NULL) {
1648 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1649 5813d178 2019-03-09 stsp goto done;
1651 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1652 27a741e5 2019-09-11 stsp date_display_cols);
1653 5813d178 2019-03-09 stsp if (author_cols < width)
1654 5813d178 2019-03-09 stsp author_cols = width;
1655 5813d178 2019-03-09 stsp free(wauthor);
1656 5813d178 2019-03-09 stsp free(author);
1657 7ca04879 2019-10-19 stsp ncommits++;
1658 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1661 5813d178 2019-03-09 stsp entry = first;
1662 899d86c2 2018-05-10 stsp *last = first;
1663 867c6645 2018-07-10 stsp ncommits = 0;
1664 899d86c2 2018-05-10 stsp while (entry) {
1665 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1667 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1668 2814baeb 2018-08-01 stsp wstandout(view->window);
1669 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1670 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1671 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1672 2814baeb 2018-08-01 stsp wstandend(view->window);
1674 60493ae3 2019-06-20 stsp goto done;
1675 0553a4e3 2018-04-30 stsp ncommits++;
1676 899d86c2 2018-05-10 stsp *last = entry;
1677 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1680 1a57306a 2018-09-02 stsp view_vborder(view);
1682 1a76625f 2018-10-22 stsp free(id_str);
1683 8b473291 2019-02-21 stsp free(refs_str);
1684 1a76625f 2018-10-22 stsp free(ncommits_str);
1685 1a76625f 2018-10-22 stsp free(header);
1686 80ddbec8 2018-04-29 stsp return err;
1689 07b55e75 2018-05-10 stsp static void
1690 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1691 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1692 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1694 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1695 07b55e75 2018-05-10 stsp int nscrolled = 0;
1697 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1698 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1701 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1702 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1703 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1704 07b55e75 2018-05-10 stsp if (entry) {
1705 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1706 07b55e75 2018-05-10 stsp nscrolled++;
1711 aa075928 2018-05-10 stsp static const struct got_error *
1712 7c1452c1 2020-03-26 stsp trigger_log_thread(struct tog_view *log_view, int wait,
1713 7c1452c1 2020-03-26 stsp int *commits_needed, int *log_complete,
1714 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1716 5e224a3e 2019-02-22 stsp int errcode;
1718 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1720 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1721 5e224a3e 2019-02-22 stsp if (*log_complete)
1724 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1725 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1726 7aafa0d1 2019-02-22 stsp if (errcode)
1727 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1728 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1731 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1732 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1734 7c1452c1 2020-03-26 stsp if (!wait)
1737 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1738 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1739 7c1452c1 2020-03-26 stsp update_panels();
1740 7c1452c1 2020-03-26 stsp doupdate();
1742 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1743 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1744 82954512 2020-02-03 stsp if (errcode)
1745 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1746 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1748 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1749 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1750 7c1452c1 2020-03-26 stsp update_panels();
1751 7c1452c1 2020-03-26 stsp doupdate();
1754 5e224a3e 2019-02-22 stsp return NULL;
1757 5e224a3e 2019-02-22 stsp static const struct got_error *
1758 7c1452c1 2020-03-26 stsp scroll_down(struct tog_view *log_view,
1759 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1760 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1761 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1762 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1764 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1765 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1766 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1768 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1769 5e224a3e 2019-02-22 stsp return NULL;
1771 7c1452c1 2020-03-26 stsp ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1772 7c1452c1 2020-03-26 stsp if (commits->ncommits < ncommits_needed && !*log_complete) {
1774 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1776 7c1452c1 2020-03-26 stsp (*commits_needed) += maxscroll;
1777 7c1452c1 2020-03-26 stsp err = trigger_log_thread(log_view, 1, commits_needed,
1778 7c1452c1 2020-03-26 stsp log_complete, need_commits, commit_loaded);
1780 5e224a3e 2019-02-22 stsp return err;
1784 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1785 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1788 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1790 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1791 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1793 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1794 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1796 dd0a52c1 2018-05-20 stsp return err;
1799 cd0acaa7 2018-05-20 stsp static const struct got_error *
1800 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1801 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1802 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1804 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1805 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1806 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1808 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1809 15a94983 2018-12-23 stsp if (diff_view == NULL)
1810 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1812 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1813 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1814 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1815 e5a0f69f 2018-08-18 stsp if (err == NULL)
1816 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1817 cd0acaa7 2018-05-20 stsp return err;
1820 80ddbec8 2018-04-29 stsp static const struct got_error *
1821 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1822 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1824 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1826 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1827 941e9f74 2019-05-21 stsp if (parent == NULL)
1828 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1830 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1831 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1832 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1833 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1834 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1835 941e9f74 2019-05-21 stsp s->tree = subtree;
1836 941e9f74 2019-05-21 stsp s->selected = 0;
1837 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1838 941e9f74 2019-05-21 stsp return NULL;
1841 941e9f74 2019-05-21 stsp static const struct got_error *
1842 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1843 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1844 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1846 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1847 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1848 941e9f74 2019-05-21 stsp const char *p;
1849 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1851 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1853 941e9f74 2019-05-21 stsp while (*p) {
1854 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1855 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1856 56e0773d 2019-11-28 stsp char *te_name;
1858 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1861 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1862 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1863 941e9f74 2019-05-21 stsp if (slash == NULL)
1864 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1866 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1867 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1868 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1871 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1872 56e0773d 2019-11-28 stsp if (te == NULL) {
1873 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1874 56e0773d 2019-11-28 stsp free(te_name);
1877 56e0773d 2019-11-28 stsp free(te_name);
1878 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1880 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1881 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1883 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1884 941e9f74 2019-05-21 stsp if (slash)
1885 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1887 941e9f74 2019-05-21 stsp subpath = strdup(path);
1888 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1889 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1893 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1898 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1899 941e9f74 2019-05-21 stsp free(tree_id);
1903 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1904 941e9f74 2019-05-21 stsp if (err) {
1905 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1908 941e9f74 2019-05-21 stsp if (slash == NULL)
1910 941e9f74 2019-05-21 stsp free(subpath);
1911 941e9f74 2019-05-21 stsp subpath = NULL;
1912 941e9f74 2019-05-21 stsp p = slash;
1915 941e9f74 2019-05-21 stsp free(subpath);
1916 1a76625f 2018-10-22 stsp return err;
1919 61266923 2020-01-14 stsp static const struct got_error *
1920 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1921 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1922 78756c87 2020-11-24 stsp struct got_repository *repo)
1924 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1925 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1926 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1927 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1929 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1930 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1932 55cccc34 2020-02-20 stsp return err;
1934 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1935 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1936 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1938 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1939 55cccc34 2020-02-20 stsp if (err) {
1940 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1941 55cccc34 2020-02-20 stsp return err;
1943 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1945 55cccc34 2020-02-20 stsp *new_view = tree_view;
1947 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1948 55cccc34 2020-02-20 stsp return NULL;
1950 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1953 55cccc34 2020-02-20 stsp static const struct got_error *
1954 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1956 61266923 2020-01-14 stsp sigset_t sigset;
1957 61266923 2020-01-14 stsp int errcode;
1959 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1960 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1962 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1963 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1964 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1965 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1966 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1968 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1969 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1970 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1972 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1973 61266923 2020-01-14 stsp if (errcode)
1974 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1976 61266923 2020-01-14 stsp return NULL;
1979 1a76625f 2018-10-22 stsp static void *
1980 1a76625f 2018-10-22 stsp log_thread(void *arg)
1982 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1983 1a76625f 2018-10-22 stsp int errcode = 0;
1984 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1985 1a76625f 2018-10-22 stsp int done = 0;
1987 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1989 61266923 2020-01-14 stsp return (void *)err;
1991 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1992 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1993 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1994 13add988 2019-10-15 stsp a->regex);
1995 1a76625f 2018-10-22 stsp if (err) {
1996 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1997 1a76625f 2018-10-22 stsp return (void *)err;
1998 1a76625f 2018-10-22 stsp err = NULL;
2000 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2001 1a76625f 2018-10-22 stsp a->commits_needed--;
2003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2004 3abe8080 2019-04-10 stsp if (errcode) {
2005 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2006 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2008 3abe8080 2019-04-10 stsp } else if (*a->quit)
2010 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2011 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2012 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2013 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2016 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2017 7c1452c1 2020-03-26 stsp if (errcode) {
2018 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2019 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2020 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2025 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2027 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2028 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2029 7c1452c1 2020-03-26 stsp &tog_mutex);
2030 7c1452c1 2020-03-26 stsp if (errcode)
2031 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2032 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2036 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2037 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2038 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2039 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2041 3abe8080 2019-04-10 stsp a->log_complete = 1;
2042 1a76625f 2018-10-22 stsp return (void *)err;
2045 1a76625f 2018-10-22 stsp static const struct got_error *
2046 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2048 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2049 1a76625f 2018-10-22 stsp int errcode;
2051 1a76625f 2018-10-22 stsp if (s->thread) {
2052 1a76625f 2018-10-22 stsp s->quit = 1;
2053 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2054 1a76625f 2018-10-22 stsp if (errcode)
2055 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2056 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2057 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2058 1a76625f 2018-10-22 stsp if (errcode)
2059 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2060 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2061 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2062 1a76625f 2018-10-22 stsp if (errcode)
2063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2064 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2065 1a76625f 2018-10-22 stsp if (errcode)
2066 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2067 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2068 1a76625f 2018-10-22 stsp s->thread = NULL;
2071 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2072 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2073 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2076 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2077 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2078 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2081 9343a5fb 2018-06-23 stsp return err;
2084 9343a5fb 2018-06-23 stsp static const struct got_error *
2085 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2087 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2088 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2089 276b94a1 2020-11-13 naddy int errcode;
2091 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2093 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2094 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2095 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2097 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2098 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2099 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2101 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2102 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2103 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2104 1a76625f 2018-10-22 stsp free(s->start_id);
2105 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2106 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2107 1a76625f 2018-10-22 stsp return err;
2110 1a76625f 2018-10-22 stsp static const struct got_error *
2111 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2113 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2115 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2116 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2117 60493ae3 2019-06-20 stsp return NULL;
2120 60493ae3 2019-06-20 stsp static const struct got_error *
2121 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2123 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2124 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2125 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2127 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2128 f9686aa5 2020-03-27 stsp show_log_view(view);
2129 f9686aa5 2020-03-27 stsp update_panels();
2130 f9686aa5 2020-03-27 stsp doupdate();
2132 96e2b566 2019-07-08 stsp if (s->search_entry) {
2133 13add988 2019-10-15 stsp int errcode, ch;
2134 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2135 13add988 2019-10-15 stsp if (errcode)
2136 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2137 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2138 13add988 2019-10-15 stsp ch = wgetch(view->window);
2139 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2140 13add988 2019-10-15 stsp if (errcode)
2141 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2142 13add988 2019-10-15 stsp "pthread_mutex_lock");
2143 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2144 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2145 678cbce5 2019-07-28 stsp return NULL;
2147 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2148 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2150 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2151 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2152 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2153 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2154 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2156 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2157 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2159 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2160 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2162 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2165 60493ae3 2019-06-20 stsp while (1) {
2166 13add988 2019-10-15 stsp int have_match = 0;
2168 60493ae3 2019-06-20 stsp if (entry == NULL) {
2169 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2170 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2171 f9967bca 2020-03-27 stsp view->search_next_done =
2172 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2173 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2174 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2175 f801134a 2019-06-25 stsp return NULL;
2178 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2179 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2180 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2182 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2183 7c1452c1 2020-03-26 stsp return trigger_log_thread(view, 0,
2184 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2185 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2186 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2187 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2190 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2191 13add988 2019-10-15 stsp &view->regex);
2194 13add988 2019-10-15 stsp if (have_match) {
2195 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2196 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2200 96e2b566 2019-07-08 stsp s->search_entry = entry;
2201 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2202 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2204 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2207 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2208 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2209 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2210 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2212 60493ae3 2019-06-20 stsp return err;
2215 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2216 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2218 60493ae3 2019-06-20 stsp return err;
2223 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2225 60493ae3 2019-06-20 stsp return NULL;
2228 60493ae3 2019-06-20 stsp static const struct got_error *
2229 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2230 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2231 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2233 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2234 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2235 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2236 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2237 1a76625f 2018-10-22 stsp int errcode;
2239 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2241 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2242 f135c941 2020-02-20 stsp free(s->in_repo_path);
2243 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2244 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2245 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2248 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2249 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2250 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2252 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2254 78756c87 2020-11-24 stsp goto done;
2256 fb2756b9 2018-08-04 stsp s->repo = repo;
2257 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2258 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2259 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2260 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2261 5036bf37 2018-09-24 stsp goto done;
2263 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2265 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2266 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2267 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2268 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2270 11b20872 2019-11-08 stsp goto done;
2271 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2272 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2273 11b20872 2019-11-08 stsp if (err) {
2274 11b20872 2019-11-08 stsp free_colors(&s->colors);
2275 11b20872 2019-11-08 stsp goto done;
2277 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2278 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2279 11b20872 2019-11-08 stsp if (err) {
2280 11b20872 2019-11-08 stsp free_colors(&s->colors);
2281 11b20872 2019-11-08 stsp goto done;
2285 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2286 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2287 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2288 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2289 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2291 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2293 1a76625f 2018-10-22 stsp goto done;
2294 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2295 b672a97a 2020-01-27 stsp !s->log_branches);
2297 1a76625f 2018-10-22 stsp goto done;
2298 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2299 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2301 c5b78334 2020-01-12 stsp goto done;
2303 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2304 1a76625f 2018-10-22 stsp if (errcode) {
2305 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2306 1a76625f 2018-10-22 stsp goto done;
2308 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2309 7c1452c1 2020-03-26 stsp if (errcode) {
2310 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2311 7c1452c1 2020-03-26 stsp goto done;
2314 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2315 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2316 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2317 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2318 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2319 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2320 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2321 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2322 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2323 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2324 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2325 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2326 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2329 1a76625f 2018-10-22 stsp close_log_view(view);
2330 ba4f502b 2018-08-04 stsp return err;
2333 e5a0f69f 2018-08-18 stsp static const struct got_error *
2334 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2336 f2f6d207 2020-11-24 stsp const struct got_error *err;
2337 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2339 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2340 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2341 2b380cc8 2018-10-24 stsp &s->thread_args);
2342 2b380cc8 2018-10-24 stsp if (errcode)
2343 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2344 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2345 f2f6d207 2020-11-24 stsp err = trigger_log_thread(view, 1,
2346 f2f6d207 2020-11-24 stsp &s->thread_args.commits_needed,
2347 f2f6d207 2020-11-24 stsp &s->thread_args.log_complete,
2348 f2f6d207 2020-11-24 stsp &s->thread_args.need_commits,
2349 f2f6d207 2020-11-24 stsp &s->thread_args.commit_loaded);
2351 f2f6d207 2020-11-24 stsp return err;
2355 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2356 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2357 78756c87 2020-11-24 stsp &s->commits, s->selected, view->nlines, &s->refs,
2358 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2361 e5a0f69f 2018-08-18 stsp static const struct got_error *
2362 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2363 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2365 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2366 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2367 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2368 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2369 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2370 669b5ffa 2018-10-07 stsp int begin_x = 0;
2371 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2373 e5a0f69f 2018-08-18 stsp switch (ch) {
2375 1e37a5c2 2019-05-12 jcs s->quit = 1;
2378 1e37a5c2 2019-05-12 jcs case KEY_UP:
2381 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2383 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2384 1e37a5c2 2019-05-12 jcs s->selected--;
2386 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2387 1144d21a 2019-06-21 stsp &s->commits);
2389 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2390 a4292ac5 2019-05-12 jcs case CTRL('b'):
2391 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2393 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2394 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2395 1e37a5c2 2019-05-12 jcs s->selected = 0;
2398 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2399 1831ac02 2020-03-23 naddy view->nlines - 1, &s->commits);
2402 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2405 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2407 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2408 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2409 1e37a5c2 2019-05-12 jcs s->selected++;
2412 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2413 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2414 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2415 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2416 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2417 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2419 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2420 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2421 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2422 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2423 1e37a5c2 2019-05-12 jcs if (first == NULL)
2425 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2426 1831ac02 2020-03-23 naddy view->nlines - 1, &s->last_displayed_entry,
2427 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2428 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2429 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2430 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2433 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2434 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2435 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2436 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2437 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2438 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2440 1e37a5c2 2019-05-12 jcs err = NULL;
2443 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2444 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2445 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2446 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2447 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2449 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2452 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2454 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2455 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2456 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2457 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2458 78756c87 2020-11-24 stsp view, s->repo);
2461 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2462 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2464 1e37a5c2 2019-05-12 jcs return err;
2465 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2467 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2470 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2471 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2473 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2476 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2478 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2479 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2480 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2481 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2484 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2485 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2487 1e37a5c2 2019-05-12 jcs return err;
2488 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2490 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2493 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2494 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2496 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2498 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2499 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2500 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2502 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2504 74cfe85e 2020-10-20 stsp return err;
2505 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2506 74cfe85e 2020-10-20 stsp if (err) {
2507 74cfe85e 2020-10-20 stsp free(parent_path);
2508 74cfe85e 2020-10-20 stsp return err;
2510 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2511 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2512 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2513 74cfe85e 2020-10-20 stsp free(parent_path);
2514 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2516 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2517 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2518 74cfe85e 2020-10-20 stsp free(parent_path);
2520 74cfe85e 2020-10-20 stsp return err;;
2521 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2522 74cfe85e 2020-10-20 stsp *new_view = lv;
2524 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2525 74cfe85e 2020-10-20 stsp *focus_view = lv;
2528 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2529 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2531 d01904d4 2019-06-25 stsp return err;
2532 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2533 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2534 d01904d4 2019-06-25 stsp if (lv == NULL)
2535 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2536 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2537 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2538 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2539 ef129c5e 2019-08-03 stsp if (err) {
2540 ef129c5e 2019-08-03 stsp view_close(lv);
2541 d01904d4 2019-06-25 stsp return err;
2543 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2544 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2545 e6b23735 2019-10-04 stsp free(start_id);
2546 e6b23735 2019-10-04 stsp view_close(lv);
2547 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2549 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2550 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2551 ef129c5e 2019-08-03 stsp if (err) {
2552 ef129c5e 2019-08-03 stsp free(start_id);
2553 b672a97a 2020-01-27 stsp view_close(lv);
2554 b672a97a 2020-01-27 stsp return err;;
2556 b672a97a 2020-01-27 stsp *dead_view = view;
2557 b672a97a 2020-01-27 stsp *new_view = lv;
2560 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2561 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2563 b672a97a 2020-01-27 stsp return err;
2564 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2565 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2566 b672a97a 2020-01-27 stsp if (lv == NULL)
2567 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2568 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2569 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2570 b672a97a 2020-01-27 stsp if (err) {
2571 ef129c5e 2019-08-03 stsp view_close(lv);
2572 d01904d4 2019-06-25 stsp return err;;
2574 d01904d4 2019-06-25 stsp *dead_view = view;
2575 d01904d4 2019-06-25 stsp *new_view = lv;
2578 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2579 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2580 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2581 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2582 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2583 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2584 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2585 6458efa5 2020-11-24 stsp if (err) {
2586 6458efa5 2020-11-24 stsp view_close(ref_view);
2587 6458efa5 2020-11-24 stsp return err;
2589 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2590 6458efa5 2020-11-24 stsp err = view_close_child(view);
2592 6458efa5 2020-11-24 stsp return err;
2593 6458efa5 2020-11-24 stsp err = view_set_child(view, ref_view);
2594 6458efa5 2020-11-24 stsp if (err) {
2595 6458efa5 2020-11-24 stsp view_close(ref_view);
2598 6458efa5 2020-11-24 stsp *focus_view = ref_view;
2599 6458efa5 2020-11-24 stsp view->child_focussed = 1;
2601 6458efa5 2020-11-24 stsp *new_view = ref_view;
2607 80ddbec8 2018-04-29 stsp return err;
2610 4ed7e80c 2018-05-20 stsp static const struct got_error *
2611 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2613 c2db6724 2019-01-04 stsp const struct got_error *error;
2615 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2616 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2617 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2619 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2620 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2622 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2623 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2625 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2626 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2628 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2629 c2db6724 2019-01-04 stsp if (error != NULL)
2630 c2db6724 2019-01-04 stsp return error;
2632 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2633 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2635 c2db6724 2019-01-04 stsp return NULL;
2638 a915003a 2019-02-05 stsp static void
2639 a915003a 2019-02-05 stsp init_curses(void)
2641 a915003a 2019-02-05 stsp initscr();
2643 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2646 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2647 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2648 a915003a 2019-02-05 stsp curs_set(0);
2649 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2650 6d17833f 2019-11-08 stsp start_color();
2651 6d17833f 2019-11-08 stsp use_default_colors();
2653 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2654 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2655 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2658 c2db6724 2019-01-04 stsp static const struct got_error *
2659 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2660 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2662 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2664 f135c941 2020-02-20 stsp if (argc == 0) {
2665 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2666 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2667 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2668 f135c941 2020-02-20 stsp return NULL;
2671 f135c941 2020-02-20 stsp if (worktree) {
2672 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2675 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2677 f135c941 2020-02-20 stsp return err;
2678 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2679 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2680 bfd61697 2020-11-14 stsp p) == -1) {
2681 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2682 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2686 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2688 f135c941 2020-02-20 stsp return err;
2691 f135c941 2020-02-20 stsp static const struct got_error *
2692 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2694 80ddbec8 2018-04-29 stsp const struct got_error *error;
2695 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2696 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2697 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2698 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2699 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2700 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2701 04cc582a 2018-08-01 stsp struct tog_view *view;
2703 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2704 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2705 c2db6724 2019-01-04 stsp NULL) == -1)
2706 80ddbec8 2018-04-29 stsp err(1, "pledge");
2709 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2710 80ddbec8 2018-04-29 stsp switch (ch) {
2712 b672a97a 2020-01-27 stsp log_branches = 1;
2715 80ddbec8 2018-04-29 stsp start_commit = optarg;
2718 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2719 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2720 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2724 17020d27 2019-03-07 stsp usage_log();
2725 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2729 80ddbec8 2018-04-29 stsp argc -= optind;
2730 80ddbec8 2018-04-29 stsp argv += optind;
2732 f135c941 2020-02-20 stsp if (argc > 1)
2733 f135c941 2020-02-20 stsp usage_log();
2735 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2736 6962eb72 2020-02-20 stsp if (cwd == NULL)
2737 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2739 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2740 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2741 963f97a1 2019-03-18 stsp goto done;
2743 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2744 a1fbf39a 2019-08-11 stsp if (worktree)
2745 6962eb72 2020-02-20 stsp repo_path =
2746 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2748 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2750 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2751 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2752 963f97a1 2019-03-18 stsp goto done;
2755 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2756 80ddbec8 2018-04-29 stsp if (error != NULL)
2757 ecb28ae0 2018-07-16 stsp goto done;
2759 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2760 f135c941 2020-02-20 stsp repo, worktree);
2761 f135c941 2020-02-20 stsp if (error)
2762 f135c941 2020-02-20 stsp goto done;
2764 f135c941 2020-02-20 stsp init_curses();
2766 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2767 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2768 c02c541e 2019-03-29 stsp if (error)
2769 c02c541e 2019-03-29 stsp goto done;
2771 15a94983 2018-12-23 stsp if (start_commit == NULL)
2772 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2773 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2774 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2776 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2777 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2778 80ddbec8 2018-04-29 stsp if (error != NULL)
2779 8b473291 2019-02-21 stsp goto done;
2781 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2782 04cc582a 2018-08-01 stsp if (view == NULL) {
2783 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2784 04cc582a 2018-08-01 stsp goto done;
2786 2fc00ff4 2019-08-31 stsp if (worktree) {
2787 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2788 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2789 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2790 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2791 2fc00ff4 2019-08-31 stsp goto done;
2794 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2795 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2796 ba4f502b 2018-08-04 stsp if (error)
2797 ba4f502b 2018-08-04 stsp goto done;
2798 2fc00ff4 2019-08-31 stsp if (worktree) {
2799 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2800 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2801 2fc00ff4 2019-08-31 stsp worktree = NULL;
2803 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2805 f135c941 2020-02-20 stsp free(in_repo_path);
2806 ecb28ae0 2018-07-16 stsp free(repo_path);
2807 ecb28ae0 2018-07-16 stsp free(cwd);
2808 899d86c2 2018-05-10 stsp free(start_id);
2809 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2811 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2812 ec142235 2019-03-07 stsp if (worktree)
2813 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2814 80ddbec8 2018-04-29 stsp return error;
2817 4ed7e80c 2018-05-20 stsp __dead static void
2818 9f7d7167 2018-04-29 stsp usage_diff(void)
2821 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2822 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2826 b304db33 2018-05-20 stsp static char *
2827 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2829 b304db33 2018-05-20 stsp char *line;
2830 b304db33 2018-05-20 stsp size_t linelen;
2831 b304db33 2018-05-20 stsp size_t lineno;
2832 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2834 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2836 b304db33 2018-05-20 stsp *len = linelen;
2837 b304db33 2018-05-20 stsp return line;
2840 6d17833f 2019-11-08 stsp static int
2841 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2842 41605754 2020-11-12 stsp regmatch_t *regmatch)
2844 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2847 f26dddb7 2019-11-08 stsp struct tog_color *
2848 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2850 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2852 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2853 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2854 6d17833f 2019-11-08 stsp return tc;
2857 6d17833f 2019-11-08 stsp return NULL;
2860 4ed7e80c 2018-05-20 stsp static const struct got_error *
2861 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2862 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2864 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2865 41605754 2020-11-12 stsp wchar_t *wline;
2866 41605754 2020-11-12 stsp int width;
2869 41605754 2020-11-12 stsp *wtotal = 0;
2871 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2872 41605754 2020-11-12 stsp if (s == NULL)
2873 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2875 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2876 41605754 2020-11-12 stsp if (err) {
2878 41605754 2020-11-12 stsp return err;
2880 41605754 2020-11-12 stsp waddwstr(window, wline);
2881 41605754 2020-11-12 stsp free(wline);
2883 41605754 2020-11-12 stsp wlimit -= width;
2884 41605754 2020-11-12 stsp *wtotal += width;
2886 41605754 2020-11-12 stsp if (wlimit > 0) {
2887 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2888 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2889 41605754 2020-11-12 stsp if (s == NULL) {
2890 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2892 41605754 2020-11-12 stsp return err;
2894 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2895 41605754 2020-11-12 stsp if (err) {
2897 41605754 2020-11-12 stsp return err;
2899 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2900 41605754 2020-11-12 stsp waddwstr(window, wline);
2901 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2902 41605754 2020-11-12 stsp free(wline);
2904 41605754 2020-11-12 stsp wlimit -= width;
2905 41605754 2020-11-12 stsp *wtotal += width;
2908 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2909 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2910 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2912 41605754 2020-11-12 stsp return err;
2913 41605754 2020-11-12 stsp waddwstr(window, wline);
2914 41605754 2020-11-12 stsp free(wline);
2915 41605754 2020-11-12 stsp *wtotal += width;
2918 41605754 2020-11-12 stsp return NULL;
2921 41605754 2020-11-12 stsp static const struct got_error *
2922 fe621944 2020-11-10 stsp draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2923 fe621944 2020-11-10 stsp off_t *line_offsets, int selected_line, int max_lines,
2924 3dbaef42 2020-11-24 stsp int *last_displayed_line, int *eof, const char *header,
2925 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2927 61e69b96 2018-05-20 stsp const struct got_error *err;
2928 fe621944 2020-11-10 stsp int nprinted = 0;
2929 b304db33 2018-05-20 stsp char *line;
2930 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2931 b304db33 2018-05-20 stsp size_t len;
2932 61e69b96 2018-05-20 stsp wchar_t *wline;
2933 e0b650dd 2018-05-20 stsp int width;
2934 fe621944 2020-11-10 stsp off_t line_offset;
2936 fe621944 2020-11-10 stsp line_offset = line_offsets[first_displayed_line - 1];
2937 fe621944 2020-11-10 stsp if (fseeko(f, line_offset, SEEK_SET) == -1)
2938 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2940 f7d12f7e 2018-08-01 stsp werase(view->window);
2942 a3404814 2018-09-02 stsp if (header) {
2943 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2944 135a2da0 2020-11-11 stsp first_displayed_line - 1 + selected_line, nlines,
2945 135a2da0 2020-11-11 stsp header) == -1)
2946 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2947 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2948 135a2da0 2020-11-11 stsp free(line);
2950 a3404814 2018-09-02 stsp return err;
2952 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2953 a3404814 2018-09-02 stsp wstandout(view->window);
2954 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2955 e54cc94a 2020-11-11 stsp free(wline);
2956 e54cc94a 2020-11-11 stsp wline = NULL;
2957 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2958 a3404814 2018-09-02 stsp wstandend(view->window);
2959 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2960 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2962 a3404814 2018-09-02 stsp if (max_lines <= 1)
2963 a3404814 2018-09-02 stsp return NULL;
2964 a3404814 2018-09-02 stsp max_lines--;
2968 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2969 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2970 26ed57b2 2018-05-19 stsp if (line == NULL) {
2975 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2977 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2978 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2979 41605754 2020-11-12 stsp if (first_displayed_line + nprinted == matched_line &&
2980 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2981 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2982 41605754 2020-11-12 stsp view->window, regmatch);
2983 41605754 2020-11-12 stsp if (err) {
2984 41605754 2020-11-12 stsp free(line);
2985 41605754 2020-11-12 stsp return err;
2988 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2989 41605754 2020-11-12 stsp if (err) {
2990 41605754 2020-11-12 stsp free(line);
2991 41605754 2020-11-12 stsp return err;
2993 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2994 41605754 2020-11-12 stsp free(wline);
2995 41605754 2020-11-12 stsp wline = NULL;
2998 6d17833f 2019-11-08 stsp wattr_off(view->window,
2999 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3000 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3001 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3002 fe621944 2020-11-10 stsp nprinted++;
3003 26ed57b2 2018-05-19 stsp free(line);
3005 fe621944 2020-11-10 stsp if (nprinted >= 1)
3006 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line + (nprinted - 1);
3008 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line;
3010 1a57306a 2018-09-02 stsp view_vborder(view);
3012 c3e9aa98 2019-05-13 jcs if (*eof) {
3013 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3014 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3015 c3e9aa98 2019-05-13 jcs nprinted++;
3018 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3020 c3e9aa98 2019-05-13 jcs return err;
3023 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3024 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3025 e54cc94a 2020-11-11 stsp free(wline);
3026 e54cc94a 2020-11-11 stsp wline = NULL;
3027 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3030 26ed57b2 2018-05-19 stsp return NULL;
3033 abd2672a 2018-12-23 stsp static char *
3034 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3036 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3037 09867e48 2019-08-13 stsp char *p, *s;
3039 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3040 09867e48 2019-08-13 stsp if (tm == NULL)
3041 09867e48 2019-08-13 stsp return NULL;
3042 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3043 09867e48 2019-08-13 stsp if (s == NULL)
3044 09867e48 2019-08-13 stsp return NULL;
3045 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3047 abd2672a 2018-12-23 stsp *p = '\0';
3051 4ed7e80c 2018-05-20 stsp static const struct got_error *
3052 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3053 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3055 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3056 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3057 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3058 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3060 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3061 0208f208 2020-05-05 stsp if (qid != NULL) {
3062 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3063 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3066 0208f208 2020-05-05 stsp return err;
3068 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3069 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3073 0208f208 2020-05-05 stsp if (tree_id1) {
3074 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3076 0208f208 2020-05-05 stsp goto done;
3079 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3080 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3082 0208f208 2020-05-05 stsp goto done;
3084 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3085 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3087 0208f208 2020-05-05 stsp if (tree1)
3088 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3089 0208f208 2020-05-05 stsp if (tree2)
3090 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3091 0208f208 2020-05-05 stsp return err;
3094 0208f208 2020-05-05 stsp static const struct got_error *
3095 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3099 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3100 fe621944 2020-11-10 stsp if (p == NULL)
3101 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3102 fe621944 2020-11-10 stsp *line_offsets = p;
3103 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3104 fe621944 2020-11-10 stsp (*nlines)++;
3105 fe621944 2020-11-10 stsp return NULL;
3108 fe621944 2020-11-10 stsp static const struct got_error *
3109 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3110 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3111 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3113 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3114 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3115 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3116 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3117 45d799e2 2018-12-23 stsp time_t committer_time;
3118 45d799e2 2018-12-23 stsp const char *author, *committer;
3119 8b473291 2019-02-21 stsp char *refs_str = NULL;
3120 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3121 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3122 fe621944 2020-11-10 stsp off_t outoff = 0;
3125 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3127 8b473291 2019-02-21 stsp if (refs) {
3128 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3130 8b473291 2019-02-21 stsp return err;
3133 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3135 abd2672a 2018-12-23 stsp return err;
3137 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3138 15a94983 2018-12-23 stsp if (err) {
3139 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3140 15a94983 2018-12-23 stsp goto done;
3143 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3145 fe621944 2020-11-10 stsp goto done;
3147 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3148 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3149 fe621944 2020-11-10 stsp if (n < 0) {
3150 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3151 abd2672a 2018-12-23 stsp goto done;
3153 fe621944 2020-11-10 stsp outoff += n;
3154 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3156 fe621944 2020-11-10 stsp goto done;
3158 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3159 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3160 fe621944 2020-11-10 stsp if (n < 0) {
3161 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3162 abd2672a 2018-12-23 stsp goto done;
3164 fe621944 2020-11-10 stsp outoff += n;
3165 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3167 fe621944 2020-11-10 stsp goto done;
3169 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3170 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3171 fe621944 2020-11-10 stsp if (datestr) {
3172 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3173 fe621944 2020-11-10 stsp if (n < 0) {
3174 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3175 fe621944 2020-11-10 stsp goto done;
3177 fe621944 2020-11-10 stsp outoff += n;
3178 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3180 fe621944 2020-11-10 stsp goto done;
3182 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3183 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3184 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3185 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3186 fe621944 2020-11-10 stsp if (n < 0) {
3187 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3188 fe621944 2020-11-10 stsp goto done;
3190 fe621944 2020-11-10 stsp outoff += n;
3191 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3193 fe621944 2020-11-10 stsp goto done;
3195 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3197 5943eee2 2019-08-13 stsp goto done;
3198 fe621944 2020-11-10 stsp s = logmsg;
3199 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3200 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3201 fe621944 2020-11-10 stsp if (n < 0) {
3202 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3203 fe621944 2020-11-10 stsp goto done;
3205 fe621944 2020-11-10 stsp outoff += n;
3206 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3208 fe621944 2020-11-10 stsp goto done;
3211 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3213 0208f208 2020-05-05 stsp goto done;
3214 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3215 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3216 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3217 fe621944 2020-11-10 stsp if (n < 0) {
3218 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3219 fe621944 2020-11-10 stsp goto done;
3221 fe621944 2020-11-10 stsp outoff += n;
3222 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3224 fe621944 2020-11-10 stsp goto done;
3225 0208f208 2020-05-05 stsp free((char *)pe->path);
3226 0208f208 2020-05-05 stsp free(pe->data);
3229 0208f208 2020-05-05 stsp fputc('\n', outfile);
3231 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3233 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3234 abd2672a 2018-12-23 stsp free(id_str);
3235 5943eee2 2019-08-13 stsp free(logmsg);
3236 8b473291 2019-02-21 stsp free(refs_str);
3237 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3238 7510f233 2020-08-09 stsp if (err) {
3239 ae6a6978 2020-08-09 stsp free(*line_offsets);
3240 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3241 ae6a6978 2020-08-09 stsp *nlines = 0;
3243 fe621944 2020-11-10 stsp return err;
3246 abd2672a 2018-12-23 stsp static const struct got_error *
3247 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3249 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3250 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3251 15a94983 2018-12-23 stsp int obj_type;
3253 fe621944 2020-11-10 stsp free(s->line_offsets);
3254 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3255 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3256 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3257 fe621944 2020-11-10 stsp s->nlines = 0;
3259 511a516b 2018-05-19 stsp f = got_opentemp();
3260 48ae06ee 2018-10-18 stsp if (f == NULL) {
3261 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3262 48ae06ee 2018-10-18 stsp goto done;
3264 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3265 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3266 fb43ecf1 2019-02-11 stsp goto done;
3270 15a94983 2018-12-23 stsp if (s->id1)
3271 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3273 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3275 15a94983 2018-12-23 stsp goto done;
3277 15a94983 2018-12-23 stsp switch (obj_type) {
3278 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3279 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3280 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3281 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3283 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3284 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3285 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3286 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3288 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3289 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3290 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3291 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3293 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3295 3ffacbe1 2020-02-02 tracey goto done;
3296 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3297 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3298 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3299 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3300 f44b1f58 2020-02-02 tracey if (err)
3301 f44b1f58 2020-02-02 tracey goto done;
3302 f44b1f58 2020-02-02 tracey } else {
3303 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3304 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3305 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3306 fe621944 2020-11-10 stsp err = write_commit_info(
3307 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3308 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3309 f44b1f58 2020-02-02 tracey if (err)
3310 f44b1f58 2020-02-02 tracey goto done;
3315 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3317 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3318 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3319 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3323 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3326 f44b1f58 2020-02-02 tracey if (err)
3327 f44b1f58 2020-02-02 tracey goto done;
3329 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3330 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3331 48ae06ee 2018-10-18 stsp return err;
3334 f5215bb9 2019-02-22 stsp static void
3335 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3337 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3338 f5215bb9 2019-02-22 stsp update_panels();
3339 f5215bb9 2019-02-22 stsp doupdate();
3342 f44b1f58 2020-02-02 tracey static const struct got_error *
3343 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3345 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3347 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3348 f44b1f58 2020-02-02 tracey return NULL;
3351 f44b1f58 2020-02-02 tracey static const struct got_error *
3352 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3354 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3355 f44b1f58 2020-02-02 tracey int lineno;
3357 f44b1f58 2020-02-02 tracey if (!view->searching) {
3358 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3359 f44b1f58 2020-02-02 tracey return NULL;
3362 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3363 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3364 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3366 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3367 f44b1f58 2020-02-02 tracey } else {
3368 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3369 f44b1f58 2020-02-02 tracey lineno = 1;
3371 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3374 f44b1f58 2020-02-02 tracey while (1) {
3375 f44b1f58 2020-02-02 tracey char *line = NULL;
3376 f44b1f58 2020-02-02 tracey off_t offset;
3377 f44b1f58 2020-02-02 tracey size_t len;
3379 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3380 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3381 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3382 f44b1f58 2020-02-02 tracey free(line);
3386 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3387 f44b1f58 2020-02-02 tracey lineno = 1;
3389 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3392 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3393 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3394 f44b1f58 2020-02-02 tracey free(line);
3395 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3397 f44b1f58 2020-02-02 tracey free(line);
3398 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3399 41605754 2020-11-12 stsp if (line &&
3400 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3401 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3402 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3403 f44b1f58 2020-02-02 tracey free(line);
3406 f44b1f58 2020-02-02 tracey free(line);
3407 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3408 f44b1f58 2020-02-02 tracey lineno++;
3410 f44b1f58 2020-02-02 tracey lineno--;
3413 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3414 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3415 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3418 f44b1f58 2020-02-02 tracey return NULL;
3421 48ae06ee 2018-10-18 stsp static const struct got_error *
3422 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3423 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3424 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3425 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3427 48ae06ee 2018-10-18 stsp const struct got_error *err;
3428 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3430 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3432 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3433 15a94983 2018-12-23 stsp int type1, type2;
3434 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3436 15a94983 2018-12-23 stsp return err;
3437 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3439 15a94983 2018-12-23 stsp return err;
3441 15a94983 2018-12-23 stsp if (type1 != type2)
3442 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3444 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3445 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3446 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3447 f44b1f58 2020-02-02 tracey s->repo = repo;
3448 f44b1f58 2020-02-02 tracey s->id1 = id1;
3449 f44b1f58 2020-02-02 tracey s->id2 = id2;
3450 3dbaef42 2020-11-24 stsp s->label1 = label1;
3451 3dbaef42 2020-11-24 stsp s->label2 = label2;
3453 15a94983 2018-12-23 stsp if (id1) {
3454 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3455 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3456 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3458 5465d566 2020-02-01 tracey s->id1 = NULL;
3460 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3461 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3462 5465d566 2020-02-01 tracey free(s->id1);
3463 5465d566 2020-02-01 tracey s->id1 = NULL;
3464 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3466 5465d566 2020-02-01 tracey s->f = NULL;
3467 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3468 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3469 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3470 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3471 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3472 5465d566 2020-02-01 tracey s->log_view = log_view;
3473 5465d566 2020-02-01 tracey s->repo = repo;
3475 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3476 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3477 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3478 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3479 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3481 6d17833f 2019-11-08 stsp return err;
3482 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3483 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3484 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3485 6d17833f 2019-11-08 stsp if (err) {
3486 5465d566 2020-02-01 tracey free_colors(&s->colors);
3487 6d17833f 2019-11-08 stsp return err;
3489 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3490 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3491 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3492 6d17833f 2019-11-08 stsp if (err) {
3493 5465d566 2020-02-01 tracey free_colors(&s->colors);
3494 6d17833f 2019-11-08 stsp return err;
3497 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3498 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3499 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3500 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3501 6d17833f 2019-11-08 stsp if (err) {
3502 5465d566 2020-02-01 tracey free_colors(&s->colors);
3503 6d17833f 2019-11-08 stsp return err;
3506 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3507 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3508 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3509 11b20872 2019-11-08 stsp if (err) {
3510 5465d566 2020-02-01 tracey free_colors(&s->colors);
3511 11b20872 2019-11-08 stsp return err;
3514 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3515 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3516 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3517 11b20872 2019-11-08 stsp if (err) {
3518 5465d566 2020-02-01 tracey free_colors(&s->colors);
3519 11b20872 2019-11-08 stsp return err;
3523 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3524 78756c87 2020-11-24 stsp if (err) {
3525 78756c87 2020-11-24 stsp free(s->id1);
3526 78756c87 2020-11-24 stsp s->id1 = NULL;
3527 78756c87 2020-11-24 stsp free(s->id2);
3528 78756c87 2020-11-24 stsp s->id2 = NULL;
3529 78756c87 2020-11-24 stsp free_colors(&s->colors);
3530 78756c87 2020-11-24 stsp return err;
3533 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3534 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3535 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3537 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3538 fe621944 2020-11-10 stsp s->nlines = 0;
3539 5465d566 2020-02-01 tracey err = create_diff(s);
3540 48ae06ee 2018-10-18 stsp if (err) {
3541 5465d566 2020-02-01 tracey free(s->id1);
3542 5465d566 2020-02-01 tracey s->id1 = NULL;
3543 5465d566 2020-02-01 tracey free(s->id2);
3544 5465d566 2020-02-01 tracey s->id2 = NULL;
3545 78756c87 2020-11-24 stsp free_colors(&s->colors);
3546 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3547 48ae06ee 2018-10-18 stsp return err;
3550 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3551 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3552 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3553 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3554 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3556 5dc9f4bc 2018-08-04 stsp return NULL;
3559 e5a0f69f 2018-08-18 stsp static const struct got_error *
3560 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3562 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3563 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3565 5465d566 2020-02-01 tracey free(s->id1);
3566 5465d566 2020-02-01 tracey s->id1 = NULL;
3567 5465d566 2020-02-01 tracey free(s->id2);
3568 5465d566 2020-02-01 tracey s->id2 = NULL;
3569 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3570 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3571 5465d566 2020-02-01 tracey free_colors(&s->colors);
3572 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3573 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3574 fe621944 2020-11-10 stsp s->nlines = 0;
3575 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3576 e5a0f69f 2018-08-18 stsp return err;
3579 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3580 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3582 a3404814 2018-09-02 stsp const struct got_error *err;
3583 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3584 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3585 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3587 a3404814 2018-09-02 stsp if (s->id1) {
3588 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3590 a3404814 2018-09-02 stsp return err;
3591 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3593 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3595 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3597 a3404814 2018-09-02 stsp return err;
3598 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3600 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3601 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3602 a3404814 2018-09-02 stsp free(id_str1);
3603 a3404814 2018-09-02 stsp free(id_str2);
3604 a3404814 2018-09-02 stsp return err;
3606 a3404814 2018-09-02 stsp free(id_str1);
3607 a3404814 2018-09-02 stsp free(id_str2);
3609 fe621944 2020-11-10 stsp return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3610 fe621944 2020-11-10 stsp s->line_offsets, s->selected_line, view->nlines,
3611 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, header, &s->colors,
3612 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
3615 15a087fe 2019-02-21 stsp static const struct got_error *
3616 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3617 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3619 d7a04538 2019-02-21 stsp const struct got_error *err;
3620 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3621 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3622 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3624 15a087fe 2019-02-21 stsp free(s->id2);
3625 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3626 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3627 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3629 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3631 d7a04538 2019-02-21 stsp return err;
3632 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3633 15a087fe 2019-02-21 stsp free(s->id1);
3634 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3635 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3636 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3637 15a087fe 2019-02-21 stsp return NULL;
3640 0cf4efb1 2018-09-29 stsp static const struct got_error *
3641 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3642 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3644 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3645 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3646 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3647 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3650 e5a0f69f 2018-08-18 stsp switch (ch) {
3653 3dbaef42 2020-11-24 stsp if (ch == 'a')
3654 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3655 3dbaef42 2020-11-24 stsp if (ch == 'w')
3656 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3657 64453f7e 2020-11-21 stsp wclear(view->window);
3658 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3659 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3660 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3661 64453f7e 2020-11-21 stsp err = create_diff(s);
3664 1e37a5c2 2019-05-12 jcs case KEY_UP:
3665 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3666 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3668 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3669 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3670 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3673 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3674 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3675 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3678 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3679 1e37a5c2 2019-05-12 jcs if (!s->eof)
3680 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3682 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3683 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3685 00ba99a7 2019-05-12 jcs if (s->eof)
3688 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3689 1e37a5c2 2019-05-12 jcs char *line;
3690 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3691 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3692 1e37a5c2 2019-05-12 jcs if (line == NULL)
3697 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3698 1e37a5c2 2019-05-12 jcs s->diff_context--;
3699 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3700 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3701 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3702 27829c9e 2020-11-21 stsp s->nlines) {
3703 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3704 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3709 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3710 1e37a5c2 2019-05-12 jcs s->diff_context++;
3711 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3712 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3717 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3719 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3720 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3721 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3722 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3725 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3730 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3734 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3735 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3737 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3738 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3742 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3744 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3746 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3747 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3748 7c1452c1 2020-03-26 stsp err = trigger_log_thread(s->log_view, 1,
3749 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3750 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3751 7c1452c1 2020-03-26 stsp &ls->thread_args.need_commits,
3752 7c1452c1 2020-03-26 stsp &ls->thread_args.commit_loaded);
3756 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3761 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3762 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3765 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3769 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3770 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3772 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3773 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3779 bcbd79e2 2018-08-19 stsp return err;
3782 4ed7e80c 2018-05-20 stsp static const struct got_error *
3783 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3785 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3786 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3787 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3788 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3789 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3790 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3791 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3792 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3793 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3794 3dbaef42 2020-11-24 stsp const char *errstr;
3795 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3797 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3798 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3799 eb6600df 2019-01-04 stsp NULL) == -1)
3800 26ed57b2 2018-05-19 stsp err(1, "pledge");
3802 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3803 26ed57b2 2018-05-19 stsp switch (ch) {
3805 64453f7e 2020-11-21 stsp force_text_diff = 1;
3808 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3810 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3811 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3813 09b5bff8 2020-02-23 naddy case 'r':
3814 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3815 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3816 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3818 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3821 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3824 17020d27 2019-03-07 stsp usage_diff();
3825 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3829 26ed57b2 2018-05-19 stsp argc -= optind;
3830 26ed57b2 2018-05-19 stsp argv += optind;
3832 26ed57b2 2018-05-19 stsp if (argc == 0) {
3833 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3834 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3835 15a94983 2018-12-23 stsp id_str1 = argv[0];
3836 15a94983 2018-12-23 stsp id_str2 = argv[1];
3838 26ed57b2 2018-05-19 stsp usage_diff();
3840 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3841 a273ac94 2020-02-23 naddy if (cwd == NULL)
3842 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3844 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3845 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3846 a273ac94 2020-02-23 naddy goto done;
3848 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3849 a273ac94 2020-02-23 naddy if (worktree)
3850 a273ac94 2020-02-23 naddy repo_path =
3851 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3853 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3855 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3856 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3857 a273ac94 2020-02-23 naddy goto done;
3860 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3861 eb6600df 2019-01-04 stsp if (error)
3862 eb6600df 2019-01-04 stsp goto done;
3864 a273ac94 2020-02-23 naddy init_curses();
3866 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3867 26ed57b2 2018-05-19 stsp if (error)
3868 26ed57b2 2018-05-19 stsp goto done;
3870 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3871 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3872 26ed57b2 2018-05-19 stsp if (error)
3873 26ed57b2 2018-05-19 stsp goto done;
3875 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3876 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3877 26ed57b2 2018-05-19 stsp if (error)
3878 26ed57b2 2018-05-19 stsp goto done;
3880 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3881 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3882 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3883 ea5e7bb5 2018-08-01 stsp goto done;
3885 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3886 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3887 5dc9f4bc 2018-08-04 stsp if (error)
3888 5dc9f4bc 2018-08-04 stsp goto done;
3889 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3891 3dbaef42 2020-11-24 stsp free(label1);
3892 3dbaef42 2020-11-24 stsp free(label2);
3893 c02c541e 2019-03-29 stsp free(repo_path);
3894 a273ac94 2020-02-23 naddy free(cwd);
3896 921be706 2019-06-28 stsp got_repo_close(repo);
3897 a273ac94 2020-02-23 naddy if (worktree)
3898 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3899 26ed57b2 2018-05-19 stsp return error;
3902 4ed7e80c 2018-05-20 stsp __dead static void
3903 9f7d7167 2018-04-29 stsp usage_blame(void)
3906 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3907 9f7d7167 2018-04-29 stsp getprogname());
3911 84451b3e 2018-07-10 stsp struct tog_blame_line {
3912 84451b3e 2018-07-10 stsp int annotated;
3913 84451b3e 2018-07-10 stsp struct got_object_id *id;
3916 4ed7e80c 2018-05-20 stsp static const struct got_error *
3917 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3918 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3919 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3920 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3921 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3923 84451b3e 2018-07-10 stsp const struct got_error *err;
3924 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3925 84451b3e 2018-07-10 stsp char *line;
3926 84451b3e 2018-07-10 stsp size_t len;
3927 84451b3e 2018-07-10 stsp wchar_t *wline;
3928 27a741e5 2019-09-11 stsp int width;
3929 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3930 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3931 ab089a2a 2018-07-12 stsp char *id_str;
3932 11b20872 2019-11-08 stsp struct tog_color *tc;
3934 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3936 ab089a2a 2018-07-12 stsp return err;
3938 84451b3e 2018-07-10 stsp rewind(f);
3939 f7d12f7e 2018-08-01 stsp werase(view->window);
3941 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3942 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3943 ab089a2a 2018-07-12 stsp free(id_str);
3944 ab089a2a 2018-07-12 stsp return err;
3947 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3948 ab089a2a 2018-07-12 stsp free(line);
3949 2550e4c3 2018-07-13 stsp line = NULL;
3951 1cae65b4 2019-09-22 stsp return err;
3952 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3953 a3404814 2018-09-02 stsp wstandout(view->window);
3954 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3956 11b20872 2019-11-08 stsp wattr_on(view->window,
3957 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3958 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3960 11b20872 2019-11-08 stsp wattr_off(view->window,
3961 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3962 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3963 a3404814 2018-09-02 stsp wstandend(view->window);
3964 2550e4c3 2018-07-13 stsp free(wline);
3965 2550e4c3 2018-07-13 stsp wline = NULL;
3966 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3967 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3969 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3970 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3971 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3972 ab089a2a 2018-07-12 stsp free(id_str);
3973 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3975 ab089a2a 2018-07-12 stsp free(id_str);
3976 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3977 3f60a8ef 2018-07-10 stsp free(line);
3978 2550e4c3 2018-07-13 stsp line = NULL;
3980 3f60a8ef 2018-07-10 stsp return err;
3981 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3982 2550e4c3 2018-07-13 stsp free(wline);
3983 2550e4c3 2018-07-13 stsp wline = NULL;
3984 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3985 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3988 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3989 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3990 84451b3e 2018-07-10 stsp if (line == NULL) {
3994 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3995 84451b3e 2018-07-10 stsp free(line);
3999 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
4000 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4002 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
4003 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
4004 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4005 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4006 27a741e5 2019-09-11 stsp !(view->focussed &&
4007 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
4008 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4009 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4010 8d0fe45a 2019-08-12 stsp char *id_str;
4011 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4012 8d0fe45a 2019-08-12 stsp if (err) {
4013 8d0fe45a 2019-08-12 stsp free(line);
4014 8d0fe45a 2019-08-12 stsp return err;
4016 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4018 11b20872 2019-11-08 stsp wattr_on(view->window,
4019 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4020 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4022 11b20872 2019-11-08 stsp wattr_off(view->window,
4023 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4024 8d0fe45a 2019-08-12 stsp free(id_str);
4025 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4027 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4028 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4031 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4032 ee41ec32 2018-07-10 stsp prev_id = NULL;
4035 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
4036 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4037 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4039 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4040 41605754 2020-11-12 stsp width = 9;
4041 41605754 2020-11-12 stsp wline = wcsdup(L"");
4042 41605754 2020-11-12 stsp if (wline == NULL) {
4043 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4044 41605754 2020-11-12 stsp free(line);
4045 41605754 2020-11-12 stsp return err;
4047 41605754 2020-11-12 stsp } else if (*first_displayed_line + nprinted == matched_line &&
4048 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4049 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4050 41605754 2020-11-12 stsp view->window, regmatch);
4051 41605754 2020-11-12 stsp if (err) {
4052 41605754 2020-11-12 stsp free(line);
4053 41605754 2020-11-12 stsp return err;
4055 41605754 2020-11-12 stsp width += 9;
4057 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4058 41605754 2020-11-12 stsp view->ncols - 9, 9);
4059 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4060 41605754 2020-11-12 stsp free(wline);
4061 41605754 2020-11-12 stsp wline = NULL;
4062 41605754 2020-11-12 stsp width += 9;
4065 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4066 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4067 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4068 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
4069 84451b3e 2018-07-10 stsp free(line);
4071 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
4073 1a57306a 2018-09-02 stsp view_vborder(view);
4075 84451b3e 2018-07-10 stsp return NULL;
4078 84451b3e 2018-07-10 stsp static const struct got_error *
4079 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4081 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4082 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4083 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4084 1a76625f 2018-10-22 stsp int errcode;
4086 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4087 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4088 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4090 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4091 1a76625f 2018-10-22 stsp if (errcode)
4092 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4094 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4095 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4096 d68a0a7d 2018-07-10 stsp goto done;
4099 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4100 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4102 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4103 d68a0a7d 2018-07-10 stsp if (line->annotated)
4104 d68a0a7d 2018-07-10 stsp goto done;
4106 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4107 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4108 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4109 84451b3e 2018-07-10 stsp goto done;
4111 84451b3e 2018-07-10 stsp line->annotated = 1;
4113 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4114 1a76625f 2018-10-22 stsp if (errcode)
4115 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4116 84451b3e 2018-07-10 stsp return err;
4119 84451b3e 2018-07-10 stsp static void *
4120 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4122 18430de3 2018-07-10 stsp const struct got_error *err;
4123 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4124 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4125 1a76625f 2018-10-22 stsp int errcode;
4127 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4129 61266923 2020-01-14 stsp return (void *)err;
4131 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4132 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4133 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4134 fc06ba56 2019-08-22 stsp err = NULL;
4136 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4137 1a76625f 2018-10-22 stsp if (errcode)
4138 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4139 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4141 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4142 c9beca56 2018-07-22 stsp ta->repo = NULL;
4143 c9beca56 2018-07-22 stsp *ta->complete = 1;
4145 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4146 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4147 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4149 18430de3 2018-07-10 stsp return (void *)err;
4152 245d91c1 2018-07-12 stsp static struct got_object_id *
4153 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4154 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4156 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4158 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4159 8d0fe45a 2019-08-12 stsp return NULL;
4161 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4162 245d91c1 2018-07-12 stsp if (!line->annotated)
4163 245d91c1 2018-07-12 stsp return NULL;
4165 245d91c1 2018-07-12 stsp return line->id;
4168 b880a918 2018-07-10 stsp static const struct got_error *
4169 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4171 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4174 245d91c1 2018-07-12 stsp if (blame->thread) {
4175 1a76625f 2018-10-22 stsp int errcode;
4176 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4177 1a76625f 2018-10-22 stsp if (errcode)
4178 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4179 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4180 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4181 1a76625f 2018-10-22 stsp if (errcode)
4182 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4183 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4184 1a76625f 2018-10-22 stsp if (errcode)
4185 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4186 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4187 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4188 245d91c1 2018-07-12 stsp err = NULL;
4189 245d91c1 2018-07-12 stsp blame->thread = NULL;
4191 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4192 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4193 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4195 245d91c1 2018-07-12 stsp if (blame->f) {
4196 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4197 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4198 245d91c1 2018-07-12 stsp blame->f = NULL;
4200 57670559 2018-12-23 stsp if (blame->lines) {
4201 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4202 57670559 2018-12-23 stsp free(blame->lines[i].id);
4203 57670559 2018-12-23 stsp free(blame->lines);
4204 57670559 2018-12-23 stsp blame->lines = NULL;
4206 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4207 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4209 245d91c1 2018-07-12 stsp return err;
4212 245d91c1 2018-07-12 stsp static const struct got_error *
4213 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4215 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4216 fc06ba56 2019-08-22 stsp int *done = arg;
4217 fc06ba56 2019-08-22 stsp int errcode;
4219 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4220 fc06ba56 2019-08-22 stsp if (errcode)
4221 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4222 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4224 fc06ba56 2019-08-22 stsp if (*done)
4225 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4227 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4228 fc06ba56 2019-08-22 stsp if (errcode)
4229 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4230 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4232 fc06ba56 2019-08-22 stsp return err;
4235 fc06ba56 2019-08-22 stsp static const struct got_error *
4236 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4237 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
4238 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
4239 245d91c1 2018-07-12 stsp struct got_repository *repo)
4241 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4242 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4243 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4244 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4245 15a94983 2018-12-23 stsp int obj_type;
4247 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4249 15a94983 2018-12-23 stsp return err;
4251 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
4253 84451b3e 2018-07-10 stsp goto done;
4255 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4256 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4257 84451b3e 2018-07-10 stsp goto done;
4260 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4262 a70480e0 2018-06-23 stsp goto done;
4263 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4264 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4265 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4266 84451b3e 2018-07-10 stsp goto done;
4268 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4269 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4270 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4271 84451b3e 2018-07-10 stsp goto done;
4273 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4274 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4275 b02560ec 2019-08-19 stsp blame->nlines--;
4277 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4278 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4279 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4280 84451b3e 2018-07-10 stsp goto done;
4283 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4285 bd24772e 2018-07-11 stsp goto done;
4287 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4288 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4289 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4290 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
4291 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4292 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4293 245d91c1 2018-07-12 stsp goto done;
4295 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
4297 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
4298 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4299 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4300 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
4301 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4302 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
4303 245d91c1 2018-07-12 stsp *blame_complete = 0;
4307 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4308 27d434c2 2018-09-15 stsp free(obj_id);
4310 245d91c1 2018-07-12 stsp stop_blame(blame);
4311 245d91c1 2018-07-12 stsp return err;
4314 245d91c1 2018-07-12 stsp static const struct got_error *
4315 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4316 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4318 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4319 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4321 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4323 c4843652 2019-08-12 stsp s->path = strdup(path);
4324 c4843652 2019-08-12 stsp if (s->path == NULL)
4325 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4327 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4328 c4843652 2019-08-12 stsp if (err) {
4329 c4843652 2019-08-12 stsp free(s->path);
4330 7cbe629d 2018-08-04 stsp return err;
4333 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4334 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4335 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4336 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4337 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4338 fb2756b9 2018-08-04 stsp s->repo = repo;
4339 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4340 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4342 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4343 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4344 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4345 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4347 11b20872 2019-11-08 stsp return err;
4350 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4351 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4352 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4353 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4354 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4356 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
4357 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
4358 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
4359 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
4362 e5a0f69f 2018-08-18 stsp static const struct got_error *
4363 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4365 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4366 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4368 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4369 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4371 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4372 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4373 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4374 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4375 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4378 e5a0f69f 2018-08-18 stsp free(s->path);
4379 11b20872 2019-11-08 stsp free_colors(&s->colors);
4381 e5a0f69f 2018-08-18 stsp return err;
4384 7cbe629d 2018-08-04 stsp static const struct got_error *
4385 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4387 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4389 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4390 6c4c42e0 2019-06-24 stsp return NULL;
4393 6c4c42e0 2019-06-24 stsp static const struct got_error *
4394 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4396 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4397 6c4c42e0 2019-06-24 stsp int lineno;
4399 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4400 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 6c4c42e0 2019-06-24 stsp return NULL;
4404 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4405 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4406 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4408 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4410 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4411 6c4c42e0 2019-06-24 stsp lineno = 1;
4413 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4416 6c4c42e0 2019-06-24 stsp while (1) {
4417 6c4c42e0 2019-06-24 stsp char *line = NULL;
4418 6c4c42e0 2019-06-24 stsp off_t offset;
4419 6c4c42e0 2019-06-24 stsp size_t len;
4421 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4422 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4423 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4424 6c4c42e0 2019-06-24 stsp free(line);
4428 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4429 6c4c42e0 2019-06-24 stsp lineno = 1;
4431 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4434 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4435 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4436 6c4c42e0 2019-06-24 stsp free(line);
4437 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4439 6c4c42e0 2019-06-24 stsp free(line);
4440 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4441 41605754 2020-11-12 stsp if (line &&
4442 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4443 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4444 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4445 6c4c42e0 2019-06-24 stsp free(line);
4448 6c4c42e0 2019-06-24 stsp free(line);
4449 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4455 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4456 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4457 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4460 6c4c42e0 2019-06-24 stsp return NULL;
4463 6c4c42e0 2019-06-24 stsp static const struct got_error *
4464 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4466 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4467 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4468 2b380cc8 2018-10-24 stsp int errcode;
4470 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4471 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4472 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4473 2b380cc8 2018-10-24 stsp if (errcode)
4474 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4476 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4479 51fe7530 2019-08-19 stsp if (s->blame_complete)
4480 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4482 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4483 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4484 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4485 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4486 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
4488 669b5ffa 2018-10-07 stsp view_vborder(view);
4489 e5a0f69f 2018-08-18 stsp return err;
4492 e5a0f69f 2018-08-18 stsp static const struct got_error *
4493 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4494 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4496 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4497 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4498 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4499 669b5ffa 2018-10-07 stsp int begin_x = 0;
4501 e5a0f69f 2018-08-18 stsp switch (ch) {
4503 1e37a5c2 2019-05-12 jcs s->done = 1;
4506 1e37a5c2 2019-05-12 jcs case KEY_UP:
4507 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4508 1e37a5c2 2019-05-12 jcs s->selected_line--;
4509 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4510 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4511 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4513 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4514 ea025d1d 2020-02-22 naddy case CTRL('b'):
4515 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4516 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4519 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4520 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4521 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4523 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4526 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4527 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4528 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4529 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4530 1e37a5c2 2019-05-12 jcs s->selected_line++;
4531 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4532 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4533 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4536 1e37a5c2 2019-05-12 jcs case 'p': {
4537 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4538 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4539 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4540 1e37a5c2 2019-05-12 jcs if (id == NULL)
4542 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4543 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4544 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4545 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4546 1e37a5c2 2019-05-12 jcs int obj_type;
4547 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4548 1e37a5c2 2019-05-12 jcs s->repo, id);
4551 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4552 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4553 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4554 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4557 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4558 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4559 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4560 e5a0f69f 2018-08-18 stsp if (err) {
4561 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4562 1e37a5c2 2019-05-12 jcs err = NULL;
4563 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4566 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4568 1e37a5c2 2019-05-12 jcs free(blob_id);
4569 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4570 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4571 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4574 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4576 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4578 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4579 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4581 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4586 1e37a5c2 2019-05-12 jcs s->done = 1;
4587 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4588 1e37a5c2 2019-05-12 jcs s->done = 0;
4589 1e37a5c2 2019-05-12 jcs if (thread_err)
4591 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4592 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4593 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4594 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4595 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4596 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4601 1e37a5c2 2019-05-12 jcs case 'B': {
4602 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4603 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4604 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4606 1e37a5c2 2019-05-12 jcs s->done = 1;
4607 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4608 1e37a5c2 2019-05-12 jcs s->done = 0;
4609 1e37a5c2 2019-05-12 jcs if (thread_err)
4611 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4612 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4613 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4614 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4615 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4616 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4617 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4618 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4623 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4624 1e37a5c2 2019-05-12 jcs case '\r': {
4625 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4626 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4627 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4628 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4629 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4630 1e37a5c2 2019-05-12 jcs if (id == NULL)
4632 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4635 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4636 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4637 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4638 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4639 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4640 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4641 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4645 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4646 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4647 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4649 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4652 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4653 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4656 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4658 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4661 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4662 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4664 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4669 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4670 ea025d1d 2020-02-22 naddy case CTRL('f'):
4672 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4673 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4674 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4677 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4678 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4679 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4680 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4683 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4684 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4685 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4686 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4688 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4689 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4690 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4692 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4693 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4694 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4695 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4701 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4704 a70480e0 2018-06-23 stsp static const struct got_error *
4705 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4707 a70480e0 2018-06-23 stsp const struct got_error *error;
4708 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4709 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4710 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4711 0587e10c 2020-07-23 stsp char *link_target = NULL;
4712 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4713 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4715 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4717 a70480e0 2018-06-23 stsp #ifndef PROFILE
4718 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4719 8e94dd5b 2019-01-04 stsp NULL) == -1)
4720 a70480e0 2018-06-23 stsp err(1, "pledge");
4723 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4724 a70480e0 2018-06-23 stsp switch (ch) {
4726 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4729 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4730 69069811 2018-08-02 stsp if (repo_path == NULL)
4731 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4735 17020d27 2019-03-07 stsp usage_blame();
4736 a70480e0 2018-06-23 stsp /* NOTREACHED */
4740 a70480e0 2018-06-23 stsp argc -= optind;
4741 a70480e0 2018-06-23 stsp argv += optind;
4743 f135c941 2020-02-20 stsp if (argc != 1)
4744 a70480e0 2018-06-23 stsp usage_blame();
4746 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4747 6962eb72 2020-02-20 stsp if (cwd == NULL)
4748 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4750 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4751 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4752 f135c941 2020-02-20 stsp goto done;
4754 69069811 2018-08-02 stsp if (repo_path == NULL) {
4755 f135c941 2020-02-20 stsp if (worktree)
4756 eb41ed75 2019-02-05 stsp repo_path =
4757 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4759 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4761 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4762 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4763 f135c941 2020-02-20 stsp goto done;
4766 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4767 c02c541e 2019-03-29 stsp if (error != NULL)
4768 8e94dd5b 2019-01-04 stsp goto done;
4770 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4771 f135c941 2020-02-20 stsp worktree);
4772 c02c541e 2019-03-29 stsp if (error)
4773 92205607 2019-01-04 stsp goto done;
4775 f135c941 2020-02-20 stsp init_curses();
4777 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4778 eb41ed75 2019-02-05 stsp if (error)
4779 69069811 2018-08-02 stsp goto done;
4781 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4782 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4783 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4784 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4785 a70480e0 2018-06-23 stsp if (error != NULL)
4786 66b4983c 2018-06-23 stsp goto done;
4787 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4788 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4790 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4791 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4793 a19e88aa 2018-06-23 stsp if (error != NULL)
4794 8b473291 2019-02-21 stsp goto done;
4796 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4797 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4798 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4799 e1cd8fed 2018-08-01 stsp goto done;
4802 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4803 0587e10c 2020-07-23 stsp commit_id, repo);
4804 7cbe629d 2018-08-04 stsp if (error)
4805 7cbe629d 2018-08-04 stsp goto done;
4807 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4808 78756c87 2020-11-24 stsp commit_id, repo);
4809 0587e10c 2020-07-23 stsp if (error)
4810 0587e10c 2020-07-23 stsp goto done;
4811 12314ad4 2019-08-31 stsp if (worktree) {
4812 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4813 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4814 12314ad4 2019-08-31 stsp worktree = NULL;
4816 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4818 69069811 2018-08-02 stsp free(repo_path);
4819 f135c941 2020-02-20 stsp free(in_repo_path);
4820 0587e10c 2020-07-23 stsp free(link_target);
4821 69069811 2018-08-02 stsp free(cwd);
4822 a70480e0 2018-06-23 stsp free(commit_id);
4823 eb41ed75 2019-02-05 stsp if (worktree)
4824 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4826 a70480e0 2018-06-23 stsp got_repo_close(repo);
4827 a70480e0 2018-06-23 stsp return error;
4830 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4831 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4832 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4833 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4834 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4835 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4836 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4837 0d6c6ee3 2020-05-20 stsp int isroot, struct tog_colors *colors, struct got_repository *repo)
4839 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4840 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4841 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4842 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4843 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4845 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4847 f7d12f7e 2018-08-01 stsp werase(view->window);
4849 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4850 ffd1d5e5 2018-06-23 stsp return NULL;
4852 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4854 ffd1d5e5 2018-06-23 stsp return err;
4855 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4856 a3404814 2018-09-02 stsp wstandout(view->window);
4857 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4859 11b20872 2019-11-08 stsp wattr_on(view->window,
4860 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4861 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4863 11b20872 2019-11-08 stsp wattr_off(view->window,
4864 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4865 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4866 a3404814 2018-09-02 stsp wstandend(view->window);
4867 2550e4c3 2018-07-13 stsp free(wline);
4868 2550e4c3 2018-07-13 stsp wline = NULL;
4869 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4870 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4871 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4872 ffd1d5e5 2018-06-23 stsp return NULL;
4873 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4875 ce52c690 2018-06-23 stsp return err;
4876 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4877 2550e4c3 2018-07-13 stsp free(wline);
4878 2550e4c3 2018-07-13 stsp wline = NULL;
4879 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4880 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4881 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4882 ffd1d5e5 2018-06-23 stsp return NULL;
4883 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4884 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4885 a1eca9bb 2018-06-23 stsp return NULL;
4887 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4888 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4889 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4890 0cf4efb1 2018-09-29 stsp if (view->focussed)
4891 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4892 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4894 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4895 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4896 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4897 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4898 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4899 ffd1d5e5 2018-06-23 stsp return NULL;
4903 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4906 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4907 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4908 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4909 848d6979 2019-08-12 stsp const char *modestr = "";
4910 56e0773d 2019-11-28 stsp mode_t mode;
4912 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4913 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4915 1d13200f 2018-07-12 stsp if (show_ids) {
4916 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4917 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4919 638f9024 2019-05-13 stsp return got_error_from_errno(
4920 230a42bd 2019-05-11 jcs "got_object_id_str");
4922 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4923 63c5ca5d 2019-08-24 stsp modestr = "$";
4924 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4927 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4928 0d6c6ee3 2020-05-20 stsp te, repo);
4929 0d6c6ee3 2020-05-20 stsp if (err) {
4930 0d6c6ee3 2020-05-20 stsp free(id_str);
4931 0d6c6ee3 2020-05-20 stsp return err;
4933 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4934 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4935 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4937 848d6979 2019-08-12 stsp modestr = "@";
4939 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4940 848d6979 2019-08-12 stsp modestr = "/";
4941 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4942 848d6979 2019-08-12 stsp modestr = "*";
4943 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4944 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4945 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4946 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4947 1d13200f 2018-07-12 stsp free(id_str);
4948 0d6c6ee3 2020-05-20 stsp free(link_target);
4949 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4951 1d13200f 2018-07-12 stsp free(id_str);
4952 0d6c6ee3 2020-05-20 stsp free(link_target);
4953 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4954 ffd1d5e5 2018-06-23 stsp if (err) {
4955 ffd1d5e5 2018-06-23 stsp free(line);
4958 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4959 0cf4efb1 2018-09-29 stsp if (view->focussed)
4960 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4961 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4963 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4965 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4966 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4967 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4969 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4970 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4971 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4972 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4973 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4974 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4975 ffd1d5e5 2018-06-23 stsp free(line);
4976 2550e4c3 2018-07-13 stsp free(wline);
4977 2550e4c3 2018-07-13 stsp wline = NULL;
4979 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4980 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4981 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4985 ffd1d5e5 2018-06-23 stsp return err;
4988 ffd1d5e5 2018-06-23 stsp static void
4989 694d3271 2020-12-01 naddy tree_scroll_up(struct tog_view *view, int maxscroll)
4991 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4992 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4993 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4994 fa86c4bf 2020-11-29 stsp int i = 0;
4996 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4999 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5000 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5001 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5002 fa86c4bf 2020-11-29 stsp if (!isroot)
5003 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5006 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5007 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5011 fa86c4bf 2020-11-29 stsp static void
5012 694d3271 2020-12-01 naddy tree_scroll_down(struct tog_view *view, int maxscroll)
5014 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5015 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5016 ffd1d5e5 2018-06-23 stsp int n = 0;
5018 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5019 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5020 694d3271 2020-12-01 naddy s->first_displayed_entry);
5022 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5024 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5025 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5026 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5027 768394f3 2019-01-24 stsp if (last) {
5028 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5029 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5034 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5035 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5036 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5038 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5039 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5040 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5042 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5043 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5044 56e0773d 2019-11-28 stsp + 1 /* slash */;
5046 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5048 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5049 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5050 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5052 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5053 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5054 d9765a41 2018-06-23 stsp while (pt) {
5055 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5056 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5057 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5058 cb2ebc8a 2018-06-23 stsp goto done;
5060 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5061 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5062 cb2ebc8a 2018-06-23 stsp goto done;
5064 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5067 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5068 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5069 ce52c690 2018-06-23 stsp goto done;
5073 ce52c690 2018-06-23 stsp if (err) {
5074 ce52c690 2018-06-23 stsp free(*path);
5075 ce52c690 2018-06-23 stsp *path = NULL;
5077 ce52c690 2018-06-23 stsp return err;
5080 ce52c690 2018-06-23 stsp static const struct got_error *
5081 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5082 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5083 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5085 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5086 ce52c690 2018-06-23 stsp char *path;
5087 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5089 a0de39f3 2019-08-09 stsp *new_view = NULL;
5091 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5093 ce52c690 2018-06-23 stsp return err;
5095 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5096 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5097 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5098 83ce39e3 2019-08-12 stsp goto done;
5101 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5102 e5a0f69f 2018-08-18 stsp if (err) {
5103 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5104 fc06ba56 2019-08-22 stsp err = NULL;
5105 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5107 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5109 83ce39e3 2019-08-12 stsp free(path);
5110 69efd4c4 2018-07-18 stsp return err;
5113 69efd4c4 2018-07-18 stsp static const struct got_error *
5114 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5115 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5116 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5118 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5119 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5120 69efd4c4 2018-07-18 stsp char *path;
5122 a0de39f3 2019-08-09 stsp *new_view = NULL;
5124 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5125 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5126 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5128 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5130 69efd4c4 2018-07-18 stsp return err;
5132 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5134 e5a0f69f 2018-08-18 stsp view_close(log_view);
5136 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5137 cb2ebc8a 2018-06-23 stsp free(path);
5138 cb2ebc8a 2018-06-23 stsp return err;
5141 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5142 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5143 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5145 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5146 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5147 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5149 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5151 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5152 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5153 ffd1d5e5 2018-06-23 stsp goto done;
5155 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5156 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5157 ffd1d5e5 2018-06-23 stsp goto done;
5160 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5161 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5162 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5163 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5164 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5165 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5166 6484ec90 2018-09-29 stsp goto done;
5168 fb2756b9 2018-08-04 stsp s->repo = repo;
5170 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5172 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5173 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5174 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5175 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5177 c0b01bdb 2019-11-08 stsp goto done;
5178 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5179 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5180 c0b01bdb 2019-11-08 stsp if (err) {
5181 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5182 c0b01bdb 2019-11-08 stsp goto done;
5184 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5185 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5186 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5187 c0b01bdb 2019-11-08 stsp if (err) {
5188 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5189 c0b01bdb 2019-11-08 stsp goto done;
5192 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5193 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5194 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5195 11b20872 2019-11-08 stsp if (err) {
5196 11b20872 2019-11-08 stsp free_colors(&s->colors);
5197 11b20872 2019-11-08 stsp goto done;
5200 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5201 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5202 c0b01bdb 2019-11-08 stsp if (err) {
5203 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5204 c0b01bdb 2019-11-08 stsp goto done;
5208 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5209 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5210 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5211 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5212 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5214 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5215 6484ec90 2018-09-29 stsp if (err) {
5216 fb2756b9 2018-08-04 stsp free(s->tree_label);
5217 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5219 ad80ab7b 2018-08-04 stsp return err;
5222 e5a0f69f 2018-08-18 stsp static const struct got_error *
5223 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5225 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5227 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5228 fb2756b9 2018-08-04 stsp free(s->tree_label);
5229 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5230 6484ec90 2018-09-29 stsp free(s->commit_id);
5231 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5232 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5233 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5234 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5235 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5236 ad80ab7b 2018-08-04 stsp free(parent);
5239 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5240 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5241 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5242 7c32bd05 2019-06-22 stsp return NULL;
5245 7c32bd05 2019-06-22 stsp static const struct got_error *
5246 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5248 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5250 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5251 7c32bd05 2019-06-22 stsp return NULL;
5254 7c32bd05 2019-06-22 stsp static int
5255 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5257 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5259 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5263 7c32bd05 2019-06-22 stsp static const struct got_error *
5264 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5266 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5267 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5269 7c32bd05 2019-06-22 stsp if (!view->searching) {
5270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 7c32bd05 2019-06-22 stsp return NULL;
5274 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5275 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5276 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5277 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5278 56e0773d 2019-11-28 stsp s->selected_entry);
5280 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5282 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5283 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5285 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5286 56e0773d 2019-11-28 stsp s->selected_entry);
5289 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5290 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5292 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5295 7c32bd05 2019-06-22 stsp while (1) {
5296 56e0773d 2019-11-28 stsp if (te == NULL) {
5297 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5298 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5299 ac66afb8 2019-06-24 stsp return NULL;
5301 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5302 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5304 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5307 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5308 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 56e0773d 2019-11-28 stsp s->matched_entry = te;
5313 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5314 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5316 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5319 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5320 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5321 7c32bd05 2019-06-22 stsp s->selected = 0;
5324 e5a0f69f 2018-08-18 stsp return NULL;
5327 ad80ab7b 2018-08-04 stsp static const struct got_error *
5328 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5330 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5331 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5332 e5a0f69f 2018-08-18 stsp char *parent_path;
5334 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5336 e5a0f69f 2018-08-18 stsp return err;
5338 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5339 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5340 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5341 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5342 0d6c6ee3 2020-05-20 stsp &s->colors, s->repo);
5343 e5a0f69f 2018-08-18 stsp free(parent_path);
5345 669b5ffa 2018-10-07 stsp view_vborder(view);
5346 e5a0f69f 2018-08-18 stsp return err;
5349 e5a0f69f 2018-08-18 stsp static const struct got_error *
5350 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5351 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5353 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5354 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5355 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5356 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5358 e5a0f69f 2018-08-18 stsp switch (ch) {
5360 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5363 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5365 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5366 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5367 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5368 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5369 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5370 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5372 1e37a5c2 2019-05-12 jcs return err;
5373 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5375 1e37a5c2 2019-05-12 jcs view_close(log_view);
5378 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5379 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5381 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5384 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5385 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5386 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5387 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5388 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5389 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5390 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5391 152c1c93 2020-11-29 stsp if (err) {
5392 152c1c93 2020-11-29 stsp view_close(ref_view);
5393 152c1c93 2020-11-29 stsp return err;
5395 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5396 152c1c93 2020-11-29 stsp err = view_close_child(view);
5398 152c1c93 2020-11-29 stsp return err;
5399 152c1c93 2020-11-29 stsp err = view_set_child(view, ref_view);
5400 152c1c93 2020-11-29 stsp if (err) {
5401 152c1c93 2020-11-29 stsp view_close(ref_view);
5404 152c1c93 2020-11-29 stsp *focus_view = ref_view;
5405 152c1c93 2020-11-29 stsp view->child_focussed = 1;
5407 152c1c93 2020-11-29 stsp *new_view = ref_view;
5410 1e37a5c2 2019-05-12 jcs case KEY_UP:
5411 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5412 1e37a5c2 2019-05-12 jcs s->selected--;
5415 694d3271 2020-12-01 naddy tree_scroll_up(view, 1);
5417 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5418 ea025d1d 2020-02-22 naddy case CTRL('b'):
5419 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5420 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5421 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5422 fa86c4bf 2020-11-29 stsp s->selected = 0;
5424 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5425 fa86c4bf 2020-11-29 stsp s->selected = 0;
5427 694d3271 2020-12-01 naddy tree_scroll_up(view, MAX(0, view->nlines - 3));
5430 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5431 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5432 1e37a5c2 2019-05-12 jcs s->selected++;
5435 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5437 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5439 694d3271 2020-12-01 naddy tree_scroll_down(view, 1);
5441 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5442 ea025d1d 2020-02-22 naddy case CTRL('f'):
5443 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5445 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5446 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5447 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5450 694d3271 2020-12-01 naddy tree_scroll_down(view, view->nlines - 3);
5452 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5454 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5455 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5456 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5457 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5458 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5460 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5461 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5463 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5464 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5465 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5466 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5467 1e37a5c2 2019-05-12 jcs s->selected_entry =
5468 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5469 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5470 1e37a5c2 2019-05-12 jcs free(parent);
5471 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5472 56e0773d 2019-11-28 stsp s->selected_entry))) {
5473 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5474 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5475 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5478 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5479 941e9f74 2019-05-21 stsp if (err) {
5480 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5483 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5484 56e0773d 2019-11-28 stsp s->selected_entry))) {
5485 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5486 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5487 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5489 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5490 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5491 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5494 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5495 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5497 669b5ffa 2018-10-07 stsp return err;
5498 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5499 669b5ffa 2018-10-07 stsp if (err) {
5500 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5503 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5504 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5506 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5509 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5510 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5511 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5517 ffd1d5e5 2018-06-23 stsp return err;
5520 ffd1d5e5 2018-06-23 stsp __dead static void
5521 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5524 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5525 ffd1d5e5 2018-06-23 stsp getprogname());
5529 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5530 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5532 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5533 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5534 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5535 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5536 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5537 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5538 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5539 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5541 5221c383 2018-08-01 stsp struct tog_view *view;
5543 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5544 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5545 d188b9a6 2019-01-04 stsp NULL) == -1)
5546 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5549 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5550 ffd1d5e5 2018-06-23 stsp switch (ch) {
5552 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5555 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5556 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5557 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5561 e99e2d15 2020-11-24 naddy usage_tree();
5562 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5566 ffd1d5e5 2018-06-23 stsp argc -= optind;
5567 ffd1d5e5 2018-06-23 stsp argv += optind;
5569 55cccc34 2020-02-20 stsp if (argc > 1)
5570 e99e2d15 2020-11-24 naddy usage_tree();
5572 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5573 55cccc34 2020-02-20 stsp if (cwd == NULL)
5574 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5576 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5577 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5578 55cccc34 2020-02-20 stsp goto done;
5580 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5581 55cccc34 2020-02-20 stsp if (worktree)
5582 52185f70 2019-02-05 stsp repo_path =
5583 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5585 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5587 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5588 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5589 55cccc34 2020-02-20 stsp goto done;
5592 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5593 c02c541e 2019-03-29 stsp if (error != NULL)
5594 52185f70 2019-02-05 stsp goto done;
5596 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5597 55cccc34 2020-02-20 stsp repo, worktree);
5598 55cccc34 2020-02-20 stsp if (error)
5599 55cccc34 2020-02-20 stsp goto done;
5601 55cccc34 2020-02-20 stsp init_curses();
5603 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5604 c02c541e 2019-03-29 stsp if (error)
5605 52185f70 2019-02-05 stsp goto done;
5607 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5608 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5609 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5610 55cccc34 2020-02-20 stsp if (error)
5611 ffd1d5e5 2018-06-23 stsp goto done;
5613 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5614 55cccc34 2020-02-20 stsp if (error)
5615 ffd1d5e5 2018-06-23 stsp goto done;
5617 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5618 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5619 55cccc34 2020-02-20 stsp if (error)
5620 8b473291 2019-02-21 stsp goto done;
5622 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5623 5221c383 2018-08-01 stsp if (view == NULL) {
5624 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5625 5221c383 2018-08-01 stsp goto done;
5627 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5628 ad80ab7b 2018-08-04 stsp if (error)
5629 ad80ab7b 2018-08-04 stsp goto done;
5630 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5631 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5632 55cccc34 2020-02-20 stsp in_repo_path, repo);
5633 55cccc34 2020-02-20 stsp if (error)
5634 55cccc34 2020-02-20 stsp goto done;
5637 55cccc34 2020-02-20 stsp if (worktree) {
5638 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5639 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5640 55cccc34 2020-02-20 stsp worktree = NULL;
5642 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5644 52185f70 2019-02-05 stsp free(repo_path);
5645 e4a0e26d 2020-02-20 stsp free(cwd);
5646 ffd1d5e5 2018-06-23 stsp free(commit_id);
5647 ffd1d5e5 2018-06-23 stsp if (commit)
5648 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5650 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5652 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5653 ffd1d5e5 2018-06-23 stsp return error;
5656 6458efa5 2020-11-24 stsp static const struct got_error *
5657 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5659 6458efa5 2020-11-24 stsp const struct got_error *err;
5660 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5661 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5663 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5664 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5666 6458efa5 2020-11-24 stsp return err;
5668 6458efa5 2020-11-24 stsp s->nrefs = 0;
5669 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5670 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5673 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5674 6458efa5 2020-11-24 stsp if (re == NULL)
5675 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5677 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5678 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5679 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5682 6458efa5 2020-11-24 stsp return NULL;
5686 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5688 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5690 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5691 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5692 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5695 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5698 6458efa5 2020-11-24 stsp static const struct got_error *
5699 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5701 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5702 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5704 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5705 6458efa5 2020-11-24 stsp s->repo = repo;
5707 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5708 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5709 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5711 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5713 6458efa5 2020-11-24 stsp return err;
5715 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5717 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5718 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5719 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5720 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5722 6458efa5 2020-11-24 stsp goto done;
5724 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5725 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5726 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5728 6458efa5 2020-11-24 stsp goto done;
5730 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5731 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5732 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5734 6458efa5 2020-11-24 stsp goto done;
5737 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5738 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5739 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5740 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5741 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5744 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5745 6458efa5 2020-11-24 stsp return err;
5748 6458efa5 2020-11-24 stsp static const struct got_error *
5749 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5751 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5753 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5754 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5756 6458efa5 2020-11-24 stsp return NULL;
5759 6458efa5 2020-11-24 stsp static const struct got_error *
5760 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5761 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5763 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5764 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5765 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5766 6458efa5 2020-11-24 stsp int obj_type;
5768 c42c9805 2020-11-24 stsp *commit_id = NULL;
5770 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5772 6458efa5 2020-11-24 stsp return err;
5774 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5776 6458efa5 2020-11-24 stsp goto done;
5778 6458efa5 2020-11-24 stsp switch (obj_type) {
5779 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5780 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5782 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5783 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5785 6458efa5 2020-11-24 stsp goto done;
5786 c42c9805 2020-11-24 stsp free(obj_id);
5787 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5788 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5790 6458efa5 2020-11-24 stsp goto done;
5791 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5792 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5793 c42c9805 2020-11-24 stsp goto done;
5795 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5796 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5797 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5798 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5799 c42c9805 2020-11-24 stsp goto done;
5803 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5809 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5810 c42c9805 2020-11-24 stsp if (err) {
5811 c42c9805 2020-11-24 stsp free(*commit_id);
5812 c42c9805 2020-11-24 stsp *commit_id = NULL;
5814 c42c9805 2020-11-24 stsp return err;
5817 c42c9805 2020-11-24 stsp static const struct got_error *
5818 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5819 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5821 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5822 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5823 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5825 c42c9805 2020-11-24 stsp *new_view = NULL;
5827 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5828 c42c9805 2020-11-24 stsp if (err) {
5829 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5830 c42c9805 2020-11-24 stsp return err;
5832 c42c9805 2020-11-24 stsp return NULL;
5835 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5836 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5837 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5838 6458efa5 2020-11-24 stsp goto done;
5841 6458efa5 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5844 6458efa5 2020-11-24 stsp view_close(log_view);
5846 6458efa5 2020-11-24 stsp *new_view = log_view;
5847 c42c9805 2020-11-24 stsp free(commit_id);
5848 6458efa5 2020-11-24 stsp return err;
5851 ce5b7c56 2019-07-09 stsp static void
5852 694d3271 2020-12-01 naddy ref_scroll_up(struct tog_view *view, int maxscroll)
5854 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5855 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5856 34ba6917 2020-11-30 stsp int i = 0;
5858 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5861 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5862 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5863 34ba6917 2020-11-30 stsp if (re == NULL)
5865 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5866 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5870 34ba6917 2020-11-30 stsp static void
5871 694d3271 2020-12-01 naddy ref_scroll_down(struct tog_view *view, int maxscroll)
5873 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5874 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5875 6458efa5 2020-11-24 stsp int n = 0;
5877 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5878 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5880 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5882 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5883 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5884 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5885 6458efa5 2020-11-24 stsp if (last) {
5886 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5887 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5892 6458efa5 2020-11-24 stsp static const struct got_error *
5893 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5895 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5897 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5898 6458efa5 2020-11-24 stsp return NULL;
5901 6458efa5 2020-11-24 stsp static int
5902 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5904 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5906 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5910 6458efa5 2020-11-24 stsp static const struct got_error *
5911 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5913 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5914 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5916 6458efa5 2020-11-24 stsp if (!view->searching) {
5917 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5918 6458efa5 2020-11-24 stsp return NULL;
5921 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5922 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5923 6458efa5 2020-11-24 stsp if (s->selected_entry)
5924 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5926 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5927 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5929 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5930 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5932 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5933 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5936 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5937 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5939 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5942 6458efa5 2020-11-24 stsp while (1) {
5943 6458efa5 2020-11-24 stsp if (re == NULL) {
5944 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5945 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5946 6458efa5 2020-11-24 stsp return NULL;
5948 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5949 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5951 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5954 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5955 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5956 6458efa5 2020-11-24 stsp s->matched_entry = re;
5960 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5961 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5963 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5966 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5967 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5968 6458efa5 2020-11-24 stsp s->selected = 0;
5971 6458efa5 2020-11-24 stsp return NULL;
5974 6458efa5 2020-11-24 stsp static const struct got_error *
5975 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5977 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5978 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5979 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5980 6458efa5 2020-11-24 stsp char *line = NULL;
5981 6458efa5 2020-11-24 stsp wchar_t *wline;
5982 6458efa5 2020-11-24 stsp struct tog_color *tc;
5983 6458efa5 2020-11-24 stsp int width, n;
5984 6458efa5 2020-11-24 stsp int limit = view->nlines;
5986 6458efa5 2020-11-24 stsp werase(view->window);
5988 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5990 6458efa5 2020-11-24 stsp if (limit == 0)
5991 6458efa5 2020-11-24 stsp return NULL;
5993 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5995 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5996 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5997 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5999 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6000 6458efa5 2020-11-24 stsp if (err) {
6001 6458efa5 2020-11-24 stsp free(line);
6002 6458efa5 2020-11-24 stsp return err;
6004 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6005 6458efa5 2020-11-24 stsp wstandout(view->window);
6006 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6007 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6008 6458efa5 2020-11-24 stsp wstandend(view->window);
6009 6458efa5 2020-11-24 stsp free(wline);
6010 6458efa5 2020-11-24 stsp wline = NULL;
6011 6458efa5 2020-11-24 stsp free(line);
6012 6458efa5 2020-11-24 stsp line = NULL;
6013 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6014 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6015 6458efa5 2020-11-24 stsp if (--limit <= 0)
6016 6458efa5 2020-11-24 stsp return NULL;
6019 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6020 6458efa5 2020-11-24 stsp char *line = NULL;
6022 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6023 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6024 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6025 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6026 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6027 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6028 6458efa5 2020-11-24 stsp struct got_object_id *id;
6029 6458efa5 2020-11-24 stsp char *id_str;
6030 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6032 6458efa5 2020-11-24 stsp return err;
6033 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6034 6458efa5 2020-11-24 stsp if (err) {
6036 6458efa5 2020-11-24 stsp return err;
6038 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6039 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6040 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6042 6458efa5 2020-11-24 stsp free(id_str);
6043 6458efa5 2020-11-24 stsp return err;
6046 6458efa5 2020-11-24 stsp free(id_str);
6048 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6049 6458efa5 2020-11-24 stsp if (line == NULL)
6050 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6053 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6054 6458efa5 2020-11-24 stsp if (err) {
6055 6458efa5 2020-11-24 stsp free(line);
6056 6458efa5 2020-11-24 stsp return err;
6058 6458efa5 2020-11-24 stsp if (n == s->selected) {
6059 6458efa5 2020-11-24 stsp if (view->focussed)
6060 6458efa5 2020-11-24 stsp wstandout(view->window);
6061 6458efa5 2020-11-24 stsp s->selected_entry = re;
6063 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6065 6458efa5 2020-11-24 stsp wattr_on(view->window,
6066 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6067 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6069 6458efa5 2020-11-24 stsp wattr_off(view->window,
6070 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6071 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6072 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6073 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6074 6458efa5 2020-11-24 stsp wstandend(view->window);
6075 6458efa5 2020-11-24 stsp free(line);
6076 6458efa5 2020-11-24 stsp free(wline);
6077 6458efa5 2020-11-24 stsp wline = NULL;
6079 6458efa5 2020-11-24 stsp s->ndisplayed++;
6080 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6083 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6086 6458efa5 2020-11-24 stsp view_vborder(view);
6087 6458efa5 2020-11-24 stsp return err;
6090 6458efa5 2020-11-24 stsp static const struct got_error *
6091 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6092 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6094 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6095 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6096 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6097 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6099 c42c9805 2020-11-24 stsp *new_view = NULL;
6101 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6102 c42c9805 2020-11-24 stsp if (err) {
6103 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6104 c42c9805 2020-11-24 stsp return err;
6106 c42c9805 2020-11-24 stsp return NULL;
6109 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6111 c42c9805 2020-11-24 stsp goto done;
6113 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6115 c42c9805 2020-11-24 stsp goto done;
6117 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6118 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6119 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6120 c42c9805 2020-11-24 stsp goto done;
6123 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6125 c42c9805 2020-11-24 stsp goto done;
6127 c42c9805 2020-11-24 stsp *new_view = tree_view;
6129 c42c9805 2020-11-24 stsp free(commit_id);
6130 c42c9805 2020-11-24 stsp free(tree_id);
6131 c42c9805 2020-11-24 stsp if (err) {
6133 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6135 c42c9805 2020-11-24 stsp return err;
6137 c42c9805 2020-11-24 stsp static const struct got_error *
6138 6458efa5 2020-11-24 stsp input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6139 6458efa5 2020-11-24 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
6141 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6142 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6143 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6144 34ba6917 2020-11-30 stsp int begin_x = 0;
6146 6458efa5 2020-11-24 stsp switch (ch) {
6148 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6150 6458efa5 2020-11-24 stsp case KEY_ENTER:
6151 6458efa5 2020-11-24 stsp case '\r':
6152 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6154 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6155 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6156 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6158 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6159 6458efa5 2020-11-24 stsp err = view_close_child(view);
6161 6458efa5 2020-11-24 stsp return err;
6162 6458efa5 2020-11-24 stsp err = view_set_child(view, log_view);
6163 6458efa5 2020-11-24 stsp if (err) {
6164 6458efa5 2020-11-24 stsp view_close(log_view);
6167 6458efa5 2020-11-24 stsp *focus_view = log_view;
6168 6458efa5 2020-11-24 stsp view->child_focussed = 1;
6170 6458efa5 2020-11-24 stsp *new_view = log_view;
6173 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6175 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6176 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6177 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6179 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6181 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6182 c42c9805 2020-11-24 stsp err = view_close_child(view);
6184 c42c9805 2020-11-24 stsp return err;
6185 c42c9805 2020-11-24 stsp err = view_set_child(view, tree_view);
6186 c42c9805 2020-11-24 stsp if (err) {
6187 c42c9805 2020-11-24 stsp view_close(tree_view);
6190 c42c9805 2020-11-24 stsp *focus_view = tree_view;
6191 c42c9805 2020-11-24 stsp view->child_focussed = 1;
6193 c42c9805 2020-11-24 stsp *new_view = tree_view;
6196 6458efa5 2020-11-24 stsp case KEY_UP:
6197 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6198 6458efa5 2020-11-24 stsp s->selected--;
6201 694d3271 2020-12-01 naddy ref_scroll_up(view, 1);
6203 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6204 6458efa5 2020-11-24 stsp case CTRL('b'):
6205 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6206 34ba6917 2020-11-30 stsp s->selected = 0;
6207 694d3271 2020-12-01 naddy ref_scroll_up(view, MAX(0, view->nlines - 1));
6210 6458efa5 2020-11-24 stsp case KEY_DOWN:
6211 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6212 6458efa5 2020-11-24 stsp s->selected++;
6215 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6216 6458efa5 2020-11-24 stsp /* can't scroll any further */
6218 694d3271 2020-12-01 naddy ref_scroll_down(view, 1);
6220 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6221 6458efa5 2020-11-24 stsp case CTRL('f'):
6222 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6223 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6224 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6225 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6228 694d3271 2020-12-01 naddy ref_scroll_down(view, view->nlines - 1);
6230 6458efa5 2020-11-24 stsp case CTRL('l'):
6231 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6232 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6234 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6235 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6236 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6242 6458efa5 2020-11-24 stsp return err;
6245 6458efa5 2020-11-24 stsp __dead static void
6246 6458efa5 2020-11-24 stsp usage_ref(void)
6249 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6250 6458efa5 2020-11-24 stsp getprogname());
6254 6458efa5 2020-11-24 stsp static const struct got_error *
6255 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6257 6458efa5 2020-11-24 stsp const struct got_error *error;
6258 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6259 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6260 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6262 6458efa5 2020-11-24 stsp struct tog_view *view;
6264 6458efa5 2020-11-24 stsp #ifndef PROFILE
6265 6458efa5 2020-11-24 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6266 6458efa5 2020-11-24 stsp NULL) == -1)
6267 6458efa5 2020-11-24 stsp err(1, "pledge");
6270 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6271 6458efa5 2020-11-24 stsp switch (ch) {
6273 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6274 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6275 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6279 e99e2d15 2020-11-24 naddy usage_ref();
6280 6458efa5 2020-11-24 stsp /* NOTREACHED */
6284 6458efa5 2020-11-24 stsp argc -= optind;
6285 6458efa5 2020-11-24 stsp argv += optind;
6287 6458efa5 2020-11-24 stsp if (argc > 1)
6288 e99e2d15 2020-11-24 naddy usage_ref();
6290 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6291 6458efa5 2020-11-24 stsp if (cwd == NULL)
6292 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6294 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6295 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6296 6458efa5 2020-11-24 stsp goto done;
6298 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6299 6458efa5 2020-11-24 stsp if (worktree)
6300 6458efa5 2020-11-24 stsp repo_path =
6301 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6303 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6305 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6306 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6307 6458efa5 2020-11-24 stsp goto done;
6310 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6311 6458efa5 2020-11-24 stsp if (error != NULL)
6312 6458efa5 2020-11-24 stsp goto done;
6314 6458efa5 2020-11-24 stsp init_curses();
6316 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6317 6458efa5 2020-11-24 stsp if (error)
6318 6458efa5 2020-11-24 stsp goto done;
6320 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6321 6458efa5 2020-11-24 stsp if (view == NULL) {
6322 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6323 6458efa5 2020-11-24 stsp goto done;
6326 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6327 6458efa5 2020-11-24 stsp if (error)
6328 6458efa5 2020-11-24 stsp goto done;
6330 6458efa5 2020-11-24 stsp if (worktree) {
6331 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6332 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6333 6458efa5 2020-11-24 stsp worktree = NULL;
6335 6458efa5 2020-11-24 stsp error = view_loop(view);
6337 6458efa5 2020-11-24 stsp free(repo_path);
6338 6458efa5 2020-11-24 stsp free(cwd);
6340 6458efa5 2020-11-24 stsp got_repo_close(repo);
6341 6458efa5 2020-11-24 stsp return error;
6344 6458efa5 2020-11-24 stsp static void
6345 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6349 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6350 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6351 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6352 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6354 6879ba42 2020-10-01 naddy fputc('\n', fp);
6357 4ed7e80c 2018-05-20 stsp __dead static void
6358 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6360 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6362 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6363 6879ba42 2020-10-01 naddy getprogname());
6364 6879ba42 2020-10-01 naddy if (hflag) {
6365 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6366 6879ba42 2020-10-01 naddy list_commands(fp);
6368 6879ba42 2020-10-01 naddy exit(status);
6371 c2301be8 2018-04-30 stsp static char **
6372 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6374 ee85c5e8 2020-02-29 stsp va_list ap;
6375 c2301be8 2018-04-30 stsp char **argv;
6378 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6380 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6381 c2301be8 2018-04-30 stsp if (argv == NULL)
6382 c2301be8 2018-04-30 stsp err(1, "calloc");
6383 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6384 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6385 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6386 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6389 ee85c5e8 2020-02-29 stsp va_end(ap);
6390 c2301be8 2018-04-30 stsp return argv;
6394 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6395 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6396 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6397 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6398 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6400 ee85c5e8 2020-02-29 stsp static const struct got_error *
6401 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6403 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6404 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6405 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6406 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6407 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6408 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6411 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6412 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6413 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6415 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6416 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6417 ee85c5e8 2020-02-29 stsp goto done;
6419 ee85c5e8 2020-02-29 stsp if (worktree)
6420 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6422 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6423 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6424 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6425 ee85c5e8 2020-02-29 stsp goto done;
6428 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6429 ee85c5e8 2020-02-29 stsp if (error != NULL)
6430 ee85c5e8 2020-02-29 stsp goto done;
6432 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6433 ee85c5e8 2020-02-29 stsp repo, worktree);
6434 ee85c5e8 2020-02-29 stsp if (error)
6435 ee85c5e8 2020-02-29 stsp goto done;
6437 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6438 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6439 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6440 ee85c5e8 2020-02-29 stsp if (error)
6441 ee85c5e8 2020-02-29 stsp goto done;
6443 ee85c5e8 2020-02-29 stsp if (worktree) {
6444 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6445 ee85c5e8 2020-02-29 stsp worktree = NULL;
6448 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6449 ee85c5e8 2020-02-29 stsp if (error) {
6450 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6451 ee85c5e8 2020-02-29 stsp goto done;
6452 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6453 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6454 6879ba42 2020-10-01 naddy usage(1, 1);
6455 ee85c5e8 2020-02-29 stsp /* not reached */
6458 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6459 ee85c5e8 2020-02-29 stsp repo = NULL;
6461 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6462 ee85c5e8 2020-02-29 stsp if (error)
6463 ee85c5e8 2020-02-29 stsp goto done;
6465 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6467 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6468 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6471 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6472 ee85c5e8 2020-02-29 stsp if (worktree)
6473 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6475 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6476 ee85c5e8 2020-02-29 stsp free(commit_id);
6477 ee85c5e8 2020-02-29 stsp free(cwd);
6478 ee85c5e8 2020-02-29 stsp free(repo_path);
6479 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6480 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6482 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6483 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6484 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6486 ee85c5e8 2020-02-29 stsp return error;
6490 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6492 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6493 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6494 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6495 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6496 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6497 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6498 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6501 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6503 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6504 9f7d7167 2018-04-29 stsp switch (ch) {
6506 9f7d7167 2018-04-29 stsp hflag = 1;
6509 53ccebc2 2019-07-30 stsp Vflag = 1;
6512 6879ba42 2020-10-01 naddy usage(hflag, 1);
6513 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6517 9f7d7167 2018-04-29 stsp argc -= optind;
6518 9f7d7167 2018-04-29 stsp argv += optind;
6519 9814e6a3 2020-09-27 naddy optind = 1;
6520 c2301be8 2018-04-30 stsp optreset = 1;
6522 53ccebc2 2019-07-30 stsp if (Vflag) {
6523 53ccebc2 2019-07-30 stsp got_version_print_str();
6524 6879ba42 2020-10-01 naddy return 0;
6527 c2301be8 2018-04-30 stsp if (argc == 0) {
6528 f29d3e89 2018-06-23 stsp if (hflag)
6529 6879ba42 2020-10-01 naddy usage(hflag, 0);
6530 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6531 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6533 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6537 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6538 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6539 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6540 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6541 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6547 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6548 ee85c5e8 2020-02-29 stsp if (argc != 1)
6549 6879ba42 2020-10-01 naddy usage(0, 1);
6550 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6551 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6553 ee85c5e8 2020-02-29 stsp if (hflag)
6554 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6556 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6560 b46c1e04 2020-09-20 naddy putchar('\n');
6561 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6563 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6564 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6565 a2f4a359 2020-02-28 stsp free(cmd_argv);
6568 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6569 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);