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 ffe38506 2020-12-01 naddy log_scroll_up(struct tog_view *view, int maxscroll)
1692 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1693 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1694 07b55e75 2018-05-10 stsp int nscrolled = 0;
1696 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1697 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1700 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1701 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1702 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1703 07b55e75 2018-05-10 stsp if (entry) {
1704 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1705 07b55e75 2018-05-10 stsp nscrolled++;
1710 aa075928 2018-05-10 stsp static const struct got_error *
1711 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1713 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1714 5e224a3e 2019-02-22 stsp int errcode;
1716 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1718 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1719 ffe38506 2020-12-01 naddy if (ta->log_complete)
1722 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1723 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1724 7aafa0d1 2019-02-22 stsp if (errcode)
1725 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1726 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1729 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1730 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1732 7c1452c1 2020-03-26 stsp if (!wait)
1735 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1736 ffe38506 2020-12-01 naddy show_log_view(view);
1737 7c1452c1 2020-03-26 stsp update_panels();
1738 7c1452c1 2020-03-26 stsp doupdate();
1740 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1741 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1742 82954512 2020-02-03 stsp if (errcode)
1743 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1744 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1746 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1747 ffe38506 2020-12-01 naddy show_log_view(view);
1748 7c1452c1 2020-03-26 stsp update_panels();
1749 7c1452c1 2020-03-26 stsp doupdate();
1752 5e224a3e 2019-02-22 stsp return NULL;
1755 5e224a3e 2019-02-22 stsp static const struct got_error *
1756 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1758 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1759 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1760 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1761 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1763 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1764 5e224a3e 2019-02-22 stsp return NULL;
1766 ffe38506 2020-12-01 naddy ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1767 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1768 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1770 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1772 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1773 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1775 5e224a3e 2019-02-22 stsp return err;
1779 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1780 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1783 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1785 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1786 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1788 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1789 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1791 dd0a52c1 2018-05-20 stsp return err;
1794 cd0acaa7 2018-05-20 stsp static const struct got_error *
1795 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1796 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1797 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1799 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1800 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1801 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1803 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1804 15a94983 2018-12-23 stsp if (diff_view == NULL)
1805 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1807 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1809 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1810 e5a0f69f 2018-08-18 stsp if (err == NULL)
1811 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1812 cd0acaa7 2018-05-20 stsp return err;
1815 80ddbec8 2018-04-29 stsp static const struct got_error *
1816 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1817 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1819 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1821 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1822 941e9f74 2019-05-21 stsp if (parent == NULL)
1823 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1825 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1826 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1827 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1828 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1829 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1830 941e9f74 2019-05-21 stsp s->tree = subtree;
1831 941e9f74 2019-05-21 stsp s->selected = 0;
1832 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1833 941e9f74 2019-05-21 stsp return NULL;
1836 941e9f74 2019-05-21 stsp static const struct got_error *
1837 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1838 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1839 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1841 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1842 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1843 941e9f74 2019-05-21 stsp const char *p;
1844 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1846 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1848 941e9f74 2019-05-21 stsp while (*p) {
1849 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1850 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1851 56e0773d 2019-11-28 stsp char *te_name;
1853 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1856 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1857 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1858 941e9f74 2019-05-21 stsp if (slash == NULL)
1859 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1861 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1862 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1863 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1866 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1867 56e0773d 2019-11-28 stsp if (te == NULL) {
1868 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1869 56e0773d 2019-11-28 stsp free(te_name);
1872 56e0773d 2019-11-28 stsp free(te_name);
1873 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1875 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1876 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1878 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1879 941e9f74 2019-05-21 stsp if (slash)
1880 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1882 941e9f74 2019-05-21 stsp subpath = strdup(path);
1883 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1884 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1888 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1893 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1894 941e9f74 2019-05-21 stsp free(tree_id);
1898 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1899 941e9f74 2019-05-21 stsp if (err) {
1900 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1903 941e9f74 2019-05-21 stsp if (slash == NULL)
1905 941e9f74 2019-05-21 stsp free(subpath);
1906 941e9f74 2019-05-21 stsp subpath = NULL;
1907 941e9f74 2019-05-21 stsp p = slash;
1910 941e9f74 2019-05-21 stsp free(subpath);
1911 1a76625f 2018-10-22 stsp return err;
1914 61266923 2020-01-14 stsp static const struct got_error *
1915 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1916 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1917 78756c87 2020-11-24 stsp struct got_repository *repo)
1919 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1920 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1921 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1922 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1924 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1925 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1927 55cccc34 2020-02-20 stsp return err;
1929 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1930 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1931 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1933 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1934 55cccc34 2020-02-20 stsp if (err) {
1935 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1936 55cccc34 2020-02-20 stsp return err;
1938 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1940 55cccc34 2020-02-20 stsp *new_view = tree_view;
1942 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1943 55cccc34 2020-02-20 stsp return NULL;
1945 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1948 55cccc34 2020-02-20 stsp static const struct got_error *
1949 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1951 61266923 2020-01-14 stsp sigset_t sigset;
1952 61266923 2020-01-14 stsp int errcode;
1954 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1955 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1957 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1958 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1959 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1960 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1961 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1963 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1964 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1965 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1967 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1968 61266923 2020-01-14 stsp if (errcode)
1969 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1971 61266923 2020-01-14 stsp return NULL;
1974 1a76625f 2018-10-22 stsp static void *
1975 1a76625f 2018-10-22 stsp log_thread(void *arg)
1977 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1978 1a76625f 2018-10-22 stsp int errcode = 0;
1979 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1980 1a76625f 2018-10-22 stsp int done = 0;
1982 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1984 61266923 2020-01-14 stsp return (void *)err;
1986 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1987 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1988 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1989 13add988 2019-10-15 stsp a->regex);
1990 1a76625f 2018-10-22 stsp if (err) {
1991 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1992 1a76625f 2018-10-22 stsp return (void *)err;
1993 1a76625f 2018-10-22 stsp err = NULL;
1995 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1996 1a76625f 2018-10-22 stsp a->commits_needed--;
1998 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1999 3abe8080 2019-04-10 stsp if (errcode) {
2000 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2001 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2003 3abe8080 2019-04-10 stsp } else if (*a->quit)
2005 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2006 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2007 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2008 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2011 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2012 7c1452c1 2020-03-26 stsp if (errcode) {
2013 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2014 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2015 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2020 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2022 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2023 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2024 7c1452c1 2020-03-26 stsp &tog_mutex);
2025 7c1452c1 2020-03-26 stsp if (errcode)
2026 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2027 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2031 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2032 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2033 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2034 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2036 3abe8080 2019-04-10 stsp a->log_complete = 1;
2037 1a76625f 2018-10-22 stsp return (void *)err;
2040 1a76625f 2018-10-22 stsp static const struct got_error *
2041 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2043 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2044 1a76625f 2018-10-22 stsp int errcode;
2046 1a76625f 2018-10-22 stsp if (s->thread) {
2047 1a76625f 2018-10-22 stsp s->quit = 1;
2048 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2049 1a76625f 2018-10-22 stsp if (errcode)
2050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2051 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2052 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2053 1a76625f 2018-10-22 stsp if (errcode)
2054 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2055 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2056 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2057 1a76625f 2018-10-22 stsp if (errcode)
2058 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2059 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2060 1a76625f 2018-10-22 stsp if (errcode)
2061 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2062 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2063 1a76625f 2018-10-22 stsp s->thread = NULL;
2066 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2067 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2068 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2071 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2072 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2073 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2076 9343a5fb 2018-06-23 stsp return err;
2079 9343a5fb 2018-06-23 stsp static const struct got_error *
2080 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2082 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2083 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2084 276b94a1 2020-11-13 naddy int errcode;
2086 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2088 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2089 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2090 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2092 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2093 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2094 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2096 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2097 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2098 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2099 1a76625f 2018-10-22 stsp free(s->start_id);
2100 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2101 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2102 1a76625f 2018-10-22 stsp return err;
2105 1a76625f 2018-10-22 stsp static const struct got_error *
2106 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2108 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2110 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2111 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2112 60493ae3 2019-06-20 stsp return NULL;
2115 60493ae3 2019-06-20 stsp static const struct got_error *
2116 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2118 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2119 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2120 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2122 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2123 f9686aa5 2020-03-27 stsp show_log_view(view);
2124 f9686aa5 2020-03-27 stsp update_panels();
2125 f9686aa5 2020-03-27 stsp doupdate();
2127 96e2b566 2019-07-08 stsp if (s->search_entry) {
2128 13add988 2019-10-15 stsp int errcode, ch;
2129 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2130 13add988 2019-10-15 stsp if (errcode)
2131 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2132 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2133 13add988 2019-10-15 stsp ch = wgetch(view->window);
2134 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&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_lock");
2138 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2139 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 678cbce5 2019-07-28 stsp return NULL;
2142 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2143 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2145 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2146 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2147 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2148 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2149 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2151 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2152 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2154 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2155 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2157 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2160 60493ae3 2019-06-20 stsp while (1) {
2161 13add988 2019-10-15 stsp int have_match = 0;
2163 60493ae3 2019-06-20 stsp if (entry == NULL) {
2164 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2165 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2166 f9967bca 2020-03-27 stsp view->search_next_done =
2167 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2168 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2169 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2170 f801134a 2019-06-25 stsp return NULL;
2173 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2174 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2175 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2177 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2178 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2181 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2182 13add988 2019-10-15 stsp &view->regex);
2185 13add988 2019-10-15 stsp if (have_match) {
2186 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2187 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2191 96e2b566 2019-07-08 stsp s->search_entry = entry;
2192 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2193 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2195 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2198 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2199 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2200 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2201 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2203 60493ae3 2019-06-20 stsp return err;
2206 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2207 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2209 60493ae3 2019-06-20 stsp return err;
2214 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2216 60493ae3 2019-06-20 stsp return NULL;
2219 60493ae3 2019-06-20 stsp static const struct got_error *
2220 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2221 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2222 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2224 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2225 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2226 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2227 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2228 1a76625f 2018-10-22 stsp int errcode;
2230 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2232 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2233 f135c941 2020-02-20 stsp free(s->in_repo_path);
2234 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2235 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2236 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2239 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2240 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2241 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2243 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2245 78756c87 2020-11-24 stsp goto done;
2247 fb2756b9 2018-08-04 stsp s->repo = repo;
2248 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2249 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2250 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2251 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2252 5036bf37 2018-09-24 stsp goto done;
2254 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2256 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2257 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2261 11b20872 2019-11-08 stsp goto done;
2262 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2263 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2264 11b20872 2019-11-08 stsp if (err) {
2265 11b20872 2019-11-08 stsp free_colors(&s->colors);
2266 11b20872 2019-11-08 stsp goto done;
2268 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2269 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2270 11b20872 2019-11-08 stsp if (err) {
2271 11b20872 2019-11-08 stsp free_colors(&s->colors);
2272 11b20872 2019-11-08 stsp goto done;
2276 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2277 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2278 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2279 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2280 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2282 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2284 1a76625f 2018-10-22 stsp goto done;
2285 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2286 b672a97a 2020-01-27 stsp !s->log_branches);
2288 1a76625f 2018-10-22 stsp goto done;
2289 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2290 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2292 c5b78334 2020-01-12 stsp goto done;
2294 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2295 1a76625f 2018-10-22 stsp if (errcode) {
2296 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2297 1a76625f 2018-10-22 stsp goto done;
2299 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2300 7c1452c1 2020-03-26 stsp if (errcode) {
2301 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2302 7c1452c1 2020-03-26 stsp goto done;
2305 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2306 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2307 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2308 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2309 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2310 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2311 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2312 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2313 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2314 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2315 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2316 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2317 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2320 1a76625f 2018-10-22 stsp close_log_view(view);
2321 ba4f502b 2018-08-04 stsp return err;
2324 e5a0f69f 2018-08-18 stsp static const struct got_error *
2325 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2327 f2f6d207 2020-11-24 stsp const struct got_error *err;
2328 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2330 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2331 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2332 2b380cc8 2018-10-24 stsp &s->thread_args);
2333 2b380cc8 2018-10-24 stsp if (errcode)
2334 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2335 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2336 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2338 f2f6d207 2020-11-24 stsp return err;
2342 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2343 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2344 78756c87 2020-11-24 stsp &s->commits, s->selected, view->nlines, &s->refs,
2345 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2348 e5a0f69f 2018-08-18 stsp static const struct got_error *
2349 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2350 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2352 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2353 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2354 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2355 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2356 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2357 669b5ffa 2018-10-07 stsp int begin_x = 0;
2358 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2360 e5a0f69f 2018-08-18 stsp switch (ch) {
2362 1e37a5c2 2019-05-12 jcs s->quit = 1;
2365 1e37a5c2 2019-05-12 jcs case KEY_UP:
2368 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2370 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2371 1e37a5c2 2019-05-12 jcs s->selected--;
2373 ffe38506 2020-12-01 naddy log_scroll_up(view, 1);
2375 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2376 a4292ac5 2019-05-12 jcs case CTRL('b'):
2377 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2379 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2380 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2381 1e37a5c2 2019-05-12 jcs s->selected = 0;
2384 ffe38506 2020-12-01 naddy log_scroll_up(view, view->nlines - 1);
2387 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2390 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2392 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2393 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2394 1e37a5c2 2019-05-12 jcs s->selected++;
2397 ffe38506 2020-12-01 naddy err = log_scroll_down(view, 1);
2399 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2400 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2401 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2402 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2403 1e37a5c2 2019-05-12 jcs if (first == NULL)
2405 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2408 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2409 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2410 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2411 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2412 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2413 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2415 1e37a5c2 2019-05-12 jcs err = NULL;
2418 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2419 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2420 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2421 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2422 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2424 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2427 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2429 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2430 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2431 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2432 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2433 78756c87 2020-11-24 stsp view, s->repo);
2436 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2437 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2439 1e37a5c2 2019-05-12 jcs return err;
2440 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2442 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2445 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2446 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2448 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2451 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2453 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2454 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2455 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2456 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2459 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2460 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2462 1e37a5c2 2019-05-12 jcs return err;
2463 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2465 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2468 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2469 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2471 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2473 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2474 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2475 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2477 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2479 74cfe85e 2020-10-20 stsp return err;
2480 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2481 74cfe85e 2020-10-20 stsp if (err) {
2482 74cfe85e 2020-10-20 stsp free(parent_path);
2483 74cfe85e 2020-10-20 stsp return err;
2485 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2486 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2487 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2488 74cfe85e 2020-10-20 stsp free(parent_path);
2489 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2491 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2492 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2493 74cfe85e 2020-10-20 stsp free(parent_path);
2495 74cfe85e 2020-10-20 stsp return err;;
2496 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2497 74cfe85e 2020-10-20 stsp *new_view = lv;
2499 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2500 74cfe85e 2020-10-20 stsp *focus_view = lv;
2503 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2504 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2506 d01904d4 2019-06-25 stsp return err;
2507 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2508 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2509 d01904d4 2019-06-25 stsp if (lv == NULL)
2510 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2511 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2512 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2513 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2514 ef129c5e 2019-08-03 stsp if (err) {
2515 ef129c5e 2019-08-03 stsp view_close(lv);
2516 d01904d4 2019-06-25 stsp return err;
2518 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2519 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2520 e6b23735 2019-10-04 stsp free(start_id);
2521 e6b23735 2019-10-04 stsp view_close(lv);
2522 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2524 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2525 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2526 ef129c5e 2019-08-03 stsp if (err) {
2527 ef129c5e 2019-08-03 stsp free(start_id);
2528 b672a97a 2020-01-27 stsp view_close(lv);
2529 b672a97a 2020-01-27 stsp return err;;
2531 b672a97a 2020-01-27 stsp *dead_view = view;
2532 b672a97a 2020-01-27 stsp *new_view = lv;
2535 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2536 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2538 b672a97a 2020-01-27 stsp return err;
2539 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2540 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2541 b672a97a 2020-01-27 stsp if (lv == NULL)
2542 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2543 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2544 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2545 b672a97a 2020-01-27 stsp if (err) {
2546 ef129c5e 2019-08-03 stsp view_close(lv);
2547 d01904d4 2019-06-25 stsp return err;;
2549 d01904d4 2019-06-25 stsp *dead_view = view;
2550 d01904d4 2019-06-25 stsp *new_view = lv;
2553 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2554 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2555 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2556 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2557 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2558 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2559 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2560 6458efa5 2020-11-24 stsp if (err) {
2561 6458efa5 2020-11-24 stsp view_close(ref_view);
2562 6458efa5 2020-11-24 stsp return err;
2564 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2565 6458efa5 2020-11-24 stsp err = view_close_child(view);
2567 6458efa5 2020-11-24 stsp return err;
2568 6458efa5 2020-11-24 stsp err = view_set_child(view, ref_view);
2569 6458efa5 2020-11-24 stsp if (err) {
2570 6458efa5 2020-11-24 stsp view_close(ref_view);
2573 6458efa5 2020-11-24 stsp *focus_view = ref_view;
2574 6458efa5 2020-11-24 stsp view->child_focussed = 1;
2576 6458efa5 2020-11-24 stsp *new_view = ref_view;
2582 80ddbec8 2018-04-29 stsp return err;
2585 4ed7e80c 2018-05-20 stsp static const struct got_error *
2586 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2588 c2db6724 2019-01-04 stsp const struct got_error *error;
2590 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2591 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2592 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2594 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2595 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2597 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2598 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2600 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2601 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2603 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2604 c2db6724 2019-01-04 stsp if (error != NULL)
2605 c2db6724 2019-01-04 stsp return error;
2607 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2608 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2610 c2db6724 2019-01-04 stsp return NULL;
2613 a915003a 2019-02-05 stsp static void
2614 a915003a 2019-02-05 stsp init_curses(void)
2616 a915003a 2019-02-05 stsp initscr();
2618 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2621 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2622 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2623 a915003a 2019-02-05 stsp curs_set(0);
2624 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2625 6d17833f 2019-11-08 stsp start_color();
2626 6d17833f 2019-11-08 stsp use_default_colors();
2628 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2629 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2630 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2633 c2db6724 2019-01-04 stsp static const struct got_error *
2634 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2635 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2637 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2639 f135c941 2020-02-20 stsp if (argc == 0) {
2640 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2641 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2642 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2643 f135c941 2020-02-20 stsp return NULL;
2646 f135c941 2020-02-20 stsp if (worktree) {
2647 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2650 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2652 f135c941 2020-02-20 stsp return err;
2653 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2654 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2655 bfd61697 2020-11-14 stsp p) == -1) {
2656 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2657 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2661 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2663 f135c941 2020-02-20 stsp return err;
2666 f135c941 2020-02-20 stsp static const struct got_error *
2667 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2669 80ddbec8 2018-04-29 stsp const struct got_error *error;
2670 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2671 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2672 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2673 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2674 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2675 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2676 04cc582a 2018-08-01 stsp struct tog_view *view;
2678 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2679 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2680 c2db6724 2019-01-04 stsp NULL) == -1)
2681 80ddbec8 2018-04-29 stsp err(1, "pledge");
2684 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2685 80ddbec8 2018-04-29 stsp switch (ch) {
2687 b672a97a 2020-01-27 stsp log_branches = 1;
2690 80ddbec8 2018-04-29 stsp start_commit = optarg;
2693 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2694 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2695 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2699 17020d27 2019-03-07 stsp usage_log();
2700 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2704 80ddbec8 2018-04-29 stsp argc -= optind;
2705 80ddbec8 2018-04-29 stsp argv += optind;
2707 f135c941 2020-02-20 stsp if (argc > 1)
2708 f135c941 2020-02-20 stsp usage_log();
2710 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2711 6962eb72 2020-02-20 stsp if (cwd == NULL)
2712 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2714 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2715 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2716 963f97a1 2019-03-18 stsp goto done;
2718 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2719 a1fbf39a 2019-08-11 stsp if (worktree)
2720 6962eb72 2020-02-20 stsp repo_path =
2721 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2723 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2725 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2726 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2727 963f97a1 2019-03-18 stsp goto done;
2730 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2731 80ddbec8 2018-04-29 stsp if (error != NULL)
2732 ecb28ae0 2018-07-16 stsp goto done;
2734 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2735 f135c941 2020-02-20 stsp repo, worktree);
2736 f135c941 2020-02-20 stsp if (error)
2737 f135c941 2020-02-20 stsp goto done;
2739 f135c941 2020-02-20 stsp init_curses();
2741 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2742 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2743 c02c541e 2019-03-29 stsp if (error)
2744 c02c541e 2019-03-29 stsp goto done;
2746 15a94983 2018-12-23 stsp if (start_commit == NULL)
2747 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2748 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2749 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2751 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2752 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2753 80ddbec8 2018-04-29 stsp if (error != NULL)
2754 8b473291 2019-02-21 stsp goto done;
2756 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2757 04cc582a 2018-08-01 stsp if (view == NULL) {
2758 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2759 04cc582a 2018-08-01 stsp goto done;
2761 2fc00ff4 2019-08-31 stsp if (worktree) {
2762 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2763 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2764 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2765 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2766 2fc00ff4 2019-08-31 stsp goto done;
2769 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2770 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2771 ba4f502b 2018-08-04 stsp if (error)
2772 ba4f502b 2018-08-04 stsp goto done;
2773 2fc00ff4 2019-08-31 stsp if (worktree) {
2774 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2775 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2776 2fc00ff4 2019-08-31 stsp worktree = NULL;
2778 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2780 f135c941 2020-02-20 stsp free(in_repo_path);
2781 ecb28ae0 2018-07-16 stsp free(repo_path);
2782 ecb28ae0 2018-07-16 stsp free(cwd);
2783 899d86c2 2018-05-10 stsp free(start_id);
2784 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2786 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2787 ec142235 2019-03-07 stsp if (worktree)
2788 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2789 80ddbec8 2018-04-29 stsp return error;
2792 4ed7e80c 2018-05-20 stsp __dead static void
2793 9f7d7167 2018-04-29 stsp usage_diff(void)
2796 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2797 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2801 b304db33 2018-05-20 stsp static char *
2802 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2804 b304db33 2018-05-20 stsp char *line;
2805 b304db33 2018-05-20 stsp size_t linelen;
2806 b304db33 2018-05-20 stsp size_t lineno;
2807 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2809 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2811 b304db33 2018-05-20 stsp *len = linelen;
2812 b304db33 2018-05-20 stsp return line;
2815 6d17833f 2019-11-08 stsp static int
2816 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2817 41605754 2020-11-12 stsp regmatch_t *regmatch)
2819 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2822 f26dddb7 2019-11-08 stsp struct tog_color *
2823 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2825 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2827 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2828 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2829 6d17833f 2019-11-08 stsp return tc;
2832 6d17833f 2019-11-08 stsp return NULL;
2835 4ed7e80c 2018-05-20 stsp static const struct got_error *
2836 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2837 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2839 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2840 41605754 2020-11-12 stsp wchar_t *wline;
2841 41605754 2020-11-12 stsp int width;
2844 41605754 2020-11-12 stsp *wtotal = 0;
2846 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2847 41605754 2020-11-12 stsp if (s == NULL)
2848 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2850 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2851 41605754 2020-11-12 stsp if (err) {
2853 41605754 2020-11-12 stsp return err;
2855 41605754 2020-11-12 stsp waddwstr(window, wline);
2856 41605754 2020-11-12 stsp free(wline);
2858 41605754 2020-11-12 stsp wlimit -= width;
2859 41605754 2020-11-12 stsp *wtotal += width;
2861 41605754 2020-11-12 stsp if (wlimit > 0) {
2862 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2863 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2864 41605754 2020-11-12 stsp if (s == NULL) {
2865 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2867 41605754 2020-11-12 stsp return err;
2869 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2870 41605754 2020-11-12 stsp if (err) {
2872 41605754 2020-11-12 stsp return err;
2874 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2875 41605754 2020-11-12 stsp waddwstr(window, wline);
2876 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2877 41605754 2020-11-12 stsp free(wline);
2879 41605754 2020-11-12 stsp wlimit -= width;
2880 41605754 2020-11-12 stsp *wtotal += width;
2883 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2884 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2885 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2887 41605754 2020-11-12 stsp return err;
2888 41605754 2020-11-12 stsp waddwstr(window, wline);
2889 41605754 2020-11-12 stsp free(wline);
2890 41605754 2020-11-12 stsp *wtotal += width;
2893 41605754 2020-11-12 stsp return NULL;
2896 41605754 2020-11-12 stsp static const struct got_error *
2897 fe621944 2020-11-10 stsp draw_file(struct tog_view *view, FILE *f, int first_displayed_line, int nlines,
2898 fe621944 2020-11-10 stsp off_t *line_offsets, int selected_line, int max_lines,
2899 3dbaef42 2020-11-24 stsp int *last_displayed_line, int *eof, const char *header,
2900 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
2902 61e69b96 2018-05-20 stsp const struct got_error *err;
2903 fe621944 2020-11-10 stsp int nprinted = 0;
2904 b304db33 2018-05-20 stsp char *line;
2905 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2906 b304db33 2018-05-20 stsp size_t len;
2907 61e69b96 2018-05-20 stsp wchar_t *wline;
2908 e0b650dd 2018-05-20 stsp int width;
2909 fe621944 2020-11-10 stsp off_t line_offset;
2911 fe621944 2020-11-10 stsp line_offset = line_offsets[first_displayed_line - 1];
2912 fe621944 2020-11-10 stsp if (fseeko(f, line_offset, SEEK_SET) == -1)
2913 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2915 f7d12f7e 2018-08-01 stsp werase(view->window);
2917 a3404814 2018-09-02 stsp if (header) {
2918 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2919 135a2da0 2020-11-11 stsp first_displayed_line - 1 + selected_line, nlines,
2920 135a2da0 2020-11-11 stsp header) == -1)
2921 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2922 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2923 135a2da0 2020-11-11 stsp free(line);
2925 a3404814 2018-09-02 stsp return err;
2927 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2928 a3404814 2018-09-02 stsp wstandout(view->window);
2929 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2930 e54cc94a 2020-11-11 stsp free(wline);
2931 e54cc94a 2020-11-11 stsp wline = NULL;
2932 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2933 a3404814 2018-09-02 stsp wstandend(view->window);
2934 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2935 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2937 a3404814 2018-09-02 stsp if (max_lines <= 1)
2938 a3404814 2018-09-02 stsp return NULL;
2939 a3404814 2018-09-02 stsp max_lines--;
2943 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2944 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2945 26ed57b2 2018-05-19 stsp if (line == NULL) {
2950 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2952 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2953 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2954 41605754 2020-11-12 stsp if (first_displayed_line + nprinted == matched_line &&
2955 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2956 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2957 41605754 2020-11-12 stsp view->window, regmatch);
2958 41605754 2020-11-12 stsp if (err) {
2959 41605754 2020-11-12 stsp free(line);
2960 41605754 2020-11-12 stsp return err;
2963 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2964 41605754 2020-11-12 stsp if (err) {
2965 41605754 2020-11-12 stsp free(line);
2966 41605754 2020-11-12 stsp return err;
2968 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2969 41605754 2020-11-12 stsp free(wline);
2970 41605754 2020-11-12 stsp wline = NULL;
2973 6d17833f 2019-11-08 stsp wattr_off(view->window,
2974 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2975 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2976 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2977 fe621944 2020-11-10 stsp nprinted++;
2978 26ed57b2 2018-05-19 stsp free(line);
2980 fe621944 2020-11-10 stsp if (nprinted >= 1)
2981 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line + (nprinted - 1);
2983 fe621944 2020-11-10 stsp *last_displayed_line = first_displayed_line;
2985 1a57306a 2018-09-02 stsp view_vborder(view);
2987 c3e9aa98 2019-05-13 jcs if (*eof) {
2988 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2989 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2990 c3e9aa98 2019-05-13 jcs nprinted++;
2993 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2995 c3e9aa98 2019-05-13 jcs return err;
2998 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2999 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3000 e54cc94a 2020-11-11 stsp free(wline);
3001 e54cc94a 2020-11-11 stsp wline = NULL;
3002 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3005 26ed57b2 2018-05-19 stsp return NULL;
3008 abd2672a 2018-12-23 stsp static char *
3009 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3011 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3012 09867e48 2019-08-13 stsp char *p, *s;
3014 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3015 09867e48 2019-08-13 stsp if (tm == NULL)
3016 09867e48 2019-08-13 stsp return NULL;
3017 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3018 09867e48 2019-08-13 stsp if (s == NULL)
3019 09867e48 2019-08-13 stsp return NULL;
3020 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3022 abd2672a 2018-12-23 stsp *p = '\0';
3026 4ed7e80c 2018-05-20 stsp static const struct got_error *
3027 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3028 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3030 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3031 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3032 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3033 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3035 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3036 0208f208 2020-05-05 stsp if (qid != NULL) {
3037 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3038 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3041 0208f208 2020-05-05 stsp return err;
3043 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3044 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3048 0208f208 2020-05-05 stsp if (tree_id1) {
3049 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3051 0208f208 2020-05-05 stsp goto done;
3054 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3055 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3057 0208f208 2020-05-05 stsp goto done;
3059 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3060 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3062 0208f208 2020-05-05 stsp if (tree1)
3063 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3064 0208f208 2020-05-05 stsp if (tree2)
3065 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3066 0208f208 2020-05-05 stsp return err;
3069 0208f208 2020-05-05 stsp static const struct got_error *
3070 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3074 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3075 fe621944 2020-11-10 stsp if (p == NULL)
3076 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3077 fe621944 2020-11-10 stsp *line_offsets = p;
3078 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3079 fe621944 2020-11-10 stsp (*nlines)++;
3080 fe621944 2020-11-10 stsp return NULL;
3083 fe621944 2020-11-10 stsp static const struct got_error *
3084 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3085 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3086 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3088 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3089 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3090 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3091 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3092 45d799e2 2018-12-23 stsp time_t committer_time;
3093 45d799e2 2018-12-23 stsp const char *author, *committer;
3094 8b473291 2019-02-21 stsp char *refs_str = NULL;
3095 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3096 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3097 fe621944 2020-11-10 stsp off_t outoff = 0;
3100 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3102 8b473291 2019-02-21 stsp if (refs) {
3103 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3105 8b473291 2019-02-21 stsp return err;
3108 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3110 abd2672a 2018-12-23 stsp return err;
3112 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3113 15a94983 2018-12-23 stsp if (err) {
3114 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3115 15a94983 2018-12-23 stsp goto done;
3118 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3120 fe621944 2020-11-10 stsp goto done;
3122 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3123 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3124 fe621944 2020-11-10 stsp if (n < 0) {
3125 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3126 abd2672a 2018-12-23 stsp goto done;
3128 fe621944 2020-11-10 stsp outoff += n;
3129 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3131 fe621944 2020-11-10 stsp goto done;
3133 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3134 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3135 fe621944 2020-11-10 stsp if (n < 0) {
3136 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3137 abd2672a 2018-12-23 stsp goto done;
3139 fe621944 2020-11-10 stsp outoff += n;
3140 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3142 fe621944 2020-11-10 stsp goto done;
3144 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3145 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3146 fe621944 2020-11-10 stsp if (datestr) {
3147 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3148 fe621944 2020-11-10 stsp if (n < 0) {
3149 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3150 fe621944 2020-11-10 stsp goto done;
3152 fe621944 2020-11-10 stsp outoff += n;
3153 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3155 fe621944 2020-11-10 stsp goto done;
3157 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3158 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3159 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3160 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3161 fe621944 2020-11-10 stsp if (n < 0) {
3162 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3163 fe621944 2020-11-10 stsp goto done;
3165 fe621944 2020-11-10 stsp outoff += n;
3166 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3168 fe621944 2020-11-10 stsp goto done;
3170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3172 5943eee2 2019-08-13 stsp goto done;
3173 fe621944 2020-11-10 stsp s = logmsg;
3174 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3175 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3176 fe621944 2020-11-10 stsp if (n < 0) {
3177 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3178 fe621944 2020-11-10 stsp goto done;
3180 fe621944 2020-11-10 stsp outoff += n;
3181 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3183 fe621944 2020-11-10 stsp goto done;
3186 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3188 0208f208 2020-05-05 stsp goto done;
3189 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3190 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3191 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3192 fe621944 2020-11-10 stsp if (n < 0) {
3193 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3194 fe621944 2020-11-10 stsp goto done;
3196 fe621944 2020-11-10 stsp outoff += n;
3197 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3199 fe621944 2020-11-10 stsp goto done;
3200 0208f208 2020-05-05 stsp free((char *)pe->path);
3201 0208f208 2020-05-05 stsp free(pe->data);
3204 0208f208 2020-05-05 stsp fputc('\n', outfile);
3206 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3208 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3209 abd2672a 2018-12-23 stsp free(id_str);
3210 5943eee2 2019-08-13 stsp free(logmsg);
3211 8b473291 2019-02-21 stsp free(refs_str);
3212 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3213 7510f233 2020-08-09 stsp if (err) {
3214 ae6a6978 2020-08-09 stsp free(*line_offsets);
3215 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3216 ae6a6978 2020-08-09 stsp *nlines = 0;
3218 fe621944 2020-11-10 stsp return err;
3221 abd2672a 2018-12-23 stsp static const struct got_error *
3222 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3224 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3225 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3226 15a94983 2018-12-23 stsp int obj_type;
3228 fe621944 2020-11-10 stsp free(s->line_offsets);
3229 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3230 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3231 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3232 fe621944 2020-11-10 stsp s->nlines = 0;
3234 511a516b 2018-05-19 stsp f = got_opentemp();
3235 48ae06ee 2018-10-18 stsp if (f == NULL) {
3236 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3237 48ae06ee 2018-10-18 stsp goto done;
3239 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3240 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3241 fb43ecf1 2019-02-11 stsp goto done;
3245 15a94983 2018-12-23 stsp if (s->id1)
3246 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3248 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3250 15a94983 2018-12-23 stsp goto done;
3252 15a94983 2018-12-23 stsp switch (obj_type) {
3253 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3254 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3255 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3256 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3258 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3259 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3260 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3261 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3263 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3264 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3265 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3266 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3268 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3270 3ffacbe1 2020-02-02 tracey goto done;
3271 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3272 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3273 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3274 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3275 f44b1f58 2020-02-02 tracey if (err)
3276 f44b1f58 2020-02-02 tracey goto done;
3277 f44b1f58 2020-02-02 tracey } else {
3278 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3279 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3280 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3281 fe621944 2020-11-10 stsp err = write_commit_info(
3282 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3283 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3284 f44b1f58 2020-02-02 tracey if (err)
3285 f44b1f58 2020-02-02 tracey goto done;
3290 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3292 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3293 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3294 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3298 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3301 f44b1f58 2020-02-02 tracey if (err)
3302 f44b1f58 2020-02-02 tracey goto done;
3304 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3305 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3306 48ae06ee 2018-10-18 stsp return err;
3309 f5215bb9 2019-02-22 stsp static void
3310 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3312 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3313 f5215bb9 2019-02-22 stsp update_panels();
3314 f5215bb9 2019-02-22 stsp doupdate();
3317 f44b1f58 2020-02-02 tracey static const struct got_error *
3318 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3320 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3322 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3323 f44b1f58 2020-02-02 tracey return NULL;
3326 f44b1f58 2020-02-02 tracey static const struct got_error *
3327 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3329 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3330 f44b1f58 2020-02-02 tracey int lineno;
3332 f44b1f58 2020-02-02 tracey if (!view->searching) {
3333 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3334 f44b1f58 2020-02-02 tracey return NULL;
3337 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3338 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3339 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3341 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3342 f44b1f58 2020-02-02 tracey } else {
3343 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3344 f44b1f58 2020-02-02 tracey lineno = 1;
3346 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3349 f44b1f58 2020-02-02 tracey while (1) {
3350 f44b1f58 2020-02-02 tracey char *line = NULL;
3351 f44b1f58 2020-02-02 tracey off_t offset;
3352 f44b1f58 2020-02-02 tracey size_t len;
3354 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3355 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3356 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3357 f44b1f58 2020-02-02 tracey free(line);
3361 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3362 f44b1f58 2020-02-02 tracey lineno = 1;
3364 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3367 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3368 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3369 f44b1f58 2020-02-02 tracey free(line);
3370 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3372 f44b1f58 2020-02-02 tracey free(line);
3373 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3374 41605754 2020-11-12 stsp if (line &&
3375 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3376 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3377 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3378 f44b1f58 2020-02-02 tracey free(line);
3381 f44b1f58 2020-02-02 tracey free(line);
3382 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3383 f44b1f58 2020-02-02 tracey lineno++;
3385 f44b1f58 2020-02-02 tracey lineno--;
3388 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3389 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3390 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3393 f44b1f58 2020-02-02 tracey return NULL;
3396 48ae06ee 2018-10-18 stsp static const struct got_error *
3397 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3398 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3399 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3400 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3402 48ae06ee 2018-10-18 stsp const struct got_error *err;
3403 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3405 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3407 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3408 15a94983 2018-12-23 stsp int type1, type2;
3409 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3411 15a94983 2018-12-23 stsp return err;
3412 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3414 15a94983 2018-12-23 stsp return err;
3416 15a94983 2018-12-23 stsp if (type1 != type2)
3417 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3419 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3420 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3421 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3422 f44b1f58 2020-02-02 tracey s->repo = repo;
3423 f44b1f58 2020-02-02 tracey s->id1 = id1;
3424 f44b1f58 2020-02-02 tracey s->id2 = id2;
3425 3dbaef42 2020-11-24 stsp s->label1 = label1;
3426 3dbaef42 2020-11-24 stsp s->label2 = label2;
3428 15a94983 2018-12-23 stsp if (id1) {
3429 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3430 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3431 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3433 5465d566 2020-02-01 tracey s->id1 = NULL;
3435 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3436 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3437 5465d566 2020-02-01 tracey free(s->id1);
3438 5465d566 2020-02-01 tracey s->id1 = NULL;
3439 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3441 5465d566 2020-02-01 tracey s->f = NULL;
3442 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3443 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3444 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3445 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3446 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3447 5465d566 2020-02-01 tracey s->log_view = log_view;
3448 5465d566 2020-02-01 tracey s->repo = repo;
3450 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3451 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3452 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3453 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3454 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3456 6d17833f 2019-11-08 stsp return err;
3457 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3458 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3459 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3460 6d17833f 2019-11-08 stsp if (err) {
3461 5465d566 2020-02-01 tracey free_colors(&s->colors);
3462 6d17833f 2019-11-08 stsp return err;
3464 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3465 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3466 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3467 6d17833f 2019-11-08 stsp if (err) {
3468 5465d566 2020-02-01 tracey free_colors(&s->colors);
3469 6d17833f 2019-11-08 stsp return err;
3472 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3473 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3474 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3475 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3476 6d17833f 2019-11-08 stsp if (err) {
3477 5465d566 2020-02-01 tracey free_colors(&s->colors);
3478 6d17833f 2019-11-08 stsp return err;
3481 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3482 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3483 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3484 11b20872 2019-11-08 stsp if (err) {
3485 5465d566 2020-02-01 tracey free_colors(&s->colors);
3486 11b20872 2019-11-08 stsp return err;
3489 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3490 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3491 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3492 11b20872 2019-11-08 stsp if (err) {
3493 5465d566 2020-02-01 tracey free_colors(&s->colors);
3494 11b20872 2019-11-08 stsp return err;
3498 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3499 78756c87 2020-11-24 stsp if (err) {
3500 78756c87 2020-11-24 stsp free(s->id1);
3501 78756c87 2020-11-24 stsp s->id1 = NULL;
3502 78756c87 2020-11-24 stsp free(s->id2);
3503 78756c87 2020-11-24 stsp s->id2 = NULL;
3504 78756c87 2020-11-24 stsp free_colors(&s->colors);
3505 78756c87 2020-11-24 stsp return err;
3508 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3509 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3510 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3512 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3513 fe621944 2020-11-10 stsp s->nlines = 0;
3514 5465d566 2020-02-01 tracey err = create_diff(s);
3515 48ae06ee 2018-10-18 stsp if (err) {
3516 5465d566 2020-02-01 tracey free(s->id1);
3517 5465d566 2020-02-01 tracey s->id1 = NULL;
3518 5465d566 2020-02-01 tracey free(s->id2);
3519 5465d566 2020-02-01 tracey s->id2 = NULL;
3520 78756c87 2020-11-24 stsp free_colors(&s->colors);
3521 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3522 48ae06ee 2018-10-18 stsp return err;
3525 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3526 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3527 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3528 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3529 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3531 5dc9f4bc 2018-08-04 stsp return NULL;
3534 e5a0f69f 2018-08-18 stsp static const struct got_error *
3535 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3537 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3538 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3540 5465d566 2020-02-01 tracey free(s->id1);
3541 5465d566 2020-02-01 tracey s->id1 = NULL;
3542 5465d566 2020-02-01 tracey free(s->id2);
3543 5465d566 2020-02-01 tracey s->id2 = NULL;
3544 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3545 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3546 5465d566 2020-02-01 tracey free_colors(&s->colors);
3547 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3548 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3549 fe621944 2020-11-10 stsp s->nlines = 0;
3550 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3551 e5a0f69f 2018-08-18 stsp return err;
3554 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3555 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3557 a3404814 2018-09-02 stsp const struct got_error *err;
3558 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3559 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3560 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3562 a3404814 2018-09-02 stsp if (s->id1) {
3563 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3565 a3404814 2018-09-02 stsp return err;
3566 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3568 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3570 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3572 a3404814 2018-09-02 stsp return err;
3573 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3575 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3576 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3577 a3404814 2018-09-02 stsp free(id_str1);
3578 a3404814 2018-09-02 stsp free(id_str2);
3579 a3404814 2018-09-02 stsp return err;
3581 a3404814 2018-09-02 stsp free(id_str1);
3582 a3404814 2018-09-02 stsp free(id_str2);
3584 fe621944 2020-11-10 stsp return draw_file(view, s->f, s->first_displayed_line, s->nlines,
3585 fe621944 2020-11-10 stsp s->line_offsets, s->selected_line, view->nlines,
3586 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, header, &s->colors,
3587 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
3590 15a087fe 2019-02-21 stsp static const struct got_error *
3591 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3592 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3594 d7a04538 2019-02-21 stsp const struct got_error *err;
3595 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3596 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3597 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3599 15a087fe 2019-02-21 stsp free(s->id2);
3600 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3601 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3602 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3604 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3606 d7a04538 2019-02-21 stsp return err;
3607 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3608 15a087fe 2019-02-21 stsp free(s->id1);
3609 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3610 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3611 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3612 15a087fe 2019-02-21 stsp return NULL;
3615 0cf4efb1 2018-09-29 stsp static const struct got_error *
3616 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3617 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3619 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3620 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3621 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3622 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3625 e5a0f69f 2018-08-18 stsp switch (ch) {
3628 3dbaef42 2020-11-24 stsp if (ch == 'a')
3629 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3630 3dbaef42 2020-11-24 stsp if (ch == 'w')
3631 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3632 64453f7e 2020-11-21 stsp wclear(view->window);
3633 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3634 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3635 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3636 64453f7e 2020-11-21 stsp err = create_diff(s);
3639 1e37a5c2 2019-05-12 jcs case KEY_UP:
3640 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3641 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3643 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3644 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3645 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3648 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3649 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3650 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3653 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3654 1e37a5c2 2019-05-12 jcs if (!s->eof)
3655 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3657 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3658 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3660 00ba99a7 2019-05-12 jcs if (s->eof)
3663 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3664 1e37a5c2 2019-05-12 jcs char *line;
3665 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3666 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3667 1e37a5c2 2019-05-12 jcs if (line == NULL)
3672 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3673 1e37a5c2 2019-05-12 jcs s->diff_context--;
3674 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3675 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3676 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3677 27829c9e 2020-11-21 stsp s->nlines) {
3678 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3679 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3684 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3685 1e37a5c2 2019-05-12 jcs s->diff_context++;
3686 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3687 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3692 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3694 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3695 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3696 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3697 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3700 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3705 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3709 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3710 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3712 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3713 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;
3721 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3722 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3723 ffe38506 2020-12-01 naddy err = trigger_log_thread(s->log_view, 1);
3727 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3732 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3733 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3736 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3740 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3741 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3743 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3744 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3750 bcbd79e2 2018-08-19 stsp return err;
3753 4ed7e80c 2018-05-20 stsp static const struct got_error *
3754 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3756 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3757 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3758 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3759 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3760 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3761 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3762 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3763 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3764 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3765 3dbaef42 2020-11-24 stsp const char *errstr;
3766 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3768 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3769 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3770 eb6600df 2019-01-04 stsp NULL) == -1)
3771 26ed57b2 2018-05-19 stsp err(1, "pledge");
3773 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3774 26ed57b2 2018-05-19 stsp switch (ch) {
3776 64453f7e 2020-11-21 stsp force_text_diff = 1;
3779 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3781 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3782 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3784 09b5bff8 2020-02-23 naddy case 'r':
3785 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3786 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3787 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3789 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3792 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3795 17020d27 2019-03-07 stsp usage_diff();
3796 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3800 26ed57b2 2018-05-19 stsp argc -= optind;
3801 26ed57b2 2018-05-19 stsp argv += optind;
3803 26ed57b2 2018-05-19 stsp if (argc == 0) {
3804 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3805 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3806 15a94983 2018-12-23 stsp id_str1 = argv[0];
3807 15a94983 2018-12-23 stsp id_str2 = argv[1];
3809 26ed57b2 2018-05-19 stsp usage_diff();
3811 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3812 a273ac94 2020-02-23 naddy if (cwd == NULL)
3813 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3815 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3816 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3817 a273ac94 2020-02-23 naddy goto done;
3819 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3820 a273ac94 2020-02-23 naddy if (worktree)
3821 a273ac94 2020-02-23 naddy repo_path =
3822 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3824 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3826 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3827 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3828 a273ac94 2020-02-23 naddy goto done;
3831 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3832 eb6600df 2019-01-04 stsp if (error)
3833 eb6600df 2019-01-04 stsp goto done;
3835 a273ac94 2020-02-23 naddy init_curses();
3837 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3838 26ed57b2 2018-05-19 stsp if (error)
3839 26ed57b2 2018-05-19 stsp goto done;
3841 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3842 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3843 26ed57b2 2018-05-19 stsp if (error)
3844 26ed57b2 2018-05-19 stsp goto done;
3846 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3847 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3848 26ed57b2 2018-05-19 stsp if (error)
3849 26ed57b2 2018-05-19 stsp goto done;
3851 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3852 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3853 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3854 ea5e7bb5 2018-08-01 stsp goto done;
3856 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3857 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3858 5dc9f4bc 2018-08-04 stsp if (error)
3859 5dc9f4bc 2018-08-04 stsp goto done;
3860 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3862 3dbaef42 2020-11-24 stsp free(label1);
3863 3dbaef42 2020-11-24 stsp free(label2);
3864 c02c541e 2019-03-29 stsp free(repo_path);
3865 a273ac94 2020-02-23 naddy free(cwd);
3867 921be706 2019-06-28 stsp got_repo_close(repo);
3868 a273ac94 2020-02-23 naddy if (worktree)
3869 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3870 26ed57b2 2018-05-19 stsp return error;
3873 4ed7e80c 2018-05-20 stsp __dead static void
3874 9f7d7167 2018-04-29 stsp usage_blame(void)
3877 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3878 9f7d7167 2018-04-29 stsp getprogname());
3882 84451b3e 2018-07-10 stsp struct tog_blame_line {
3883 84451b3e 2018-07-10 stsp int annotated;
3884 84451b3e 2018-07-10 stsp struct got_object_id *id;
3887 4ed7e80c 2018-05-20 stsp static const struct got_error *
3888 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3889 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3890 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3891 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3892 41605754 2020-11-12 stsp struct tog_colors *colors, int matched_line, regmatch_t *regmatch)
3894 84451b3e 2018-07-10 stsp const struct got_error *err;
3895 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3896 84451b3e 2018-07-10 stsp char *line;
3897 84451b3e 2018-07-10 stsp size_t len;
3898 84451b3e 2018-07-10 stsp wchar_t *wline;
3899 27a741e5 2019-09-11 stsp int width;
3900 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3901 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3902 ab089a2a 2018-07-12 stsp char *id_str;
3903 11b20872 2019-11-08 stsp struct tog_color *tc;
3905 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3907 ab089a2a 2018-07-12 stsp return err;
3909 84451b3e 2018-07-10 stsp rewind(f);
3910 f7d12f7e 2018-08-01 stsp werase(view->window);
3912 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3913 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3914 ab089a2a 2018-07-12 stsp free(id_str);
3915 ab089a2a 2018-07-12 stsp return err;
3918 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3919 ab089a2a 2018-07-12 stsp free(line);
3920 2550e4c3 2018-07-13 stsp line = NULL;
3922 1cae65b4 2019-09-22 stsp return err;
3923 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3924 a3404814 2018-09-02 stsp wstandout(view->window);
3925 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3927 11b20872 2019-11-08 stsp wattr_on(view->window,
3928 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3929 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3931 11b20872 2019-11-08 stsp wattr_off(view->window,
3932 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3933 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3934 a3404814 2018-09-02 stsp wstandend(view->window);
3935 2550e4c3 2018-07-13 stsp free(wline);
3936 2550e4c3 2018-07-13 stsp wline = NULL;
3937 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3938 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3940 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3941 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3942 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3943 ab089a2a 2018-07-12 stsp free(id_str);
3944 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3946 ab089a2a 2018-07-12 stsp free(id_str);
3947 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3948 3f60a8ef 2018-07-10 stsp free(line);
3949 2550e4c3 2018-07-13 stsp line = NULL;
3951 3f60a8ef 2018-07-10 stsp return err;
3952 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3953 2550e4c3 2018-07-13 stsp free(wline);
3954 2550e4c3 2018-07-13 stsp wline = NULL;
3955 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3956 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3959 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3960 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3961 84451b3e 2018-07-10 stsp if (line == NULL) {
3965 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3966 84451b3e 2018-07-10 stsp free(line);
3970 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3971 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3973 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3974 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3975 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3976 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3977 27a741e5 2019-09-11 stsp !(view->focussed &&
3978 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3979 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3980 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3981 8d0fe45a 2019-08-12 stsp char *id_str;
3982 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3983 8d0fe45a 2019-08-12 stsp if (err) {
3984 8d0fe45a 2019-08-12 stsp free(line);
3985 8d0fe45a 2019-08-12 stsp return err;
3987 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3989 11b20872 2019-11-08 stsp wattr_on(view->window,
3990 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3991 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3993 11b20872 2019-11-08 stsp wattr_off(view->window,
3994 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3995 8d0fe45a 2019-08-12 stsp free(id_str);
3996 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3998 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3999 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4002 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4003 ee41ec32 2018-07-10 stsp prev_id = NULL;
4006 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
4007 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4008 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4010 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4011 41605754 2020-11-12 stsp width = 9;
4012 41605754 2020-11-12 stsp wline = wcsdup(L"");
4013 41605754 2020-11-12 stsp if (wline == NULL) {
4014 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4015 41605754 2020-11-12 stsp free(line);
4016 41605754 2020-11-12 stsp return err;
4018 41605754 2020-11-12 stsp } else if (*first_displayed_line + nprinted == matched_line &&
4019 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4020 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4021 41605754 2020-11-12 stsp view->window, regmatch);
4022 41605754 2020-11-12 stsp if (err) {
4023 41605754 2020-11-12 stsp free(line);
4024 41605754 2020-11-12 stsp return err;
4026 41605754 2020-11-12 stsp width += 9;
4028 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4029 41605754 2020-11-12 stsp view->ncols - 9, 9);
4030 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4031 41605754 2020-11-12 stsp free(wline);
4032 41605754 2020-11-12 stsp wline = NULL;
4033 41605754 2020-11-12 stsp width += 9;
4036 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4037 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4038 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4039 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
4040 84451b3e 2018-07-10 stsp free(line);
4042 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
4044 1a57306a 2018-09-02 stsp view_vborder(view);
4046 84451b3e 2018-07-10 stsp return NULL;
4049 84451b3e 2018-07-10 stsp static const struct got_error *
4050 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4052 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4053 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4054 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4055 1a76625f 2018-10-22 stsp int errcode;
4057 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4058 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4059 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4061 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4062 1a76625f 2018-10-22 stsp if (errcode)
4063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4065 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4066 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4067 d68a0a7d 2018-07-10 stsp goto done;
4070 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4071 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4073 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4074 d68a0a7d 2018-07-10 stsp if (line->annotated)
4075 d68a0a7d 2018-07-10 stsp goto done;
4077 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4078 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4079 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4080 84451b3e 2018-07-10 stsp goto done;
4082 84451b3e 2018-07-10 stsp line->annotated = 1;
4084 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4085 1a76625f 2018-10-22 stsp if (errcode)
4086 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4087 84451b3e 2018-07-10 stsp return err;
4090 84451b3e 2018-07-10 stsp static void *
4091 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4093 18430de3 2018-07-10 stsp const struct got_error *err;
4094 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4095 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4096 1a76625f 2018-10-22 stsp int errcode;
4098 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4100 61266923 2020-01-14 stsp return (void *)err;
4102 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4103 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4104 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4105 fc06ba56 2019-08-22 stsp err = NULL;
4107 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4108 1a76625f 2018-10-22 stsp if (errcode)
4109 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4110 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4112 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4113 c9beca56 2018-07-22 stsp ta->repo = NULL;
4114 c9beca56 2018-07-22 stsp *ta->complete = 1;
4116 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4117 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4118 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4120 18430de3 2018-07-10 stsp return (void *)err;
4123 245d91c1 2018-07-12 stsp static struct got_object_id *
4124 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4125 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4127 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4129 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4130 8d0fe45a 2019-08-12 stsp return NULL;
4132 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4133 245d91c1 2018-07-12 stsp if (!line->annotated)
4134 245d91c1 2018-07-12 stsp return NULL;
4136 245d91c1 2018-07-12 stsp return line->id;
4139 b880a918 2018-07-10 stsp static const struct got_error *
4140 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4142 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4145 245d91c1 2018-07-12 stsp if (blame->thread) {
4146 1a76625f 2018-10-22 stsp int errcode;
4147 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4148 1a76625f 2018-10-22 stsp if (errcode)
4149 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4150 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4151 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4152 1a76625f 2018-10-22 stsp if (errcode)
4153 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4155 1a76625f 2018-10-22 stsp if (errcode)
4156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4157 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4158 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4159 245d91c1 2018-07-12 stsp err = NULL;
4160 245d91c1 2018-07-12 stsp blame->thread = NULL;
4162 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4163 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4164 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4166 245d91c1 2018-07-12 stsp if (blame->f) {
4167 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4168 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4169 245d91c1 2018-07-12 stsp blame->f = NULL;
4171 57670559 2018-12-23 stsp if (blame->lines) {
4172 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4173 57670559 2018-12-23 stsp free(blame->lines[i].id);
4174 57670559 2018-12-23 stsp free(blame->lines);
4175 57670559 2018-12-23 stsp blame->lines = NULL;
4177 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4178 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4180 245d91c1 2018-07-12 stsp return err;
4183 245d91c1 2018-07-12 stsp static const struct got_error *
4184 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4186 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4187 fc06ba56 2019-08-22 stsp int *done = arg;
4188 fc06ba56 2019-08-22 stsp int errcode;
4190 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4191 fc06ba56 2019-08-22 stsp if (errcode)
4192 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4193 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4195 fc06ba56 2019-08-22 stsp if (*done)
4196 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4198 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4199 fc06ba56 2019-08-22 stsp if (errcode)
4200 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4201 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4203 fc06ba56 2019-08-22 stsp return err;
4206 fc06ba56 2019-08-22 stsp static const struct got_error *
4207 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4209 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4210 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4211 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4212 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4213 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4214 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4215 15a94983 2018-12-23 stsp int obj_type;
4217 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4218 a5388363 2020-12-01 naddy s->path);
4220 15a94983 2018-12-23 stsp return err;
4222 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4224 84451b3e 2018-07-10 stsp goto done;
4226 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4227 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4228 84451b3e 2018-07-10 stsp goto done;
4231 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4233 a70480e0 2018-06-23 stsp goto done;
4234 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4235 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4236 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4237 84451b3e 2018-07-10 stsp goto done;
4239 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4240 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4241 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4242 84451b3e 2018-07-10 stsp goto done;
4244 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4245 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4246 b02560ec 2019-08-19 stsp blame->nlines--;
4248 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4249 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4250 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4251 84451b3e 2018-07-10 stsp goto done;
4254 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4256 bd24772e 2018-07-11 stsp goto done;
4258 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4259 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4260 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4261 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4262 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4263 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4264 245d91c1 2018-07-12 stsp goto done;
4266 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4268 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4269 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4270 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4271 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4272 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4273 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4274 a5388363 2020-12-01 naddy s->blame_complete = 0;
4278 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4279 27d434c2 2018-09-15 stsp free(obj_id);
4281 245d91c1 2018-07-12 stsp stop_blame(blame);
4282 245d91c1 2018-07-12 stsp return err;
4285 245d91c1 2018-07-12 stsp static const struct got_error *
4286 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4287 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4289 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4290 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4292 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4294 c4843652 2019-08-12 stsp s->path = strdup(path);
4295 c4843652 2019-08-12 stsp if (s->path == NULL)
4296 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4298 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4299 c4843652 2019-08-12 stsp if (err) {
4300 c4843652 2019-08-12 stsp free(s->path);
4301 7cbe629d 2018-08-04 stsp return err;
4304 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4305 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4306 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4307 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4308 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4309 fb2756b9 2018-08-04 stsp s->repo = repo;
4310 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4311 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4313 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4314 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4315 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4316 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4318 11b20872 2019-11-08 stsp return err;
4321 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4322 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4323 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4324 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4325 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4327 a5388363 2020-12-01 naddy return run_blame(view);
4330 e5a0f69f 2018-08-18 stsp static const struct got_error *
4331 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4333 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4334 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4336 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4337 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4339 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4340 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4341 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4342 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4343 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4346 e5a0f69f 2018-08-18 stsp free(s->path);
4347 11b20872 2019-11-08 stsp free_colors(&s->colors);
4349 e5a0f69f 2018-08-18 stsp return err;
4352 7cbe629d 2018-08-04 stsp static const struct got_error *
4353 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4355 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4357 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4358 6c4c42e0 2019-06-24 stsp return NULL;
4361 6c4c42e0 2019-06-24 stsp static const struct got_error *
4362 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4364 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4365 6c4c42e0 2019-06-24 stsp int lineno;
4367 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4368 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4369 6c4c42e0 2019-06-24 stsp return NULL;
4372 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4373 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4374 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4376 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4378 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4379 6c4c42e0 2019-06-24 stsp lineno = 1;
4381 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4384 6c4c42e0 2019-06-24 stsp while (1) {
4385 6c4c42e0 2019-06-24 stsp char *line = NULL;
4386 6c4c42e0 2019-06-24 stsp off_t offset;
4387 6c4c42e0 2019-06-24 stsp size_t len;
4389 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4390 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4391 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 6c4c42e0 2019-06-24 stsp free(line);
4396 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4397 6c4c42e0 2019-06-24 stsp lineno = 1;
4399 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4402 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4403 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4404 6c4c42e0 2019-06-24 stsp free(line);
4405 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4407 6c4c42e0 2019-06-24 stsp free(line);
4408 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4409 41605754 2020-11-12 stsp if (line &&
4410 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4411 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4412 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4413 6c4c42e0 2019-06-24 stsp free(line);
4416 6c4c42e0 2019-06-24 stsp free(line);
4417 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4423 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4424 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4425 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4428 6c4c42e0 2019-06-24 stsp return NULL;
4431 6c4c42e0 2019-06-24 stsp static const struct got_error *
4432 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4434 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4435 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4436 2b380cc8 2018-10-24 stsp int errcode;
4438 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4439 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4440 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4441 2b380cc8 2018-10-24 stsp if (errcode)
4442 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4444 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4447 51fe7530 2019-08-19 stsp if (s->blame_complete)
4448 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4450 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4451 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4452 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4453 41605754 2020-11-12 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors,
4454 41605754 2020-11-12 stsp s->matched_line, &view->regmatch);
4456 669b5ffa 2018-10-07 stsp view_vborder(view);
4457 e5a0f69f 2018-08-18 stsp return err;
4460 e5a0f69f 2018-08-18 stsp static const struct got_error *
4461 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4462 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4464 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4465 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4466 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4467 669b5ffa 2018-10-07 stsp int begin_x = 0;
4469 e5a0f69f 2018-08-18 stsp switch (ch) {
4471 1e37a5c2 2019-05-12 jcs s->done = 1;
4474 1e37a5c2 2019-05-12 jcs case KEY_UP:
4475 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4476 1e37a5c2 2019-05-12 jcs s->selected_line--;
4477 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4478 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4479 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4481 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4482 ea025d1d 2020-02-22 naddy case CTRL('b'):
4483 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4484 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4487 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4488 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4489 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4491 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4494 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4495 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4496 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4497 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4498 1e37a5c2 2019-05-12 jcs s->selected_line++;
4499 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4500 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4501 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4504 1e37a5c2 2019-05-12 jcs case 'p': {
4505 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4506 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4507 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4508 1e37a5c2 2019-05-12 jcs if (id == NULL)
4510 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4511 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4512 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4513 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4514 1e37a5c2 2019-05-12 jcs int obj_type;
4515 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4516 1e37a5c2 2019-05-12 jcs s->repo, id);
4519 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4520 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4521 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4522 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4525 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4526 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4527 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4528 e5a0f69f 2018-08-18 stsp if (err) {
4529 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4530 1e37a5c2 2019-05-12 jcs err = NULL;
4531 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4534 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4536 1e37a5c2 2019-05-12 jcs free(blob_id);
4537 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4538 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4539 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4542 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4544 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4546 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4547 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4549 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4554 1e37a5c2 2019-05-12 jcs s->done = 1;
4555 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4556 1e37a5c2 2019-05-12 jcs s->done = 0;
4557 1e37a5c2 2019-05-12 jcs if (thread_err)
4559 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4560 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4561 a5388363 2020-12-01 naddy err = run_blame(view);
4566 1e37a5c2 2019-05-12 jcs case 'B': {
4567 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4568 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4569 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4571 1e37a5c2 2019-05-12 jcs s->done = 1;
4572 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4573 1e37a5c2 2019-05-12 jcs s->done = 0;
4574 1e37a5c2 2019-05-12 jcs if (thread_err)
4576 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4577 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4578 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4579 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4580 a5388363 2020-12-01 naddy err = run_blame(view);
4585 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4586 1e37a5c2 2019-05-12 jcs case '\r': {
4587 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4588 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4589 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4590 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4591 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4592 1e37a5c2 2019-05-12 jcs if (id == NULL)
4594 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4597 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4598 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4599 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4600 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4601 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4602 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4603 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4604 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4607 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4608 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4609 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4611 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4614 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4615 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4618 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4620 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4623 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4624 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4626 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4631 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4632 ea025d1d 2020-02-22 naddy case CTRL('f'):
4634 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4635 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4636 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4639 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4640 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4641 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4642 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4645 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4646 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4647 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4648 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4650 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4651 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4652 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4654 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4655 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4656 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4657 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4663 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4666 a70480e0 2018-06-23 stsp static const struct got_error *
4667 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4669 a70480e0 2018-06-23 stsp const struct got_error *error;
4670 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4671 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4672 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4673 0587e10c 2020-07-23 stsp char *link_target = NULL;
4674 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4675 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4677 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4679 a70480e0 2018-06-23 stsp #ifndef PROFILE
4680 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4681 8e94dd5b 2019-01-04 stsp NULL) == -1)
4682 a70480e0 2018-06-23 stsp err(1, "pledge");
4685 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4686 a70480e0 2018-06-23 stsp switch (ch) {
4688 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4691 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4692 69069811 2018-08-02 stsp if (repo_path == NULL)
4693 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4697 17020d27 2019-03-07 stsp usage_blame();
4698 a70480e0 2018-06-23 stsp /* NOTREACHED */
4702 a70480e0 2018-06-23 stsp argc -= optind;
4703 a70480e0 2018-06-23 stsp argv += optind;
4705 f135c941 2020-02-20 stsp if (argc != 1)
4706 a70480e0 2018-06-23 stsp usage_blame();
4708 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4709 6962eb72 2020-02-20 stsp if (cwd == NULL)
4710 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4712 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4713 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4714 f135c941 2020-02-20 stsp goto done;
4716 69069811 2018-08-02 stsp if (repo_path == NULL) {
4717 f135c941 2020-02-20 stsp if (worktree)
4718 eb41ed75 2019-02-05 stsp repo_path =
4719 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4721 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4723 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4724 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4725 f135c941 2020-02-20 stsp goto done;
4728 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4729 c02c541e 2019-03-29 stsp if (error != NULL)
4730 8e94dd5b 2019-01-04 stsp goto done;
4732 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4733 f135c941 2020-02-20 stsp worktree);
4734 c02c541e 2019-03-29 stsp if (error)
4735 92205607 2019-01-04 stsp goto done;
4737 f135c941 2020-02-20 stsp init_curses();
4739 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4740 eb41ed75 2019-02-05 stsp if (error)
4741 69069811 2018-08-02 stsp goto done;
4743 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4744 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4745 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4746 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4747 a70480e0 2018-06-23 stsp if (error != NULL)
4748 66b4983c 2018-06-23 stsp goto done;
4749 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4750 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4752 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4753 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4755 a19e88aa 2018-06-23 stsp if (error != NULL)
4756 8b473291 2019-02-21 stsp goto done;
4758 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4759 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4760 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4761 e1cd8fed 2018-08-01 stsp goto done;
4764 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4765 0587e10c 2020-07-23 stsp commit_id, repo);
4766 7cbe629d 2018-08-04 stsp if (error)
4767 7cbe629d 2018-08-04 stsp goto done;
4769 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4770 78756c87 2020-11-24 stsp commit_id, repo);
4771 0587e10c 2020-07-23 stsp if (error)
4772 0587e10c 2020-07-23 stsp goto done;
4773 12314ad4 2019-08-31 stsp if (worktree) {
4774 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4775 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4776 12314ad4 2019-08-31 stsp worktree = NULL;
4778 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4780 69069811 2018-08-02 stsp free(repo_path);
4781 f135c941 2020-02-20 stsp free(in_repo_path);
4782 0587e10c 2020-07-23 stsp free(link_target);
4783 69069811 2018-08-02 stsp free(cwd);
4784 a70480e0 2018-06-23 stsp free(commit_id);
4785 eb41ed75 2019-02-05 stsp if (worktree)
4786 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4788 a70480e0 2018-06-23 stsp got_repo_close(repo);
4789 a70480e0 2018-06-23 stsp return error;
4792 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4793 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4794 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4795 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4796 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4797 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4798 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4799 0d6c6ee3 2020-05-20 stsp int isroot, struct tog_colors *colors, struct got_repository *repo)
4801 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4802 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4803 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4804 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4805 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4807 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4809 f7d12f7e 2018-08-01 stsp werase(view->window);
4811 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4812 ffd1d5e5 2018-06-23 stsp return NULL;
4814 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4816 ffd1d5e5 2018-06-23 stsp return err;
4817 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4818 a3404814 2018-09-02 stsp wstandout(view->window);
4819 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4821 11b20872 2019-11-08 stsp wattr_on(view->window,
4822 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4823 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4825 11b20872 2019-11-08 stsp wattr_off(view->window,
4826 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4827 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4828 a3404814 2018-09-02 stsp wstandend(view->window);
4829 2550e4c3 2018-07-13 stsp free(wline);
4830 2550e4c3 2018-07-13 stsp wline = NULL;
4831 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4832 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4833 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4834 ffd1d5e5 2018-06-23 stsp return NULL;
4835 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4837 ce52c690 2018-06-23 stsp return err;
4838 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4839 2550e4c3 2018-07-13 stsp free(wline);
4840 2550e4c3 2018-07-13 stsp wline = NULL;
4841 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4842 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4843 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4844 ffd1d5e5 2018-06-23 stsp return NULL;
4845 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4846 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4847 a1eca9bb 2018-06-23 stsp return NULL;
4849 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4850 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4851 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4852 0cf4efb1 2018-09-29 stsp if (view->focussed)
4853 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4854 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4856 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4857 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4858 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4859 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4860 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4861 ffd1d5e5 2018-06-23 stsp return NULL;
4865 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4868 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4869 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4870 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4871 848d6979 2019-08-12 stsp const char *modestr = "";
4872 56e0773d 2019-11-28 stsp mode_t mode;
4874 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4875 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4877 1d13200f 2018-07-12 stsp if (show_ids) {
4878 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4879 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4881 638f9024 2019-05-13 stsp return got_error_from_errno(
4882 230a42bd 2019-05-11 jcs "got_object_id_str");
4884 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4885 63c5ca5d 2019-08-24 stsp modestr = "$";
4886 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4889 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4890 0d6c6ee3 2020-05-20 stsp te, repo);
4891 0d6c6ee3 2020-05-20 stsp if (err) {
4892 0d6c6ee3 2020-05-20 stsp free(id_str);
4893 0d6c6ee3 2020-05-20 stsp return err;
4895 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4896 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4897 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4899 848d6979 2019-08-12 stsp modestr = "@";
4901 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4902 848d6979 2019-08-12 stsp modestr = "/";
4903 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4904 848d6979 2019-08-12 stsp modestr = "*";
4905 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4906 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4907 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4908 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4909 1d13200f 2018-07-12 stsp free(id_str);
4910 0d6c6ee3 2020-05-20 stsp free(link_target);
4911 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4913 1d13200f 2018-07-12 stsp free(id_str);
4914 0d6c6ee3 2020-05-20 stsp free(link_target);
4915 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4916 ffd1d5e5 2018-06-23 stsp if (err) {
4917 ffd1d5e5 2018-06-23 stsp free(line);
4920 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4921 0cf4efb1 2018-09-29 stsp if (view->focussed)
4922 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4923 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4925 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4927 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4928 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4929 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4931 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4932 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4933 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4934 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4935 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4936 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4937 ffd1d5e5 2018-06-23 stsp free(line);
4938 2550e4c3 2018-07-13 stsp free(wline);
4939 2550e4c3 2018-07-13 stsp wline = NULL;
4941 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4942 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4943 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4947 ffd1d5e5 2018-06-23 stsp return err;
4950 ffd1d5e5 2018-06-23 stsp static void
4951 694d3271 2020-12-01 naddy tree_scroll_up(struct tog_view *view, int maxscroll)
4953 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4954 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4955 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4956 fa86c4bf 2020-11-29 stsp int i = 0;
4958 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4961 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4962 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4963 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4964 fa86c4bf 2020-11-29 stsp if (!isroot)
4965 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4968 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4969 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4973 fa86c4bf 2020-11-29 stsp static void
4974 694d3271 2020-12-01 naddy tree_scroll_down(struct tog_view *view, int maxscroll)
4976 694d3271 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4977 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4978 ffd1d5e5 2018-06-23 stsp int n = 0;
4980 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4981 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4982 694d3271 2020-12-01 naddy s->first_displayed_entry);
4984 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4986 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4987 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4988 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4989 768394f3 2019-01-24 stsp if (last) {
4990 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4991 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4996 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4997 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4998 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5000 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5001 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5002 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5004 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5005 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5006 56e0773d 2019-11-28 stsp + 1 /* slash */;
5008 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5010 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5011 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5012 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5014 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5015 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5016 d9765a41 2018-06-23 stsp while (pt) {
5017 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5018 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5019 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5020 cb2ebc8a 2018-06-23 stsp goto done;
5022 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5023 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5024 cb2ebc8a 2018-06-23 stsp goto done;
5026 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5029 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5030 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5031 ce52c690 2018-06-23 stsp goto done;
5035 ce52c690 2018-06-23 stsp if (err) {
5036 ce52c690 2018-06-23 stsp free(*path);
5037 ce52c690 2018-06-23 stsp *path = NULL;
5039 ce52c690 2018-06-23 stsp return err;
5042 ce52c690 2018-06-23 stsp static const struct got_error *
5043 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5044 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5045 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5047 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5048 ce52c690 2018-06-23 stsp char *path;
5049 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5051 a0de39f3 2019-08-09 stsp *new_view = NULL;
5053 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5055 ce52c690 2018-06-23 stsp return err;
5057 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5058 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5059 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5060 83ce39e3 2019-08-12 stsp goto done;
5063 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5064 e5a0f69f 2018-08-18 stsp if (err) {
5065 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5066 fc06ba56 2019-08-22 stsp err = NULL;
5067 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5069 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5071 83ce39e3 2019-08-12 stsp free(path);
5072 69efd4c4 2018-07-18 stsp return err;
5075 69efd4c4 2018-07-18 stsp static const struct got_error *
5076 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5077 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5078 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5080 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5081 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5082 69efd4c4 2018-07-18 stsp char *path;
5084 a0de39f3 2019-08-09 stsp *new_view = NULL;
5086 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5087 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5088 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5090 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5092 69efd4c4 2018-07-18 stsp return err;
5094 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5096 e5a0f69f 2018-08-18 stsp view_close(log_view);
5098 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5099 cb2ebc8a 2018-06-23 stsp free(path);
5100 cb2ebc8a 2018-06-23 stsp return err;
5103 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5104 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5105 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5107 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5108 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5109 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5111 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5113 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5114 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5115 ffd1d5e5 2018-06-23 stsp goto done;
5117 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5118 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5119 ffd1d5e5 2018-06-23 stsp goto done;
5122 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5123 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5124 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5125 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5126 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5127 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5128 6484ec90 2018-09-29 stsp goto done;
5130 fb2756b9 2018-08-04 stsp s->repo = repo;
5132 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5134 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5135 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5136 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5137 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5139 c0b01bdb 2019-11-08 stsp goto done;
5140 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5141 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5142 c0b01bdb 2019-11-08 stsp if (err) {
5143 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5144 c0b01bdb 2019-11-08 stsp goto done;
5146 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5147 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5148 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5149 c0b01bdb 2019-11-08 stsp if (err) {
5150 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5151 c0b01bdb 2019-11-08 stsp goto done;
5154 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5155 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5156 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5157 11b20872 2019-11-08 stsp if (err) {
5158 11b20872 2019-11-08 stsp free_colors(&s->colors);
5159 11b20872 2019-11-08 stsp goto done;
5162 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5163 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5164 c0b01bdb 2019-11-08 stsp if (err) {
5165 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5166 c0b01bdb 2019-11-08 stsp goto done;
5170 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5171 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5172 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5173 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5174 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5176 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5177 6484ec90 2018-09-29 stsp if (err) {
5178 fb2756b9 2018-08-04 stsp free(s->tree_label);
5179 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5181 ad80ab7b 2018-08-04 stsp return err;
5184 e5a0f69f 2018-08-18 stsp static const struct got_error *
5185 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5187 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5189 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5190 fb2756b9 2018-08-04 stsp free(s->tree_label);
5191 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5192 6484ec90 2018-09-29 stsp free(s->commit_id);
5193 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5194 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5195 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5196 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5197 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5198 ad80ab7b 2018-08-04 stsp free(parent);
5201 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5202 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5203 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5204 7c32bd05 2019-06-22 stsp return NULL;
5207 7c32bd05 2019-06-22 stsp static const struct got_error *
5208 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5210 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5212 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5213 7c32bd05 2019-06-22 stsp return NULL;
5216 7c32bd05 2019-06-22 stsp static int
5217 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5219 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5221 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5225 7c32bd05 2019-06-22 stsp static const struct got_error *
5226 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5228 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5229 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5231 7c32bd05 2019-06-22 stsp if (!view->searching) {
5232 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 7c32bd05 2019-06-22 stsp return NULL;
5236 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5237 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5238 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5239 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5240 56e0773d 2019-11-28 stsp s->selected_entry);
5242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5244 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5245 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5247 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5248 56e0773d 2019-11-28 stsp s->selected_entry);
5251 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5252 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5254 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5257 7c32bd05 2019-06-22 stsp while (1) {
5258 56e0773d 2019-11-28 stsp if (te == NULL) {
5259 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5260 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5261 ac66afb8 2019-06-24 stsp return NULL;
5263 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5264 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5266 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5269 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5271 56e0773d 2019-11-28 stsp s->matched_entry = te;
5275 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5276 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5278 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5281 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5282 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5283 7c32bd05 2019-06-22 stsp s->selected = 0;
5286 e5a0f69f 2018-08-18 stsp return NULL;
5289 ad80ab7b 2018-08-04 stsp static const struct got_error *
5290 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5292 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5293 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5294 e5a0f69f 2018-08-18 stsp char *parent_path;
5296 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5298 e5a0f69f 2018-08-18 stsp return err;
5300 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5301 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5302 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5303 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5304 0d6c6ee3 2020-05-20 stsp &s->colors, s->repo);
5305 e5a0f69f 2018-08-18 stsp free(parent_path);
5307 669b5ffa 2018-10-07 stsp view_vborder(view);
5308 e5a0f69f 2018-08-18 stsp return err;
5311 e5a0f69f 2018-08-18 stsp static const struct got_error *
5312 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5313 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5315 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5316 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5317 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5318 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5320 e5a0f69f 2018-08-18 stsp switch (ch) {
5322 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5325 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5327 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5328 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5329 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5330 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5331 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5332 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5334 1e37a5c2 2019-05-12 jcs return err;
5335 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5337 1e37a5c2 2019-05-12 jcs view_close(log_view);
5340 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5341 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5343 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5346 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5347 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5348 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5349 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5350 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5351 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5352 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5353 152c1c93 2020-11-29 stsp if (err) {
5354 152c1c93 2020-11-29 stsp view_close(ref_view);
5355 152c1c93 2020-11-29 stsp return err;
5357 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5358 152c1c93 2020-11-29 stsp err = view_close_child(view);
5360 152c1c93 2020-11-29 stsp return err;
5361 152c1c93 2020-11-29 stsp err = view_set_child(view, ref_view);
5362 152c1c93 2020-11-29 stsp if (err) {
5363 152c1c93 2020-11-29 stsp view_close(ref_view);
5366 152c1c93 2020-11-29 stsp *focus_view = ref_view;
5367 152c1c93 2020-11-29 stsp view->child_focussed = 1;
5369 152c1c93 2020-11-29 stsp *new_view = ref_view;
5372 1e37a5c2 2019-05-12 jcs case KEY_UP:
5373 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5374 1e37a5c2 2019-05-12 jcs s->selected--;
5377 694d3271 2020-12-01 naddy tree_scroll_up(view, 1);
5379 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5380 ea025d1d 2020-02-22 naddy case CTRL('b'):
5381 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5382 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5383 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5384 fa86c4bf 2020-11-29 stsp s->selected = 0;
5386 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5387 fa86c4bf 2020-11-29 stsp s->selected = 0;
5389 694d3271 2020-12-01 naddy tree_scroll_up(view, MAX(0, view->nlines - 3));
5392 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5393 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5394 1e37a5c2 2019-05-12 jcs s->selected++;
5397 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5399 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5401 694d3271 2020-12-01 naddy tree_scroll_down(view, 1);
5403 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5404 ea025d1d 2020-02-22 naddy case CTRL('f'):
5405 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5407 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5408 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5409 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5412 694d3271 2020-12-01 naddy tree_scroll_down(view, view->nlines - 3);
5414 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5416 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5417 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5418 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5419 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5420 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5422 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5423 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5425 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5426 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5427 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5428 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5429 1e37a5c2 2019-05-12 jcs s->selected_entry =
5430 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5431 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5432 1e37a5c2 2019-05-12 jcs free(parent);
5433 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5434 56e0773d 2019-11-28 stsp s->selected_entry))) {
5435 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5436 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5437 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5440 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5441 941e9f74 2019-05-21 stsp if (err) {
5442 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5445 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5446 56e0773d 2019-11-28 stsp s->selected_entry))) {
5447 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5448 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5449 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5451 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5452 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5453 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5456 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5457 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5459 669b5ffa 2018-10-07 stsp return err;
5460 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5461 669b5ffa 2018-10-07 stsp if (err) {
5462 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5465 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5466 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5468 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5471 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5472 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5473 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5479 ffd1d5e5 2018-06-23 stsp return err;
5482 ffd1d5e5 2018-06-23 stsp __dead static void
5483 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5486 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5487 ffd1d5e5 2018-06-23 stsp getprogname());
5491 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5492 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5494 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5495 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5496 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5497 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5498 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5499 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5500 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5501 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5503 5221c383 2018-08-01 stsp struct tog_view *view;
5505 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5506 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5507 d188b9a6 2019-01-04 stsp NULL) == -1)
5508 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5511 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5512 ffd1d5e5 2018-06-23 stsp switch (ch) {
5514 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5517 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5518 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5519 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5523 e99e2d15 2020-11-24 naddy usage_tree();
5524 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5528 ffd1d5e5 2018-06-23 stsp argc -= optind;
5529 ffd1d5e5 2018-06-23 stsp argv += optind;
5531 55cccc34 2020-02-20 stsp if (argc > 1)
5532 e99e2d15 2020-11-24 naddy usage_tree();
5534 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5535 55cccc34 2020-02-20 stsp if (cwd == NULL)
5536 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5538 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5539 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5540 55cccc34 2020-02-20 stsp goto done;
5542 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5543 55cccc34 2020-02-20 stsp if (worktree)
5544 52185f70 2019-02-05 stsp repo_path =
5545 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5547 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5549 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5550 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5551 55cccc34 2020-02-20 stsp goto done;
5554 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5555 c02c541e 2019-03-29 stsp if (error != NULL)
5556 52185f70 2019-02-05 stsp goto done;
5558 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5559 55cccc34 2020-02-20 stsp repo, worktree);
5560 55cccc34 2020-02-20 stsp if (error)
5561 55cccc34 2020-02-20 stsp goto done;
5563 55cccc34 2020-02-20 stsp init_curses();
5565 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5566 c02c541e 2019-03-29 stsp if (error)
5567 52185f70 2019-02-05 stsp goto done;
5569 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5570 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5571 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5572 55cccc34 2020-02-20 stsp if (error)
5573 ffd1d5e5 2018-06-23 stsp goto done;
5575 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5576 55cccc34 2020-02-20 stsp if (error)
5577 ffd1d5e5 2018-06-23 stsp goto done;
5579 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5580 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5581 55cccc34 2020-02-20 stsp if (error)
5582 8b473291 2019-02-21 stsp goto done;
5584 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5585 5221c383 2018-08-01 stsp if (view == NULL) {
5586 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5587 5221c383 2018-08-01 stsp goto done;
5589 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5590 ad80ab7b 2018-08-04 stsp if (error)
5591 ad80ab7b 2018-08-04 stsp goto done;
5592 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5593 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5594 55cccc34 2020-02-20 stsp in_repo_path, repo);
5595 55cccc34 2020-02-20 stsp if (error)
5596 55cccc34 2020-02-20 stsp goto done;
5599 55cccc34 2020-02-20 stsp if (worktree) {
5600 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5601 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5602 55cccc34 2020-02-20 stsp worktree = NULL;
5604 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5606 52185f70 2019-02-05 stsp free(repo_path);
5607 e4a0e26d 2020-02-20 stsp free(cwd);
5608 ffd1d5e5 2018-06-23 stsp free(commit_id);
5609 ffd1d5e5 2018-06-23 stsp if (commit)
5610 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5612 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5614 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5615 ffd1d5e5 2018-06-23 stsp return error;
5618 6458efa5 2020-11-24 stsp static const struct got_error *
5619 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5621 6458efa5 2020-11-24 stsp const struct got_error *err;
5622 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5623 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5625 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5626 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5628 6458efa5 2020-11-24 stsp return err;
5630 6458efa5 2020-11-24 stsp s->nrefs = 0;
5631 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5632 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5635 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5636 6458efa5 2020-11-24 stsp if (re == NULL)
5637 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5639 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5640 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5641 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5644 6458efa5 2020-11-24 stsp return NULL;
5648 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5650 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5652 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5653 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5654 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5657 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5660 6458efa5 2020-11-24 stsp static const struct got_error *
5661 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5663 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5664 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5666 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5667 6458efa5 2020-11-24 stsp s->repo = repo;
5669 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5670 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5671 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5673 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5675 6458efa5 2020-11-24 stsp return err;
5677 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5679 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5680 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5681 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5682 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5684 6458efa5 2020-11-24 stsp goto done;
5686 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5687 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5688 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5690 6458efa5 2020-11-24 stsp goto done;
5692 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5693 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5694 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5696 6458efa5 2020-11-24 stsp goto done;
5699 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5700 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5701 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5702 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5703 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5706 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5707 6458efa5 2020-11-24 stsp return err;
5710 6458efa5 2020-11-24 stsp static const struct got_error *
5711 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5713 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5715 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5716 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5718 6458efa5 2020-11-24 stsp return NULL;
5721 6458efa5 2020-11-24 stsp static const struct got_error *
5722 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5723 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5725 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5726 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5727 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5728 6458efa5 2020-11-24 stsp int obj_type;
5730 c42c9805 2020-11-24 stsp *commit_id = NULL;
5732 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5734 6458efa5 2020-11-24 stsp return err;
5736 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5738 6458efa5 2020-11-24 stsp goto done;
5740 6458efa5 2020-11-24 stsp switch (obj_type) {
5741 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5742 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5744 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5745 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5747 6458efa5 2020-11-24 stsp goto done;
5748 c42c9805 2020-11-24 stsp free(obj_id);
5749 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5750 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5752 6458efa5 2020-11-24 stsp goto done;
5753 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5754 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5755 c42c9805 2020-11-24 stsp goto done;
5757 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5758 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5759 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5760 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5761 c42c9805 2020-11-24 stsp goto done;
5765 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5771 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5772 c42c9805 2020-11-24 stsp if (err) {
5773 c42c9805 2020-11-24 stsp free(*commit_id);
5774 c42c9805 2020-11-24 stsp *commit_id = NULL;
5776 c42c9805 2020-11-24 stsp return err;
5779 c42c9805 2020-11-24 stsp static const struct got_error *
5780 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5781 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5783 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5784 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5785 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5787 c42c9805 2020-11-24 stsp *new_view = NULL;
5789 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5790 c42c9805 2020-11-24 stsp if (err) {
5791 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5792 c42c9805 2020-11-24 stsp return err;
5794 c42c9805 2020-11-24 stsp return NULL;
5797 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5798 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5799 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5800 6458efa5 2020-11-24 stsp goto done;
5803 6458efa5 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5806 6458efa5 2020-11-24 stsp view_close(log_view);
5808 6458efa5 2020-11-24 stsp *new_view = log_view;
5809 c42c9805 2020-11-24 stsp free(commit_id);
5810 6458efa5 2020-11-24 stsp return err;
5813 ce5b7c56 2019-07-09 stsp static void
5814 694d3271 2020-12-01 naddy ref_scroll_up(struct tog_view *view, int maxscroll)
5816 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5817 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5818 34ba6917 2020-11-30 stsp int i = 0;
5820 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5823 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5824 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5825 34ba6917 2020-11-30 stsp if (re == NULL)
5827 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5828 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5832 34ba6917 2020-11-30 stsp static void
5833 694d3271 2020-12-01 naddy ref_scroll_down(struct tog_view *view, int maxscroll)
5835 694d3271 2020-12-01 naddy struct tog_ref_view_state *s = &view->state.ref;
5836 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5837 6458efa5 2020-11-24 stsp int n = 0;
5839 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5840 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5842 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5844 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5845 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5846 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5847 6458efa5 2020-11-24 stsp if (last) {
5848 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5849 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5854 6458efa5 2020-11-24 stsp static const struct got_error *
5855 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5857 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5859 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5860 6458efa5 2020-11-24 stsp return NULL;
5863 6458efa5 2020-11-24 stsp static int
5864 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5866 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5868 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5872 6458efa5 2020-11-24 stsp static const struct got_error *
5873 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5875 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5876 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5878 6458efa5 2020-11-24 stsp if (!view->searching) {
5879 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5880 6458efa5 2020-11-24 stsp return NULL;
5883 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5884 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5885 6458efa5 2020-11-24 stsp if (s->selected_entry)
5886 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5888 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5889 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5891 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5892 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5894 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5895 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5898 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5899 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5901 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5904 6458efa5 2020-11-24 stsp while (1) {
5905 6458efa5 2020-11-24 stsp if (re == NULL) {
5906 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5907 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5908 6458efa5 2020-11-24 stsp return NULL;
5910 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5911 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5913 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5916 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5917 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5918 6458efa5 2020-11-24 stsp s->matched_entry = re;
5922 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5923 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5925 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5928 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5929 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5930 6458efa5 2020-11-24 stsp s->selected = 0;
5933 6458efa5 2020-11-24 stsp return NULL;
5936 6458efa5 2020-11-24 stsp static const struct got_error *
5937 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5939 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5940 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5941 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5942 6458efa5 2020-11-24 stsp char *line = NULL;
5943 6458efa5 2020-11-24 stsp wchar_t *wline;
5944 6458efa5 2020-11-24 stsp struct tog_color *tc;
5945 6458efa5 2020-11-24 stsp int width, n;
5946 6458efa5 2020-11-24 stsp int limit = view->nlines;
5948 6458efa5 2020-11-24 stsp werase(view->window);
5950 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5952 6458efa5 2020-11-24 stsp if (limit == 0)
5953 6458efa5 2020-11-24 stsp return NULL;
5955 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5957 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5958 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5959 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5961 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5962 6458efa5 2020-11-24 stsp if (err) {
5963 6458efa5 2020-11-24 stsp free(line);
5964 6458efa5 2020-11-24 stsp return err;
5966 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5967 6458efa5 2020-11-24 stsp wstandout(view->window);
5968 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5969 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5970 6458efa5 2020-11-24 stsp wstandend(view->window);
5971 6458efa5 2020-11-24 stsp free(wline);
5972 6458efa5 2020-11-24 stsp wline = NULL;
5973 6458efa5 2020-11-24 stsp free(line);
5974 6458efa5 2020-11-24 stsp line = NULL;
5975 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5976 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5977 6458efa5 2020-11-24 stsp if (--limit <= 0)
5978 6458efa5 2020-11-24 stsp return NULL;
5981 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5982 6458efa5 2020-11-24 stsp char *line = NULL;
5984 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5985 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5986 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5987 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5988 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5989 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5990 6458efa5 2020-11-24 stsp struct got_object_id *id;
5991 6458efa5 2020-11-24 stsp char *id_str;
5992 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5994 6458efa5 2020-11-24 stsp return err;
5995 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5996 6458efa5 2020-11-24 stsp if (err) {
5998 6458efa5 2020-11-24 stsp return err;
6000 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6001 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6002 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6004 6458efa5 2020-11-24 stsp free(id_str);
6005 6458efa5 2020-11-24 stsp return err;
6008 6458efa5 2020-11-24 stsp free(id_str);
6010 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6011 6458efa5 2020-11-24 stsp if (line == NULL)
6012 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6015 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6016 6458efa5 2020-11-24 stsp if (err) {
6017 6458efa5 2020-11-24 stsp free(line);
6018 6458efa5 2020-11-24 stsp return err;
6020 6458efa5 2020-11-24 stsp if (n == s->selected) {
6021 6458efa5 2020-11-24 stsp if (view->focussed)
6022 6458efa5 2020-11-24 stsp wstandout(view->window);
6023 6458efa5 2020-11-24 stsp s->selected_entry = re;
6025 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6027 6458efa5 2020-11-24 stsp wattr_on(view->window,
6028 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6029 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6031 6458efa5 2020-11-24 stsp wattr_off(view->window,
6032 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6033 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6034 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6035 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6036 6458efa5 2020-11-24 stsp wstandend(view->window);
6037 6458efa5 2020-11-24 stsp free(line);
6038 6458efa5 2020-11-24 stsp free(wline);
6039 6458efa5 2020-11-24 stsp wline = NULL;
6041 6458efa5 2020-11-24 stsp s->ndisplayed++;
6042 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6045 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6048 6458efa5 2020-11-24 stsp view_vborder(view);
6049 6458efa5 2020-11-24 stsp return err;
6052 6458efa5 2020-11-24 stsp static const struct got_error *
6053 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6054 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6056 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6057 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6058 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6059 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6061 c42c9805 2020-11-24 stsp *new_view = NULL;
6063 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6064 c42c9805 2020-11-24 stsp if (err) {
6065 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6066 c42c9805 2020-11-24 stsp return err;
6068 c42c9805 2020-11-24 stsp return NULL;
6071 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6073 c42c9805 2020-11-24 stsp goto done;
6075 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6077 c42c9805 2020-11-24 stsp goto done;
6079 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6080 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6081 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6082 c42c9805 2020-11-24 stsp goto done;
6085 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6087 c42c9805 2020-11-24 stsp goto done;
6089 c42c9805 2020-11-24 stsp *new_view = tree_view;
6091 c42c9805 2020-11-24 stsp free(commit_id);
6092 c42c9805 2020-11-24 stsp free(tree_id);
6093 c42c9805 2020-11-24 stsp if (err) {
6095 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6097 c42c9805 2020-11-24 stsp return err;
6099 c42c9805 2020-11-24 stsp static const struct got_error *
6100 6458efa5 2020-11-24 stsp input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6101 6458efa5 2020-11-24 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
6103 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6104 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6105 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6106 34ba6917 2020-11-30 stsp int begin_x = 0;
6108 6458efa5 2020-11-24 stsp switch (ch) {
6110 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6112 6458efa5 2020-11-24 stsp case KEY_ENTER:
6113 6458efa5 2020-11-24 stsp case '\r':
6114 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6116 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6117 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6118 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6120 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6121 6458efa5 2020-11-24 stsp err = view_close_child(view);
6123 6458efa5 2020-11-24 stsp return err;
6124 6458efa5 2020-11-24 stsp err = view_set_child(view, log_view);
6125 6458efa5 2020-11-24 stsp if (err) {
6126 6458efa5 2020-11-24 stsp view_close(log_view);
6129 6458efa5 2020-11-24 stsp *focus_view = log_view;
6130 6458efa5 2020-11-24 stsp view->child_focussed = 1;
6132 6458efa5 2020-11-24 stsp *new_view = log_view;
6135 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6137 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6138 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6139 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6141 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6143 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6144 c42c9805 2020-11-24 stsp err = view_close_child(view);
6146 c42c9805 2020-11-24 stsp return err;
6147 c42c9805 2020-11-24 stsp err = view_set_child(view, tree_view);
6148 c42c9805 2020-11-24 stsp if (err) {
6149 c42c9805 2020-11-24 stsp view_close(tree_view);
6152 c42c9805 2020-11-24 stsp *focus_view = tree_view;
6153 c42c9805 2020-11-24 stsp view->child_focussed = 1;
6155 c42c9805 2020-11-24 stsp *new_view = tree_view;
6158 6458efa5 2020-11-24 stsp case KEY_UP:
6159 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6160 6458efa5 2020-11-24 stsp s->selected--;
6163 694d3271 2020-12-01 naddy ref_scroll_up(view, 1);
6165 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6166 6458efa5 2020-11-24 stsp case CTRL('b'):
6167 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6168 34ba6917 2020-11-30 stsp s->selected = 0;
6169 694d3271 2020-12-01 naddy ref_scroll_up(view, MAX(0, view->nlines - 1));
6172 6458efa5 2020-11-24 stsp case KEY_DOWN:
6173 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6174 6458efa5 2020-11-24 stsp s->selected++;
6177 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6178 6458efa5 2020-11-24 stsp /* can't scroll any further */
6180 694d3271 2020-12-01 naddy ref_scroll_down(view, 1);
6182 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6183 6458efa5 2020-11-24 stsp case CTRL('f'):
6184 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6185 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6186 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6187 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6190 694d3271 2020-12-01 naddy ref_scroll_down(view, view->nlines - 1);
6192 6458efa5 2020-11-24 stsp case CTRL('l'):
6193 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6194 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6196 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6197 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6198 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6204 6458efa5 2020-11-24 stsp return err;
6207 6458efa5 2020-11-24 stsp __dead static void
6208 6458efa5 2020-11-24 stsp usage_ref(void)
6211 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6212 6458efa5 2020-11-24 stsp getprogname());
6216 6458efa5 2020-11-24 stsp static const struct got_error *
6217 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6219 6458efa5 2020-11-24 stsp const struct got_error *error;
6220 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6221 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6222 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6224 6458efa5 2020-11-24 stsp struct tog_view *view;
6226 6458efa5 2020-11-24 stsp #ifndef PROFILE
6227 6458efa5 2020-11-24 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6228 6458efa5 2020-11-24 stsp NULL) == -1)
6229 6458efa5 2020-11-24 stsp err(1, "pledge");
6232 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6233 6458efa5 2020-11-24 stsp switch (ch) {
6235 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6236 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6237 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6241 e99e2d15 2020-11-24 naddy usage_ref();
6242 6458efa5 2020-11-24 stsp /* NOTREACHED */
6246 6458efa5 2020-11-24 stsp argc -= optind;
6247 6458efa5 2020-11-24 stsp argv += optind;
6249 6458efa5 2020-11-24 stsp if (argc > 1)
6250 e99e2d15 2020-11-24 naddy usage_ref();
6252 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6253 6458efa5 2020-11-24 stsp if (cwd == NULL)
6254 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6256 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6257 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6258 6458efa5 2020-11-24 stsp goto done;
6260 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6261 6458efa5 2020-11-24 stsp if (worktree)
6262 6458efa5 2020-11-24 stsp repo_path =
6263 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6265 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6267 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6268 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6269 6458efa5 2020-11-24 stsp goto done;
6272 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6273 6458efa5 2020-11-24 stsp if (error != NULL)
6274 6458efa5 2020-11-24 stsp goto done;
6276 6458efa5 2020-11-24 stsp init_curses();
6278 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6279 6458efa5 2020-11-24 stsp if (error)
6280 6458efa5 2020-11-24 stsp goto done;
6282 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6283 6458efa5 2020-11-24 stsp if (view == NULL) {
6284 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6285 6458efa5 2020-11-24 stsp goto done;
6288 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6289 6458efa5 2020-11-24 stsp if (error)
6290 6458efa5 2020-11-24 stsp goto done;
6292 6458efa5 2020-11-24 stsp if (worktree) {
6293 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6294 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6295 6458efa5 2020-11-24 stsp worktree = NULL;
6297 6458efa5 2020-11-24 stsp error = view_loop(view);
6299 6458efa5 2020-11-24 stsp free(repo_path);
6300 6458efa5 2020-11-24 stsp free(cwd);
6302 6458efa5 2020-11-24 stsp got_repo_close(repo);
6303 6458efa5 2020-11-24 stsp return error;
6306 6458efa5 2020-11-24 stsp static void
6307 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6311 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6312 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6313 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6314 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6316 6879ba42 2020-10-01 naddy fputc('\n', fp);
6319 4ed7e80c 2018-05-20 stsp __dead static void
6320 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6322 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6324 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6325 6879ba42 2020-10-01 naddy getprogname());
6326 6879ba42 2020-10-01 naddy if (hflag) {
6327 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6328 6879ba42 2020-10-01 naddy list_commands(fp);
6330 6879ba42 2020-10-01 naddy exit(status);
6333 c2301be8 2018-04-30 stsp static char **
6334 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6336 ee85c5e8 2020-02-29 stsp va_list ap;
6337 c2301be8 2018-04-30 stsp char **argv;
6340 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6342 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6343 c2301be8 2018-04-30 stsp if (argv == NULL)
6344 c2301be8 2018-04-30 stsp err(1, "calloc");
6345 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6346 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6347 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6348 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6351 ee85c5e8 2020-02-29 stsp va_end(ap);
6352 c2301be8 2018-04-30 stsp return argv;
6356 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6357 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6358 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6359 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6360 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6362 ee85c5e8 2020-02-29 stsp static const struct got_error *
6363 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6365 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6366 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6367 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6368 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6369 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6370 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6371 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6373 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6374 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6375 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6377 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6378 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6379 ee85c5e8 2020-02-29 stsp goto done;
6381 ee85c5e8 2020-02-29 stsp if (worktree)
6382 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6384 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6385 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6386 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6387 ee85c5e8 2020-02-29 stsp goto done;
6390 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6391 ee85c5e8 2020-02-29 stsp if (error != NULL)
6392 ee85c5e8 2020-02-29 stsp goto done;
6394 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6395 ee85c5e8 2020-02-29 stsp repo, worktree);
6396 ee85c5e8 2020-02-29 stsp if (error)
6397 ee85c5e8 2020-02-29 stsp goto done;
6399 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6400 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6401 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6402 ee85c5e8 2020-02-29 stsp if (error)
6403 ee85c5e8 2020-02-29 stsp goto done;
6405 ee85c5e8 2020-02-29 stsp if (worktree) {
6406 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6407 ee85c5e8 2020-02-29 stsp worktree = NULL;
6410 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6411 ee85c5e8 2020-02-29 stsp if (error) {
6412 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6413 ee85c5e8 2020-02-29 stsp goto done;
6414 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6415 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6416 6879ba42 2020-10-01 naddy usage(1, 1);
6417 ee85c5e8 2020-02-29 stsp /* not reached */
6420 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6421 ee85c5e8 2020-02-29 stsp repo = NULL;
6423 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6424 ee85c5e8 2020-02-29 stsp if (error)
6425 ee85c5e8 2020-02-29 stsp goto done;
6427 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6429 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6430 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6433 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6434 ee85c5e8 2020-02-29 stsp if (worktree)
6435 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6437 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6438 ee85c5e8 2020-02-29 stsp free(commit_id);
6439 ee85c5e8 2020-02-29 stsp free(cwd);
6440 ee85c5e8 2020-02-29 stsp free(repo_path);
6441 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6442 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6444 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6445 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6446 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6448 ee85c5e8 2020-02-29 stsp return error;
6452 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6454 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6455 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6456 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6457 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6458 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6459 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6460 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6463 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6465 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6466 9f7d7167 2018-04-29 stsp switch (ch) {
6468 9f7d7167 2018-04-29 stsp hflag = 1;
6471 53ccebc2 2019-07-30 stsp Vflag = 1;
6474 6879ba42 2020-10-01 naddy usage(hflag, 1);
6475 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6479 9f7d7167 2018-04-29 stsp argc -= optind;
6480 9f7d7167 2018-04-29 stsp argv += optind;
6481 9814e6a3 2020-09-27 naddy optind = 1;
6482 c2301be8 2018-04-30 stsp optreset = 1;
6484 53ccebc2 2019-07-30 stsp if (Vflag) {
6485 53ccebc2 2019-07-30 stsp got_version_print_str();
6486 6879ba42 2020-10-01 naddy return 0;
6489 c2301be8 2018-04-30 stsp if (argc == 0) {
6490 f29d3e89 2018-06-23 stsp if (hflag)
6491 6879ba42 2020-10-01 naddy usage(hflag, 0);
6492 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6493 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6495 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6499 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6500 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6501 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6502 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6503 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6509 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6510 ee85c5e8 2020-02-29 stsp if (argc != 1)
6511 6879ba42 2020-10-01 naddy usage(0, 1);
6512 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6513 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6515 ee85c5e8 2020-02-29 stsp if (hflag)
6516 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6518 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6522 b46c1e04 2020-09-20 naddy putchar('\n');
6523 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6525 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6526 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6527 a2f4a359 2020-02-28 stsp free(cmd_argv);
6530 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6531 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);