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 9cd7cbd1 2020-12-07 stsp 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 bf30f154 2020-12-07 naddy #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 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
400 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
401 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
402 6458efa5 2020-11-24 stsp struct tog_colors colors;
405 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
406 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
407 6458efa5 2020-11-24 stsp struct got_reference *ref;
411 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
413 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
414 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
418 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
419 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
420 6458efa5 2020-11-24 stsp struct got_repository *repo;
421 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
422 bddb1296 2019-11-08 stsp struct tog_colors colors;
426 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
428 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
429 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
430 669b5ffa 2018-10-07 stsp * there is enough screen estate.
432 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
433 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
435 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
436 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
437 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
439 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
440 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
442 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
444 cc3c9aac 2018-08-01 stsp struct tog_view {
445 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
446 26ed57b2 2018-05-19 stsp WINDOW *window;
447 26ed57b2 2018-05-19 stsp PANEL *panel;
448 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
449 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
450 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
452 669b5ffa 2018-10-07 stsp struct tog_view *parent;
453 669b5ffa 2018-10-07 stsp struct tog_view *child;
456 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
457 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
458 e78dc838 2020-12-04 stsp * between parent and child.
459 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
460 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
461 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
462 e78dc838 2020-12-04 stsp * situations.
464 e78dc838 2020-12-04 stsp int focus_child;
466 5dc9f4bc 2018-08-04 stsp /* type-specific state */
467 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
469 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
470 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
471 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
472 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
473 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
476 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
477 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
478 e78dc838 2020-12-04 stsp struct tog_view *, int);
479 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
481 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
482 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
483 60493ae3 2019-06-20 stsp int searching;
484 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
485 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
486 60493ae3 2019-06-20 stsp int search_next_done;
487 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
488 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
489 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
490 1803e47f 2019-06-22 stsp regex_t regex;
491 41605754 2020-11-12 stsp regmatch_t regmatch;
494 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
495 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
496 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
497 78756c87 2020-11-24 stsp struct got_repository *);
498 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
499 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
500 e78dc838 2020-12-04 stsp struct tog_view *, int);
501 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
502 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
503 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
505 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
506 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
507 78756c87 2020-11-24 stsp const char *, const char *, int);
508 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
509 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
510 e78dc838 2020-12-04 stsp struct tog_view *, int);
511 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
512 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
513 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
515 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
516 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
517 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
518 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
519 e78dc838 2020-12-04 stsp struct tog_view *, int);
520 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
521 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
522 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
524 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
525 4e97c21c 2020-12-06 stsp struct got_tree_object *, struct got_object_id *, const char *,
526 4e97c21c 2020-12-06 stsp struct got_repository *);
527 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
528 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
529 e78dc838 2020-12-04 stsp struct tog_view *, int);
530 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
531 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
532 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
534 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
535 6458efa5 2020-11-24 stsp struct got_repository *);
536 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
537 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
538 e78dc838 2020-12-04 stsp struct tog_view *, int);
539 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
540 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
541 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
543 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
544 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
545 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
547 25791caa 2018-10-24 stsp static void
548 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
550 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
553 83baff54 2019-08-12 stsp static void
554 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
556 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
559 61266923 2020-01-14 stsp static void
560 61266923 2020-01-14 stsp tog_sigcont(int signo)
562 61266923 2020-01-14 stsp tog_sigcont_received = 1;
565 e5a0f69f 2018-08-18 stsp static const struct got_error *
566 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
570 669b5ffa 2018-10-07 stsp if (view->child) {
571 669b5ffa 2018-10-07 stsp view_close(view->child);
572 669b5ffa 2018-10-07 stsp view->child = NULL;
574 e5a0f69f 2018-08-18 stsp if (view->close)
575 e5a0f69f 2018-08-18 stsp err = view->close(view);
576 ea5e7bb5 2018-08-01 stsp if (view->panel)
577 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
578 ea5e7bb5 2018-08-01 stsp if (view->window)
579 ea5e7bb5 2018-08-01 stsp delwin(view->window);
580 ea5e7bb5 2018-08-01 stsp free(view);
581 e5a0f69f 2018-08-18 stsp return err;
584 ea5e7bb5 2018-08-01 stsp static struct tog_view *
585 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
586 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
588 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
590 ea5e7bb5 2018-08-01 stsp if (view == NULL)
591 ea5e7bb5 2018-08-01 stsp return NULL;
593 d6b05b5b 2018-08-04 stsp view->type = type;
594 f7d12f7e 2018-08-01 stsp view->lines = LINES;
595 f7d12f7e 2018-08-01 stsp view->cols = COLS;
596 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
597 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
598 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
599 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
600 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
601 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
602 96a765a8 2018-08-04 stsp view_close(view);
603 ea5e7bb5 2018-08-01 stsp return NULL;
605 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
606 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
607 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
608 96a765a8 2018-08-04 stsp view_close(view);
609 ea5e7bb5 2018-08-01 stsp return NULL;
612 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
613 ea5e7bb5 2018-08-01 stsp return view;
617 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
619 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
621 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
624 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
626 5c60c32a 2018-10-18 stsp static const struct got_error *
627 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
629 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
631 5c60c32a 2018-10-18 stsp view->begin_y = 0;
632 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
633 5c60c32a 2018-10-18 stsp view->nlines = LINES;
634 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
635 5c60c32a 2018-10-18 stsp view->lines = LINES;
636 5c60c32a 2018-10-18 stsp view->cols = COLS;
637 5c60c32a 2018-10-18 stsp err = view_resize(view);
639 5c60c32a 2018-10-18 stsp return err;
641 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
642 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
644 5c60c32a 2018-10-18 stsp return NULL;
647 5c60c32a 2018-10-18 stsp static const struct got_error *
648 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
650 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
652 5c60c32a 2018-10-18 stsp view->begin_x = 0;
653 5c60c32a 2018-10-18 stsp view->begin_y = 0;
654 5c60c32a 2018-10-18 stsp view->nlines = LINES;
655 5c60c32a 2018-10-18 stsp view->ncols = COLS;
656 5c60c32a 2018-10-18 stsp view->lines = LINES;
657 5c60c32a 2018-10-18 stsp view->cols = COLS;
658 5c60c32a 2018-10-18 stsp err = view_resize(view);
660 5c60c32a 2018-10-18 stsp return err;
662 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
663 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
665 5c60c32a 2018-10-18 stsp return NULL;
669 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
671 5c60c32a 2018-10-18 stsp return view->parent == NULL;
674 4d8c2215 2018-08-19 stsp static const struct got_error *
675 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
677 f7d12f7e 2018-08-01 stsp int nlines, ncols;
679 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
680 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
682 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
684 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
685 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
687 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
689 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
690 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
691 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
692 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
693 25791caa 2018-10-24 stsp wclear(view->window);
695 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
696 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
697 0cf4efb1 2018-09-29 stsp view->lines = LINES;
698 0cf4efb1 2018-09-29 stsp view->cols = COLS;
700 6e3e5d9c 2018-10-18 stsp if (view->child) {
701 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
702 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
703 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
704 5c60c32a 2018-10-18 stsp if (view->child->focussed)
705 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
707 5c60c32a 2018-10-18 stsp show_panel(view->panel);
709 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
710 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
714 5c60c32a 2018-10-18 stsp return NULL;
717 669b5ffa 2018-10-07 stsp static const struct got_error *
718 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
720 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
722 669b5ffa 2018-10-07 stsp if (view->child == NULL)
723 669b5ffa 2018-10-07 stsp return NULL;
725 669b5ffa 2018-10-07 stsp err = view_close(view->child);
726 669b5ffa 2018-10-07 stsp view->child = NULL;
727 669b5ffa 2018-10-07 stsp return err;
730 72a9cb46 2020-12-03 stsp static void
731 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
733 669b5ffa 2018-10-07 stsp view->child = child;
734 669b5ffa 2018-10-07 stsp child->parent = view;
738 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
740 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
743 34bc9ec9 2019-02-22 stsp static void
744 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
746 25791caa 2018-10-24 stsp int cols, lines;
747 25791caa 2018-10-24 stsp struct winsize size;
749 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
750 25791caa 2018-10-24 stsp cols = 80; /* Default */
751 25791caa 2018-10-24 stsp lines = 24;
753 25791caa 2018-10-24 stsp cols = size.ws_col;
754 25791caa 2018-10-24 stsp lines = size.ws_row;
756 25791caa 2018-10-24 stsp resize_term(lines, cols);
759 2b49a8ae 2019-06-22 stsp static const struct got_error *
760 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
762 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
763 2b49a8ae 2019-06-22 stsp char pattern[1024];
766 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
767 2b49a8ae 2019-06-22 stsp return NULL;
769 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
770 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
772 2b49a8ae 2019-06-22 stsp nocbreak();
774 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
777 2b49a8ae 2019-06-22 stsp if (ret == ERR)
778 2b49a8ae 2019-06-22 stsp return NULL;
780 2b49a8ae 2019-06-22 stsp if (view->searching) {
781 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
782 2b49a8ae 2019-06-22 stsp view->searching = 0;
785 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
786 7c32bd05 2019-06-22 stsp err = view->search_start(view);
788 7c32bd05 2019-06-22 stsp regfree(&view->regex);
789 7c32bd05 2019-06-22 stsp return err;
791 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
792 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
793 2b49a8ae 2019-06-22 stsp view->search_next(view);
796 2b49a8ae 2019-06-22 stsp return NULL;
799 0cf4efb1 2018-09-29 stsp static const struct got_error *
800 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
801 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
803 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
804 669b5ffa 2018-10-07 stsp struct tog_view *v;
805 1a76625f 2018-10-22 stsp int ch, errcode;
807 e5a0f69f 2018-08-18 stsp *new = NULL;
809 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
810 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
811 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
812 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
814 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
815 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
816 82954512 2020-02-03 stsp if (errcode)
817 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
818 82954512 2020-02-03 stsp "pthread_mutex_unlock");
819 82954512 2020-02-03 stsp pthread_yield();
820 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
821 82954512 2020-02-03 stsp if (errcode)
822 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
823 82954512 2020-02-03 stsp "pthread_mutex_lock");
824 60493ae3 2019-06-20 stsp view->search_next(view);
825 60493ae3 2019-06-20 stsp return NULL;
828 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
829 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
830 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
831 1a76625f 2018-10-22 stsp if (errcode)
832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
833 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
834 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
835 1a76625f 2018-10-22 stsp if (errcode)
836 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
837 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
839 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
840 25791caa 2018-10-24 stsp tog_resizeterm();
841 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
842 61266923 2020-01-14 stsp tog_sigcont_received = 0;
843 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
844 25791caa 2018-10-24 stsp err = view_resize(v);
846 25791caa 2018-10-24 stsp return err;
847 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
849 25791caa 2018-10-24 stsp return err;
850 cdfcfb03 2020-12-06 stsp if (v->child) {
851 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
853 cdfcfb03 2020-12-06 stsp return err;
854 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
855 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
857 cdfcfb03 2020-12-06 stsp return err;
862 e5a0f69f 2018-08-18 stsp switch (ch) {
866 1e37a5c2 2019-05-12 jcs if (view->child) {
867 e78dc838 2020-12-04 stsp view->focussed = 0;
868 e78dc838 2020-12-04 stsp view->child->focussed = 1;
869 e78dc838 2020-12-04 stsp view->focus_child = 1;
870 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
871 e78dc838 2020-12-04 stsp view->focussed = 0;
872 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
873 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
877 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
878 9970f7fc 2020-12-03 stsp view->dying = 1;
884 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
885 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
887 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
888 e78dc838 2020-12-04 stsp view->focussed = 0;
889 e78dc838 2020-12-04 stsp view->child->focussed = 1;
890 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
892 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
895 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
896 9970f7fc 2020-12-03 stsp KEY_RESIZE);
898 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
899 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
900 e78dc838 2020-12-04 stsp view->focussed = 1;
901 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
903 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
907 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
910 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
913 60493ae3 2019-06-20 stsp if (view->search_start)
914 2b49a8ae 2019-06-22 stsp view_search_start(view);
916 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
920 8f4ed634 2020-03-26 stsp if (view->search_next) {
921 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
922 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
923 60493ae3 2019-06-20 stsp view->search_next_done = 0;
924 60493ae3 2019-06-20 stsp view->search_next(view);
926 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
929 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
933 e5a0f69f 2018-08-18 stsp return err;
937 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
939 0cf4efb1 2018-09-29 stsp PANEL *panel;
940 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
942 669b5ffa 2018-10-07 stsp if (view->parent)
943 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
945 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
946 0cf4efb1 2018-09-29 stsp if (panel == NULL)
949 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
950 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
951 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
955 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
957 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
958 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
960 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
962 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
965 669b5ffa 2018-10-07 stsp return view->focussed;
968 bcbd79e2 2018-08-19 stsp static const struct got_error *
969 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
971 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
972 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
973 fb59748f 2020-12-05 stsp struct tog_view *new_view;
974 fd823528 2018-10-22 stsp int fast_refresh = 10;
975 1a76625f 2018-10-22 stsp int done = 0, errcode;
977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
978 1a76625f 2018-10-22 stsp if (errcode)
979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
981 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
982 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
984 1004088d 2018-09-29 stsp view->focussed = 1;
985 878940b7 2018-09-29 stsp err = view->show(view);
987 0cf4efb1 2018-09-29 stsp return err;
988 0cf4efb1 2018-09-29 stsp update_panels();
989 0cf4efb1 2018-09-29 stsp doupdate();
990 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
991 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
992 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
993 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
995 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
998 9970f7fc 2020-12-03 stsp if (view->dying) {
999 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1001 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1002 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1004 e78dc838 2020-12-04 stsp else if (view->parent)
1005 669b5ffa 2018-10-07 stsp prev = view->parent;
1007 e78dc838 2020-12-04 stsp if (view->parent) {
1008 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1009 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1011 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1013 9970f7fc 2020-12-03 stsp err = view_close(view);
1015 e5a0f69f 2018-08-18 stsp goto done;
1017 e78dc838 2020-12-04 stsp view = NULL;
1018 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1019 e78dc838 2020-12-04 stsp if (v->focussed)
1022 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1023 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1025 e78dc838 2020-12-04 stsp view = prev;
1026 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1027 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1028 e78dc838 2020-12-04 stsp tog_view_list_head);
1030 e78dc838 2020-12-04 stsp if (view) {
1031 e78dc838 2020-12-04 stsp if (view->focus_child) {
1032 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1033 e78dc838 2020-12-04 stsp view = view->child;
1035 e78dc838 2020-12-04 stsp view->focussed = 1;
1039 bcbd79e2 2018-08-19 stsp if (new_view) {
1040 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1041 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1042 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1043 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1045 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1046 86c66b02 2018-10-18 stsp err = view_close(v);
1048 86c66b02 2018-10-18 stsp goto done;
1051 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1052 fed7eaa8 2018-10-24 stsp view = new_view;
1054 669b5ffa 2018-10-07 stsp if (view) {
1055 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1056 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1057 e78dc838 2020-12-04 stsp view = view->child;
1059 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1060 e78dc838 2020-12-04 stsp view = view->parent;
1062 e78dc838 2020-12-04 stsp show_panel(view->panel);
1063 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1064 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1065 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1066 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1068 1a76625f 2018-10-22 stsp goto done;
1070 669b5ffa 2018-10-07 stsp err = view->show(view);
1072 1a76625f 2018-10-22 stsp goto done;
1073 669b5ffa 2018-10-07 stsp if (view->child) {
1074 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1076 1a76625f 2018-10-22 stsp goto done;
1078 1a76625f 2018-10-22 stsp update_panels();
1079 1a76625f 2018-10-22 stsp doupdate();
1083 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1084 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1085 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1086 e5a0f69f 2018-08-18 stsp view_close(view);
1089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1090 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1091 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1093 e5a0f69f 2018-08-18 stsp return err;
1096 4ed7e80c 2018-05-20 stsp __dead static void
1097 9f7d7167 2018-04-29 stsp usage_log(void)
1100 c70c5802 2018-08-01 stsp fprintf(stderr,
1101 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1102 9f7d7167 2018-04-29 stsp getprogname());
1106 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1107 80ddbec8 2018-04-29 stsp static const struct got_error *
1108 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1110 00dfcb92 2018-06-11 stsp char *vis = NULL;
1111 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1113 963b370f 2018-05-20 stsp *ws = NULL;
1114 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1115 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1116 00dfcb92 2018-06-11 stsp int vislen;
1117 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1118 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1120 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1121 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1123 00dfcb92 2018-06-11 stsp return err;
1124 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1125 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1127 a7f50699 2018-06-11 stsp goto done;
1131 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1132 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1133 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1134 a7f50699 2018-06-11 stsp goto done;
1137 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1138 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1140 00dfcb92 2018-06-11 stsp free(vis);
1141 963b370f 2018-05-20 stsp if (err) {
1142 963b370f 2018-05-20 stsp free(*ws);
1143 963b370f 2018-05-20 stsp *ws = NULL;
1144 963b370f 2018-05-20 stsp *wlen = 0;
1146 963b370f 2018-05-20 stsp return err;
1149 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1150 963b370f 2018-05-20 stsp static const struct got_error *
1151 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1152 27a741e5 2019-09-11 stsp int col_tab_align)
1154 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1155 963b370f 2018-05-20 stsp int cols = 0;
1156 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1157 963b370f 2018-05-20 stsp size_t wlen;
1160 963b370f 2018-05-20 stsp *wlinep = NULL;
1161 b700b5d6 2018-07-10 stsp *widthp = 0;
1163 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1165 963b370f 2018-05-20 stsp return err;
1168 27a741e5 2019-09-11 stsp while (i < wlen) {
1169 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1171 27a741e5 2019-09-11 stsp if (width == 0) {
1176 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1177 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1179 27a741e5 2019-09-11 stsp cols += width;
1181 27a741e5 2019-09-11 stsp } else if (width == -1) {
1182 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1183 27a741e5 2019-09-11 stsp width = TABSIZE -
1184 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1185 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1187 27a741e5 2019-09-11 stsp cols += width;
1191 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1192 963b370f 2018-05-20 stsp goto done;
1195 963b370f 2018-05-20 stsp wline[i] = L'\0';
1196 b700b5d6 2018-07-10 stsp if (widthp)
1197 b700b5d6 2018-07-10 stsp *widthp = cols;
1200 963b370f 2018-05-20 stsp free(wline);
1202 963b370f 2018-05-20 stsp *wlinep = wline;
1203 963b370f 2018-05-20 stsp return err;
1206 8b473291 2019-02-21 stsp static const struct got_error*
1207 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1208 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1210 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1211 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1213 8b473291 2019-02-21 stsp const char *name;
1215 8b473291 2019-02-21 stsp *refs_str = NULL;
1217 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1218 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1219 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1222 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1223 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1225 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1226 8b473291 2019-02-21 stsp name += 5;
1227 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1229 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1230 8b473291 2019-02-21 stsp name += 6;
1231 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1232 8b473291 2019-02-21 stsp name += 8;
1233 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1234 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1237 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1240 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1241 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1242 5d844a1e 2019-08-13 stsp if (err) {
1243 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1244 48cae60d 2020-09-22 stsp free(ref_id);
1247 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1248 5d844a1e 2019-08-13 stsp err = NULL;
1249 5d844a1e 2019-08-13 stsp tag = NULL;
1252 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1253 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1254 48cae60d 2020-09-22 stsp free(ref_id);
1256 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1257 52b5abe1 2019-08-13 stsp if (cmp != 0)
1259 8b473291 2019-02-21 stsp s = *refs_str;
1260 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1261 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1262 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1264 8b473291 2019-02-21 stsp *refs_str = NULL;
1270 8b473291 2019-02-21 stsp return err;
1273 963b370f 2018-05-20 stsp static const struct got_error *
1274 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1275 27a741e5 2019-09-11 stsp int col_tab_align)
1277 5813d178 2019-03-09 stsp char *smallerthan, *at;
1279 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1280 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1281 5813d178 2019-03-09 stsp author = smallerthan + 1;
1282 5813d178 2019-03-09 stsp at = strchr(author, '@');
1284 5813d178 2019-03-09 stsp *at = '\0';
1285 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1288 5813d178 2019-03-09 stsp static const struct got_error *
1289 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1290 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1291 8fdc79fe 2020-12-01 naddy int author_display_cols)
1293 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1294 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1295 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1296 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1297 5813d178 2019-03-09 stsp char *author = NULL;
1298 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1299 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1300 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1301 bb737323 2018-05-20 stsp int col, limit;
1302 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1303 ccb26ccd 2018-11-05 stsp struct tm tm;
1304 45d799e2 2018-12-23 stsp time_t committer_time;
1305 11b20872 2019-11-08 stsp struct tog_color *tc;
1307 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1308 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1309 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1310 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1311 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1312 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1314 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1315 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1317 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1318 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1320 11b20872 2019-11-08 stsp wattr_on(view->window,
1321 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1322 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1324 11b20872 2019-11-08 stsp wattr_off(view->window,
1325 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1326 27a741e5 2019-09-11 stsp col = limit;
1327 b39d25c7 2018-07-10 stsp if (col > avail)
1328 b39d25c7 2018-07-10 stsp goto done;
1330 6570a66d 2019-11-08 stsp if (avail >= 120) {
1331 6570a66d 2019-11-08 stsp char *id_str;
1332 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1334 6570a66d 2019-11-08 stsp goto done;
1335 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1337 11b20872 2019-11-08 stsp wattr_on(view->window,
1338 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1339 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1341 11b20872 2019-11-08 stsp wattr_off(view->window,
1342 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1343 6570a66d 2019-11-08 stsp free(id_str);
1345 6570a66d 2019-11-08 stsp if (col > avail)
1346 6570a66d 2019-11-08 stsp goto done;
1349 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1350 5813d178 2019-03-09 stsp if (author == NULL) {
1351 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1352 80ddbec8 2018-04-29 stsp goto done;
1354 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1356 bb737323 2018-05-20 stsp goto done;
1357 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1359 11b20872 2019-11-08 stsp wattr_on(view->window,
1360 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1361 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1363 11b20872 2019-11-08 stsp wattr_off(view->window,
1364 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1365 bb737323 2018-05-20 stsp col += author_width;
1366 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1367 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1369 bb737323 2018-05-20 stsp author_width++;
1371 9c2eaf34 2018-05-20 stsp if (col > avail)
1372 9c2eaf34 2018-05-20 stsp goto done;
1374 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1376 6d9fbc00 2018-04-29 stsp goto done;
1377 bb737323 2018-05-20 stsp logmsg = logmsg0;
1378 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1380 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1381 bb737323 2018-05-20 stsp if (newline)
1382 bb737323 2018-05-20 stsp *newline = '\0';
1383 bb737323 2018-05-20 stsp limit = avail - col;
1384 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1386 bb737323 2018-05-20 stsp goto done;
1387 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1388 bb737323 2018-05-20 stsp col += logmsg_width;
1389 27a741e5 2019-09-11 stsp while (col < avail) {
1390 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1394 80ddbec8 2018-04-29 stsp free(logmsg0);
1395 bb737323 2018-05-20 stsp free(wlogmsg);
1396 5813d178 2019-03-09 stsp free(author);
1397 bb737323 2018-05-20 stsp free(wauthor);
1398 80ddbec8 2018-04-29 stsp free(line);
1399 80ddbec8 2018-04-29 stsp return err;
1402 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1403 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1404 899d86c2 2018-05-10 stsp struct got_object_id *id)
1406 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1408 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1409 80ddbec8 2018-04-29 stsp if (entry == NULL)
1410 899d86c2 2018-05-10 stsp return NULL;
1412 899d86c2 2018-05-10 stsp entry->id = id;
1413 99db9666 2018-05-07 stsp entry->commit = commit;
1414 899d86c2 2018-05-10 stsp return entry;
1417 99db9666 2018-05-07 stsp static void
1418 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1420 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1422 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1423 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1424 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1425 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1426 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1427 99db9666 2018-05-07 stsp free(entry);
1430 99db9666 2018-05-07 stsp static void
1431 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1433 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1434 99db9666 2018-05-07 stsp pop_commit(commits);
1437 c4972b91 2018-05-07 stsp static const struct got_error *
1438 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1439 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1441 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1442 13add988 2019-10-15 stsp regmatch_t regmatch;
1443 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1445 13add988 2019-10-15 stsp *have_match = 0;
1447 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1449 13add988 2019-10-15 stsp return err;
1451 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1453 13add988 2019-10-15 stsp goto done;
1455 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1456 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1457 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1458 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1459 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1460 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1461 13add988 2019-10-15 stsp *have_match = 1;
1463 13add988 2019-10-15 stsp free(id_str);
1464 13add988 2019-10-15 stsp free(logmsg);
1465 13add988 2019-10-15 stsp return err;
1468 13add988 2019-10-15 stsp static const struct got_error *
1469 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1471 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1474 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1475 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1476 1a76625f 2018-10-22 stsp * while updating the display.
1479 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1480 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1481 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1482 1a76625f 2018-10-22 stsp int errcode;
1484 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1485 4e0d2870 2020-12-07 naddy NULL, NULL);
1486 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1489 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1492 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1493 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1494 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1498 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1499 1a76625f 2018-10-22 stsp if (errcode) {
1500 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1501 13add988 2019-10-15 stsp "pthread_mutex_lock");
1505 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1506 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1507 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1509 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1510 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1511 7c1452c1 2020-03-26 stsp int have_match;
1512 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1515 7c1452c1 2020-03-26 stsp if (have_match)
1516 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1519 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1520 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1521 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1522 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1525 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1527 9ba79e04 2018-06-11 stsp return err;
1530 2b779855 2020-12-05 naddy static void
1531 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1533 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1534 2b779855 2020-12-05 naddy int ncommits = 0;
1536 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1537 2b779855 2020-12-05 naddy while (entry) {
1538 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1539 2b779855 2020-12-05 naddy s->selected_entry = entry;
1542 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1543 2b779855 2020-12-05 naddy ncommits++;
1547 0553a4e3 2018-04-30 stsp static const struct got_error *
1548 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1550 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1551 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1552 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1553 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1554 60493ae3 2019-06-20 stsp int width;
1555 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1556 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1557 8b473291 2019-02-21 stsp char *refs_str = NULL;
1558 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1559 11b20872 2019-11-08 stsp struct tog_color *tc;
1560 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1562 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1563 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1564 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1566 ecb28ae0 2018-07-16 stsp return err;
1567 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1568 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1570 8fdc79fe 2020-12-01 naddy goto done;
1573 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1574 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1576 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1577 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1578 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1579 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1580 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1581 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1582 8f4ed634 2020-03-26 stsp goto done;
1585 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1587 f9686aa5 2020-03-27 stsp if (view->searching) {
1588 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1589 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1590 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1591 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1592 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1593 f9686aa5 2020-03-27 stsp search_str = "searching...";
1596 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1597 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1598 f9686aa5 2020-03-27 stsp search_str ? search_str :
1599 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1600 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1601 8f4ed634 2020-03-26 stsp goto done;
1605 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1606 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1607 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1608 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1609 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1610 1a76625f 2018-10-22 stsp header = NULL;
1611 1a76625f 2018-10-22 stsp goto done;
1613 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1614 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1615 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1616 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1617 1a76625f 2018-10-22 stsp header = NULL;
1618 1a76625f 2018-10-22 stsp goto done;
1620 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1622 1a76625f 2018-10-22 stsp goto done;
1624 2814baeb 2018-08-01 stsp werase(view->window);
1626 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1627 a3404814 2018-09-02 stsp wstandout(view->window);
1628 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1630 11b20872 2019-11-08 stsp wattr_on(view->window,
1631 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1632 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1634 11b20872 2019-11-08 stsp wattr_off(view->window,
1635 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1636 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1637 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1640 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1641 a3404814 2018-09-02 stsp wstandend(view->window);
1642 ecb28ae0 2018-07-16 stsp free(wline);
1643 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1644 1a76625f 2018-10-22 stsp goto done;
1646 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1647 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1648 5813d178 2019-03-09 stsp ncommits = 0;
1649 5813d178 2019-03-09 stsp while (entry) {
1650 5813d178 2019-03-09 stsp char *author;
1651 5813d178 2019-03-09 stsp wchar_t *wauthor;
1652 5813d178 2019-03-09 stsp int width;
1653 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1655 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1656 5813d178 2019-03-09 stsp if (author == NULL) {
1657 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1658 5813d178 2019-03-09 stsp goto done;
1660 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1661 27a741e5 2019-09-11 stsp date_display_cols);
1662 5813d178 2019-03-09 stsp if (author_cols < width)
1663 5813d178 2019-03-09 stsp author_cols = width;
1664 5813d178 2019-03-09 stsp free(wauthor);
1665 5813d178 2019-03-09 stsp free(author);
1666 7ca04879 2019-10-19 stsp ncommits++;
1667 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1670 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1671 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1672 867c6645 2018-07-10 stsp ncommits = 0;
1673 899d86c2 2018-05-10 stsp while (entry) {
1674 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1676 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1677 2814baeb 2018-08-01 stsp wstandout(view->window);
1678 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1679 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1680 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1681 2814baeb 2018-08-01 stsp wstandend(view->window);
1683 60493ae3 2019-06-20 stsp goto done;
1684 0553a4e3 2018-04-30 stsp ncommits++;
1685 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1686 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1689 1a57306a 2018-09-02 stsp view_vborder(view);
1691 1a76625f 2018-10-22 stsp free(id_str);
1692 8b473291 2019-02-21 stsp free(refs_str);
1693 1a76625f 2018-10-22 stsp free(ncommits_str);
1694 1a76625f 2018-10-22 stsp free(header);
1695 80ddbec8 2018-04-29 stsp return err;
1698 07b55e75 2018-05-10 stsp static void
1699 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1701 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1702 07b55e75 2018-05-10 stsp int nscrolled = 0;
1704 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1705 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1708 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1709 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1710 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1711 07b55e75 2018-05-10 stsp if (entry) {
1712 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1713 07b55e75 2018-05-10 stsp nscrolled++;
1718 aa075928 2018-05-10 stsp static const struct got_error *
1719 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1721 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1722 5e224a3e 2019-02-22 stsp int errcode;
1724 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1726 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1727 ffe38506 2020-12-01 naddy if (ta->log_complete)
1730 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1731 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1732 7aafa0d1 2019-02-22 stsp if (errcode)
1733 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1734 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1737 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1738 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1740 7c1452c1 2020-03-26 stsp if (!wait)
1743 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1744 ffe38506 2020-12-01 naddy show_log_view(view);
1745 7c1452c1 2020-03-26 stsp update_panels();
1746 7c1452c1 2020-03-26 stsp doupdate();
1748 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1749 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1750 82954512 2020-02-03 stsp if (errcode)
1751 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1752 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1754 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1755 ffe38506 2020-12-01 naddy show_log_view(view);
1756 7c1452c1 2020-03-26 stsp update_panels();
1757 7c1452c1 2020-03-26 stsp doupdate();
1760 5e224a3e 2019-02-22 stsp return NULL;
1763 5e224a3e 2019-02-22 stsp static const struct got_error *
1764 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1766 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1767 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1768 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1769 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1771 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1772 5e224a3e 2019-02-22 stsp return NULL;
1774 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1775 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1776 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1778 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1780 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1781 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1783 5e224a3e 2019-02-22 stsp return err;
1787 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1788 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1791 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1793 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1794 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1796 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1797 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1799 dd0a52c1 2018-05-20 stsp return err;
1802 cd0acaa7 2018-05-20 stsp static const struct got_error *
1803 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1804 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1805 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1807 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1808 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1809 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1811 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1812 15a94983 2018-12-23 stsp if (diff_view == NULL)
1813 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1815 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1816 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1817 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1818 e5a0f69f 2018-08-18 stsp if (err == NULL)
1819 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1820 cd0acaa7 2018-05-20 stsp return err;
1823 80ddbec8 2018-04-29 stsp static const struct got_error *
1824 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1825 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1827 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1829 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1830 941e9f74 2019-05-21 stsp if (parent == NULL)
1831 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1833 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1834 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1835 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1836 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1837 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1838 941e9f74 2019-05-21 stsp s->tree = subtree;
1839 941e9f74 2019-05-21 stsp s->selected = 0;
1840 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1841 941e9f74 2019-05-21 stsp return NULL;
1844 941e9f74 2019-05-21 stsp static const struct got_error *
1845 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1846 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1848 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1849 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1850 941e9f74 2019-05-21 stsp const char *p;
1851 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1853 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1855 941e9f74 2019-05-21 stsp while (*p) {
1856 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1857 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1858 56e0773d 2019-11-28 stsp char *te_name;
1860 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1863 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1864 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1865 941e9f74 2019-05-21 stsp if (slash == NULL)
1866 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1868 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1869 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1870 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1873 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1874 56e0773d 2019-11-28 stsp if (te == NULL) {
1875 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1876 56e0773d 2019-11-28 stsp free(te_name);
1879 56e0773d 2019-11-28 stsp free(te_name);
1880 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1882 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1883 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1885 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1886 941e9f74 2019-05-21 stsp if (slash)
1887 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1889 941e9f74 2019-05-21 stsp subpath = strdup(path);
1890 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1891 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1895 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1900 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1901 941e9f74 2019-05-21 stsp free(tree_id);
1905 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1906 941e9f74 2019-05-21 stsp if (err) {
1907 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1910 941e9f74 2019-05-21 stsp if (slash == NULL)
1912 941e9f74 2019-05-21 stsp free(subpath);
1913 941e9f74 2019-05-21 stsp subpath = NULL;
1914 941e9f74 2019-05-21 stsp p = slash;
1917 941e9f74 2019-05-21 stsp free(subpath);
1918 1a76625f 2018-10-22 stsp return err;
1921 61266923 2020-01-14 stsp static const struct got_error *
1922 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1923 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1924 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1926 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1927 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1928 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1929 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1931 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1932 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1934 55cccc34 2020-02-20 stsp return err;
1936 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1937 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1938 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1940 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1941 55cccc34 2020-02-20 stsp if (err) {
1942 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1943 55cccc34 2020-02-20 stsp return err;
1945 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1947 55cccc34 2020-02-20 stsp *new_view = tree_view;
1949 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1950 55cccc34 2020-02-20 stsp return NULL;
1952 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1955 55cccc34 2020-02-20 stsp static const struct got_error *
1956 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1958 61266923 2020-01-14 stsp sigset_t sigset;
1959 61266923 2020-01-14 stsp int errcode;
1961 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1962 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1964 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1965 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1966 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1967 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1968 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1970 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1971 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1972 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1974 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1975 61266923 2020-01-14 stsp if (errcode)
1976 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1978 61266923 2020-01-14 stsp return NULL;
1981 1a76625f 2018-10-22 stsp static void *
1982 1a76625f 2018-10-22 stsp log_thread(void *arg)
1984 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1985 1a76625f 2018-10-22 stsp int errcode = 0;
1986 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1987 1a76625f 2018-10-22 stsp int done = 0;
1989 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1991 61266923 2020-01-14 stsp return (void *)err;
1993 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1994 4e0d2870 2020-12-07 naddy err = queue_commits(a);
1995 1a76625f 2018-10-22 stsp if (err) {
1996 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1997 1a76625f 2018-10-22 stsp return (void *)err;
1998 1a76625f 2018-10-22 stsp err = NULL;
2000 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2001 1a76625f 2018-10-22 stsp a->commits_needed--;
2003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2004 3abe8080 2019-04-10 stsp if (errcode) {
2005 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2006 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2008 3abe8080 2019-04-10 stsp } else if (*a->quit)
2010 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2011 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2012 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2013 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2016 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2017 7c1452c1 2020-03-26 stsp if (errcode) {
2018 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2019 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2020 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2025 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2027 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2028 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2029 7c1452c1 2020-03-26 stsp &tog_mutex);
2030 7c1452c1 2020-03-26 stsp if (errcode)
2031 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2032 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2033 21355643 2020-12-06 stsp if (*a->quit)
2038 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2039 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2040 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2041 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2043 3abe8080 2019-04-10 stsp a->log_complete = 1;
2044 1a76625f 2018-10-22 stsp return (void *)err;
2047 1a76625f 2018-10-22 stsp static const struct got_error *
2048 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2050 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2051 1a76625f 2018-10-22 stsp int errcode;
2053 1a76625f 2018-10-22 stsp if (s->thread) {
2054 1a76625f 2018-10-22 stsp s->quit = 1;
2055 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2056 1a76625f 2018-10-22 stsp if (errcode)
2057 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2058 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2059 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&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_unlock");
2063 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2064 1a76625f 2018-10-22 stsp if (errcode)
2065 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2066 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2067 1a76625f 2018-10-22 stsp if (errcode)
2068 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2069 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2070 1a76625f 2018-10-22 stsp s->thread = NULL;
2073 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2074 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2075 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2078 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2079 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2080 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2083 9343a5fb 2018-06-23 stsp return err;
2086 9343a5fb 2018-06-23 stsp static const struct got_error *
2087 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2089 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2090 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2091 276b94a1 2020-11-13 naddy int errcode;
2093 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2095 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2096 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2097 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2099 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2100 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2101 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2103 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2104 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2105 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2106 1a76625f 2018-10-22 stsp free(s->start_id);
2107 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2108 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2109 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2110 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2111 1a76625f 2018-10-22 stsp return err;
2114 1a76625f 2018-10-22 stsp static const struct got_error *
2115 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2117 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2119 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2120 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2121 60493ae3 2019-06-20 stsp return NULL;
2124 60493ae3 2019-06-20 stsp static const struct got_error *
2125 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2127 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2128 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2129 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2131 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2132 f9686aa5 2020-03-27 stsp show_log_view(view);
2133 f9686aa5 2020-03-27 stsp update_panels();
2134 f9686aa5 2020-03-27 stsp doupdate();
2136 96e2b566 2019-07-08 stsp if (s->search_entry) {
2137 13add988 2019-10-15 stsp int errcode, ch;
2138 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2139 13add988 2019-10-15 stsp if (errcode)
2140 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2141 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2142 13add988 2019-10-15 stsp ch = wgetch(view->window);
2143 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2144 13add988 2019-10-15 stsp if (errcode)
2145 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2146 13add988 2019-10-15 stsp "pthread_mutex_lock");
2147 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2148 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2149 678cbce5 2019-07-28 stsp return NULL;
2151 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2152 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2154 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2155 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2156 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2157 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2158 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2160 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2161 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2163 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2164 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2166 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2169 60493ae3 2019-06-20 stsp while (1) {
2170 13add988 2019-10-15 stsp int have_match = 0;
2172 60493ae3 2019-06-20 stsp if (entry == NULL) {
2173 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2174 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2175 f9967bca 2020-03-27 stsp view->search_next_done =
2176 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2177 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2178 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2179 f801134a 2019-06-25 stsp return NULL;
2182 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2183 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2184 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2186 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2187 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2190 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2191 13add988 2019-10-15 stsp &view->regex);
2194 13add988 2019-10-15 stsp if (have_match) {
2195 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2196 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2200 96e2b566 2019-07-08 stsp s->search_entry = entry;
2201 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2202 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2204 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2207 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2208 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2209 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2210 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2212 60493ae3 2019-06-20 stsp return err;
2215 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2216 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2218 60493ae3 2019-06-20 stsp return err;
2223 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2225 60493ae3 2019-06-20 stsp return NULL;
2228 60493ae3 2019-06-20 stsp static const struct got_error *
2229 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2230 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2231 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2233 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2234 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2235 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2236 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2237 1a76625f 2018-10-22 stsp int errcode;
2239 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2241 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2242 f135c941 2020-02-20 stsp free(s->in_repo_path);
2243 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2244 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2245 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2248 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2249 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2250 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2252 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2254 78756c87 2020-11-24 stsp goto done;
2256 fb2756b9 2018-08-04 stsp s->repo = repo;
2257 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2258 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2259 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2260 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2261 9cd7cbd1 2020-12-07 stsp goto done;
2264 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2265 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2266 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2267 5036bf37 2018-09-24 stsp goto done;
2269 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2271 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2272 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2273 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2274 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2276 11b20872 2019-11-08 stsp goto done;
2277 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2278 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2279 11b20872 2019-11-08 stsp if (err) {
2280 11b20872 2019-11-08 stsp free_colors(&s->colors);
2281 11b20872 2019-11-08 stsp goto done;
2283 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2284 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2285 11b20872 2019-11-08 stsp if (err) {
2286 11b20872 2019-11-08 stsp free_colors(&s->colors);
2287 11b20872 2019-11-08 stsp goto done;
2291 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2292 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2293 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2294 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2295 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2297 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2299 1a76625f 2018-10-22 stsp goto done;
2300 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2301 b672a97a 2020-01-27 stsp !s->log_branches);
2303 1a76625f 2018-10-22 stsp goto done;
2304 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2305 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2307 c5b78334 2020-01-12 stsp goto done;
2309 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2310 1a76625f 2018-10-22 stsp if (errcode) {
2311 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2312 1a76625f 2018-10-22 stsp goto done;
2314 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2315 7c1452c1 2020-03-26 stsp if (errcode) {
2316 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2317 7c1452c1 2020-03-26 stsp goto done;
2320 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2321 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2322 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2323 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2324 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2325 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2326 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2327 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2328 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2329 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2330 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2331 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2332 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2335 1a76625f 2018-10-22 stsp close_log_view(view);
2336 ba4f502b 2018-08-04 stsp return err;
2339 e5a0f69f 2018-08-18 stsp static const struct got_error *
2340 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2342 f2f6d207 2020-11-24 stsp const struct got_error *err;
2343 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2345 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2346 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2347 2b380cc8 2018-10-24 stsp &s->thread_args);
2348 2b380cc8 2018-10-24 stsp if (errcode)
2349 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2350 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2351 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2353 f2f6d207 2020-11-24 stsp return err;
2357 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2360 e5a0f69f 2018-08-18 stsp static const struct got_error *
2361 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2363 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2364 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2365 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2366 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2367 669b5ffa 2018-10-07 stsp int begin_x = 0;
2369 e5a0f69f 2018-08-18 stsp switch (ch) {
2371 1e37a5c2 2019-05-12 jcs s->quit = 1;
2374 1e37a5c2 2019-05-12 jcs case KEY_UP:
2377 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2379 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2380 1e37a5c2 2019-05-12 jcs s->selected--;
2382 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2383 2b779855 2020-12-05 naddy select_commit(s);
2385 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2386 a4292ac5 2019-05-12 jcs case CTRL('b'):
2387 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2389 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2390 1e37a5c2 2019-05-12 jcs s->selected = 0;
2392 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2393 2b779855 2020-12-05 naddy select_commit(s);
2396 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2399 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2401 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2402 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2403 1e37a5c2 2019-05-12 jcs s->selected++;
2405 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2409 2b779855 2020-12-05 naddy select_commit(s);
2411 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2412 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2413 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2414 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2415 1e37a5c2 2019-05-12 jcs if (first == NULL)
2417 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2420 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2421 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2422 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2423 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2424 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2425 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2427 2b779855 2020-12-05 naddy select_commit(s);
2430 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2431 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2432 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2433 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2434 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2435 2b779855 2020-12-05 naddy select_commit(s);
2436 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2437 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2438 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2439 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2440 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2443 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2446 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2448 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2449 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2450 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2451 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2452 78756c87 2020-11-24 stsp view, s->repo);
2455 e78dc838 2020-12-04 stsp view->focussed = 0;
2456 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2457 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2458 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2460 1e37a5c2 2019-05-12 jcs return err;
2461 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2462 e78dc838 2020-12-04 stsp view->focus_child = 1;
2464 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2467 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2469 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2470 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2471 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2472 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2476 e78dc838 2020-12-04 stsp view->focussed = 0;
2477 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2478 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2479 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2481 1e37a5c2 2019-05-12 jcs return err;
2482 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2483 e78dc838 2020-12-04 stsp view->focus_child = 1;
2485 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2487 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2488 21355643 2020-12-06 stsp case CTRL('l'):
2490 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2491 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2493 21355643 2020-12-06 stsp err = stop_log_thread(s);
2495 74cfe85e 2020-10-20 stsp return err;
2496 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2497 21355643 2020-12-06 stsp char *parent_path;
2498 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2500 21355643 2020-12-06 stsp return err;
2501 21355643 2020-12-06 stsp free(s->in_repo_path);
2502 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2503 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2504 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2505 21355643 2020-12-06 stsp struct got_object_id *start_id;
2506 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2507 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2508 21355643 2020-12-06 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2510 21355643 2020-12-06 stsp return err;
2511 21355643 2020-12-06 stsp free(s->start_id);
2512 21355643 2020-12-06 stsp s->start_id = start_id;
2513 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2514 21355643 2020-12-06 stsp } else /* 'B' */
2515 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2517 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2518 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2520 21355643 2020-12-06 stsp return err;
2521 ca51c541 2020-12-07 stsp got_ref_list_free(&s->refs);
2522 ca51c541 2020-12-07 stsp err = got_ref_list(&s->refs, s->repo, NULL,
2523 ca51c541 2020-12-07 stsp got_ref_cmp_by_name, NULL);
2525 ca51c541 2020-12-07 stsp return err;
2526 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2527 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2529 d01904d4 2019-06-25 stsp return err;
2530 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2531 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2533 b672a97a 2020-01-27 stsp return err;
2534 21355643 2020-12-06 stsp free_commits(&s->commits);
2535 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2536 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2537 21355643 2020-12-06 stsp s->selected_entry = NULL;
2538 21355643 2020-12-06 stsp s->selected = 0;
2539 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2540 21355643 2020-12-06 stsp s->quit = 0;
2541 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2544 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2545 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2546 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2547 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2548 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2549 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2550 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2551 6458efa5 2020-11-24 stsp if (err) {
2552 6458efa5 2020-11-24 stsp view_close(ref_view);
2553 6458efa5 2020-11-24 stsp return err;
2555 e78dc838 2020-12-04 stsp view->focussed = 0;
2556 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2557 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2558 6458efa5 2020-11-24 stsp err = view_close_child(view);
2560 6458efa5 2020-11-24 stsp return err;
2561 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2562 e78dc838 2020-12-04 stsp view->focus_child = 1;
2564 6458efa5 2020-11-24 stsp *new_view = ref_view;
2570 80ddbec8 2018-04-29 stsp return err;
2573 4ed7e80c 2018-05-20 stsp static const struct got_error *
2574 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2576 c2db6724 2019-01-04 stsp const struct got_error *error;
2578 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2579 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2580 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2582 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2583 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2585 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2586 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2588 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2589 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2591 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2592 c2db6724 2019-01-04 stsp if (error != NULL)
2593 c2db6724 2019-01-04 stsp return error;
2595 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2596 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2598 c2db6724 2019-01-04 stsp return NULL;
2601 a915003a 2019-02-05 stsp static void
2602 a915003a 2019-02-05 stsp init_curses(void)
2604 a915003a 2019-02-05 stsp initscr();
2606 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2609 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2610 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2611 a915003a 2019-02-05 stsp curs_set(0);
2612 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2613 6d17833f 2019-11-08 stsp start_color();
2614 6d17833f 2019-11-08 stsp use_default_colors();
2616 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2617 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2618 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2621 c2db6724 2019-01-04 stsp static const struct got_error *
2622 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2623 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2625 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2627 f135c941 2020-02-20 stsp if (argc == 0) {
2628 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2629 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2630 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2631 f135c941 2020-02-20 stsp return NULL;
2634 f135c941 2020-02-20 stsp if (worktree) {
2635 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2638 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2640 f135c941 2020-02-20 stsp return err;
2641 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2642 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2643 bfd61697 2020-11-14 stsp p) == -1) {
2644 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2645 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2649 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2651 f135c941 2020-02-20 stsp return err;
2654 f135c941 2020-02-20 stsp static const struct got_error *
2655 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2657 80ddbec8 2018-04-29 stsp const struct got_error *error;
2658 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2659 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2660 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2661 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2662 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2663 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2664 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2665 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2666 04cc582a 2018-08-01 stsp struct tog_view *view;
2668 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2669 80ddbec8 2018-04-29 stsp switch (ch) {
2671 b672a97a 2020-01-27 stsp log_branches = 1;
2674 80ddbec8 2018-04-29 stsp start_commit = optarg;
2677 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2678 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2679 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2683 17020d27 2019-03-07 stsp usage_log();
2684 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2688 80ddbec8 2018-04-29 stsp argc -= optind;
2689 80ddbec8 2018-04-29 stsp argv += optind;
2691 f135c941 2020-02-20 stsp if (argc > 1)
2692 f135c941 2020-02-20 stsp usage_log();
2694 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2695 6962eb72 2020-02-20 stsp if (cwd == NULL)
2696 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2698 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2699 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2700 963f97a1 2019-03-18 stsp goto done;
2702 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2703 a1fbf39a 2019-08-11 stsp if (worktree)
2704 6962eb72 2020-02-20 stsp repo_path =
2705 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2707 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2709 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2710 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2711 963f97a1 2019-03-18 stsp goto done;
2714 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2715 80ddbec8 2018-04-29 stsp if (error != NULL)
2716 ecb28ae0 2018-07-16 stsp goto done;
2718 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2719 f135c941 2020-02-20 stsp repo, worktree);
2720 f135c941 2020-02-20 stsp if (error)
2721 f135c941 2020-02-20 stsp goto done;
2723 f135c941 2020-02-20 stsp init_curses();
2725 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2726 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2727 c02c541e 2019-03-29 stsp if (error)
2728 c02c541e 2019-03-29 stsp goto done;
2730 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2731 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2732 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2733 d8f38dc4 2020-12-05 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2734 d8f38dc4 2020-12-05 stsp if (error)
2735 d8f38dc4 2020-12-05 stsp goto done;
2736 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2738 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2739 d8f38dc4 2020-12-05 stsp if (error == NULL)
2740 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2741 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2742 d8f38dc4 2020-12-05 stsp goto done;
2743 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2744 d8f38dc4 2020-12-05 stsp start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 d8f38dc4 2020-12-05 stsp if (error)
2746 d8f38dc4 2020-12-05 stsp goto done;
2749 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2750 04cc582a 2018-08-01 stsp if (view == NULL) {
2751 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2752 04cc582a 2018-08-01 stsp goto done;
2754 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2755 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2756 ba4f502b 2018-08-04 stsp if (error)
2757 ba4f502b 2018-08-04 stsp goto done;
2758 2fc00ff4 2019-08-31 stsp if (worktree) {
2759 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2760 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2761 2fc00ff4 2019-08-31 stsp worktree = NULL;
2763 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2765 f135c941 2020-02-20 stsp free(in_repo_path);
2766 ecb28ae0 2018-07-16 stsp free(repo_path);
2767 ecb28ae0 2018-07-16 stsp free(cwd);
2768 899d86c2 2018-05-10 stsp free(start_id);
2769 d8f38dc4 2020-12-05 stsp free(label);
2771 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2773 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2774 ec142235 2019-03-07 stsp if (worktree)
2775 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2776 80ddbec8 2018-04-29 stsp return error;
2779 4ed7e80c 2018-05-20 stsp __dead static void
2780 9f7d7167 2018-04-29 stsp usage_diff(void)
2783 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2784 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2788 b304db33 2018-05-20 stsp static char *
2789 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2791 b304db33 2018-05-20 stsp char *line;
2792 b304db33 2018-05-20 stsp size_t linelen;
2793 b304db33 2018-05-20 stsp size_t lineno;
2794 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2796 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2798 b304db33 2018-05-20 stsp *len = linelen;
2799 b304db33 2018-05-20 stsp return line;
2802 6d17833f 2019-11-08 stsp static int
2803 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2804 41605754 2020-11-12 stsp regmatch_t *regmatch)
2806 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2809 f26dddb7 2019-11-08 stsp struct tog_color *
2810 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2812 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2814 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2815 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2816 6d17833f 2019-11-08 stsp return tc;
2819 6d17833f 2019-11-08 stsp return NULL;
2822 4ed7e80c 2018-05-20 stsp static const struct got_error *
2823 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2824 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2826 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2827 41605754 2020-11-12 stsp wchar_t *wline;
2828 41605754 2020-11-12 stsp int width;
2831 41605754 2020-11-12 stsp *wtotal = 0;
2833 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2834 41605754 2020-11-12 stsp if (s == NULL)
2835 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2837 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2838 41605754 2020-11-12 stsp if (err) {
2840 41605754 2020-11-12 stsp return err;
2842 41605754 2020-11-12 stsp waddwstr(window, wline);
2843 41605754 2020-11-12 stsp free(wline);
2845 41605754 2020-11-12 stsp wlimit -= width;
2846 41605754 2020-11-12 stsp *wtotal += width;
2848 41605754 2020-11-12 stsp if (wlimit > 0) {
2849 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2850 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2851 41605754 2020-11-12 stsp if (s == NULL) {
2852 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2854 41605754 2020-11-12 stsp return err;
2856 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2857 41605754 2020-11-12 stsp if (err) {
2859 41605754 2020-11-12 stsp return err;
2861 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2862 41605754 2020-11-12 stsp waddwstr(window, wline);
2863 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2864 41605754 2020-11-12 stsp free(wline);
2866 41605754 2020-11-12 stsp wlimit -= width;
2867 41605754 2020-11-12 stsp *wtotal += width;
2870 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2871 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2872 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2874 41605754 2020-11-12 stsp return err;
2875 41605754 2020-11-12 stsp waddwstr(window, wline);
2876 41605754 2020-11-12 stsp free(wline);
2877 41605754 2020-11-12 stsp *wtotal += width;
2880 41605754 2020-11-12 stsp return NULL;
2883 41605754 2020-11-12 stsp static const struct got_error *
2884 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2886 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2887 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2888 61e69b96 2018-05-20 stsp const struct got_error *err;
2889 fe621944 2020-11-10 stsp int nprinted = 0;
2890 b304db33 2018-05-20 stsp char *line;
2891 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2892 b304db33 2018-05-20 stsp size_t len;
2893 61e69b96 2018-05-20 stsp wchar_t *wline;
2894 e0b650dd 2018-05-20 stsp int width;
2895 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2896 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2897 fe621944 2020-11-10 stsp off_t line_offset;
2899 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2900 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2901 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2903 f7d12f7e 2018-08-01 stsp werase(view->window);
2905 a3404814 2018-09-02 stsp if (header) {
2906 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2907 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2908 135a2da0 2020-11-11 stsp header) == -1)
2909 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2910 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2911 135a2da0 2020-11-11 stsp free(line);
2913 a3404814 2018-09-02 stsp return err;
2915 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2916 a3404814 2018-09-02 stsp wstandout(view->window);
2917 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2918 e54cc94a 2020-11-11 stsp free(wline);
2919 e54cc94a 2020-11-11 stsp wline = NULL;
2920 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2921 a3404814 2018-09-02 stsp wstandend(view->window);
2922 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2923 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2925 a3404814 2018-09-02 stsp if (max_lines <= 1)
2926 a3404814 2018-09-02 stsp return NULL;
2927 a3404814 2018-09-02 stsp max_lines--;
2930 89f1a395 2020-12-01 naddy s->eof = 0;
2931 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2932 89f1a395 2020-12-01 naddy line = parse_next_line(s->f, &len);
2933 26ed57b2 2018-05-19 stsp if (line == NULL) {
2934 89f1a395 2020-12-01 naddy s->eof = 1;
2938 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2940 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2941 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2942 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2943 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2944 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2945 41605754 2020-11-12 stsp view->window, regmatch);
2946 41605754 2020-11-12 stsp if (err) {
2947 41605754 2020-11-12 stsp free(line);
2948 41605754 2020-11-12 stsp return err;
2951 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2952 41605754 2020-11-12 stsp if (err) {
2953 41605754 2020-11-12 stsp free(line);
2954 41605754 2020-11-12 stsp return err;
2956 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2957 41605754 2020-11-12 stsp free(wline);
2958 41605754 2020-11-12 stsp wline = NULL;
2961 6d17833f 2019-11-08 stsp wattr_off(view->window,
2962 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2963 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2964 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2965 fe621944 2020-11-10 stsp nprinted++;
2966 26ed57b2 2018-05-19 stsp free(line);
2968 fe621944 2020-11-10 stsp if (nprinted >= 1)
2969 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2970 89f1a395 2020-12-01 naddy (nprinted - 1);
2972 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2974 1a57306a 2018-09-02 stsp view_vborder(view);
2976 89f1a395 2020-12-01 naddy if (s->eof) {
2977 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2978 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2979 c3e9aa98 2019-05-13 jcs nprinted++;
2982 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2984 c3e9aa98 2019-05-13 jcs return err;
2987 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2988 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2989 e54cc94a 2020-11-11 stsp free(wline);
2990 e54cc94a 2020-11-11 stsp wline = NULL;
2991 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2994 26ed57b2 2018-05-19 stsp return NULL;
2997 abd2672a 2018-12-23 stsp static char *
2998 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3000 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3001 09867e48 2019-08-13 stsp char *p, *s;
3003 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3004 09867e48 2019-08-13 stsp if (tm == NULL)
3005 09867e48 2019-08-13 stsp return NULL;
3006 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3007 09867e48 2019-08-13 stsp if (s == NULL)
3008 09867e48 2019-08-13 stsp return NULL;
3009 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3011 abd2672a 2018-12-23 stsp *p = '\0';
3015 4ed7e80c 2018-05-20 stsp static const struct got_error *
3016 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3017 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3019 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3020 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3021 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3022 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3024 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3025 0208f208 2020-05-05 stsp if (qid != NULL) {
3026 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3027 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3030 0208f208 2020-05-05 stsp return err;
3032 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3033 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3037 0208f208 2020-05-05 stsp if (tree_id1) {
3038 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3040 0208f208 2020-05-05 stsp goto done;
3043 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3044 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3046 0208f208 2020-05-05 stsp goto done;
3048 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3049 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3051 0208f208 2020-05-05 stsp if (tree1)
3052 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3053 0208f208 2020-05-05 stsp if (tree2)
3054 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3055 0208f208 2020-05-05 stsp return err;
3058 0208f208 2020-05-05 stsp static const struct got_error *
3059 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3063 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3064 fe621944 2020-11-10 stsp if (p == NULL)
3065 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3066 fe621944 2020-11-10 stsp *line_offsets = p;
3067 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3068 fe621944 2020-11-10 stsp (*nlines)++;
3069 fe621944 2020-11-10 stsp return NULL;
3072 fe621944 2020-11-10 stsp static const struct got_error *
3073 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3074 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3075 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3077 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3078 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3079 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3080 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3081 45d799e2 2018-12-23 stsp time_t committer_time;
3082 45d799e2 2018-12-23 stsp const char *author, *committer;
3083 8b473291 2019-02-21 stsp char *refs_str = NULL;
3084 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3085 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3086 fe621944 2020-11-10 stsp off_t outoff = 0;
3089 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3091 8b473291 2019-02-21 stsp if (refs) {
3092 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3094 8b473291 2019-02-21 stsp return err;
3097 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3099 abd2672a 2018-12-23 stsp return err;
3101 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3102 15a94983 2018-12-23 stsp if (err) {
3103 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3104 15a94983 2018-12-23 stsp goto done;
3107 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3109 fe621944 2020-11-10 stsp goto done;
3111 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3112 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3113 fe621944 2020-11-10 stsp if (n < 0) {
3114 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3115 abd2672a 2018-12-23 stsp goto done;
3117 fe621944 2020-11-10 stsp outoff += n;
3118 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3120 fe621944 2020-11-10 stsp goto done;
3122 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3123 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
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 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3134 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3135 fe621944 2020-11-10 stsp if (datestr) {
3136 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3137 fe621944 2020-11-10 stsp if (n < 0) {
3138 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3139 fe621944 2020-11-10 stsp goto done;
3141 fe621944 2020-11-10 stsp outoff += n;
3142 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3144 fe621944 2020-11-10 stsp goto done;
3146 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3147 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3148 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3149 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3150 fe621944 2020-11-10 stsp if (n < 0) {
3151 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3152 fe621944 2020-11-10 stsp goto done;
3154 fe621944 2020-11-10 stsp outoff += n;
3155 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3157 fe621944 2020-11-10 stsp goto done;
3159 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3161 5943eee2 2019-08-13 stsp goto done;
3162 fe621944 2020-11-10 stsp s = logmsg;
3163 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3164 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3165 fe621944 2020-11-10 stsp if (n < 0) {
3166 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3167 fe621944 2020-11-10 stsp goto done;
3169 fe621944 2020-11-10 stsp outoff += n;
3170 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3172 fe621944 2020-11-10 stsp goto done;
3175 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3177 0208f208 2020-05-05 stsp goto done;
3178 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3179 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3180 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3181 fe621944 2020-11-10 stsp if (n < 0) {
3182 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3183 fe621944 2020-11-10 stsp goto done;
3185 fe621944 2020-11-10 stsp outoff += n;
3186 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3188 fe621944 2020-11-10 stsp goto done;
3189 0208f208 2020-05-05 stsp free((char *)pe->path);
3190 0208f208 2020-05-05 stsp free(pe->data);
3193 0208f208 2020-05-05 stsp fputc('\n', outfile);
3195 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3197 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3198 abd2672a 2018-12-23 stsp free(id_str);
3199 5943eee2 2019-08-13 stsp free(logmsg);
3200 8b473291 2019-02-21 stsp free(refs_str);
3201 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3202 7510f233 2020-08-09 stsp if (err) {
3203 ae6a6978 2020-08-09 stsp free(*line_offsets);
3204 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3205 ae6a6978 2020-08-09 stsp *nlines = 0;
3207 fe621944 2020-11-10 stsp return err;
3210 abd2672a 2018-12-23 stsp static const struct got_error *
3211 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3213 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3214 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3215 15a94983 2018-12-23 stsp int obj_type;
3217 fe621944 2020-11-10 stsp free(s->line_offsets);
3218 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3219 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3220 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3221 fe621944 2020-11-10 stsp s->nlines = 0;
3223 511a516b 2018-05-19 stsp f = got_opentemp();
3224 48ae06ee 2018-10-18 stsp if (f == NULL) {
3225 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3226 48ae06ee 2018-10-18 stsp goto done;
3228 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3229 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3230 fb43ecf1 2019-02-11 stsp goto done;
3234 15a94983 2018-12-23 stsp if (s->id1)
3235 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3237 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3239 15a94983 2018-12-23 stsp goto done;
3241 15a94983 2018-12-23 stsp switch (obj_type) {
3242 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3243 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3244 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3245 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3247 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3248 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3249 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3250 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3252 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3253 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3254 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3255 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3257 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3259 3ffacbe1 2020-02-02 tracey goto done;
3260 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3261 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3262 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3263 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3264 f44b1f58 2020-02-02 tracey if (err)
3265 f44b1f58 2020-02-02 tracey goto done;
3266 f44b1f58 2020-02-02 tracey } else {
3267 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3268 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3269 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3270 fe621944 2020-11-10 stsp err = write_commit_info(
3271 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3272 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3273 f44b1f58 2020-02-02 tracey if (err)
3274 f44b1f58 2020-02-02 tracey goto done;
3279 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3281 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3282 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3283 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3287 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3290 f44b1f58 2020-02-02 tracey if (err)
3291 f44b1f58 2020-02-02 tracey goto done;
3293 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3294 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3295 48ae06ee 2018-10-18 stsp return err;
3298 f5215bb9 2019-02-22 stsp static void
3299 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3301 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3302 f5215bb9 2019-02-22 stsp update_panels();
3303 f5215bb9 2019-02-22 stsp doupdate();
3306 f44b1f58 2020-02-02 tracey static const struct got_error *
3307 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3309 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3311 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3312 f44b1f58 2020-02-02 tracey return NULL;
3315 f44b1f58 2020-02-02 tracey static const struct got_error *
3316 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3318 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3319 f44b1f58 2020-02-02 tracey int lineno;
3321 f44b1f58 2020-02-02 tracey if (!view->searching) {
3322 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3323 f44b1f58 2020-02-02 tracey return NULL;
3326 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3327 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3328 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3330 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3331 f44b1f58 2020-02-02 tracey } else {
3332 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3333 f44b1f58 2020-02-02 tracey lineno = 1;
3335 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3338 f44b1f58 2020-02-02 tracey while (1) {
3339 f44b1f58 2020-02-02 tracey char *line = NULL;
3340 f44b1f58 2020-02-02 tracey off_t offset;
3341 f44b1f58 2020-02-02 tracey size_t len;
3343 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3344 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3345 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3346 f44b1f58 2020-02-02 tracey free(line);
3350 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3351 f44b1f58 2020-02-02 tracey lineno = 1;
3353 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3356 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3357 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3358 f44b1f58 2020-02-02 tracey free(line);
3359 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3361 f44b1f58 2020-02-02 tracey free(line);
3362 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3363 41605754 2020-11-12 stsp if (line &&
3364 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3365 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3366 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3367 f44b1f58 2020-02-02 tracey free(line);
3370 f44b1f58 2020-02-02 tracey free(line);
3371 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3372 f44b1f58 2020-02-02 tracey lineno++;
3374 f44b1f58 2020-02-02 tracey lineno--;
3377 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3378 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3379 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3382 f44b1f58 2020-02-02 tracey return NULL;
3385 48ae06ee 2018-10-18 stsp static const struct got_error *
3386 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3387 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3388 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3389 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3391 48ae06ee 2018-10-18 stsp const struct got_error *err;
3392 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3394 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3396 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3397 15a94983 2018-12-23 stsp int type1, type2;
3398 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3400 15a94983 2018-12-23 stsp return err;
3401 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3403 15a94983 2018-12-23 stsp return err;
3405 15a94983 2018-12-23 stsp if (type1 != type2)
3406 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3408 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3409 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3410 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3411 f44b1f58 2020-02-02 tracey s->repo = repo;
3412 f44b1f58 2020-02-02 tracey s->id1 = id1;
3413 f44b1f58 2020-02-02 tracey s->id2 = id2;
3414 3dbaef42 2020-11-24 stsp s->label1 = label1;
3415 3dbaef42 2020-11-24 stsp s->label2 = label2;
3417 15a94983 2018-12-23 stsp if (id1) {
3418 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3419 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3420 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3422 5465d566 2020-02-01 tracey s->id1 = NULL;
3424 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3425 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3426 5465d566 2020-02-01 tracey free(s->id1);
3427 5465d566 2020-02-01 tracey s->id1 = NULL;
3428 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3430 5465d566 2020-02-01 tracey s->f = NULL;
3431 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3432 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3433 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3434 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3435 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3436 5465d566 2020-02-01 tracey s->log_view = log_view;
3437 5465d566 2020-02-01 tracey s->repo = repo;
3439 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3440 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3441 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3442 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3443 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3445 6d17833f 2019-11-08 stsp return err;
3446 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3447 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3448 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3449 6d17833f 2019-11-08 stsp if (err) {
3450 5465d566 2020-02-01 tracey free_colors(&s->colors);
3451 6d17833f 2019-11-08 stsp return err;
3453 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3454 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3455 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3456 6d17833f 2019-11-08 stsp if (err) {
3457 5465d566 2020-02-01 tracey free_colors(&s->colors);
3458 6d17833f 2019-11-08 stsp return err;
3461 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3462 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3463 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3464 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3465 6d17833f 2019-11-08 stsp if (err) {
3466 5465d566 2020-02-01 tracey free_colors(&s->colors);
3467 6d17833f 2019-11-08 stsp return err;
3470 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3471 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3472 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3473 11b20872 2019-11-08 stsp if (err) {
3474 5465d566 2020-02-01 tracey free_colors(&s->colors);
3475 11b20872 2019-11-08 stsp return err;
3478 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3479 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3480 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3481 11b20872 2019-11-08 stsp if (err) {
3482 5465d566 2020-02-01 tracey free_colors(&s->colors);
3483 11b20872 2019-11-08 stsp return err;
3487 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3488 78756c87 2020-11-24 stsp if (err) {
3489 78756c87 2020-11-24 stsp free(s->id1);
3490 78756c87 2020-11-24 stsp s->id1 = NULL;
3491 78756c87 2020-11-24 stsp free(s->id2);
3492 78756c87 2020-11-24 stsp s->id2 = NULL;
3493 78756c87 2020-11-24 stsp free_colors(&s->colors);
3494 78756c87 2020-11-24 stsp return err;
3497 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3498 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3499 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3501 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3502 fe621944 2020-11-10 stsp s->nlines = 0;
3503 5465d566 2020-02-01 tracey err = create_diff(s);
3504 48ae06ee 2018-10-18 stsp if (err) {
3505 5465d566 2020-02-01 tracey free(s->id1);
3506 5465d566 2020-02-01 tracey s->id1 = NULL;
3507 5465d566 2020-02-01 tracey free(s->id2);
3508 5465d566 2020-02-01 tracey s->id2 = NULL;
3509 78756c87 2020-11-24 stsp free_colors(&s->colors);
3510 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3511 48ae06ee 2018-10-18 stsp return err;
3514 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3515 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3516 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3517 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3518 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3520 5dc9f4bc 2018-08-04 stsp return NULL;
3523 e5a0f69f 2018-08-18 stsp static const struct got_error *
3524 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3526 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3527 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3529 5465d566 2020-02-01 tracey free(s->id1);
3530 5465d566 2020-02-01 tracey s->id1 = NULL;
3531 5465d566 2020-02-01 tracey free(s->id2);
3532 5465d566 2020-02-01 tracey s->id2 = NULL;
3533 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3534 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3535 5465d566 2020-02-01 tracey free_colors(&s->colors);
3536 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3537 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3538 fe621944 2020-11-10 stsp s->nlines = 0;
3539 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3540 e5a0f69f 2018-08-18 stsp return err;
3543 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3544 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3546 a3404814 2018-09-02 stsp const struct got_error *err;
3547 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3548 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3549 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3551 a3404814 2018-09-02 stsp if (s->id1) {
3552 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3554 a3404814 2018-09-02 stsp return err;
3555 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3557 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3559 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3561 a3404814 2018-09-02 stsp return err;
3562 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3564 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3566 a3404814 2018-09-02 stsp free(id_str1);
3567 a3404814 2018-09-02 stsp free(id_str2);
3568 a3404814 2018-09-02 stsp return err;
3570 a3404814 2018-09-02 stsp free(id_str1);
3571 a3404814 2018-09-02 stsp free(id_str2);
3573 89f1a395 2020-12-01 naddy return draw_file(view, header);
3576 15a087fe 2019-02-21 stsp static const struct got_error *
3577 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3578 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3580 d7a04538 2019-02-21 stsp const struct got_error *err;
3581 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3582 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3583 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3585 15a087fe 2019-02-21 stsp free(s->id2);
3586 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3587 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3588 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3590 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3592 d7a04538 2019-02-21 stsp return err;
3593 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3594 15a087fe 2019-02-21 stsp free(s->id1);
3595 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3596 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3597 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3598 15a087fe 2019-02-21 stsp return NULL;
3601 0cf4efb1 2018-09-29 stsp static const struct got_error *
3602 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3604 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3605 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3606 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3607 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3610 e5a0f69f 2018-08-18 stsp switch (ch) {
3613 3dbaef42 2020-11-24 stsp if (ch == 'a')
3614 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3615 3dbaef42 2020-11-24 stsp if (ch == 'w')
3616 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3617 64453f7e 2020-11-21 stsp wclear(view->window);
3618 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3619 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3620 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3621 64453f7e 2020-11-21 stsp err = create_diff(s);
3624 1e37a5c2 2019-05-12 jcs case KEY_UP:
3625 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3626 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3628 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3629 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3630 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3633 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3634 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3635 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3638 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3639 1e37a5c2 2019-05-12 jcs if (!s->eof)
3640 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3642 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3643 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3645 00ba99a7 2019-05-12 jcs if (s->eof)
3648 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3649 1e37a5c2 2019-05-12 jcs char *line;
3650 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3651 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3652 1e37a5c2 2019-05-12 jcs if (line == NULL)
3657 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3658 1e37a5c2 2019-05-12 jcs s->diff_context--;
3659 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3660 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3661 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3662 27829c9e 2020-11-21 stsp s->nlines) {
3663 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3664 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3669 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3670 1e37a5c2 2019-05-12 jcs s->diff_context++;
3671 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3672 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3677 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3679 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3680 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3682 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3686 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3689 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3693 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3694 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3696 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3697 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3701 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3703 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3704 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3706 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3710 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3713 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3717 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3718 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3720 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3721 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3727 bcbd79e2 2018-08-19 stsp return err;
3730 4ed7e80c 2018-05-20 stsp static const struct got_error *
3731 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3733 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3734 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3735 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3736 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3737 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3738 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3739 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3740 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3741 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3742 3dbaef42 2020-11-24 stsp const char *errstr;
3743 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3745 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3746 26ed57b2 2018-05-19 stsp switch (ch) {
3748 64453f7e 2020-11-21 stsp force_text_diff = 1;
3751 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3753 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3754 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3756 09b5bff8 2020-02-23 naddy case 'r':
3757 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3758 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3759 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3761 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3764 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3767 17020d27 2019-03-07 stsp usage_diff();
3768 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3772 26ed57b2 2018-05-19 stsp argc -= optind;
3773 26ed57b2 2018-05-19 stsp argv += optind;
3775 26ed57b2 2018-05-19 stsp if (argc == 0) {
3776 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3777 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3778 15a94983 2018-12-23 stsp id_str1 = argv[0];
3779 15a94983 2018-12-23 stsp id_str2 = argv[1];
3781 26ed57b2 2018-05-19 stsp usage_diff();
3783 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3784 a273ac94 2020-02-23 naddy if (cwd == NULL)
3785 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3787 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3788 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3789 a273ac94 2020-02-23 naddy goto done;
3791 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3792 a273ac94 2020-02-23 naddy if (worktree)
3793 a273ac94 2020-02-23 naddy repo_path =
3794 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3796 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3798 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3799 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3800 a273ac94 2020-02-23 naddy goto done;
3803 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3804 eb6600df 2019-01-04 stsp if (error)
3805 eb6600df 2019-01-04 stsp goto done;
3807 a273ac94 2020-02-23 naddy init_curses();
3809 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3810 26ed57b2 2018-05-19 stsp if (error)
3811 26ed57b2 2018-05-19 stsp goto done;
3813 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3814 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3815 26ed57b2 2018-05-19 stsp if (error)
3816 26ed57b2 2018-05-19 stsp goto done;
3818 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3819 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3820 26ed57b2 2018-05-19 stsp if (error)
3821 26ed57b2 2018-05-19 stsp goto done;
3823 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3824 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3825 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3826 ea5e7bb5 2018-08-01 stsp goto done;
3828 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3829 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3830 5dc9f4bc 2018-08-04 stsp if (error)
3831 5dc9f4bc 2018-08-04 stsp goto done;
3832 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3834 3dbaef42 2020-11-24 stsp free(label1);
3835 3dbaef42 2020-11-24 stsp free(label2);
3836 c02c541e 2019-03-29 stsp free(repo_path);
3837 a273ac94 2020-02-23 naddy free(cwd);
3839 921be706 2019-06-28 stsp got_repo_close(repo);
3840 a273ac94 2020-02-23 naddy if (worktree)
3841 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3842 26ed57b2 2018-05-19 stsp return error;
3845 4ed7e80c 2018-05-20 stsp __dead static void
3846 9f7d7167 2018-04-29 stsp usage_blame(void)
3849 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3850 9f7d7167 2018-04-29 stsp getprogname());
3854 84451b3e 2018-07-10 stsp struct tog_blame_line {
3855 84451b3e 2018-07-10 stsp int annotated;
3856 84451b3e 2018-07-10 stsp struct got_object_id *id;
3859 4ed7e80c 2018-05-20 stsp static const struct got_error *
3860 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3862 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3863 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3864 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3865 84451b3e 2018-07-10 stsp const struct got_error *err;
3866 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3867 84451b3e 2018-07-10 stsp char *line;
3868 84451b3e 2018-07-10 stsp size_t len;
3869 84451b3e 2018-07-10 stsp wchar_t *wline;
3870 27a741e5 2019-09-11 stsp int width;
3871 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3872 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3873 ab089a2a 2018-07-12 stsp char *id_str;
3874 11b20872 2019-11-08 stsp struct tog_color *tc;
3876 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3878 ab089a2a 2018-07-12 stsp return err;
3880 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3881 f7d12f7e 2018-08-01 stsp werase(view->window);
3883 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3884 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3885 ab089a2a 2018-07-12 stsp free(id_str);
3886 ab089a2a 2018-07-12 stsp return err;
3889 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3890 ab089a2a 2018-07-12 stsp free(line);
3891 2550e4c3 2018-07-13 stsp line = NULL;
3893 1cae65b4 2019-09-22 stsp return err;
3894 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3895 a3404814 2018-09-02 stsp wstandout(view->window);
3896 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3898 11b20872 2019-11-08 stsp wattr_on(view->window,
3899 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3900 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3902 11b20872 2019-11-08 stsp wattr_off(view->window,
3903 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3904 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3905 a3404814 2018-09-02 stsp wstandend(view->window);
3906 2550e4c3 2018-07-13 stsp free(wline);
3907 2550e4c3 2018-07-13 stsp wline = NULL;
3908 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3909 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3911 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3912 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3913 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3914 ab089a2a 2018-07-12 stsp free(id_str);
3915 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3917 ab089a2a 2018-07-12 stsp free(id_str);
3918 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3919 3f60a8ef 2018-07-10 stsp free(line);
3920 2550e4c3 2018-07-13 stsp line = NULL;
3922 3f60a8ef 2018-07-10 stsp return err;
3923 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3924 2550e4c3 2018-07-13 stsp free(wline);
3925 2550e4c3 2018-07-13 stsp wline = NULL;
3926 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3927 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3929 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3930 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3931 4f7c3e5e 2020-12-01 naddy line = parse_next_line(blame->f, &len);
3932 84451b3e 2018-07-10 stsp if (line == NULL) {
3933 4f7c3e5e 2020-12-01 naddy s->eof = 1;
3936 4f7c3e5e 2020-12-01 naddy if (++lineno < s->first_displayed_line) {
3937 84451b3e 2018-07-10 stsp free(line);
3941 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3942 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3944 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3945 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3946 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3947 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3948 27a741e5 2019-09-11 stsp !(view->focussed &&
3949 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3950 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3951 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3952 8d0fe45a 2019-08-12 stsp char *id_str;
3953 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3954 8d0fe45a 2019-08-12 stsp if (err) {
3955 8d0fe45a 2019-08-12 stsp free(line);
3956 8d0fe45a 2019-08-12 stsp return err;
3958 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3960 11b20872 2019-11-08 stsp wattr_on(view->window,
3961 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3962 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3964 11b20872 2019-11-08 stsp wattr_off(view->window,
3965 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3966 8d0fe45a 2019-08-12 stsp free(id_str);
3967 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3969 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3970 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3973 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3974 ee41ec32 2018-07-10 stsp prev_id = NULL;
3977 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3978 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3979 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3981 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3982 41605754 2020-11-12 stsp width = 9;
3983 41605754 2020-11-12 stsp wline = wcsdup(L"");
3984 41605754 2020-11-12 stsp if (wline == NULL) {
3985 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
3986 41605754 2020-11-12 stsp free(line);
3987 41605754 2020-11-12 stsp return err;
3989 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
3990 4f7c3e5e 2020-12-01 naddy s->matched_line &&
3991 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3992 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
3993 41605754 2020-11-12 stsp view->window, regmatch);
3994 41605754 2020-11-12 stsp if (err) {
3995 41605754 2020-11-12 stsp free(line);
3996 41605754 2020-11-12 stsp return err;
3998 41605754 2020-11-12 stsp width += 9;
4000 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4001 41605754 2020-11-12 stsp view->ncols - 9, 9);
4002 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4003 41605754 2020-11-12 stsp free(wline);
4004 41605754 2020-11-12 stsp wline = NULL;
4005 41605754 2020-11-12 stsp width += 9;
4008 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4009 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4010 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4011 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4012 84451b3e 2018-07-10 stsp free(line);
4014 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4016 1a57306a 2018-09-02 stsp view_vborder(view);
4018 84451b3e 2018-07-10 stsp return NULL;
4021 84451b3e 2018-07-10 stsp static const struct got_error *
4022 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4024 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4025 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4026 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4027 1a76625f 2018-10-22 stsp int errcode;
4029 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4030 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4031 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4033 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4034 1a76625f 2018-10-22 stsp if (errcode)
4035 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4037 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4038 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4039 d68a0a7d 2018-07-10 stsp goto done;
4042 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4043 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4045 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4046 d68a0a7d 2018-07-10 stsp if (line->annotated)
4047 d68a0a7d 2018-07-10 stsp goto done;
4049 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4050 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4051 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4052 84451b3e 2018-07-10 stsp goto done;
4054 84451b3e 2018-07-10 stsp line->annotated = 1;
4056 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4057 1a76625f 2018-10-22 stsp if (errcode)
4058 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4059 84451b3e 2018-07-10 stsp return err;
4062 84451b3e 2018-07-10 stsp static void *
4063 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4065 18430de3 2018-07-10 stsp const struct got_error *err;
4066 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4067 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4068 1a76625f 2018-10-22 stsp int errcode;
4070 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4072 61266923 2020-01-14 stsp return (void *)err;
4074 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4075 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4076 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4077 fc06ba56 2019-08-22 stsp err = NULL;
4079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4080 1a76625f 2018-10-22 stsp if (errcode)
4081 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4082 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4084 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4085 c9beca56 2018-07-22 stsp ta->repo = NULL;
4086 c9beca56 2018-07-22 stsp *ta->complete = 1;
4088 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4089 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4090 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4092 18430de3 2018-07-10 stsp return (void *)err;
4095 245d91c1 2018-07-12 stsp static struct got_object_id *
4096 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4097 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4099 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4101 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4102 8d0fe45a 2019-08-12 stsp return NULL;
4104 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4105 245d91c1 2018-07-12 stsp if (!line->annotated)
4106 245d91c1 2018-07-12 stsp return NULL;
4108 245d91c1 2018-07-12 stsp return line->id;
4111 b880a918 2018-07-10 stsp static const struct got_error *
4112 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4114 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4117 245d91c1 2018-07-12 stsp if (blame->thread) {
4118 1a76625f 2018-10-22 stsp int errcode;
4119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4120 1a76625f 2018-10-22 stsp if (errcode)
4121 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4122 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4123 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4124 1a76625f 2018-10-22 stsp if (errcode)
4125 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4126 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4127 1a76625f 2018-10-22 stsp if (errcode)
4128 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4129 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4130 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4131 245d91c1 2018-07-12 stsp err = NULL;
4132 245d91c1 2018-07-12 stsp blame->thread = NULL;
4134 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4135 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4136 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4138 245d91c1 2018-07-12 stsp if (blame->f) {
4139 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4140 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4141 245d91c1 2018-07-12 stsp blame->f = NULL;
4143 57670559 2018-12-23 stsp if (blame->lines) {
4144 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4145 57670559 2018-12-23 stsp free(blame->lines[i].id);
4146 57670559 2018-12-23 stsp free(blame->lines);
4147 57670559 2018-12-23 stsp blame->lines = NULL;
4149 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4150 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4152 245d91c1 2018-07-12 stsp return err;
4155 245d91c1 2018-07-12 stsp static const struct got_error *
4156 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4158 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4159 fc06ba56 2019-08-22 stsp int *done = arg;
4160 fc06ba56 2019-08-22 stsp int errcode;
4162 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4163 fc06ba56 2019-08-22 stsp if (errcode)
4164 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4165 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4167 fc06ba56 2019-08-22 stsp if (*done)
4168 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4170 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4171 fc06ba56 2019-08-22 stsp if (errcode)
4172 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4173 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4175 fc06ba56 2019-08-22 stsp return err;
4178 fc06ba56 2019-08-22 stsp static const struct got_error *
4179 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4181 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4182 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4183 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4184 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4185 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4186 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4187 15a94983 2018-12-23 stsp int obj_type;
4189 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4190 a5388363 2020-12-01 naddy s->path);
4192 15a94983 2018-12-23 stsp return err;
4194 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4196 84451b3e 2018-07-10 stsp goto done;
4198 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4199 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4200 84451b3e 2018-07-10 stsp goto done;
4203 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4205 a70480e0 2018-06-23 stsp goto done;
4206 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4207 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4208 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4209 84451b3e 2018-07-10 stsp goto done;
4211 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4212 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4213 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4214 84451b3e 2018-07-10 stsp goto done;
4216 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4217 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4218 b02560ec 2019-08-19 stsp blame->nlines--;
4220 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4221 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4222 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4223 84451b3e 2018-07-10 stsp goto done;
4226 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4228 bd24772e 2018-07-11 stsp goto done;
4230 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4231 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4232 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4233 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4234 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4235 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4236 245d91c1 2018-07-12 stsp goto done;
4238 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4240 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4241 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4242 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4243 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4244 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4245 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4246 a5388363 2020-12-01 naddy s->blame_complete = 0;
4250 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4251 27d434c2 2018-09-15 stsp free(obj_id);
4253 245d91c1 2018-07-12 stsp stop_blame(blame);
4254 245d91c1 2018-07-12 stsp return err;
4257 245d91c1 2018-07-12 stsp static const struct got_error *
4258 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4259 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4261 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4262 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4264 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4266 c4843652 2019-08-12 stsp s->path = strdup(path);
4267 c4843652 2019-08-12 stsp if (s->path == NULL)
4268 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4270 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4271 c4843652 2019-08-12 stsp if (err) {
4272 c4843652 2019-08-12 stsp free(s->path);
4273 7cbe629d 2018-08-04 stsp return err;
4276 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4277 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4278 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4279 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4280 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4281 fb2756b9 2018-08-04 stsp s->repo = repo;
4282 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4283 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4285 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4286 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4287 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4288 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4290 11b20872 2019-11-08 stsp return err;
4293 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4294 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4295 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4296 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4297 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4299 a5388363 2020-12-01 naddy return run_blame(view);
4302 e5a0f69f 2018-08-18 stsp static const struct got_error *
4303 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4305 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4306 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4308 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4309 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4311 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4312 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4313 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4314 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4315 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4318 e5a0f69f 2018-08-18 stsp free(s->path);
4319 11b20872 2019-11-08 stsp free_colors(&s->colors);
4321 e5a0f69f 2018-08-18 stsp return err;
4324 7cbe629d 2018-08-04 stsp static const struct got_error *
4325 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4327 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4329 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4330 6c4c42e0 2019-06-24 stsp return NULL;
4333 6c4c42e0 2019-06-24 stsp static const struct got_error *
4334 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4336 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4337 6c4c42e0 2019-06-24 stsp int lineno;
4339 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4340 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4341 6c4c42e0 2019-06-24 stsp return NULL;
4344 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4345 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4346 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4348 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4350 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4351 6c4c42e0 2019-06-24 stsp lineno = 1;
4353 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4356 6c4c42e0 2019-06-24 stsp while (1) {
4357 6c4c42e0 2019-06-24 stsp char *line = NULL;
4358 6c4c42e0 2019-06-24 stsp off_t offset;
4359 6c4c42e0 2019-06-24 stsp size_t len;
4361 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4362 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4363 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4364 6c4c42e0 2019-06-24 stsp free(line);
4368 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4369 6c4c42e0 2019-06-24 stsp lineno = 1;
4371 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4374 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4375 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4376 6c4c42e0 2019-06-24 stsp free(line);
4377 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4379 6c4c42e0 2019-06-24 stsp free(line);
4380 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4381 41605754 2020-11-12 stsp if (line &&
4382 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4383 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4384 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4385 6c4c42e0 2019-06-24 stsp free(line);
4388 6c4c42e0 2019-06-24 stsp free(line);
4389 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4395 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4396 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4397 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4400 6c4c42e0 2019-06-24 stsp return NULL;
4403 6c4c42e0 2019-06-24 stsp static const struct got_error *
4404 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4406 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4407 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4408 2b380cc8 2018-10-24 stsp int errcode;
4410 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4411 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4412 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4413 2b380cc8 2018-10-24 stsp if (errcode)
4414 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4416 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4419 51fe7530 2019-08-19 stsp if (s->blame_complete)
4420 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4422 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4424 669b5ffa 2018-10-07 stsp view_vborder(view);
4425 e5a0f69f 2018-08-18 stsp return err;
4428 e5a0f69f 2018-08-18 stsp static const struct got_error *
4429 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4431 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4432 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4433 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4434 669b5ffa 2018-10-07 stsp int begin_x = 0;
4436 e5a0f69f 2018-08-18 stsp switch (ch) {
4438 1e37a5c2 2019-05-12 jcs s->done = 1;
4441 1e37a5c2 2019-05-12 jcs case KEY_UP:
4442 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4443 1e37a5c2 2019-05-12 jcs s->selected_line--;
4444 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4445 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4446 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4448 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4449 ea025d1d 2020-02-22 naddy case CTRL('b'):
4450 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4451 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4454 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4455 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4456 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4458 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4461 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4462 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4463 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4464 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4465 1e37a5c2 2019-05-12 jcs s->selected_line++;
4466 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4467 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4468 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4471 1e37a5c2 2019-05-12 jcs case 'p': {
4472 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4473 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4474 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4475 1e37a5c2 2019-05-12 jcs if (id == NULL)
4477 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4478 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4479 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4480 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4481 1e37a5c2 2019-05-12 jcs int obj_type;
4482 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4483 1e37a5c2 2019-05-12 jcs s->repo, id);
4486 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4487 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4488 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4489 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4492 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4493 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4494 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4495 e5a0f69f 2018-08-18 stsp if (err) {
4496 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4497 1e37a5c2 2019-05-12 jcs err = NULL;
4498 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4501 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4503 1e37a5c2 2019-05-12 jcs free(blob_id);
4504 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4505 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4506 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4509 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4511 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4513 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4514 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4516 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4521 1e37a5c2 2019-05-12 jcs s->done = 1;
4522 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4523 1e37a5c2 2019-05-12 jcs s->done = 0;
4524 1e37a5c2 2019-05-12 jcs if (thread_err)
4526 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4527 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4528 a5388363 2020-12-01 naddy err = run_blame(view);
4533 1e37a5c2 2019-05-12 jcs case 'B': {
4534 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4535 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4536 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4538 1e37a5c2 2019-05-12 jcs s->done = 1;
4539 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4540 1e37a5c2 2019-05-12 jcs s->done = 0;
4541 1e37a5c2 2019-05-12 jcs if (thread_err)
4543 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4544 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4545 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4546 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4547 a5388363 2020-12-01 naddy err = run_blame(view);
4552 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4553 1e37a5c2 2019-05-12 jcs case '\r': {
4554 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4555 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4556 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4557 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4558 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4559 1e37a5c2 2019-05-12 jcs if (id == NULL)
4561 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4564 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4565 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4566 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4567 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4568 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4569 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4570 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4571 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4574 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4575 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4576 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4578 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4581 e78dc838 2020-12-04 stsp view->focussed = 0;
4582 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4583 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4584 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4587 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4588 e78dc838 2020-12-04 stsp view->focus_child = 1;
4590 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4595 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4596 ea025d1d 2020-02-22 naddy case CTRL('f'):
4598 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4599 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4600 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4603 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4604 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4605 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4606 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4609 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4610 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4611 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4612 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4614 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4615 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4616 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4618 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4619 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4620 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4621 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4627 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4630 a70480e0 2018-06-23 stsp static const struct got_error *
4631 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4633 a70480e0 2018-06-23 stsp const struct got_error *error;
4634 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4635 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4636 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4637 0587e10c 2020-07-23 stsp char *link_target = NULL;
4638 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4639 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4641 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4643 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4644 a70480e0 2018-06-23 stsp switch (ch) {
4646 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4649 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4650 69069811 2018-08-02 stsp if (repo_path == NULL)
4651 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4655 17020d27 2019-03-07 stsp usage_blame();
4656 a70480e0 2018-06-23 stsp /* NOTREACHED */
4660 a70480e0 2018-06-23 stsp argc -= optind;
4661 a70480e0 2018-06-23 stsp argv += optind;
4663 f135c941 2020-02-20 stsp if (argc != 1)
4664 a70480e0 2018-06-23 stsp usage_blame();
4666 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4667 6962eb72 2020-02-20 stsp if (cwd == NULL)
4668 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4670 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4671 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4672 f135c941 2020-02-20 stsp goto done;
4674 69069811 2018-08-02 stsp if (repo_path == NULL) {
4675 f135c941 2020-02-20 stsp if (worktree)
4676 eb41ed75 2019-02-05 stsp repo_path =
4677 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4679 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4681 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4682 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4683 f135c941 2020-02-20 stsp goto done;
4686 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4687 c02c541e 2019-03-29 stsp if (error != NULL)
4688 8e94dd5b 2019-01-04 stsp goto done;
4690 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4691 f135c941 2020-02-20 stsp worktree);
4692 c02c541e 2019-03-29 stsp if (error)
4693 92205607 2019-01-04 stsp goto done;
4695 f135c941 2020-02-20 stsp init_curses();
4697 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4698 eb41ed75 2019-02-05 stsp if (error)
4699 69069811 2018-08-02 stsp goto done;
4701 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4702 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4703 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4704 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4705 a70480e0 2018-06-23 stsp if (error != NULL)
4706 66b4983c 2018-06-23 stsp goto done;
4707 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4708 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4710 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4711 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4713 a19e88aa 2018-06-23 stsp if (error != NULL)
4714 8b473291 2019-02-21 stsp goto done;
4716 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4717 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4718 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4719 e1cd8fed 2018-08-01 stsp goto done;
4722 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4723 0587e10c 2020-07-23 stsp commit_id, repo);
4724 7cbe629d 2018-08-04 stsp if (error)
4725 7cbe629d 2018-08-04 stsp goto done;
4727 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4728 78756c87 2020-11-24 stsp commit_id, repo);
4729 0587e10c 2020-07-23 stsp if (error)
4730 0587e10c 2020-07-23 stsp goto done;
4731 12314ad4 2019-08-31 stsp if (worktree) {
4732 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4733 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4734 12314ad4 2019-08-31 stsp worktree = NULL;
4736 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4738 69069811 2018-08-02 stsp free(repo_path);
4739 f135c941 2020-02-20 stsp free(in_repo_path);
4740 0587e10c 2020-07-23 stsp free(link_target);
4741 69069811 2018-08-02 stsp free(cwd);
4742 a70480e0 2018-06-23 stsp free(commit_id);
4743 eb41ed75 2019-02-05 stsp if (worktree)
4744 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4746 a70480e0 2018-06-23 stsp got_repo_close(repo);
4747 a70480e0 2018-06-23 stsp return error;
4750 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4751 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4753 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4754 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4755 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4756 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4757 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4758 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4759 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4761 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4763 f7d12f7e 2018-08-01 stsp werase(view->window);
4765 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4766 ffd1d5e5 2018-06-23 stsp return NULL;
4768 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4770 ffd1d5e5 2018-06-23 stsp return err;
4771 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4772 a3404814 2018-09-02 stsp wstandout(view->window);
4773 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4775 11b20872 2019-11-08 stsp wattr_on(view->window,
4776 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4777 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4779 11b20872 2019-11-08 stsp wattr_off(view->window,
4780 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4781 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4782 a3404814 2018-09-02 stsp wstandend(view->window);
4783 2550e4c3 2018-07-13 stsp free(wline);
4784 2550e4c3 2018-07-13 stsp wline = NULL;
4785 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4786 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4787 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4788 ffd1d5e5 2018-06-23 stsp return NULL;
4789 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4791 ce52c690 2018-06-23 stsp return err;
4792 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4793 2550e4c3 2018-07-13 stsp free(wline);
4794 2550e4c3 2018-07-13 stsp wline = NULL;
4795 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4796 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4797 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4798 ffd1d5e5 2018-06-23 stsp return NULL;
4799 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4800 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4801 a1eca9bb 2018-06-23 stsp return NULL;
4803 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4804 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4805 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4806 0cf4efb1 2018-09-29 stsp if (view->focussed)
4807 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4808 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4810 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4811 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4812 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4813 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4814 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4815 ffd1d5e5 2018-06-23 stsp return NULL;
4819 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4822 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4823 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4824 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4825 848d6979 2019-08-12 stsp const char *modestr = "";
4826 56e0773d 2019-11-28 stsp mode_t mode;
4828 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4829 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4831 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4832 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4833 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4835 638f9024 2019-05-13 stsp return got_error_from_errno(
4836 230a42bd 2019-05-11 jcs "got_object_id_str");
4838 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4839 63c5ca5d 2019-08-24 stsp modestr = "$";
4840 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4843 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4844 d86d3b18 2020-12-01 naddy te, s->repo);
4845 0d6c6ee3 2020-05-20 stsp if (err) {
4846 0d6c6ee3 2020-05-20 stsp free(id_str);
4847 0d6c6ee3 2020-05-20 stsp return err;
4849 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4850 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4851 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4853 848d6979 2019-08-12 stsp modestr = "@";
4855 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4856 848d6979 2019-08-12 stsp modestr = "/";
4857 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4858 848d6979 2019-08-12 stsp modestr = "*";
4859 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4860 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4861 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4862 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4863 1d13200f 2018-07-12 stsp free(id_str);
4864 0d6c6ee3 2020-05-20 stsp free(link_target);
4865 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4867 1d13200f 2018-07-12 stsp free(id_str);
4868 0d6c6ee3 2020-05-20 stsp free(link_target);
4869 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4870 ffd1d5e5 2018-06-23 stsp if (err) {
4871 ffd1d5e5 2018-06-23 stsp free(line);
4874 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4875 0cf4efb1 2018-09-29 stsp if (view->focussed)
4876 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4877 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4879 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4881 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4882 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4883 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4885 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4886 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4887 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4888 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4889 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4890 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4891 ffd1d5e5 2018-06-23 stsp free(line);
4892 2550e4c3 2018-07-13 stsp free(wline);
4893 2550e4c3 2018-07-13 stsp wline = NULL;
4895 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4896 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4897 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4901 ffd1d5e5 2018-06-23 stsp return err;
4904 ffd1d5e5 2018-06-23 stsp static void
4905 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4907 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4908 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4909 fa86c4bf 2020-11-29 stsp int i = 0;
4911 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4914 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4915 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4916 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4917 fa86c4bf 2020-11-29 stsp if (!isroot)
4918 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4921 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4922 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4926 fa86c4bf 2020-11-29 stsp static void
4927 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4929 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4930 ffd1d5e5 2018-06-23 stsp int n = 0;
4932 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4933 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4934 694d3271 2020-12-01 naddy s->first_displayed_entry);
4936 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4938 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4939 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4940 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4941 768394f3 2019-01-24 stsp if (last) {
4942 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4943 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4948 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4949 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4950 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4952 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4953 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4954 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4956 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4957 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4958 56e0773d 2019-11-28 stsp + 1 /* slash */;
4960 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4962 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4963 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4964 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4966 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4967 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4968 d9765a41 2018-06-23 stsp while (pt) {
4969 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4970 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4971 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4972 cb2ebc8a 2018-06-23 stsp goto done;
4974 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4975 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4976 cb2ebc8a 2018-06-23 stsp goto done;
4978 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4981 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4982 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4983 ce52c690 2018-06-23 stsp goto done;
4987 ce52c690 2018-06-23 stsp if (err) {
4988 ce52c690 2018-06-23 stsp free(*path);
4989 ce52c690 2018-06-23 stsp *path = NULL;
4991 ce52c690 2018-06-23 stsp return err;
4994 ce52c690 2018-06-23 stsp static const struct got_error *
4995 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4996 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4997 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4999 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5000 ce52c690 2018-06-23 stsp char *path;
5001 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5003 a0de39f3 2019-08-09 stsp *new_view = NULL;
5005 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5007 ce52c690 2018-06-23 stsp return err;
5009 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5010 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5011 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5012 83ce39e3 2019-08-12 stsp goto done;
5015 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5016 e5a0f69f 2018-08-18 stsp if (err) {
5017 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5018 fc06ba56 2019-08-22 stsp err = NULL;
5019 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5021 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5023 83ce39e3 2019-08-12 stsp free(path);
5024 69efd4c4 2018-07-18 stsp return err;
5027 69efd4c4 2018-07-18 stsp static const struct got_error *
5028 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5029 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5031 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5032 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5033 69efd4c4 2018-07-18 stsp char *path;
5035 a0de39f3 2019-08-09 stsp *new_view = NULL;
5037 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5038 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5039 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5041 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5043 69efd4c4 2018-07-18 stsp return err;
5045 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5048 e5a0f69f 2018-08-18 stsp view_close(log_view);
5050 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5051 cb2ebc8a 2018-06-23 stsp free(path);
5052 cb2ebc8a 2018-06-23 stsp return err;
5055 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5056 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5057 4e97c21c 2020-12-06 stsp struct got_object_id *commit_id, const char *head_ref_name,
5058 4e97c21c 2020-12-06 stsp struct got_repository *repo)
5060 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5061 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5062 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5064 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5066 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5067 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5068 ffd1d5e5 2018-06-23 stsp goto done;
5070 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5071 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5072 ffd1d5e5 2018-06-23 stsp goto done;
5075 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5076 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5077 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5078 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5079 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5080 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5081 6484ec90 2018-09-29 stsp goto done;
5083 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5084 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5085 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5086 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5087 9cd7cbd1 2020-12-07 stsp goto done;
5090 fb2756b9 2018-08-04 stsp s->repo = repo;
5092 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5094 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5096 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5097 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5099 c0b01bdb 2019-11-08 stsp goto done;
5100 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5101 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5102 c0b01bdb 2019-11-08 stsp if (err) {
5103 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5104 c0b01bdb 2019-11-08 stsp goto done;
5106 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5107 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5108 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5109 c0b01bdb 2019-11-08 stsp if (err) {
5110 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5111 c0b01bdb 2019-11-08 stsp goto done;
5114 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5115 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5116 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5117 11b20872 2019-11-08 stsp if (err) {
5118 11b20872 2019-11-08 stsp free_colors(&s->colors);
5119 11b20872 2019-11-08 stsp goto done;
5122 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5123 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5124 c0b01bdb 2019-11-08 stsp if (err) {
5125 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5126 c0b01bdb 2019-11-08 stsp goto done;
5130 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5131 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5132 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5133 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5134 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5136 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5137 6484ec90 2018-09-29 stsp if (err) {
5138 fb2756b9 2018-08-04 stsp free(s->tree_label);
5139 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5141 ad80ab7b 2018-08-04 stsp return err;
5144 e5a0f69f 2018-08-18 stsp static const struct got_error *
5145 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5147 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5149 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5150 fb2756b9 2018-08-04 stsp free(s->tree_label);
5151 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5152 6484ec90 2018-09-29 stsp free(s->commit_id);
5153 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5154 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5155 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5156 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5157 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5158 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5159 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5160 ad80ab7b 2018-08-04 stsp free(parent);
5163 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5164 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5165 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5166 7c32bd05 2019-06-22 stsp return NULL;
5169 7c32bd05 2019-06-22 stsp static const struct got_error *
5170 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5172 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5174 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5175 7c32bd05 2019-06-22 stsp return NULL;
5178 7c32bd05 2019-06-22 stsp static int
5179 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5181 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5183 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5187 7c32bd05 2019-06-22 stsp static const struct got_error *
5188 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5190 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5191 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5193 7c32bd05 2019-06-22 stsp if (!view->searching) {
5194 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5195 7c32bd05 2019-06-22 stsp return NULL;
5198 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5199 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5200 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5201 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5202 56e0773d 2019-11-28 stsp s->selected_entry);
5204 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5206 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5207 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5209 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5210 56e0773d 2019-11-28 stsp s->selected_entry);
5213 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5214 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5216 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5219 7c32bd05 2019-06-22 stsp while (1) {
5220 56e0773d 2019-11-28 stsp if (te == NULL) {
5221 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5222 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5223 ac66afb8 2019-06-24 stsp return NULL;
5225 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5226 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5228 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5231 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5232 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5233 56e0773d 2019-11-28 stsp s->matched_entry = te;
5237 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5238 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5240 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5243 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5244 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5245 7c32bd05 2019-06-22 stsp s->selected = 0;
5248 e5a0f69f 2018-08-18 stsp return NULL;
5251 ad80ab7b 2018-08-04 stsp static const struct got_error *
5252 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5254 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5255 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5256 e5a0f69f 2018-08-18 stsp char *parent_path;
5258 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5260 e5a0f69f 2018-08-18 stsp return err;
5262 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5263 e5a0f69f 2018-08-18 stsp free(parent_path);
5265 669b5ffa 2018-10-07 stsp view_vborder(view);
5266 e5a0f69f 2018-08-18 stsp return err;
5269 e5a0f69f 2018-08-18 stsp static const struct got_error *
5270 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5272 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5273 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5274 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5275 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5277 e5a0f69f 2018-08-18 stsp switch (ch) {
5279 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5282 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5284 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5285 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5286 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5287 e78dc838 2020-12-04 stsp view->focussed = 0;
5288 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5289 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5290 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5292 1e37a5c2 2019-05-12 jcs return err;
5293 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5294 e78dc838 2020-12-04 stsp view->focus_child = 1;
5296 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5299 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5300 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5301 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5302 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5303 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5304 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5305 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5306 152c1c93 2020-11-29 stsp if (err) {
5307 152c1c93 2020-11-29 stsp view_close(ref_view);
5308 152c1c93 2020-11-29 stsp return err;
5310 e78dc838 2020-12-04 stsp view->focussed = 0;
5311 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5312 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5313 152c1c93 2020-11-29 stsp err = view_close_child(view);
5315 152c1c93 2020-11-29 stsp return err;
5316 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5317 e78dc838 2020-12-04 stsp view->focus_child = 1;
5319 152c1c93 2020-11-29 stsp *new_view = ref_view;
5322 1e37a5c2 2019-05-12 jcs case KEY_UP:
5323 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5324 1e37a5c2 2019-05-12 jcs s->selected--;
5327 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5329 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5330 ea025d1d 2020-02-22 naddy case CTRL('b'):
5331 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5332 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5333 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5334 fa86c4bf 2020-11-29 stsp s->selected = 0;
5336 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5337 fa86c4bf 2020-11-29 stsp s->selected = 0;
5339 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5342 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5343 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5344 1e37a5c2 2019-05-12 jcs s->selected++;
5347 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5349 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5351 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5353 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5354 ea025d1d 2020-02-22 naddy case CTRL('f'):
5355 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5357 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5358 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5359 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5362 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5364 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5366 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5367 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5368 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5369 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5370 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5372 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5373 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5375 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5376 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5377 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5378 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5379 1e37a5c2 2019-05-12 jcs s->selected_entry =
5380 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5381 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5382 1e37a5c2 2019-05-12 jcs free(parent);
5383 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5384 56e0773d 2019-11-28 stsp s->selected_entry))) {
5385 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5386 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5387 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5390 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5391 941e9f74 2019-05-21 stsp if (err) {
5392 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5395 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5396 56e0773d 2019-11-28 stsp s->selected_entry))) {
5397 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5398 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5399 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5401 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5402 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5403 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5406 e78dc838 2020-12-04 stsp view->focussed = 0;
5407 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5408 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5409 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5411 669b5ffa 2018-10-07 stsp return err;
5412 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5413 e78dc838 2020-12-04 stsp view->focus_child = 1;
5415 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5418 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5419 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5420 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5426 ffd1d5e5 2018-06-23 stsp return err;
5429 ffd1d5e5 2018-06-23 stsp __dead static void
5430 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5433 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5434 ffd1d5e5 2018-06-23 stsp getprogname());
5438 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5439 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5441 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5442 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5443 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5444 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5445 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5446 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5447 4e97c21c 2020-12-06 stsp char *label = NULL;
5448 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5449 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5450 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5451 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5453 5221c383 2018-08-01 stsp struct tog_view *view;
5455 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5456 ffd1d5e5 2018-06-23 stsp switch (ch) {
5458 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5461 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5462 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5463 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5467 e99e2d15 2020-11-24 naddy usage_tree();
5468 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5472 ffd1d5e5 2018-06-23 stsp argc -= optind;
5473 ffd1d5e5 2018-06-23 stsp argv += optind;
5475 55cccc34 2020-02-20 stsp if (argc > 1)
5476 e99e2d15 2020-11-24 naddy usage_tree();
5478 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5479 55cccc34 2020-02-20 stsp if (cwd == NULL)
5480 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5482 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5483 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5484 55cccc34 2020-02-20 stsp goto done;
5486 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5487 55cccc34 2020-02-20 stsp if (worktree)
5488 52185f70 2019-02-05 stsp repo_path =
5489 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5491 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5493 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5494 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5495 55cccc34 2020-02-20 stsp goto done;
5498 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5499 c02c541e 2019-03-29 stsp if (error != NULL)
5500 52185f70 2019-02-05 stsp goto done;
5502 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5503 55cccc34 2020-02-20 stsp repo, worktree);
5504 55cccc34 2020-02-20 stsp if (error)
5505 55cccc34 2020-02-20 stsp goto done;
5507 55cccc34 2020-02-20 stsp init_curses();
5509 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5510 c02c541e 2019-03-29 stsp if (error)
5511 52185f70 2019-02-05 stsp goto done;
5513 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5514 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5515 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5516 4e97c21c 2020-12-06 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5517 4e97c21c 2020-12-06 stsp if (error)
5518 4e97c21c 2020-12-06 stsp goto done;
5519 4e97c21c 2020-12-06 stsp head_ref_name = label;
5521 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5522 4e97c21c 2020-12-06 stsp if (error == NULL)
5523 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5524 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5525 4e97c21c 2020-12-06 stsp goto done;
5526 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5527 4e97c21c 2020-12-06 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5528 4e97c21c 2020-12-06 stsp if (error)
5529 4e97c21c 2020-12-06 stsp goto done;
5532 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5533 55cccc34 2020-02-20 stsp if (error)
5534 ffd1d5e5 2018-06-23 stsp goto done;
5536 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5537 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5538 55cccc34 2020-02-20 stsp if (error)
5539 8b473291 2019-02-21 stsp goto done;
5541 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5542 5221c383 2018-08-01 stsp if (view == NULL) {
5543 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5544 5221c383 2018-08-01 stsp goto done;
5546 4e97c21c 2020-12-06 stsp error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5547 ad80ab7b 2018-08-04 stsp if (error)
5548 ad80ab7b 2018-08-04 stsp goto done;
5549 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5550 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5551 d91faf3b 2020-12-01 naddy in_repo_path);
5552 55cccc34 2020-02-20 stsp if (error)
5553 55cccc34 2020-02-20 stsp goto done;
5556 55cccc34 2020-02-20 stsp if (worktree) {
5557 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5558 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5559 55cccc34 2020-02-20 stsp worktree = NULL;
5561 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5563 52185f70 2019-02-05 stsp free(repo_path);
5564 e4a0e26d 2020-02-20 stsp free(cwd);
5565 ffd1d5e5 2018-06-23 stsp free(commit_id);
5566 4e97c21c 2020-12-06 stsp free(label);
5568 486cd271 2020-12-06 stsp got_ref_close(ref);
5569 ffd1d5e5 2018-06-23 stsp if (commit)
5570 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5572 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5574 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5575 ffd1d5e5 2018-06-23 stsp return error;
5578 6458efa5 2020-11-24 stsp static const struct got_error *
5579 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5581 6458efa5 2020-11-24 stsp const struct got_error *err;
5582 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5583 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5585 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5586 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5588 6458efa5 2020-11-24 stsp return err;
5590 6458efa5 2020-11-24 stsp s->nrefs = 0;
5591 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5592 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5595 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5596 6458efa5 2020-11-24 stsp if (re == NULL)
5597 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5599 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5600 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5601 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5604 6458efa5 2020-11-24 stsp return NULL;
5608 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5610 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5612 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5613 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5614 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5617 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5620 6458efa5 2020-11-24 stsp static const struct got_error *
5621 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5623 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5624 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5626 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5627 6458efa5 2020-11-24 stsp s->repo = repo;
5629 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5630 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5631 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5633 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5635 6458efa5 2020-11-24 stsp return err;
5637 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5639 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5640 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5641 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5642 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5644 6458efa5 2020-11-24 stsp goto done;
5646 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5647 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5648 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5650 6458efa5 2020-11-24 stsp goto done;
5652 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5653 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5654 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5656 6458efa5 2020-11-24 stsp goto done;
5659 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5660 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5661 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5662 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5663 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5666 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5667 6458efa5 2020-11-24 stsp return err;
5670 6458efa5 2020-11-24 stsp static const struct got_error *
5671 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5673 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5675 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5676 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5678 6458efa5 2020-11-24 stsp return NULL;
5681 6458efa5 2020-11-24 stsp static const struct got_error *
5682 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5683 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5685 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5686 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5687 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5688 6458efa5 2020-11-24 stsp int obj_type;
5690 c42c9805 2020-11-24 stsp *commit_id = NULL;
5692 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5694 6458efa5 2020-11-24 stsp return err;
5696 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5698 6458efa5 2020-11-24 stsp goto done;
5700 6458efa5 2020-11-24 stsp switch (obj_type) {
5701 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5702 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5704 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5705 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5707 6458efa5 2020-11-24 stsp goto done;
5708 c42c9805 2020-11-24 stsp free(obj_id);
5709 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5710 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5712 6458efa5 2020-11-24 stsp goto done;
5713 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5714 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5715 c42c9805 2020-11-24 stsp goto done;
5717 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5718 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5719 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5720 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5721 c42c9805 2020-11-24 stsp goto done;
5725 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5731 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5732 c42c9805 2020-11-24 stsp if (err) {
5733 c42c9805 2020-11-24 stsp free(*commit_id);
5734 c42c9805 2020-11-24 stsp *commit_id = NULL;
5736 c42c9805 2020-11-24 stsp return err;
5739 c42c9805 2020-11-24 stsp static const struct got_error *
5740 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5741 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5743 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5744 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5745 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5747 c42c9805 2020-11-24 stsp *new_view = NULL;
5749 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5750 c42c9805 2020-11-24 stsp if (err) {
5751 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5752 c42c9805 2020-11-24 stsp return err;
5754 c42c9805 2020-11-24 stsp return NULL;
5757 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5758 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5759 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5760 6458efa5 2020-11-24 stsp goto done;
5763 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5764 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5767 6458efa5 2020-11-24 stsp view_close(log_view);
5769 6458efa5 2020-11-24 stsp *new_view = log_view;
5770 c42c9805 2020-11-24 stsp free(commit_id);
5771 6458efa5 2020-11-24 stsp return err;
5774 ce5b7c56 2019-07-09 stsp static void
5775 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5777 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5778 34ba6917 2020-11-30 stsp int i = 0;
5780 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5783 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5784 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5785 34ba6917 2020-11-30 stsp if (re == NULL)
5787 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5788 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5792 34ba6917 2020-11-30 stsp static void
5793 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5795 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5796 6458efa5 2020-11-24 stsp int n = 0;
5798 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5799 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5801 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5803 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5804 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5805 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5806 6458efa5 2020-11-24 stsp if (last) {
5807 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5808 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5813 6458efa5 2020-11-24 stsp static const struct got_error *
5814 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5816 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5818 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5819 6458efa5 2020-11-24 stsp return NULL;
5822 6458efa5 2020-11-24 stsp static int
5823 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5825 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5827 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5831 6458efa5 2020-11-24 stsp static const struct got_error *
5832 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5834 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5835 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5837 6458efa5 2020-11-24 stsp if (!view->searching) {
5838 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5839 6458efa5 2020-11-24 stsp return NULL;
5842 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5843 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5844 6458efa5 2020-11-24 stsp if (s->selected_entry)
5845 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5847 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5848 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5850 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5851 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5853 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5854 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5857 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5858 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5860 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5863 6458efa5 2020-11-24 stsp while (1) {
5864 6458efa5 2020-11-24 stsp if (re == NULL) {
5865 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5866 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5867 6458efa5 2020-11-24 stsp return NULL;
5869 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5870 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5872 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5875 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5876 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 6458efa5 2020-11-24 stsp s->matched_entry = re;
5881 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5882 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5884 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5887 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5888 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5889 6458efa5 2020-11-24 stsp s->selected = 0;
5892 6458efa5 2020-11-24 stsp return NULL;
5895 6458efa5 2020-11-24 stsp static const struct got_error *
5896 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5898 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5899 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5900 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5901 6458efa5 2020-11-24 stsp char *line = NULL;
5902 6458efa5 2020-11-24 stsp wchar_t *wline;
5903 6458efa5 2020-11-24 stsp struct tog_color *tc;
5904 6458efa5 2020-11-24 stsp int width, n;
5905 6458efa5 2020-11-24 stsp int limit = view->nlines;
5907 6458efa5 2020-11-24 stsp werase(view->window);
5909 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5911 6458efa5 2020-11-24 stsp if (limit == 0)
5912 6458efa5 2020-11-24 stsp return NULL;
5914 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5916 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5917 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5918 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5920 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5921 6458efa5 2020-11-24 stsp if (err) {
5922 6458efa5 2020-11-24 stsp free(line);
5923 6458efa5 2020-11-24 stsp return err;
5925 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5926 6458efa5 2020-11-24 stsp wstandout(view->window);
5927 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5928 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5929 6458efa5 2020-11-24 stsp wstandend(view->window);
5930 6458efa5 2020-11-24 stsp free(wline);
5931 6458efa5 2020-11-24 stsp wline = NULL;
5932 6458efa5 2020-11-24 stsp free(line);
5933 6458efa5 2020-11-24 stsp line = NULL;
5934 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5935 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5936 6458efa5 2020-11-24 stsp if (--limit <= 0)
5937 6458efa5 2020-11-24 stsp return NULL;
5940 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5941 6458efa5 2020-11-24 stsp char *line = NULL;
5943 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5944 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5945 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5946 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5947 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5948 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5949 6458efa5 2020-11-24 stsp struct got_object_id *id;
5950 6458efa5 2020-11-24 stsp char *id_str;
5951 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5953 6458efa5 2020-11-24 stsp return err;
5954 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5955 6458efa5 2020-11-24 stsp if (err) {
5957 6458efa5 2020-11-24 stsp return err;
5959 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5960 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5961 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5963 6458efa5 2020-11-24 stsp free(id_str);
5964 6458efa5 2020-11-24 stsp return err;
5967 6458efa5 2020-11-24 stsp free(id_str);
5969 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
5970 6458efa5 2020-11-24 stsp if (line == NULL)
5971 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
5974 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5975 6458efa5 2020-11-24 stsp if (err) {
5976 6458efa5 2020-11-24 stsp free(line);
5977 6458efa5 2020-11-24 stsp return err;
5979 6458efa5 2020-11-24 stsp if (n == s->selected) {
5980 6458efa5 2020-11-24 stsp if (view->focussed)
5981 6458efa5 2020-11-24 stsp wstandout(view->window);
5982 6458efa5 2020-11-24 stsp s->selected_entry = re;
5984 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
5986 6458efa5 2020-11-24 stsp wattr_on(view->window,
5987 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5988 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5990 6458efa5 2020-11-24 stsp wattr_off(view->window,
5991 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5992 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5993 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5994 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
5995 6458efa5 2020-11-24 stsp wstandend(view->window);
5996 6458efa5 2020-11-24 stsp free(line);
5997 6458efa5 2020-11-24 stsp free(wline);
5998 6458efa5 2020-11-24 stsp wline = NULL;
6000 6458efa5 2020-11-24 stsp s->ndisplayed++;
6001 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6004 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6007 6458efa5 2020-11-24 stsp view_vborder(view);
6008 6458efa5 2020-11-24 stsp return err;
6011 6458efa5 2020-11-24 stsp static const struct got_error *
6012 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6013 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6015 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6016 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6017 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6018 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6020 c42c9805 2020-11-24 stsp *new_view = NULL;
6022 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6023 c42c9805 2020-11-24 stsp if (err) {
6024 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6025 c42c9805 2020-11-24 stsp return err;
6027 c42c9805 2020-11-24 stsp return NULL;
6030 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6032 c42c9805 2020-11-24 stsp goto done;
6034 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6036 c42c9805 2020-11-24 stsp goto done;
6038 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6039 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6040 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6041 c42c9805 2020-11-24 stsp goto done;
6044 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, commit_id,
6045 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6047 c42c9805 2020-11-24 stsp goto done;
6049 c42c9805 2020-11-24 stsp *new_view = tree_view;
6051 c42c9805 2020-11-24 stsp free(commit_id);
6052 c42c9805 2020-11-24 stsp free(tree_id);
6053 c42c9805 2020-11-24 stsp if (err) {
6055 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6057 c42c9805 2020-11-24 stsp return err;
6059 c42c9805 2020-11-24 stsp static const struct got_error *
6060 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6062 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6063 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6064 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6065 34ba6917 2020-11-30 stsp int begin_x = 0;
6067 6458efa5 2020-11-24 stsp switch (ch) {
6069 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6071 6458efa5 2020-11-24 stsp case KEY_ENTER:
6072 6458efa5 2020-11-24 stsp case '\r':
6073 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6075 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6076 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6077 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6079 e78dc838 2020-12-04 stsp view->focussed = 0;
6080 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6081 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6082 6458efa5 2020-11-24 stsp err = view_close_child(view);
6084 6458efa5 2020-11-24 stsp return err;
6085 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6086 e78dc838 2020-12-04 stsp view->focus_child = 1;
6088 6458efa5 2020-11-24 stsp *new_view = log_view;
6091 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6093 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6094 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6095 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6097 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6099 e78dc838 2020-12-04 stsp view->focussed = 0;
6100 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6101 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6102 c42c9805 2020-11-24 stsp err = view_close_child(view);
6104 c42c9805 2020-11-24 stsp return err;
6105 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6106 e78dc838 2020-12-04 stsp view->focus_child = 1;
6108 c42c9805 2020-11-24 stsp *new_view = tree_view;
6111 6458efa5 2020-11-24 stsp case KEY_UP:
6112 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6113 6458efa5 2020-11-24 stsp s->selected--;
6116 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6118 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6119 6458efa5 2020-11-24 stsp case CTRL('b'):
6120 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6121 34ba6917 2020-11-30 stsp s->selected = 0;
6122 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6125 6458efa5 2020-11-24 stsp case KEY_DOWN:
6126 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6127 6458efa5 2020-11-24 stsp s->selected++;
6130 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6131 6458efa5 2020-11-24 stsp /* can't scroll any further */
6133 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6135 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6136 6458efa5 2020-11-24 stsp case CTRL('f'):
6137 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6138 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6139 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6140 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6143 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6145 6458efa5 2020-11-24 stsp case CTRL('l'):
6146 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6147 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6149 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6150 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6151 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6157 6458efa5 2020-11-24 stsp return err;
6160 6458efa5 2020-11-24 stsp __dead static void
6161 6458efa5 2020-11-24 stsp usage_ref(void)
6164 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6165 6458efa5 2020-11-24 stsp getprogname());
6169 6458efa5 2020-11-24 stsp static const struct got_error *
6170 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6172 6458efa5 2020-11-24 stsp const struct got_error *error;
6173 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6174 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6175 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6177 6458efa5 2020-11-24 stsp struct tog_view *view;
6179 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6180 6458efa5 2020-11-24 stsp switch (ch) {
6182 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6183 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6184 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6188 e99e2d15 2020-11-24 naddy usage_ref();
6189 6458efa5 2020-11-24 stsp /* NOTREACHED */
6193 6458efa5 2020-11-24 stsp argc -= optind;
6194 6458efa5 2020-11-24 stsp argv += optind;
6196 6458efa5 2020-11-24 stsp if (argc > 1)
6197 e99e2d15 2020-11-24 naddy usage_ref();
6199 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6200 6458efa5 2020-11-24 stsp if (cwd == NULL)
6201 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6203 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6204 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6205 6458efa5 2020-11-24 stsp goto done;
6207 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6208 6458efa5 2020-11-24 stsp if (worktree)
6209 6458efa5 2020-11-24 stsp repo_path =
6210 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6212 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6214 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6215 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6216 6458efa5 2020-11-24 stsp goto done;
6219 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6220 6458efa5 2020-11-24 stsp if (error != NULL)
6221 6458efa5 2020-11-24 stsp goto done;
6223 6458efa5 2020-11-24 stsp init_curses();
6225 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6226 6458efa5 2020-11-24 stsp if (error)
6227 6458efa5 2020-11-24 stsp goto done;
6229 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6230 6458efa5 2020-11-24 stsp if (view == NULL) {
6231 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6232 6458efa5 2020-11-24 stsp goto done;
6235 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6236 6458efa5 2020-11-24 stsp if (error)
6237 6458efa5 2020-11-24 stsp goto done;
6239 6458efa5 2020-11-24 stsp if (worktree) {
6240 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6241 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6242 6458efa5 2020-11-24 stsp worktree = NULL;
6244 6458efa5 2020-11-24 stsp error = view_loop(view);
6246 6458efa5 2020-11-24 stsp free(repo_path);
6247 6458efa5 2020-11-24 stsp free(cwd);
6249 6458efa5 2020-11-24 stsp got_repo_close(repo);
6250 6458efa5 2020-11-24 stsp return error;
6253 6458efa5 2020-11-24 stsp static void
6254 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6258 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6259 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6260 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6261 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6263 6879ba42 2020-10-01 naddy fputc('\n', fp);
6266 4ed7e80c 2018-05-20 stsp __dead static void
6267 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6269 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6271 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6272 6879ba42 2020-10-01 naddy getprogname());
6273 6879ba42 2020-10-01 naddy if (hflag) {
6274 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6275 6879ba42 2020-10-01 naddy list_commands(fp);
6277 6879ba42 2020-10-01 naddy exit(status);
6280 c2301be8 2018-04-30 stsp static char **
6281 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6283 ee85c5e8 2020-02-29 stsp va_list ap;
6284 c2301be8 2018-04-30 stsp char **argv;
6287 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6289 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6290 c2301be8 2018-04-30 stsp if (argv == NULL)
6291 c2301be8 2018-04-30 stsp err(1, "calloc");
6292 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6293 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6294 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6295 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6298 ee85c5e8 2020-02-29 stsp va_end(ap);
6299 c2301be8 2018-04-30 stsp return argv;
6303 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6304 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6305 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6306 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6307 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6309 ee85c5e8 2020-02-29 stsp static const struct got_error *
6310 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6312 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6313 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6314 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6315 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6316 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6317 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6318 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6320 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6321 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6322 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6324 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6325 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6326 ee85c5e8 2020-02-29 stsp goto done;
6328 ee85c5e8 2020-02-29 stsp if (worktree)
6329 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6331 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6332 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6333 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6334 ee85c5e8 2020-02-29 stsp goto done;
6337 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6338 ee85c5e8 2020-02-29 stsp if (error != NULL)
6339 ee85c5e8 2020-02-29 stsp goto done;
6341 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6342 ee85c5e8 2020-02-29 stsp repo, worktree);
6343 ee85c5e8 2020-02-29 stsp if (error)
6344 ee85c5e8 2020-02-29 stsp goto done;
6346 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6347 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6348 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6349 ee85c5e8 2020-02-29 stsp if (error)
6350 ee85c5e8 2020-02-29 stsp goto done;
6352 ee85c5e8 2020-02-29 stsp if (worktree) {
6353 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6354 ee85c5e8 2020-02-29 stsp worktree = NULL;
6357 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6358 ee85c5e8 2020-02-29 stsp if (error) {
6359 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6360 ee85c5e8 2020-02-29 stsp goto done;
6361 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6362 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6363 6879ba42 2020-10-01 naddy usage(1, 1);
6364 ee85c5e8 2020-02-29 stsp /* not reached */
6367 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6368 ee85c5e8 2020-02-29 stsp repo = NULL;
6370 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6371 ee85c5e8 2020-02-29 stsp if (error)
6372 ee85c5e8 2020-02-29 stsp goto done;
6374 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6376 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6377 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6380 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6381 ee85c5e8 2020-02-29 stsp if (worktree)
6382 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6384 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6385 ee85c5e8 2020-02-29 stsp free(commit_id);
6386 ee85c5e8 2020-02-29 stsp free(cwd);
6387 ee85c5e8 2020-02-29 stsp free(repo_path);
6388 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6389 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6391 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6392 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6393 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6395 ee85c5e8 2020-02-29 stsp return error;
6399 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6401 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6402 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6403 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6404 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6405 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6406 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6407 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6410 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6412 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6413 9f7d7167 2018-04-29 stsp switch (ch) {
6415 9f7d7167 2018-04-29 stsp hflag = 1;
6418 53ccebc2 2019-07-30 stsp Vflag = 1;
6421 6879ba42 2020-10-01 naddy usage(hflag, 1);
6422 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6426 9f7d7167 2018-04-29 stsp argc -= optind;
6427 9f7d7167 2018-04-29 stsp argv += optind;
6428 9814e6a3 2020-09-27 naddy optind = 1;
6429 c2301be8 2018-04-30 stsp optreset = 1;
6431 53ccebc2 2019-07-30 stsp if (Vflag) {
6432 53ccebc2 2019-07-30 stsp got_version_print_str();
6433 6879ba42 2020-10-01 naddy return 0;
6436 4010e238 2020-12-04 stsp #ifndef PROFILE
6437 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6438 4010e238 2020-12-04 stsp NULL) == -1)
6439 4010e238 2020-12-04 stsp err(1, "pledge");
6442 c2301be8 2018-04-30 stsp if (argc == 0) {
6443 f29d3e89 2018-06-23 stsp if (hflag)
6444 6879ba42 2020-10-01 naddy usage(hflag, 0);
6445 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6446 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6448 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6452 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6453 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6454 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6455 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6456 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6462 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6463 ee85c5e8 2020-02-29 stsp if (argc != 1)
6464 6879ba42 2020-10-01 naddy usage(0, 1);
6465 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6466 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6468 ee85c5e8 2020-02-29 stsp if (hflag)
6469 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6471 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6475 b46c1e04 2020-09-20 naddy putchar('\n');
6476 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6478 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6479 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6480 a2f4a359 2020-02-28 stsp free(cmd_argv);
6483 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6484 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);