Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
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>
20 80ddbec8 2018-04-29 stsp
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>
43 9f7d7167 2018-04-29 stsp
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"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
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);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
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);
85 9f7d7167 2018-04-29 stsp
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 *[]);
91 9f7d7167 2018-04-29 stsp
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 },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
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,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 d6b05b5b 2018-08-04 stsp
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;
114 1a76625f 2018-10-22 stsp int idx;
115 ba4f502b 2018-08-04 stsp };
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;
120 6d17833f 2019-11-08 stsp };
121 6d17833f 2019-11-08 stsp
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;
126 15a087fe 2019-02-21 stsp };
127 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
128 11b20872 2019-11-08 stsp
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)
132 11b20872 2019-11-08 stsp {
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;
136 11b20872 2019-11-08 stsp
137 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
138 11b20872 2019-11-08 stsp return NULL;
139 11b20872 2019-11-08 stsp
140 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
141 11b20872 2019-11-08 stsp
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);
155 11b20872 2019-11-08 stsp free(tc);
156 11b20872 2019-11-08 stsp return err;
157 11b20872 2019-11-08 stsp }
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;
161 11b20872 2019-11-08 stsp }
162 11b20872 2019-11-08 stsp
163 11b20872 2019-11-08 stsp static void
164 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
165 11b20872 2019-11-08 stsp {
166 11b20872 2019-11-08 stsp struct tog_color *tc;
167 11b20872 2019-11-08 stsp
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);
172 11b20872 2019-11-08 stsp free(tc);
173 11b20872 2019-11-08 stsp }
174 11b20872 2019-11-08 stsp }
175 11b20872 2019-11-08 stsp
176 11b20872 2019-11-08 stsp struct tog_color *
177 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
178 11b20872 2019-11-08 stsp {
179 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
180 11b20872 2019-11-08 stsp
181 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
182 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
183 11b20872 2019-11-08 stsp return tc;
184 11b20872 2019-11-08 stsp }
185 11b20872 2019-11-08 stsp
186 11b20872 2019-11-08 stsp return NULL;
187 11b20872 2019-11-08 stsp }
188 11b20872 2019-11-08 stsp
189 11b20872 2019-11-08 stsp static int
190 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
191 11b20872 2019-11-08 stsp {
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;
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp return -1;
222 11b20872 2019-11-08 stsp }
223 11b20872 2019-11-08 stsp
224 11b20872 2019-11-08 stsp static int
225 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
226 11b20872 2019-11-08 stsp {
227 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
228 11b20872 2019-11-08 stsp
229 11b20872 2019-11-08 stsp if (val == NULL)
230 11b20872 2019-11-08 stsp return default_color_value(envvar);
231 15a087fe 2019-02-21 stsp
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)
249 11b20872 2019-11-08 stsp return -1;
250 11b20872 2019-11-08 stsp
251 11b20872 2019-11-08 stsp return default_color_value(envvar);
252 11b20872 2019-11-08 stsp }
253 11b20872 2019-11-08 stsp
254 11b20872 2019-11-08 stsp
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;
258 15a087fe 2019-02-21 stsp FILE *f;
259 15a087fe 2019-02-21 stsp int first_displayed_line;
260 15a087fe 2019-02-21 stsp int last_displayed_line;
261 15a087fe 2019-02-21 stsp int eof;
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;
272 15a087fe 2019-02-21 stsp
273 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
274 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
275 b01e7d3b 2018-08-04 stsp };
276 b01e7d3b 2018-08-04 stsp
277 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 1a76625f 2018-10-22 stsp
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;
295 1a76625f 2018-10-22 stsp };
296 1a76625f 2018-10-22 stsp
297 1a76625f 2018-10-22 stsp struct tog_log_view_state {
298 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
299 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
300 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
301 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
302 b01e7d3b 2018-08-04 stsp int selected;
303 b01e7d3b 2018-08-04 stsp char *in_repo_path;
304 d01904d4 2019-06-25 stsp const char *head_ref_name;
305 b672a97a 2020-01-27 stsp int log_branches;
306 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
307 78756c87 2020-11-24 stsp struct got_reflist_head refs;
308 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
309 1a76625f 2018-10-22 stsp sig_atomic_t quit;
310 1a76625f 2018-10-22 stsp pthread_t thread;
311 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
312 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
313 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
314 11b20872 2019-11-08 stsp struct tog_colors colors;
315 ba4f502b 2018-08-04 stsp };
316 11b20872 2019-11-08 stsp
317 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
318 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
319 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
320 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
321 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
322 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
323 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
324 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
325 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
326 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
327 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
328 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
329 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
330 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
331 ba4f502b 2018-08-04 stsp
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;
335 e9424729 2018-08-04 stsp
336 e9424729 2018-08-04 stsp struct tog_view *view;
337 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
338 e9424729 2018-08-04 stsp int *quit;
339 e9424729 2018-08-04 stsp };
340 e9424729 2018-08-04 stsp
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;
348 e9424729 2018-08-04 stsp };
349 e9424729 2018-08-04 stsp
350 e9424729 2018-08-04 stsp struct tog_blame {
351 e9424729 2018-08-04 stsp FILE *f;
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;
360 e9424729 2018-08-04 stsp };
361 e9424729 2018-08-04 stsp
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;
367 e5a0f69f 2018-08-18 stsp int eof;
368 e5a0f69f 2018-08-18 stsp int done;
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;
377 ad80ab7b 2018-08-04 stsp };
378 ad80ab7b 2018-08-04 stsp
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;
385 ad80ab7b 2018-08-04 stsp };
386 ad80ab7b 2018-08-04 stsp
387 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 ad80ab7b 2018-08-04 stsp
389 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
390 ad80ab7b 2018-08-04 stsp char *tree_label;
391 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
392 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
393 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
394 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
395 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
396 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
397 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
398 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
399 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
400 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
401 6458efa5 2020-11-24 stsp struct tog_colors colors;
402 6458efa5 2020-11-24 stsp };
403 6458efa5 2020-11-24 stsp
404 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
405 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
406 6458efa5 2020-11-24 stsp struct got_reference *ref;
407 6458efa5 2020-11-24 stsp int idx;
408 6458efa5 2020-11-24 stsp };
409 6458efa5 2020-11-24 stsp
410 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
411 6458efa5 2020-11-24 stsp
412 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
413 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
414 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
418 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
419 6458efa5 2020-11-24 stsp struct got_repository *repo;
420 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
421 bddb1296 2019-11-08 stsp struct tog_colors colors;
422 7cbe629d 2018-08-04 stsp };
423 7cbe629d 2018-08-04 stsp
424 669b5ffa 2018-10-07 stsp /*
425 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
426 669b5ffa 2018-10-07 stsp *
427 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
428 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
429 669b5ffa 2018-10-07 stsp * there is enough screen estate.
430 669b5ffa 2018-10-07 stsp *
431 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
432 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
433 669b5ffa 2018-10-07 stsp *
434 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
435 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
436 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
437 669b5ffa 2018-10-07 stsp *
438 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
439 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
440 669b5ffa 2018-10-07 stsp */
441 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
442 669b5ffa 2018-10-07 stsp
443 cc3c9aac 2018-08-01 stsp struct tog_view {
444 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
445 26ed57b2 2018-05-19 stsp WINDOW *window;
446 26ed57b2 2018-05-19 stsp PANEL *panel;
447 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
448 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
449 1004088d 2018-09-29 stsp int focussed;
450 669b5ffa 2018-10-07 stsp struct tog_view *parent;
451 669b5ffa 2018-10-07 stsp struct tog_view *child;
452 5dc9f4bc 2018-08-04 stsp
453 5dc9f4bc 2018-08-04 stsp /* type-specific state */
454 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
455 5dc9f4bc 2018-08-04 stsp union {
456 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
457 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
458 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
459 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
460 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
461 5dc9f4bc 2018-08-04 stsp } state;
462 e5a0f69f 2018-08-18 stsp
463 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
464 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
465 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
466 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
467 60493ae3 2019-06-20 stsp
468 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
469 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
470 60493ae3 2019-06-20 stsp int searching;
471 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
472 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
473 60493ae3 2019-06-20 stsp int search_next_done;
474 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
475 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
476 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
477 1803e47f 2019-06-22 stsp regex_t regex;
478 41605754 2020-11-12 stsp regmatch_t regmatch;
479 cc3c9aac 2018-08-01 stsp };
480 cd0acaa7 2018-05-20 stsp
481 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
482 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
483 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
484 78756c87 2020-11-24 stsp struct got_repository *);
485 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
486 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
487 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
488 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
489 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
490 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
491 e5a0f69f 2018-08-18 stsp
492 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
493 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
494 78756c87 2020-11-24 stsp const char *, const char *, int);
495 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
496 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
497 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
498 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
499 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
500 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
501 e5a0f69f 2018-08-18 stsp
502 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
503 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
504 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
505 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
506 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
507 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
508 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
509 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
510 e5a0f69f 2018-08-18 stsp
511 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
512 78756c87 2020-11-24 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
513 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
514 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
515 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
516 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
517 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
518 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
519 6458efa5 2020-11-24 stsp
520 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
521 6458efa5 2020-11-24 stsp struct got_repository *);
522 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
523 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
524 6458efa5 2020-11-24 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
525 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
526 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
527 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
528 25791caa 2018-10-24 stsp
529 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
530 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
531 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
532 25791caa 2018-10-24 stsp
533 25791caa 2018-10-24 stsp static void
534 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
535 25791caa 2018-10-24 stsp {
536 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
537 83baff54 2019-08-12 stsp }
538 83baff54 2019-08-12 stsp
539 83baff54 2019-08-12 stsp static void
540 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
541 83baff54 2019-08-12 stsp {
542 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
543 25791caa 2018-10-24 stsp }
544 26ed57b2 2018-05-19 stsp
545 61266923 2020-01-14 stsp static void
546 61266923 2020-01-14 stsp tog_sigcont(int signo)
547 61266923 2020-01-14 stsp {
548 61266923 2020-01-14 stsp tog_sigcont_received = 1;
549 61266923 2020-01-14 stsp }
550 61266923 2020-01-14 stsp
551 e5a0f69f 2018-08-18 stsp static const struct got_error *
552 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
553 ea5e7bb5 2018-08-01 stsp {
554 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
555 e5a0f69f 2018-08-18 stsp
556 669b5ffa 2018-10-07 stsp if (view->child) {
557 669b5ffa 2018-10-07 stsp view_close(view->child);
558 669b5ffa 2018-10-07 stsp view->child = NULL;
559 669b5ffa 2018-10-07 stsp }
560 e5a0f69f 2018-08-18 stsp if (view->close)
561 e5a0f69f 2018-08-18 stsp err = view->close(view);
562 ea5e7bb5 2018-08-01 stsp if (view->panel)
563 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
564 ea5e7bb5 2018-08-01 stsp if (view->window)
565 ea5e7bb5 2018-08-01 stsp delwin(view->window);
566 ea5e7bb5 2018-08-01 stsp free(view);
567 e5a0f69f 2018-08-18 stsp return err;
568 ea5e7bb5 2018-08-01 stsp }
569 ea5e7bb5 2018-08-01 stsp
570 ea5e7bb5 2018-08-01 stsp static struct tog_view *
571 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
572 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
573 ea5e7bb5 2018-08-01 stsp {
574 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
575 ea5e7bb5 2018-08-01 stsp
576 ea5e7bb5 2018-08-01 stsp if (view == NULL)
577 ea5e7bb5 2018-08-01 stsp return NULL;
578 ea5e7bb5 2018-08-01 stsp
579 d6b05b5b 2018-08-04 stsp view->type = type;
580 f7d12f7e 2018-08-01 stsp view->lines = LINES;
581 f7d12f7e 2018-08-01 stsp view->cols = COLS;
582 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
583 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
584 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
585 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
586 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
587 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
588 96a765a8 2018-08-04 stsp view_close(view);
589 ea5e7bb5 2018-08-01 stsp return NULL;
590 ea5e7bb5 2018-08-01 stsp }
591 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
592 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
593 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
594 96a765a8 2018-08-04 stsp view_close(view);
595 ea5e7bb5 2018-08-01 stsp return NULL;
596 ea5e7bb5 2018-08-01 stsp }
597 ea5e7bb5 2018-08-01 stsp
598 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
599 ea5e7bb5 2018-08-01 stsp return view;
600 cdf1ee82 2018-08-01 stsp }
601 cdf1ee82 2018-08-01 stsp
602 0cf4efb1 2018-09-29 stsp static int
603 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
604 0cf4efb1 2018-09-29 stsp {
605 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
606 0cf4efb1 2018-09-29 stsp return 0;
607 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
608 5c60c32a 2018-10-18 stsp }
609 5c60c32a 2018-10-18 stsp
610 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
611 5c60c32a 2018-10-18 stsp
612 5c60c32a 2018-10-18 stsp static const struct got_error *
613 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
614 5c60c32a 2018-10-18 stsp {
615 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
616 5c60c32a 2018-10-18 stsp
617 5c60c32a 2018-10-18 stsp view->begin_y = 0;
618 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
619 5c60c32a 2018-10-18 stsp view->nlines = LINES;
620 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
621 5c60c32a 2018-10-18 stsp view->lines = LINES;
622 5c60c32a 2018-10-18 stsp view->cols = COLS;
623 5c60c32a 2018-10-18 stsp err = view_resize(view);
624 5c60c32a 2018-10-18 stsp if (err)
625 5c60c32a 2018-10-18 stsp return err;
626 5c60c32a 2018-10-18 stsp
627 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
628 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
629 5c60c32a 2018-10-18 stsp
630 5c60c32a 2018-10-18 stsp return NULL;
631 5c60c32a 2018-10-18 stsp }
632 5c60c32a 2018-10-18 stsp
633 5c60c32a 2018-10-18 stsp static const struct got_error *
634 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
635 5c60c32a 2018-10-18 stsp {
636 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
637 5c60c32a 2018-10-18 stsp
638 5c60c32a 2018-10-18 stsp view->begin_x = 0;
639 5c60c32a 2018-10-18 stsp view->begin_y = 0;
640 5c60c32a 2018-10-18 stsp view->nlines = LINES;
641 5c60c32a 2018-10-18 stsp view->ncols = COLS;
642 5c60c32a 2018-10-18 stsp view->lines = LINES;
643 5c60c32a 2018-10-18 stsp view->cols = COLS;
644 5c60c32a 2018-10-18 stsp err = view_resize(view);
645 5c60c32a 2018-10-18 stsp if (err)
646 5c60c32a 2018-10-18 stsp return err;
647 5c60c32a 2018-10-18 stsp
648 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
649 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
650 5c60c32a 2018-10-18 stsp
651 5c60c32a 2018-10-18 stsp return NULL;
652 0cf4efb1 2018-09-29 stsp }
653 0cf4efb1 2018-09-29 stsp
654 5c60c32a 2018-10-18 stsp static int
655 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
656 5c60c32a 2018-10-18 stsp {
657 5c60c32a 2018-10-18 stsp return view->parent == NULL;
658 5c60c32a 2018-10-18 stsp }
659 5c60c32a 2018-10-18 stsp
660 4d8c2215 2018-08-19 stsp static const struct got_error *
661 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
662 f7d12f7e 2018-08-01 stsp {
663 f7d12f7e 2018-08-01 stsp int nlines, ncols;
664 f7d12f7e 2018-08-01 stsp
665 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
666 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
667 0cf4efb1 2018-09-29 stsp else
668 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
669 f7d12f7e 2018-08-01 stsp
670 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
671 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
672 0cf4efb1 2018-09-29 stsp else
673 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
674 f7d12f7e 2018-08-01 stsp
675 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
676 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
677 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
678 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
679 25791caa 2018-10-24 stsp wclear(view->window);
680 f7d12f7e 2018-08-01 stsp
681 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
682 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
683 0cf4efb1 2018-09-29 stsp view->lines = LINES;
684 0cf4efb1 2018-09-29 stsp view->cols = COLS;
685 6d0fee91 2018-08-01 stsp
686 6e3e5d9c 2018-10-18 stsp if (view->child) {
687 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
688 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
689 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
690 5c60c32a 2018-10-18 stsp if (view->child->focussed)
691 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
692 5c60c32a 2018-10-18 stsp else
693 5c60c32a 2018-10-18 stsp show_panel(view->panel);
694 5c60c32a 2018-10-18 stsp } else {
695 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
696 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
697 5c60c32a 2018-10-18 stsp }
698 5c60c32a 2018-10-18 stsp }
699 669b5ffa 2018-10-07 stsp
700 5c60c32a 2018-10-18 stsp return NULL;
701 669b5ffa 2018-10-07 stsp }
702 669b5ffa 2018-10-07 stsp
703 669b5ffa 2018-10-07 stsp static const struct got_error *
704 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
705 669b5ffa 2018-10-07 stsp {
706 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
707 669b5ffa 2018-10-07 stsp
708 669b5ffa 2018-10-07 stsp if (view->child == NULL)
709 669b5ffa 2018-10-07 stsp return NULL;
710 669b5ffa 2018-10-07 stsp
711 669b5ffa 2018-10-07 stsp err = view_close(view->child);
712 669b5ffa 2018-10-07 stsp view->child = NULL;
713 669b5ffa 2018-10-07 stsp return err;
714 669b5ffa 2018-10-07 stsp }
715 669b5ffa 2018-10-07 stsp
716 669b5ffa 2018-10-07 stsp static const struct got_error *
717 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
718 669b5ffa 2018-10-07 stsp {
719 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
720 669b5ffa 2018-10-07 stsp
721 669b5ffa 2018-10-07 stsp view->child = child;
722 669b5ffa 2018-10-07 stsp child->parent = view;
723 669b5ffa 2018-10-07 stsp return err;
724 bfddd0d9 2018-09-29 stsp }
725 bfddd0d9 2018-09-29 stsp
726 bfddd0d9 2018-09-29 stsp static int
727 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
728 bfddd0d9 2018-09-29 stsp {
729 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
730 34bc9ec9 2019-02-22 stsp }
731 34bc9ec9 2019-02-22 stsp
732 34bc9ec9 2019-02-22 stsp static void
733 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
734 25791caa 2018-10-24 stsp {
735 25791caa 2018-10-24 stsp int cols, lines;
736 25791caa 2018-10-24 stsp struct winsize size;
737 25791caa 2018-10-24 stsp
738 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
739 25791caa 2018-10-24 stsp cols = 80; /* Default */
740 25791caa 2018-10-24 stsp lines = 24;
741 25791caa 2018-10-24 stsp } else {
742 25791caa 2018-10-24 stsp cols = size.ws_col;
743 25791caa 2018-10-24 stsp lines = size.ws_row;
744 25791caa 2018-10-24 stsp }
745 25791caa 2018-10-24 stsp resize_term(lines, cols);
746 2b49a8ae 2019-06-22 stsp }
747 2b49a8ae 2019-06-22 stsp
748 2b49a8ae 2019-06-22 stsp static const struct got_error *
749 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
750 2b49a8ae 2019-06-22 stsp {
751 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
752 2b49a8ae 2019-06-22 stsp char pattern[1024];
753 2b49a8ae 2019-06-22 stsp int ret;
754 2b49a8ae 2019-06-22 stsp
755 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
756 2b49a8ae 2019-06-22 stsp return NULL;
757 2b49a8ae 2019-06-22 stsp
758 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
759 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
760 2b49a8ae 2019-06-22 stsp
761 2b49a8ae 2019-06-22 stsp nocbreak();
762 2b49a8ae 2019-06-22 stsp echo();
763 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
764 2b49a8ae 2019-06-22 stsp cbreak();
765 2b49a8ae 2019-06-22 stsp noecho();
766 2b49a8ae 2019-06-22 stsp if (ret == ERR)
767 2b49a8ae 2019-06-22 stsp return NULL;
768 2b49a8ae 2019-06-22 stsp
769 2b49a8ae 2019-06-22 stsp if (view->searching) {
770 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
771 2b49a8ae 2019-06-22 stsp view->searching = 0;
772 2b49a8ae 2019-06-22 stsp }
773 2b49a8ae 2019-06-22 stsp
774 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
775 7c32bd05 2019-06-22 stsp err = view->search_start(view);
776 7c32bd05 2019-06-22 stsp if (err) {
777 7c32bd05 2019-06-22 stsp regfree(&view->regex);
778 7c32bd05 2019-06-22 stsp return err;
779 7c32bd05 2019-06-22 stsp }
780 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
781 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
782 2b49a8ae 2019-06-22 stsp view->search_next(view);
783 2b49a8ae 2019-06-22 stsp }
784 2b49a8ae 2019-06-22 stsp
785 2b49a8ae 2019-06-22 stsp return NULL;
786 0cf4efb1 2018-09-29 stsp }
787 6d0fee91 2018-08-01 stsp
788 0cf4efb1 2018-09-29 stsp static const struct got_error *
789 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
790 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
791 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
792 e5a0f69f 2018-08-18 stsp {
793 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
794 669b5ffa 2018-10-07 stsp struct tog_view *v;
795 1a76625f 2018-10-22 stsp int ch, errcode;
796 e5a0f69f 2018-08-18 stsp
797 e5a0f69f 2018-08-18 stsp *new = NULL;
798 e5a0f69f 2018-08-18 stsp *dead = NULL;
799 0cf4efb1 2018-09-29 stsp *focus = NULL;
800 8f4ed634 2020-03-26 stsp
801 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
802 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
803 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
804 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
805 e5a0f69f 2018-08-18 stsp
806 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
807 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
808 82954512 2020-02-03 stsp if (errcode)
809 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
810 82954512 2020-02-03 stsp "pthread_mutex_unlock");
811 82954512 2020-02-03 stsp pthread_yield();
812 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
813 82954512 2020-02-03 stsp if (errcode)
814 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
815 82954512 2020-02-03 stsp "pthread_mutex_lock");
816 60493ae3 2019-06-20 stsp view->search_next(view);
817 60493ae3 2019-06-20 stsp return NULL;
818 60493ae3 2019-06-20 stsp }
819 60493ae3 2019-06-20 stsp
820 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
821 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
822 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
823 1a76625f 2018-10-22 stsp if (errcode)
824 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
825 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
826 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
827 1a76625f 2018-10-22 stsp if (errcode)
828 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
829 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
830 25791caa 2018-10-24 stsp
831 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
832 25791caa 2018-10-24 stsp tog_resizeterm();
833 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
834 61266923 2020-01-14 stsp tog_sigcont_received = 0;
835 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
836 25791caa 2018-10-24 stsp err = view_resize(v);
837 25791caa 2018-10-24 stsp if (err)
838 25791caa 2018-10-24 stsp return err;
839 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
840 25791caa 2018-10-24 stsp if (err)
841 25791caa 2018-10-24 stsp return err;
842 25791caa 2018-10-24 stsp }
843 25791caa 2018-10-24 stsp }
844 25791caa 2018-10-24 stsp
845 e5a0f69f 2018-08-18 stsp switch (ch) {
846 1e37a5c2 2019-05-12 jcs case ERR:
847 1e37a5c2 2019-05-12 jcs break;
848 1e37a5c2 2019-05-12 jcs case '\t':
849 1e37a5c2 2019-05-12 jcs if (view->child) {
850 1e37a5c2 2019-05-12 jcs *focus = view->child;
851 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
852 1e37a5c2 2019-05-12 jcs *focus = view->parent;
853 1e37a5c2 2019-05-12 jcs }
854 1e37a5c2 2019-05-12 jcs break;
855 1e37a5c2 2019-05-12 jcs case 'q':
856 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
857 1e37a5c2 2019-05-12 jcs *dead = view;
858 1e37a5c2 2019-05-12 jcs break;
859 1e37a5c2 2019-05-12 jcs case 'Q':
860 1e37a5c2 2019-05-12 jcs *done = 1;
861 1e37a5c2 2019-05-12 jcs break;
862 1e37a5c2 2019-05-12 jcs case 'f':
863 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
864 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
865 1e37a5c2 2019-05-12 jcs break;
866 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
867 669b5ffa 2018-10-07 stsp *focus = view->child;
868 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
869 1e37a5c2 2019-05-12 jcs } else
870 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
871 1e37a5c2 2019-05-12 jcs if (err)
872 1e37a5c2 2019-05-12 jcs break;
873 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
874 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
875 1e37a5c2 2019-05-12 jcs } else {
876 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
877 1e37a5c2 2019-05-12 jcs *focus = view;
878 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
879 1e37a5c2 2019-05-12 jcs } else {
880 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
881 669b5ffa 2018-10-07 stsp }
882 1e37a5c2 2019-05-12 jcs if (err)
883 1e37a5c2 2019-05-12 jcs break;
884 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
885 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
886 1e37a5c2 2019-05-12 jcs }
887 1e37a5c2 2019-05-12 jcs break;
888 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
889 60493ae3 2019-06-20 stsp break;
890 60493ae3 2019-06-20 stsp case '/':
891 60493ae3 2019-06-20 stsp if (view->search_start)
892 2b49a8ae 2019-06-22 stsp view_search_start(view);
893 60493ae3 2019-06-20 stsp else
894 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
895 1e37a5c2 2019-05-12 jcs break;
896 b1bf1435 2019-06-21 stsp case 'N':
897 60493ae3 2019-06-20 stsp case 'n':
898 8f4ed634 2020-03-26 stsp if (view->search_next) {
899 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
900 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
901 60493ae3 2019-06-20 stsp view->search_next_done = 0;
902 60493ae3 2019-06-20 stsp view->search_next(view);
903 60493ae3 2019-06-20 stsp } else
904 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
905 60493ae3 2019-06-20 stsp break;
906 1e37a5c2 2019-05-12 jcs default:
907 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
908 1e37a5c2 2019-05-12 jcs break;
909 e5a0f69f 2018-08-18 stsp }
910 e5a0f69f 2018-08-18 stsp
911 e5a0f69f 2018-08-18 stsp return err;
912 e5a0f69f 2018-08-18 stsp }
913 e5a0f69f 2018-08-18 stsp
914 1a57306a 2018-09-02 stsp void
915 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
916 1a57306a 2018-09-02 stsp {
917 0cf4efb1 2018-09-29 stsp PANEL *panel;
918 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
919 0cf4efb1 2018-09-29 stsp
920 669b5ffa 2018-10-07 stsp if (view->parent)
921 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
922 669b5ffa 2018-10-07 stsp
923 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
924 0cf4efb1 2018-09-29 stsp if (panel == NULL)
925 1a57306a 2018-09-02 stsp return;
926 1a57306a 2018-09-02 stsp
927 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
928 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
929 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
930 bcbd79e2 2018-08-19 stsp }
931 bcbd79e2 2018-08-19 stsp
932 a3404814 2018-09-02 stsp int
933 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
934 a3404814 2018-09-02 stsp {
935 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
936 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
937 669b5ffa 2018-10-07 stsp return 0;
938 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
939 669b5ffa 2018-10-07 stsp return 0;
940 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
941 a3404814 2018-09-02 stsp return 0;
942 a3404814 2018-09-02 stsp
943 669b5ffa 2018-10-07 stsp return view->focussed;
944 a3404814 2018-09-02 stsp }
945 a3404814 2018-09-02 stsp
946 bcbd79e2 2018-08-19 stsp static const struct got_error *
947 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
948 e5a0f69f 2018-08-18 stsp {
949 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
950 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
951 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
952 fd823528 2018-10-22 stsp int fast_refresh = 10;
953 1a76625f 2018-10-22 stsp int done = 0, errcode;
954 e5a0f69f 2018-08-18 stsp
955 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
956 1a76625f 2018-10-22 stsp if (errcode)
957 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
958 1a76625f 2018-10-22 stsp
959 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
960 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
961 e5a0f69f 2018-08-18 stsp
962 a81bf10d 2018-09-29 stsp main_view = view;
963 1004088d 2018-09-29 stsp view->focussed = 1;
964 878940b7 2018-09-29 stsp err = view->show(view);
965 0cf4efb1 2018-09-29 stsp if (err)
966 0cf4efb1 2018-09-29 stsp return err;
967 0cf4efb1 2018-09-29 stsp update_panels();
968 0cf4efb1 2018-09-29 stsp doupdate();
969 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
970 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
971 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
972 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
973 fd823528 2018-10-22 stsp
974 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
975 e4197bf9 2018-08-18 stsp view, &views);
976 e5a0f69f 2018-08-18 stsp if (err)
977 e5a0f69f 2018-08-18 stsp break;
978 e5a0f69f 2018-08-18 stsp if (dead_view) {
979 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
980 669b5ffa 2018-10-07 stsp
981 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
982 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
983 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
984 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
985 669b5ffa 2018-10-07 stsp prev = view->parent;
986 669b5ffa 2018-10-07 stsp
987 669b5ffa 2018-10-07 stsp if (dead_view->parent)
988 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
989 669b5ffa 2018-10-07 stsp else
990 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
991 669b5ffa 2018-10-07 stsp
992 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
993 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
994 e5a0f69f 2018-08-18 stsp goto done;
995 669b5ffa 2018-10-07 stsp
996 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
997 0cf4efb1 2018-09-29 stsp if (focus_view)
998 0cf4efb1 2018-09-29 stsp view = focus_view;
999 669b5ffa 2018-10-07 stsp else if (prev)
1000 669b5ffa 2018-10-07 stsp view = prev;
1001 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
1002 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
1003 0cf4efb1 2018-09-29 stsp tog_view_list_head);
1004 669b5ffa 2018-10-07 stsp else
1005 0cf4efb1 2018-09-29 stsp view = NULL;
1006 669b5ffa 2018-10-07 stsp if (view) {
1007 acdafe9c 2020-12-03 stsp if (view->child &&
1008 acdafe9c 2020-12-03 stsp view->child->focussed)
1009 669b5ffa 2018-10-07 stsp focus_view = view->child;
1010 669b5ffa 2018-10-07 stsp else
1011 669b5ffa 2018-10-07 stsp focus_view = view;
1012 669b5ffa 2018-10-07 stsp }
1013 0cf4efb1 2018-09-29 stsp }
1014 e5a0f69f 2018-08-18 stsp }
1015 bcbd79e2 2018-08-19 stsp if (new_view) {
1016 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1017 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1018 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1019 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1020 86c66b02 2018-10-18 stsp continue;
1021 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1022 86c66b02 2018-10-18 stsp err = view_close(v);
1023 86c66b02 2018-10-18 stsp if (err)
1024 86c66b02 2018-10-18 stsp goto done;
1025 86c66b02 2018-10-18 stsp break;
1026 86c66b02 2018-10-18 stsp }
1027 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1028 fed7eaa8 2018-10-24 stsp view = new_view;
1029 878940b7 2018-09-29 stsp if (focus_view == NULL)
1030 878940b7 2018-09-29 stsp focus_view = new_view;
1031 1a76625f 2018-10-22 stsp }
1032 0cf4efb1 2018-09-29 stsp if (focus_view) {
1033 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
1034 669b5ffa 2018-10-07 stsp if (view)
1035 0cf4efb1 2018-09-29 stsp view->focussed = 0;
1036 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
1037 0cf4efb1 2018-09-29 stsp view = focus_view;
1038 878940b7 2018-09-29 stsp if (new_view)
1039 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1040 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1041 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1042 bcbd79e2 2018-08-19 stsp }
1043 669b5ffa 2018-10-07 stsp if (view) {
1044 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1045 758194b5 2018-10-24 stsp view->focussed = 1;
1046 758194b5 2018-10-24 stsp show_panel(view->panel);
1047 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1048 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1049 1a76625f 2018-10-22 stsp focus_view = view;
1050 1a76625f 2018-10-22 stsp }
1051 669b5ffa 2018-10-07 stsp if (view->parent) {
1052 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1053 669b5ffa 2018-10-07 stsp if (err)
1054 1a76625f 2018-10-22 stsp goto done;
1055 669b5ffa 2018-10-07 stsp }
1056 669b5ffa 2018-10-07 stsp err = view->show(view);
1057 0cf4efb1 2018-09-29 stsp if (err)
1058 1a76625f 2018-10-22 stsp goto done;
1059 669b5ffa 2018-10-07 stsp if (view->child) {
1060 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1061 669b5ffa 2018-10-07 stsp if (err)
1062 1a76625f 2018-10-22 stsp goto done;
1063 669b5ffa 2018-10-07 stsp }
1064 1a76625f 2018-10-22 stsp update_panels();
1065 1a76625f 2018-10-22 stsp doupdate();
1066 0cf4efb1 2018-09-29 stsp }
1067 e5a0f69f 2018-08-18 stsp }
1068 e5a0f69f 2018-08-18 stsp done:
1069 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1070 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1071 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1072 e5a0f69f 2018-08-18 stsp view_close(view);
1073 e5a0f69f 2018-08-18 stsp }
1074 1a76625f 2018-10-22 stsp
1075 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1076 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1077 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1078 1a76625f 2018-10-22 stsp
1079 e5a0f69f 2018-08-18 stsp return err;
1080 ea5e7bb5 2018-08-01 stsp }
1081 ea5e7bb5 2018-08-01 stsp
1082 4ed7e80c 2018-05-20 stsp __dead static void
1083 9f7d7167 2018-04-29 stsp usage_log(void)
1084 9f7d7167 2018-04-29 stsp {
1085 80ddbec8 2018-04-29 stsp endwin();
1086 c70c5802 2018-08-01 stsp fprintf(stderr,
1087 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1088 9f7d7167 2018-04-29 stsp getprogname());
1089 9f7d7167 2018-04-29 stsp exit(1);
1090 80ddbec8 2018-04-29 stsp }
1091 80ddbec8 2018-04-29 stsp
1092 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1093 80ddbec8 2018-04-29 stsp static const struct got_error *
1094 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1095 963b370f 2018-05-20 stsp {
1096 00dfcb92 2018-06-11 stsp char *vis = NULL;
1097 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1098 963b370f 2018-05-20 stsp
1099 963b370f 2018-05-20 stsp *ws = NULL;
1100 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1101 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1102 00dfcb92 2018-06-11 stsp int vislen;
1103 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1104 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1105 00dfcb92 2018-06-11 stsp
1106 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1107 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1108 00dfcb92 2018-06-11 stsp if (err)
1109 00dfcb92 2018-06-11 stsp return err;
1110 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1111 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1112 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1113 a7f50699 2018-06-11 stsp goto done;
1114 a7f50699 2018-06-11 stsp }
1115 00dfcb92 2018-06-11 stsp }
1116 963b370f 2018-05-20 stsp
1117 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1118 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1119 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1120 a7f50699 2018-06-11 stsp goto done;
1121 a7f50699 2018-06-11 stsp }
1122 963b370f 2018-05-20 stsp
1123 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1124 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1125 a7f50699 2018-06-11 stsp done:
1126 00dfcb92 2018-06-11 stsp free(vis);
1127 963b370f 2018-05-20 stsp if (err) {
1128 963b370f 2018-05-20 stsp free(*ws);
1129 963b370f 2018-05-20 stsp *ws = NULL;
1130 963b370f 2018-05-20 stsp *wlen = 0;
1131 963b370f 2018-05-20 stsp }
1132 963b370f 2018-05-20 stsp return err;
1133 963b370f 2018-05-20 stsp }
1134 963b370f 2018-05-20 stsp
1135 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1136 963b370f 2018-05-20 stsp static const struct got_error *
1137 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1138 27a741e5 2019-09-11 stsp int col_tab_align)
1139 963b370f 2018-05-20 stsp {
1140 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1141 963b370f 2018-05-20 stsp int cols = 0;
1142 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1143 963b370f 2018-05-20 stsp size_t wlen;
1144 963b370f 2018-05-20 stsp int i;
1145 963b370f 2018-05-20 stsp
1146 963b370f 2018-05-20 stsp *wlinep = NULL;
1147 b700b5d6 2018-07-10 stsp *widthp = 0;
1148 963b370f 2018-05-20 stsp
1149 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1150 963b370f 2018-05-20 stsp if (err)
1151 963b370f 2018-05-20 stsp return err;
1152 963b370f 2018-05-20 stsp
1153 963b370f 2018-05-20 stsp i = 0;
1154 27a741e5 2019-09-11 stsp while (i < wlen) {
1155 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1156 27a741e5 2019-09-11 stsp
1157 27a741e5 2019-09-11 stsp if (width == 0) {
1158 b700b5d6 2018-07-10 stsp i++;
1159 27a741e5 2019-09-11 stsp continue;
1160 27a741e5 2019-09-11 stsp }
1161 27a741e5 2019-09-11 stsp
1162 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1163 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1164 27a741e5 2019-09-11 stsp break;
1165 27a741e5 2019-09-11 stsp cols += width;
1166 3c1f04f1 2018-09-13 stsp i++;
1167 27a741e5 2019-09-11 stsp } else if (width == -1) {
1168 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1169 27a741e5 2019-09-11 stsp width = TABSIZE -
1170 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1171 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1172 27a741e5 2019-09-11 stsp break;
1173 27a741e5 2019-09-11 stsp cols += width;
1174 27a741e5 2019-09-11 stsp }
1175 b700b5d6 2018-07-10 stsp i++;
1176 27a741e5 2019-09-11 stsp } else {
1177 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1178 963b370f 2018-05-20 stsp goto done;
1179 963b370f 2018-05-20 stsp }
1180 963b370f 2018-05-20 stsp }
1181 963b370f 2018-05-20 stsp wline[i] = L'\0';
1182 b700b5d6 2018-07-10 stsp if (widthp)
1183 b700b5d6 2018-07-10 stsp *widthp = cols;
1184 963b370f 2018-05-20 stsp done:
1185 963b370f 2018-05-20 stsp if (err)
1186 963b370f 2018-05-20 stsp free(wline);
1187 963b370f 2018-05-20 stsp else
1188 963b370f 2018-05-20 stsp *wlinep = wline;
1189 963b370f 2018-05-20 stsp return err;
1190 963b370f 2018-05-20 stsp }
1191 963b370f 2018-05-20 stsp
1192 8b473291 2019-02-21 stsp static const struct got_error*
1193 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1194 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1195 8b473291 2019-02-21 stsp {
1196 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1197 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1198 8b473291 2019-02-21 stsp char *s;
1199 8b473291 2019-02-21 stsp const char *name;
1200 8b473291 2019-02-21 stsp
1201 8b473291 2019-02-21 stsp *refs_str = NULL;
1202 8b473291 2019-02-21 stsp
1203 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1204 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1205 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1206 52b5abe1 2019-08-13 stsp int cmp;
1207 52b5abe1 2019-08-13 stsp
1208 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1209 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1210 8b473291 2019-02-21 stsp continue;
1211 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1212 8b473291 2019-02-21 stsp name += 5;
1213 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1214 7143d404 2019-03-12 stsp continue;
1215 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1216 8b473291 2019-02-21 stsp name += 6;
1217 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1218 8b473291 2019-02-21 stsp name += 8;
1219 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1220 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1221 79cc719f 2020-04-24 stsp continue;
1222 79cc719f 2020-04-24 stsp }
1223 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1224 48cae60d 2020-09-22 stsp if (err)
1225 48cae60d 2020-09-22 stsp break;
1226 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1227 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1228 5d844a1e 2019-08-13 stsp if (err) {
1229 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1230 48cae60d 2020-09-22 stsp free(ref_id);
1231 5d844a1e 2019-08-13 stsp break;
1232 48cae60d 2020-09-22 stsp }
1233 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1234 5d844a1e 2019-08-13 stsp err = NULL;
1235 5d844a1e 2019-08-13 stsp tag = NULL;
1236 5d844a1e 2019-08-13 stsp }
1237 52b5abe1 2019-08-13 stsp }
1238 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1239 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1240 48cae60d 2020-09-22 stsp free(ref_id);
1241 52b5abe1 2019-08-13 stsp if (tag)
1242 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1243 52b5abe1 2019-08-13 stsp if (cmp != 0)
1244 52b5abe1 2019-08-13 stsp continue;
1245 8b473291 2019-02-21 stsp s = *refs_str;
1246 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1247 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1249 8b473291 2019-02-21 stsp free(s);
1250 8b473291 2019-02-21 stsp *refs_str = NULL;
1251 8b473291 2019-02-21 stsp break;
1252 8b473291 2019-02-21 stsp }
1253 8b473291 2019-02-21 stsp free(s);
1254 8b473291 2019-02-21 stsp }
1255 8b473291 2019-02-21 stsp
1256 8b473291 2019-02-21 stsp return err;
1257 8b473291 2019-02-21 stsp }
1258 8b473291 2019-02-21 stsp
1259 963b370f 2018-05-20 stsp static const struct got_error *
1260 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1261 27a741e5 2019-09-11 stsp int col_tab_align)
1262 5813d178 2019-03-09 stsp {
1263 5813d178 2019-03-09 stsp char *smallerthan, *at;
1264 5813d178 2019-03-09 stsp
1265 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1266 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1267 5813d178 2019-03-09 stsp author = smallerthan + 1;
1268 5813d178 2019-03-09 stsp at = strchr(author, '@');
1269 5813d178 2019-03-09 stsp if (at)
1270 5813d178 2019-03-09 stsp *at = '\0';
1271 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1272 5813d178 2019-03-09 stsp }
1273 5813d178 2019-03-09 stsp
1274 5813d178 2019-03-09 stsp static const struct got_error *
1275 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1276 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1277 8fdc79fe 2020-12-01 naddy int author_display_cols)
1278 80ddbec8 2018-04-29 stsp {
1279 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1280 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1281 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1282 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1283 5813d178 2019-03-09 stsp char *author = NULL;
1284 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1285 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1286 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1287 bb737323 2018-05-20 stsp int col, limit;
1288 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1289 ccb26ccd 2018-11-05 stsp struct tm tm;
1290 45d799e2 2018-12-23 stsp time_t committer_time;
1291 11b20872 2019-11-08 stsp struct tog_color *tc;
1292 80ddbec8 2018-04-29 stsp
1293 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1294 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1295 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1296 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1297 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1298 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1299 b39d25c7 2018-07-10 stsp
1300 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1301 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1302 b39d25c7 2018-07-10 stsp else
1303 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1304 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1305 11b20872 2019-11-08 stsp if (tc)
1306 11b20872 2019-11-08 stsp wattr_on(view->window,
1307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1308 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1309 11b20872 2019-11-08 stsp if (tc)
1310 11b20872 2019-11-08 stsp wattr_off(view->window,
1311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1312 27a741e5 2019-09-11 stsp col = limit;
1313 b39d25c7 2018-07-10 stsp if (col > avail)
1314 b39d25c7 2018-07-10 stsp goto done;
1315 6570a66d 2019-11-08 stsp
1316 6570a66d 2019-11-08 stsp if (avail >= 120) {
1317 6570a66d 2019-11-08 stsp char *id_str;
1318 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1319 6570a66d 2019-11-08 stsp if (err)
1320 6570a66d 2019-11-08 stsp goto done;
1321 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1322 11b20872 2019-11-08 stsp if (tc)
1323 11b20872 2019-11-08 stsp wattr_on(view->window,
1324 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1325 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1326 11b20872 2019-11-08 stsp if (tc)
1327 11b20872 2019-11-08 stsp wattr_off(view->window,
1328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1329 6570a66d 2019-11-08 stsp free(id_str);
1330 6570a66d 2019-11-08 stsp col += 9;
1331 6570a66d 2019-11-08 stsp if (col > avail)
1332 6570a66d 2019-11-08 stsp goto done;
1333 6570a66d 2019-11-08 stsp }
1334 b39d25c7 2018-07-10 stsp
1335 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1336 5813d178 2019-03-09 stsp if (author == NULL) {
1337 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1338 80ddbec8 2018-04-29 stsp goto done;
1339 80ddbec8 2018-04-29 stsp }
1340 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1341 bb737323 2018-05-20 stsp if (err)
1342 bb737323 2018-05-20 stsp goto done;
1343 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1344 11b20872 2019-11-08 stsp if (tc)
1345 11b20872 2019-11-08 stsp wattr_on(view->window,
1346 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1347 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1348 11b20872 2019-11-08 stsp if (tc)
1349 11b20872 2019-11-08 stsp wattr_off(view->window,
1350 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1351 bb737323 2018-05-20 stsp col += author_width;
1352 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1353 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1354 bb737323 2018-05-20 stsp col++;
1355 bb737323 2018-05-20 stsp author_width++;
1356 bb737323 2018-05-20 stsp }
1357 9c2eaf34 2018-05-20 stsp if (col > avail)
1358 9c2eaf34 2018-05-20 stsp goto done;
1359 80ddbec8 2018-04-29 stsp
1360 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1361 5943eee2 2019-08-13 stsp if (err)
1362 6d9fbc00 2018-04-29 stsp goto done;
1363 bb737323 2018-05-20 stsp logmsg = logmsg0;
1364 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1365 bb737323 2018-05-20 stsp logmsg++;
1366 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1367 bb737323 2018-05-20 stsp if (newline)
1368 bb737323 2018-05-20 stsp *newline = '\0';
1369 bb737323 2018-05-20 stsp limit = avail - col;
1370 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1371 bb737323 2018-05-20 stsp if (err)
1372 bb737323 2018-05-20 stsp goto done;
1373 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1374 bb737323 2018-05-20 stsp col += logmsg_width;
1375 27a741e5 2019-09-11 stsp while (col < avail) {
1376 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1377 bb737323 2018-05-20 stsp col++;
1378 881b2d3e 2018-04-30 stsp }
1379 80ddbec8 2018-04-29 stsp done:
1380 80ddbec8 2018-04-29 stsp free(logmsg0);
1381 bb737323 2018-05-20 stsp free(wlogmsg);
1382 5813d178 2019-03-09 stsp free(author);
1383 bb737323 2018-05-20 stsp free(wauthor);
1384 80ddbec8 2018-04-29 stsp free(line);
1385 80ddbec8 2018-04-29 stsp return err;
1386 80ddbec8 2018-04-29 stsp }
1387 26ed57b2 2018-05-19 stsp
1388 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1389 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1390 899d86c2 2018-05-10 stsp struct got_object_id *id)
1391 80ddbec8 2018-04-29 stsp {
1392 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1393 80ddbec8 2018-04-29 stsp
1394 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1395 80ddbec8 2018-04-29 stsp if (entry == NULL)
1396 899d86c2 2018-05-10 stsp return NULL;
1397 99db9666 2018-05-07 stsp
1398 899d86c2 2018-05-10 stsp entry->id = id;
1399 99db9666 2018-05-07 stsp entry->commit = commit;
1400 899d86c2 2018-05-10 stsp return entry;
1401 99db9666 2018-05-07 stsp }
1402 80ddbec8 2018-04-29 stsp
1403 99db9666 2018-05-07 stsp static void
1404 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1405 99db9666 2018-05-07 stsp {
1406 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1407 99db9666 2018-05-07 stsp
1408 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1409 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1410 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1411 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1412 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1413 99db9666 2018-05-07 stsp free(entry);
1414 99db9666 2018-05-07 stsp }
1415 99db9666 2018-05-07 stsp
1416 99db9666 2018-05-07 stsp static void
1417 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1418 99db9666 2018-05-07 stsp {
1419 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1420 99db9666 2018-05-07 stsp pop_commit(commits);
1421 c4972b91 2018-05-07 stsp }
1422 c4972b91 2018-05-07 stsp
1423 c4972b91 2018-05-07 stsp static const struct got_error *
1424 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1425 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1426 13add988 2019-10-15 stsp {
1427 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1428 13add988 2019-10-15 stsp regmatch_t regmatch;
1429 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1430 13add988 2019-10-15 stsp
1431 13add988 2019-10-15 stsp *have_match = 0;
1432 13add988 2019-10-15 stsp
1433 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1434 13add988 2019-10-15 stsp if (err)
1435 13add988 2019-10-15 stsp return err;
1436 13add988 2019-10-15 stsp
1437 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1438 13add988 2019-10-15 stsp if (err)
1439 13add988 2019-10-15 stsp goto done;
1440 13add988 2019-10-15 stsp
1441 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1442 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1443 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1444 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1445 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1446 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1447 13add988 2019-10-15 stsp *have_match = 1;
1448 13add988 2019-10-15 stsp done:
1449 13add988 2019-10-15 stsp free(id_str);
1450 13add988 2019-10-15 stsp free(logmsg);
1451 13add988 2019-10-15 stsp return err;
1452 13add988 2019-10-15 stsp }
1453 13add988 2019-10-15 stsp
1454 13add988 2019-10-15 stsp static const struct got_error *
1455 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1456 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1457 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1458 c4972b91 2018-05-07 stsp {
1459 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1460 7c1452c1 2020-03-26 stsp int nqueued = 0;
1461 9ba79e04 2018-06-11 stsp
1462 1a76625f 2018-10-22 stsp /*
1463 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1464 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1465 1a76625f 2018-10-22 stsp * while updating the display.
1466 1a76625f 2018-10-22 stsp */
1467 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1468 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1469 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1470 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1471 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1472 1a76625f 2018-10-22 stsp int errcode;
1473 899d86c2 2018-05-10 stsp
1474 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1475 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1476 ecb28ae0 2018-07-16 stsp break;
1477 899d86c2 2018-05-10 stsp
1478 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1479 9ba79e04 2018-06-11 stsp if (err)
1480 9ba79e04 2018-06-11 stsp break;
1481 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1482 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1483 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1484 9ba79e04 2018-06-11 stsp break;
1485 9ba79e04 2018-06-11 stsp }
1486 93e45b7c 2018-09-24 stsp
1487 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1488 1a76625f 2018-10-22 stsp if (errcode) {
1489 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1490 13add988 2019-10-15 stsp "pthread_mutex_lock");
1491 1a76625f 2018-10-22 stsp break;
1492 1a76625f 2018-10-22 stsp }
1493 1a76625f 2018-10-22 stsp
1494 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1495 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1496 ecb28ae0 2018-07-16 stsp nqueued++;
1497 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1498 1a76625f 2018-10-22 stsp
1499 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1500 7c1452c1 2020-03-26 stsp int have_match;
1501 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1502 7c1452c1 2020-03-26 stsp if (err)
1503 7c1452c1 2020-03-26 stsp break;
1504 7c1452c1 2020-03-26 stsp if (have_match)
1505 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1506 13add988 2019-10-15 stsp }
1507 13add988 2019-10-15 stsp
1508 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1509 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1510 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1511 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1512 7c1452c1 2020-03-26 stsp if (err)
1513 13add988 2019-10-15 stsp break;
1514 899d86c2 2018-05-10 stsp }
1515 899d86c2 2018-05-10 stsp
1516 9ba79e04 2018-06-11 stsp return err;
1517 0553a4e3 2018-04-30 stsp }
1518 0553a4e3 2018-04-30 stsp
1519 0553a4e3 2018-04-30 stsp static const struct got_error *
1520 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1521 0553a4e3 2018-04-30 stsp {
1522 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1523 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1524 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1525 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1526 60493ae3 2019-06-20 stsp int width;
1527 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1528 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1529 8b473291 2019-02-21 stsp char *refs_str = NULL;
1530 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1531 11b20872 2019-11-08 stsp struct tog_color *tc;
1532 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1533 0553a4e3 2018-04-30 stsp
1534 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1535 e0d42f60 2018-07-22 stsp ncommits = 0;
1536 e0d42f60 2018-07-22 stsp while (entry) {
1537 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected) {
1538 8fdc79fe 2020-12-01 naddy s->selected_entry = entry;
1539 e0d42f60 2018-07-22 stsp break;
1540 e0d42f60 2018-07-22 stsp }
1541 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1542 e0d42f60 2018-07-22 stsp ncommits++;
1543 e0d42f60 2018-07-22 stsp }
1544 e0d42f60 2018-07-22 stsp
1545 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1546 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1547 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1548 1a76625f 2018-10-22 stsp if (err)
1549 ecb28ae0 2018-07-16 stsp return err;
1550 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1551 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1552 8fdc79fe 2020-12-01 naddy if (err)
1553 8fdc79fe 2020-12-01 naddy goto done;
1554 867c6645 2018-07-10 stsp }
1555 359bfafd 2019-02-22 stsp
1556 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1557 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1558 1a76625f 2018-10-22 stsp
1559 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1560 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1561 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1562 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1563 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1564 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1565 8f4ed634 2020-03-26 stsp goto done;
1566 8f4ed634 2020-03-26 stsp }
1567 8f4ed634 2020-03-26 stsp } else {
1568 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1569 f9686aa5 2020-03-27 stsp
1570 f9686aa5 2020-03-27 stsp if (view->searching) {
1571 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1572 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1573 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1574 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1575 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1576 f9686aa5 2020-03-27 stsp search_str = "searching...";
1577 f9686aa5 2020-03-27 stsp }
1578 f9686aa5 2020-03-27 stsp
1579 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1580 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1581 f9686aa5 2020-03-27 stsp search_str ? search_str :
1582 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1583 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1584 8f4ed634 2020-03-26 stsp goto done;
1585 8f4ed634 2020-03-26 stsp }
1586 8b473291 2019-02-21 stsp }
1587 1a76625f 2018-10-22 stsp
1588 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1589 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1590 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1591 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1592 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1593 1a76625f 2018-10-22 stsp header = NULL;
1594 1a76625f 2018-10-22 stsp goto done;
1595 1a76625f 2018-10-22 stsp }
1596 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1597 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1598 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1599 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1600 1a76625f 2018-10-22 stsp header = NULL;
1601 1a76625f 2018-10-22 stsp goto done;
1602 ecb28ae0 2018-07-16 stsp }
1603 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1604 1a76625f 2018-10-22 stsp if (err)
1605 1a76625f 2018-10-22 stsp goto done;
1606 867c6645 2018-07-10 stsp
1607 2814baeb 2018-08-01 stsp werase(view->window);
1608 867c6645 2018-07-10 stsp
1609 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1610 a3404814 2018-09-02 stsp wstandout(view->window);
1611 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1612 11b20872 2019-11-08 stsp if (tc)
1613 11b20872 2019-11-08 stsp wattr_on(view->window,
1614 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1615 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1616 11b20872 2019-11-08 stsp if (tc)
1617 11b20872 2019-11-08 stsp wattr_off(view->window,
1618 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1619 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1620 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1621 1a76625f 2018-10-22 stsp width++;
1622 1a76625f 2018-10-22 stsp }
1623 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1624 a3404814 2018-09-02 stsp wstandend(view->window);
1625 ecb28ae0 2018-07-16 stsp free(wline);
1626 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1627 1a76625f 2018-10-22 stsp goto done;
1628 0553a4e3 2018-04-30 stsp
1629 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1630 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1631 5813d178 2019-03-09 stsp ncommits = 0;
1632 5813d178 2019-03-09 stsp while (entry) {
1633 5813d178 2019-03-09 stsp char *author;
1634 5813d178 2019-03-09 stsp wchar_t *wauthor;
1635 5813d178 2019-03-09 stsp int width;
1636 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1637 5813d178 2019-03-09 stsp break;
1638 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1639 5813d178 2019-03-09 stsp if (author == NULL) {
1640 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1641 5813d178 2019-03-09 stsp goto done;
1642 5813d178 2019-03-09 stsp }
1643 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1644 27a741e5 2019-09-11 stsp date_display_cols);
1645 5813d178 2019-03-09 stsp if (author_cols < width)
1646 5813d178 2019-03-09 stsp author_cols = width;
1647 5813d178 2019-03-09 stsp free(wauthor);
1648 5813d178 2019-03-09 stsp free(author);
1649 7ca04879 2019-10-19 stsp ncommits++;
1650 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1651 5813d178 2019-03-09 stsp }
1652 5813d178 2019-03-09 stsp
1653 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1654 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1655 867c6645 2018-07-10 stsp ncommits = 0;
1656 899d86c2 2018-05-10 stsp while (entry) {
1657 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1658 899d86c2 2018-05-10 stsp break;
1659 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1660 2814baeb 2018-08-01 stsp wstandout(view->window);
1661 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1662 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1663 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1664 2814baeb 2018-08-01 stsp wstandend(view->window);
1665 0553a4e3 2018-04-30 stsp if (err)
1666 60493ae3 2019-06-20 stsp goto done;
1667 0553a4e3 2018-04-30 stsp ncommits++;
1668 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1669 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1670 80ddbec8 2018-04-29 stsp }
1671 80ddbec8 2018-04-29 stsp
1672 1a57306a 2018-09-02 stsp view_vborder(view);
1673 1a76625f 2018-10-22 stsp done:
1674 1a76625f 2018-10-22 stsp free(id_str);
1675 8b473291 2019-02-21 stsp free(refs_str);
1676 1a76625f 2018-10-22 stsp free(ncommits_str);
1677 1a76625f 2018-10-22 stsp free(header);
1678 80ddbec8 2018-04-29 stsp return err;
1679 9f7d7167 2018-04-29 stsp }
1680 07b55e75 2018-05-10 stsp
1681 07b55e75 2018-05-10 stsp static void
1682 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1683 07b55e75 2018-05-10 stsp {
1684 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1685 07b55e75 2018-05-10 stsp int nscrolled = 0;
1686 07b55e75 2018-05-10 stsp
1687 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1688 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1689 07b55e75 2018-05-10 stsp return;
1690 9f7d7167 2018-04-29 stsp
1691 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1692 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1693 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1694 07b55e75 2018-05-10 stsp if (entry) {
1695 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1696 07b55e75 2018-05-10 stsp nscrolled++;
1697 07b55e75 2018-05-10 stsp }
1698 07b55e75 2018-05-10 stsp }
1699 aa075928 2018-05-10 stsp }
1700 aa075928 2018-05-10 stsp
1701 aa075928 2018-05-10 stsp static const struct got_error *
1702 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1703 aa075928 2018-05-10 stsp {
1704 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1705 5e224a3e 2019-02-22 stsp int errcode;
1706 8a42fca8 2019-02-22 stsp
1707 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1708 aa075928 2018-05-10 stsp
1709 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1710 ffe38506 2020-12-01 naddy if (ta->log_complete)
1711 5e224a3e 2019-02-22 stsp break;
1712 b295e71b 2019-02-22 stsp
1713 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1714 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1715 7aafa0d1 2019-02-22 stsp if (errcode)
1716 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1717 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1718 7c1452c1 2020-03-26 stsp
1719 7c1452c1 2020-03-26 stsp /*
1720 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1721 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1722 7c1452c1 2020-03-26 stsp */
1723 7c1452c1 2020-03-26 stsp if (!wait)
1724 7c1452c1 2020-03-26 stsp break;
1725 7c1452c1 2020-03-26 stsp
1726 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1727 ffe38506 2020-12-01 naddy show_log_view(view);
1728 7c1452c1 2020-03-26 stsp update_panels();
1729 7c1452c1 2020-03-26 stsp doupdate();
1730 7c1452c1 2020-03-26 stsp
1731 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1732 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1733 82954512 2020-02-03 stsp if (errcode)
1734 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1735 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1736 82954512 2020-02-03 stsp
1737 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1738 ffe38506 2020-12-01 naddy show_log_view(view);
1739 7c1452c1 2020-03-26 stsp update_panels();
1740 7c1452c1 2020-03-26 stsp doupdate();
1741 5e224a3e 2019-02-22 stsp }
1742 5e224a3e 2019-02-22 stsp
1743 5e224a3e 2019-02-22 stsp return NULL;
1744 5e224a3e 2019-02-22 stsp }
1745 5e224a3e 2019-02-22 stsp
1746 5e224a3e 2019-02-22 stsp static const struct got_error *
1747 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1748 5e224a3e 2019-02-22 stsp {
1749 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1750 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1751 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1752 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1753 5e224a3e 2019-02-22 stsp
1754 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1755 5e224a3e 2019-02-22 stsp return NULL;
1756 5e224a3e 2019-02-22 stsp
1757 ffe38506 2020-12-01 naddy ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1758 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1759 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1760 08ebd0a9 2019-02-22 stsp /*
1761 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1762 08ebd0a9 2019-02-22 stsp */
1763 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1764 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1765 5e224a3e 2019-02-22 stsp if (err)
1766 5e224a3e 2019-02-22 stsp return err;
1767 7aafa0d1 2019-02-22 stsp }
1768 b295e71b 2019-02-22 stsp
1769 7aafa0d1 2019-02-22 stsp do {
1770 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1771 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1772 88048b54 2019-02-21 stsp break;
1773 88048b54 2019-02-21 stsp
1774 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1775 aa075928 2018-05-10 stsp
1776 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1777 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1778 dd0a52c1 2018-05-20 stsp break;
1779 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1780 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1781 aa075928 2018-05-10 stsp
1782 dd0a52c1 2018-05-20 stsp return err;
1783 07b55e75 2018-05-10 stsp }
1784 4a7f7875 2018-05-10 stsp
1785 cd0acaa7 2018-05-20 stsp static const struct got_error *
1786 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1787 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1788 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1789 cd0acaa7 2018-05-20 stsp {
1790 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1791 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1792 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1793 cd0acaa7 2018-05-20 stsp
1794 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1795 15a94983 2018-12-23 stsp if (diff_view == NULL)
1796 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1797 ea5e7bb5 2018-08-01 stsp
1798 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1799 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1800 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1801 e5a0f69f 2018-08-18 stsp if (err == NULL)
1802 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1803 cd0acaa7 2018-05-20 stsp return err;
1804 4a7f7875 2018-05-10 stsp }
1805 4a7f7875 2018-05-10 stsp
1806 80ddbec8 2018-04-29 stsp static const struct got_error *
1807 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1808 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1809 9343a5fb 2018-06-23 stsp {
1810 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1811 941e9f74 2019-05-21 stsp
1812 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1813 941e9f74 2019-05-21 stsp if (parent == NULL)
1814 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1815 941e9f74 2019-05-21 stsp
1816 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1817 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1818 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1819 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1820 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1821 941e9f74 2019-05-21 stsp s->tree = subtree;
1822 941e9f74 2019-05-21 stsp s->selected = 0;
1823 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1824 941e9f74 2019-05-21 stsp return NULL;
1825 941e9f74 2019-05-21 stsp }
1826 941e9f74 2019-05-21 stsp
1827 941e9f74 2019-05-21 stsp static const struct got_error *
1828 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1829 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1830 941e9f74 2019-05-21 stsp {
1831 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1832 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1833 941e9f74 2019-05-21 stsp const char *p;
1834 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1835 9343a5fb 2018-06-23 stsp
1836 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1837 941e9f74 2019-05-21 stsp p = path;
1838 941e9f74 2019-05-21 stsp while (*p) {
1839 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1840 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1841 56e0773d 2019-11-28 stsp char *te_name;
1842 33cbf02b 2020-01-12 stsp
1843 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1844 33cbf02b 2020-01-12 stsp p++;
1845 941e9f74 2019-05-21 stsp
1846 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1847 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1848 941e9f74 2019-05-21 stsp if (slash == NULL)
1849 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1850 33cbf02b 2020-01-12 stsp else
1851 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1852 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1853 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1854 56e0773d 2019-11-28 stsp break;
1855 941e9f74 2019-05-21 stsp }
1856 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1857 56e0773d 2019-11-28 stsp if (te == NULL) {
1858 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1859 56e0773d 2019-11-28 stsp free(te_name);
1860 941e9f74 2019-05-21 stsp break;
1861 941e9f74 2019-05-21 stsp }
1862 56e0773d 2019-11-28 stsp free(te_name);
1863 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1864 941e9f74 2019-05-21 stsp
1865 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1866 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1867 b03c880f 2019-05-21 stsp
1868 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1869 941e9f74 2019-05-21 stsp if (slash)
1870 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1871 941e9f74 2019-05-21 stsp else
1872 941e9f74 2019-05-21 stsp subpath = strdup(path);
1873 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1874 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1875 941e9f74 2019-05-21 stsp break;
1876 941e9f74 2019-05-21 stsp }
1877 941e9f74 2019-05-21 stsp
1878 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1879 941e9f74 2019-05-21 stsp subpath);
1880 941e9f74 2019-05-21 stsp if (err)
1881 941e9f74 2019-05-21 stsp break;
1882 941e9f74 2019-05-21 stsp
1883 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1884 941e9f74 2019-05-21 stsp free(tree_id);
1885 941e9f74 2019-05-21 stsp if (err)
1886 941e9f74 2019-05-21 stsp break;
1887 941e9f74 2019-05-21 stsp
1888 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1889 941e9f74 2019-05-21 stsp if (err) {
1890 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1891 941e9f74 2019-05-21 stsp break;
1892 941e9f74 2019-05-21 stsp }
1893 941e9f74 2019-05-21 stsp if (slash == NULL)
1894 941e9f74 2019-05-21 stsp break;
1895 941e9f74 2019-05-21 stsp free(subpath);
1896 941e9f74 2019-05-21 stsp subpath = NULL;
1897 941e9f74 2019-05-21 stsp p = slash;
1898 941e9f74 2019-05-21 stsp }
1899 941e9f74 2019-05-21 stsp
1900 941e9f74 2019-05-21 stsp free(subpath);
1901 1a76625f 2018-10-22 stsp return err;
1902 61266923 2020-01-14 stsp }
1903 61266923 2020-01-14 stsp
1904 61266923 2020-01-14 stsp static const struct got_error *
1905 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1906 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1907 78756c87 2020-11-24 stsp struct got_repository *repo)
1908 55cccc34 2020-02-20 stsp {
1909 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1910 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1911 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1912 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1913 55cccc34 2020-02-20 stsp
1914 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1915 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1916 55cccc34 2020-02-20 stsp if (err)
1917 55cccc34 2020-02-20 stsp return err;
1918 55cccc34 2020-02-20 stsp
1919 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1920 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1921 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1922 55cccc34 2020-02-20 stsp
1923 78756c87 2020-11-24 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1924 55cccc34 2020-02-20 stsp if (err) {
1925 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1926 55cccc34 2020-02-20 stsp return err;
1927 55cccc34 2020-02-20 stsp }
1928 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1929 55cccc34 2020-02-20 stsp
1930 55cccc34 2020-02-20 stsp *new_view = tree_view;
1931 55cccc34 2020-02-20 stsp
1932 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1933 55cccc34 2020-02-20 stsp return NULL;
1934 55cccc34 2020-02-20 stsp
1935 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1936 55cccc34 2020-02-20 stsp }
1937 55cccc34 2020-02-20 stsp
1938 55cccc34 2020-02-20 stsp static const struct got_error *
1939 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1940 61266923 2020-01-14 stsp {
1941 61266923 2020-01-14 stsp sigset_t sigset;
1942 61266923 2020-01-14 stsp int errcode;
1943 61266923 2020-01-14 stsp
1944 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1945 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1946 61266923 2020-01-14 stsp
1947 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1948 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1949 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1950 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1951 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1952 61266923 2020-01-14 stsp
1953 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1954 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1955 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1956 61266923 2020-01-14 stsp
1957 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1958 61266923 2020-01-14 stsp if (errcode)
1959 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1960 61266923 2020-01-14 stsp
1961 61266923 2020-01-14 stsp return NULL;
1962 1a76625f 2018-10-22 stsp }
1963 1a76625f 2018-10-22 stsp
1964 1a76625f 2018-10-22 stsp static void *
1965 1a76625f 2018-10-22 stsp log_thread(void *arg)
1966 1a76625f 2018-10-22 stsp {
1967 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1968 1a76625f 2018-10-22 stsp int errcode = 0;
1969 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1970 1a76625f 2018-10-22 stsp int done = 0;
1971 61266923 2020-01-14 stsp
1972 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1973 61266923 2020-01-14 stsp if (err)
1974 61266923 2020-01-14 stsp return (void *)err;
1975 1a76625f 2018-10-22 stsp
1976 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1977 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1978 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1979 13add988 2019-10-15 stsp a->regex);
1980 1a76625f 2018-10-22 stsp if (err) {
1981 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1982 1a76625f 2018-10-22 stsp return (void *)err;
1983 1a76625f 2018-10-22 stsp err = NULL;
1984 1a76625f 2018-10-22 stsp done = 1;
1985 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1986 1a76625f 2018-10-22 stsp a->commits_needed--;
1987 1a76625f 2018-10-22 stsp
1988 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1989 3abe8080 2019-04-10 stsp if (errcode) {
1990 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1991 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1992 3abe8080 2019-04-10 stsp break;
1993 3abe8080 2019-04-10 stsp } else if (*a->quit)
1994 1a76625f 2018-10-22 stsp done = 1;
1995 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1996 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1997 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1998 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1999 1a76625f 2018-10-22 stsp }
2000 1a76625f 2018-10-22 stsp
2001 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2002 7c1452c1 2020-03-26 stsp if (errcode) {
2003 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2004 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2005 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2006 7c1452c1 2020-03-26 stsp break;
2007 7c1452c1 2020-03-26 stsp }
2008 7c1452c1 2020-03-26 stsp
2009 1a76625f 2018-10-22 stsp if (done)
2010 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2011 7c1452c1 2020-03-26 stsp else {
2012 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2013 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2014 7c1452c1 2020-03-26 stsp &tog_mutex);
2015 7c1452c1 2020-03-26 stsp if (errcode)
2016 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2017 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2018 7c1452c1 2020-03-26 stsp }
2019 1a76625f 2018-10-22 stsp }
2020 1a76625f 2018-10-22 stsp
2021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2022 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2023 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2024 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2025 1a76625f 2018-10-22 stsp }
2026 3abe8080 2019-04-10 stsp a->log_complete = 1;
2027 1a76625f 2018-10-22 stsp return (void *)err;
2028 1a76625f 2018-10-22 stsp }
2029 1a76625f 2018-10-22 stsp
2030 1a76625f 2018-10-22 stsp static const struct got_error *
2031 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2032 1a76625f 2018-10-22 stsp {
2033 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2034 1a76625f 2018-10-22 stsp int errcode;
2035 1a76625f 2018-10-22 stsp
2036 1a76625f 2018-10-22 stsp if (s->thread) {
2037 1a76625f 2018-10-22 stsp s->quit = 1;
2038 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2039 1a76625f 2018-10-22 stsp if (errcode)
2040 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2041 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2042 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2043 1a76625f 2018-10-22 stsp if (errcode)
2044 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2045 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2046 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2047 1a76625f 2018-10-22 stsp if (errcode)
2048 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2050 1a76625f 2018-10-22 stsp if (errcode)
2051 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2052 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2053 1a76625f 2018-10-22 stsp s->thread = NULL;
2054 1a76625f 2018-10-22 stsp }
2055 1a76625f 2018-10-22 stsp
2056 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2057 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2058 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2059 1a76625f 2018-10-22 stsp }
2060 1a76625f 2018-10-22 stsp
2061 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2062 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2063 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2064 1a76625f 2018-10-22 stsp }
2065 1a76625f 2018-10-22 stsp
2066 9343a5fb 2018-06-23 stsp return err;
2067 9343a5fb 2018-06-23 stsp }
2068 9343a5fb 2018-06-23 stsp
2069 9343a5fb 2018-06-23 stsp static const struct got_error *
2070 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2071 1a76625f 2018-10-22 stsp {
2072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2073 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2074 276b94a1 2020-11-13 naddy int errcode;
2075 1a76625f 2018-10-22 stsp
2076 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2077 276b94a1 2020-11-13 naddy
2078 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2079 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2080 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2081 276b94a1 2020-11-13 naddy
2082 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2083 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2084 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2085 276b94a1 2020-11-13 naddy
2086 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2087 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2088 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2089 1a76625f 2018-10-22 stsp free(s->start_id);
2090 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2091 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2092 1a76625f 2018-10-22 stsp return err;
2093 1a76625f 2018-10-22 stsp }
2094 1a76625f 2018-10-22 stsp
2095 1a76625f 2018-10-22 stsp static const struct got_error *
2096 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2097 60493ae3 2019-06-20 stsp {
2098 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2099 60493ae3 2019-06-20 stsp
2100 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2101 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2102 60493ae3 2019-06-20 stsp return NULL;
2103 60493ae3 2019-06-20 stsp }
2104 60493ae3 2019-06-20 stsp
2105 60493ae3 2019-06-20 stsp static const struct got_error *
2106 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2107 60493ae3 2019-06-20 stsp {
2108 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2109 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2110 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2111 60493ae3 2019-06-20 stsp
2112 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2113 f9686aa5 2020-03-27 stsp show_log_view(view);
2114 f9686aa5 2020-03-27 stsp update_panels();
2115 f9686aa5 2020-03-27 stsp doupdate();
2116 f9686aa5 2020-03-27 stsp
2117 96e2b566 2019-07-08 stsp if (s->search_entry) {
2118 13add988 2019-10-15 stsp int errcode, ch;
2119 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2120 13add988 2019-10-15 stsp if (errcode)
2121 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2122 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2123 13add988 2019-10-15 stsp ch = wgetch(view->window);
2124 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2125 13add988 2019-10-15 stsp if (errcode)
2126 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2127 13add988 2019-10-15 stsp "pthread_mutex_lock");
2128 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2129 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2130 678cbce5 2019-07-28 stsp return NULL;
2131 678cbce5 2019-07-28 stsp }
2132 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2133 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2134 96e2b566 2019-07-08 stsp else
2135 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2136 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2137 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2138 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2139 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2140 b1bf1435 2019-06-21 stsp else
2141 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2142 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2143 20be8d96 2019-06-21 stsp } else {
2144 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2145 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2146 20be8d96 2019-06-21 stsp else
2147 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2148 20be8d96 2019-06-21 stsp }
2149 60493ae3 2019-06-20 stsp
2150 60493ae3 2019-06-20 stsp while (1) {
2151 13add988 2019-10-15 stsp int have_match = 0;
2152 13add988 2019-10-15 stsp
2153 60493ae3 2019-06-20 stsp if (entry == NULL) {
2154 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2155 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2156 f9967bca 2020-03-27 stsp view->search_next_done =
2157 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2158 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2159 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2160 f801134a 2019-06-25 stsp return NULL;
2161 60493ae3 2019-06-20 stsp }
2162 96e2b566 2019-07-08 stsp /*
2163 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2164 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2165 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2166 96e2b566 2019-07-08 stsp */
2167 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2168 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2169 60493ae3 2019-06-20 stsp }
2170 60493ae3 2019-06-20 stsp
2171 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2172 13add988 2019-10-15 stsp &view->regex);
2173 5943eee2 2019-08-13 stsp if (err)
2174 13add988 2019-10-15 stsp break;
2175 13add988 2019-10-15 stsp if (have_match) {
2176 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2177 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2178 60493ae3 2019-06-20 stsp break;
2179 60493ae3 2019-06-20 stsp }
2180 13add988 2019-10-15 stsp
2181 96e2b566 2019-07-08 stsp s->search_entry = entry;
2182 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2183 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2184 b1bf1435 2019-06-21 stsp else
2185 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2186 60493ae3 2019-06-20 stsp }
2187 60493ae3 2019-06-20 stsp
2188 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2189 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2190 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2191 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2192 60493ae3 2019-06-20 stsp if (err)
2193 60493ae3 2019-06-20 stsp return err;
2194 ead14cbe 2019-06-21 stsp cur++;
2195 ead14cbe 2019-06-21 stsp }
2196 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2197 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2198 60493ae3 2019-06-20 stsp if (err)
2199 60493ae3 2019-06-20 stsp return err;
2200 ead14cbe 2019-06-21 stsp cur--;
2201 60493ae3 2019-06-20 stsp }
2202 60493ae3 2019-06-20 stsp }
2203 60493ae3 2019-06-20 stsp
2204 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2205 96e2b566 2019-07-08 stsp
2206 60493ae3 2019-06-20 stsp return NULL;
2207 60493ae3 2019-06-20 stsp }
2208 60493ae3 2019-06-20 stsp
2209 60493ae3 2019-06-20 stsp static const struct got_error *
2210 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2211 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2212 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2213 80ddbec8 2018-04-29 stsp {
2214 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2215 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2216 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2217 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2218 1a76625f 2018-10-22 stsp int errcode;
2219 80ddbec8 2018-04-29 stsp
2220 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2221 78756c87 2020-11-24 stsp
2222 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2223 f135c941 2020-02-20 stsp free(s->in_repo_path);
2224 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2225 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2226 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2227 f135c941 2020-02-20 stsp }
2228 ecb28ae0 2018-07-16 stsp
2229 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2230 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2231 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2232 9ba79e04 2018-06-11 stsp
2233 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2234 78756c87 2020-11-24 stsp if (err)
2235 78756c87 2020-11-24 stsp goto done;
2236 78756c87 2020-11-24 stsp
2237 fb2756b9 2018-08-04 stsp s->repo = repo;
2238 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2239 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2240 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2241 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2242 5036bf37 2018-09-24 stsp goto done;
2243 5036bf37 2018-09-24 stsp }
2244 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2245 e5a0f69f 2018-08-18 stsp
2246 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2247 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2248 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2250 11b20872 2019-11-08 stsp if (err)
2251 11b20872 2019-11-08 stsp goto done;
2252 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2253 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2254 11b20872 2019-11-08 stsp if (err) {
2255 11b20872 2019-11-08 stsp free_colors(&s->colors);
2256 11b20872 2019-11-08 stsp goto done;
2257 11b20872 2019-11-08 stsp }
2258 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2259 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2260 11b20872 2019-11-08 stsp if (err) {
2261 11b20872 2019-11-08 stsp free_colors(&s->colors);
2262 11b20872 2019-11-08 stsp goto done;
2263 11b20872 2019-11-08 stsp }
2264 11b20872 2019-11-08 stsp }
2265 11b20872 2019-11-08 stsp
2266 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2267 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2268 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2269 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2270 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2271 1a76625f 2018-10-22 stsp
2272 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2273 1a76625f 2018-10-22 stsp if (err)
2274 1a76625f 2018-10-22 stsp goto done;
2275 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2276 b672a97a 2020-01-27 stsp !s->log_branches);
2277 1a76625f 2018-10-22 stsp if (err)
2278 1a76625f 2018-10-22 stsp goto done;
2279 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2280 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2281 c5b78334 2020-01-12 stsp if (err)
2282 c5b78334 2020-01-12 stsp goto done;
2283 1a76625f 2018-10-22 stsp
2284 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2285 1a76625f 2018-10-22 stsp if (errcode) {
2286 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2287 1a76625f 2018-10-22 stsp goto done;
2288 1a76625f 2018-10-22 stsp }
2289 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2290 7c1452c1 2020-03-26 stsp if (errcode) {
2291 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2292 7c1452c1 2020-03-26 stsp goto done;
2293 7c1452c1 2020-03-26 stsp }
2294 1a76625f 2018-10-22 stsp
2295 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2296 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2297 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2298 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2299 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2300 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2301 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2302 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2303 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2304 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2305 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2306 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2307 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2308 ba4f502b 2018-08-04 stsp done:
2309 1a76625f 2018-10-22 stsp if (err)
2310 1a76625f 2018-10-22 stsp close_log_view(view);
2311 ba4f502b 2018-08-04 stsp return err;
2312 ba4f502b 2018-08-04 stsp }
2313 ba4f502b 2018-08-04 stsp
2314 e5a0f69f 2018-08-18 stsp static const struct got_error *
2315 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2316 ba4f502b 2018-08-04 stsp {
2317 f2f6d207 2020-11-24 stsp const struct got_error *err;
2318 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2319 ba4f502b 2018-08-04 stsp
2320 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2321 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2322 2b380cc8 2018-10-24 stsp &s->thread_args);
2323 2b380cc8 2018-10-24 stsp if (errcode)
2324 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2325 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2326 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2327 f2f6d207 2020-11-24 stsp if (err)
2328 f2f6d207 2020-11-24 stsp return err;
2329 f2f6d207 2020-11-24 stsp }
2330 2b380cc8 2018-10-24 stsp }
2331 2b380cc8 2018-10-24 stsp
2332 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2333 e5a0f69f 2018-08-18 stsp }
2334 04cc582a 2018-08-01 stsp
2335 e5a0f69f 2018-08-18 stsp static const struct got_error *
2336 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2337 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2338 e5a0f69f 2018-08-18 stsp {
2339 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2340 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2341 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2342 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2343 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2344 669b5ffa 2018-10-07 stsp int begin_x = 0;
2345 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2346 80ddbec8 2018-04-29 stsp
2347 e5a0f69f 2018-08-18 stsp switch (ch) {
2348 1e37a5c2 2019-05-12 jcs case 'q':
2349 1e37a5c2 2019-05-12 jcs s->quit = 1;
2350 1e37a5c2 2019-05-12 jcs break;
2351 1e37a5c2 2019-05-12 jcs case 'k':
2352 1e37a5c2 2019-05-12 jcs case KEY_UP:
2353 1e37a5c2 2019-05-12 jcs case '<':
2354 1e37a5c2 2019-05-12 jcs case ',':
2355 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2356 1a76625f 2018-10-22 stsp break;
2357 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2358 1e37a5c2 2019-05-12 jcs s->selected--;
2359 1144d21a 2019-06-21 stsp else
2360 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2361 1e37a5c2 2019-05-12 jcs break;
2362 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2363 a4292ac5 2019-05-12 jcs case CTRL('b'):
2364 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2365 e5a0f69f 2018-08-18 stsp break;
2366 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2367 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2368 1e37a5c2 2019-05-12 jcs s->selected = 0;
2369 e5a0f69f 2018-08-18 stsp break;
2370 1e37a5c2 2019-05-12 jcs }
2371 3e135950 2020-12-01 naddy log_scroll_up(s, view->nlines - 1);
2372 1e37a5c2 2019-05-12 jcs break;
2373 1e37a5c2 2019-05-12 jcs case 'j':
2374 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2375 1e37a5c2 2019-05-12 jcs case '>':
2376 1e37a5c2 2019-05-12 jcs case '.':
2377 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2378 e5a0f69f 2018-08-18 stsp break;
2379 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2380 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2381 1e37a5c2 2019-05-12 jcs s->selected++;
2382 1e37a5c2 2019-05-12 jcs break;
2383 80ddbec8 2018-04-29 stsp }
2384 ffe38506 2020-12-01 naddy err = log_scroll_down(view, 1);
2385 1e37a5c2 2019-05-12 jcs break;
2386 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2387 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2388 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2389 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2390 1e37a5c2 2019-05-12 jcs if (first == NULL)
2391 e5a0f69f 2018-08-18 stsp break;
2392 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2393 1cae65b4 2019-09-22 stsp if (err)
2394 1cae65b4 2019-09-22 stsp break;
2395 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2396 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2397 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2398 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2399 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2400 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2401 1e37a5c2 2019-05-12 jcs }
2402 1e37a5c2 2019-05-12 jcs err = NULL;
2403 1e37a5c2 2019-05-12 jcs break;
2404 1e37a5c2 2019-05-12 jcs }
2405 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2406 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2407 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2408 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2409 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2410 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2411 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2412 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2413 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2414 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2415 0bf7f153 2020-12-02 naddy }
2416 1e37a5c2 2019-05-12 jcs break;
2417 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2418 87c7274c 2019-05-12 jcs case ' ':
2419 1e37a5c2 2019-05-12 jcs case '\r':
2420 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2421 e5a0f69f 2018-08-18 stsp break;
2422 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2423 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2424 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2425 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2426 78756c87 2020-11-24 stsp view, s->repo);
2427 1e37a5c2 2019-05-12 jcs if (err)
2428 1e37a5c2 2019-05-12 jcs break;
2429 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2430 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2431 f7013a22 2018-10-24 stsp if (err)
2432 1e37a5c2 2019-05-12 jcs return err;
2433 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2434 1e37a5c2 2019-05-12 jcs if (err) {
2435 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2436 f7013a22 2018-10-24 stsp break;
2437 5036bf37 2018-09-24 stsp }
2438 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2439 1e37a5c2 2019-05-12 jcs } else
2440 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2441 1e37a5c2 2019-05-12 jcs break;
2442 1e37a5c2 2019-05-12 jcs case 't':
2443 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2444 5036bf37 2018-09-24 stsp break;
2445 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2446 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2447 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2448 78756c87 2020-11-24 stsp s->selected_entry, s->in_repo_path, s->repo);
2449 1e37a5c2 2019-05-12 jcs if (err)
2450 e5a0f69f 2018-08-18 stsp break;
2451 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2452 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2453 1e37a5c2 2019-05-12 jcs if (err)
2454 1e37a5c2 2019-05-12 jcs return err;
2455 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2456 1e37a5c2 2019-05-12 jcs if (err) {
2457 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2458 1e37a5c2 2019-05-12 jcs break;
2459 1e37a5c2 2019-05-12 jcs }
2460 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2461 1e37a5c2 2019-05-12 jcs } else
2462 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2463 1e37a5c2 2019-05-12 jcs break;
2464 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2465 74cfe85e 2020-10-20 stsp if (got_path_cmp(s->in_repo_path, "/",
2466 74cfe85e 2020-10-20 stsp strlen(s->in_repo_path), 1) == 0)
2467 1e37a5c2 2019-05-12 jcs break;
2468 74cfe85e 2020-10-20 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2469 74cfe85e 2020-10-20 stsp if (err)
2470 74cfe85e 2020-10-20 stsp return err;
2471 74cfe85e 2020-10-20 stsp err = stop_log_thread(s);
2472 74cfe85e 2020-10-20 stsp if (err) {
2473 74cfe85e 2020-10-20 stsp free(parent_path);
2474 74cfe85e 2020-10-20 stsp return err;
2475 74cfe85e 2020-10-20 stsp }
2476 74cfe85e 2020-10-20 stsp lv = view_open(view->nlines, view->ncols,
2477 74cfe85e 2020-10-20 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2478 74cfe85e 2020-10-20 stsp if (lv == NULL) {
2479 74cfe85e 2020-10-20 stsp free(parent_path);
2480 74cfe85e 2020-10-20 stsp return got_error_from_errno("view_open");
2481 74cfe85e 2020-10-20 stsp }
2482 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2483 78756c87 2020-11-24 stsp parent_path, s->log_branches);
2484 74cfe85e 2020-10-20 stsp free(parent_path);
2485 74cfe85e 2020-10-20 stsp if (err)
2486 74cfe85e 2020-10-20 stsp return err;;
2487 74cfe85e 2020-10-20 stsp if (view_is_parent_view(view))
2488 74cfe85e 2020-10-20 stsp *new_view = lv;
2489 74cfe85e 2020-10-20 stsp else {
2490 74cfe85e 2020-10-20 stsp view_set_child(view->parent, lv);
2491 74cfe85e 2020-10-20 stsp *focus_view = lv;
2492 1e37a5c2 2019-05-12 jcs }
2493 1e37a5c2 2019-05-12 jcs break;
2494 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2495 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2496 d01904d4 2019-06-25 stsp if (err)
2497 d01904d4 2019-06-25 stsp return err;
2498 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2499 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2500 d01904d4 2019-06-25 stsp if (lv == NULL)
2501 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2502 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2503 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2504 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2505 ef129c5e 2019-08-03 stsp if (err) {
2506 ef129c5e 2019-08-03 stsp view_close(lv);
2507 d01904d4 2019-06-25 stsp return err;
2508 ef129c5e 2019-08-03 stsp }
2509 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2510 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2511 e6b23735 2019-10-04 stsp free(start_id);
2512 e6b23735 2019-10-04 stsp view_close(lv);
2513 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2514 e6b23735 2019-10-04 stsp }
2515 78756c87 2020-11-24 stsp err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2516 78756c87 2020-11-24 stsp in_repo_path, s->log_branches);
2517 ef129c5e 2019-08-03 stsp if (err) {
2518 ef129c5e 2019-08-03 stsp free(start_id);
2519 b672a97a 2020-01-27 stsp view_close(lv);
2520 b672a97a 2020-01-27 stsp return err;;
2521 b672a97a 2020-01-27 stsp }
2522 b672a97a 2020-01-27 stsp *dead_view = view;
2523 b672a97a 2020-01-27 stsp *new_view = lv;
2524 b672a97a 2020-01-27 stsp break;
2525 b672a97a 2020-01-27 stsp case 'B':
2526 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2527 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2528 b672a97a 2020-01-27 stsp if (err)
2529 b672a97a 2020-01-27 stsp return err;
2530 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2531 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2532 b672a97a 2020-01-27 stsp if (lv == NULL)
2533 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2534 78756c87 2020-11-24 stsp err = open_log_view(lv, s->start_id, s->repo,
2535 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2536 b672a97a 2020-01-27 stsp if (err) {
2537 ef129c5e 2019-08-03 stsp view_close(lv);
2538 d01904d4 2019-06-25 stsp return err;;
2539 ef129c5e 2019-08-03 stsp }
2540 d01904d4 2019-06-25 stsp *dead_view = view;
2541 d01904d4 2019-06-25 stsp *new_view = lv;
2542 d01904d4 2019-06-25 stsp break;
2543 6458efa5 2020-11-24 stsp case 'r':
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;
2554 6458efa5 2020-11-24 stsp }
2555 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2556 6458efa5 2020-11-24 stsp err = view_close_child(view);
2557 6458efa5 2020-11-24 stsp if (err)
2558 6458efa5 2020-11-24 stsp return err;
2559 6458efa5 2020-11-24 stsp err = view_set_child(view, ref_view);
2560 6458efa5 2020-11-24 stsp if (err) {
2561 6458efa5 2020-11-24 stsp view_close(ref_view);
2562 6458efa5 2020-11-24 stsp break;
2563 6458efa5 2020-11-24 stsp }
2564 6458efa5 2020-11-24 stsp *focus_view = ref_view;
2565 6458efa5 2020-11-24 stsp } else
2566 6458efa5 2020-11-24 stsp *new_view = ref_view;
2567 6458efa5 2020-11-24 stsp break;
2568 1e37a5c2 2019-05-12 jcs default:
2569 1e37a5c2 2019-05-12 jcs break;
2570 899d86c2 2018-05-10 stsp }
2571 e5a0f69f 2018-08-18 stsp
2572 80ddbec8 2018-04-29 stsp return err;
2573 80ddbec8 2018-04-29 stsp }
2574 80ddbec8 2018-04-29 stsp
2575 4ed7e80c 2018-05-20 stsp static const struct got_error *
2576 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2577 c2db6724 2019-01-04 stsp {
2578 c2db6724 2019-01-04 stsp const struct got_error *error;
2579 c2db6724 2019-01-04 stsp
2580 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2581 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2582 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2583 37c06ea4 2019-07-15 stsp #endif
2584 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2585 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2586 c2db6724 2019-01-04 stsp
2587 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2588 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2589 c2db6724 2019-01-04 stsp
2590 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2591 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2592 c2db6724 2019-01-04 stsp
2593 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2594 c2db6724 2019-01-04 stsp if (error != NULL)
2595 c2db6724 2019-01-04 stsp return error;
2596 c2db6724 2019-01-04 stsp
2597 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2598 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2599 c2db6724 2019-01-04 stsp
2600 c2db6724 2019-01-04 stsp return NULL;
2601 c2db6724 2019-01-04 stsp }
2602 c2db6724 2019-01-04 stsp
2603 a915003a 2019-02-05 stsp static void
2604 a915003a 2019-02-05 stsp init_curses(void)
2605 a915003a 2019-02-05 stsp {
2606 a915003a 2019-02-05 stsp initscr();
2607 a915003a 2019-02-05 stsp cbreak();
2608 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2609 a915003a 2019-02-05 stsp noecho();
2610 a915003a 2019-02-05 stsp nonl();
2611 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2612 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2613 a915003a 2019-02-05 stsp curs_set(0);
2614 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2615 6d17833f 2019-11-08 stsp start_color();
2616 6d17833f 2019-11-08 stsp use_default_colors();
2617 6d17833f 2019-11-08 stsp }
2618 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2619 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2620 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2621 a915003a 2019-02-05 stsp }
2622 a915003a 2019-02-05 stsp
2623 c2db6724 2019-01-04 stsp static const struct got_error *
2624 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2625 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2626 f135c941 2020-02-20 stsp {
2627 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2628 f135c941 2020-02-20 stsp
2629 f135c941 2020-02-20 stsp if (argc == 0) {
2630 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2631 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2632 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2633 f135c941 2020-02-20 stsp return NULL;
2634 f135c941 2020-02-20 stsp }
2635 f135c941 2020-02-20 stsp
2636 f135c941 2020-02-20 stsp if (worktree) {
2637 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2638 bfd61697 2020-11-14 stsp char *p;
2639 f135c941 2020-02-20 stsp
2640 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2641 f135c941 2020-02-20 stsp if (err)
2642 f135c941 2020-02-20 stsp return err;
2643 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2644 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2645 bfd61697 2020-11-14 stsp p) == -1) {
2646 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2647 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2648 f135c941 2020-02-20 stsp }
2649 f135c941 2020-02-20 stsp free(p);
2650 f135c941 2020-02-20 stsp } else
2651 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2652 f135c941 2020-02-20 stsp
2653 f135c941 2020-02-20 stsp return err;
2654 f135c941 2020-02-20 stsp }
2655 f135c941 2020-02-20 stsp
2656 f135c941 2020-02-20 stsp static const struct got_error *
2657 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2658 9f7d7167 2018-04-29 stsp {
2659 80ddbec8 2018-04-29 stsp const struct got_error *error;
2660 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2661 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2662 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2663 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2664 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2665 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2666 04cc582a 2018-08-01 stsp struct tog_view *view;
2667 a55555f2 2019-03-28 stsp
2668 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2669 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2670 c2db6724 2019-01-04 stsp NULL) == -1)
2671 80ddbec8 2018-04-29 stsp err(1, "pledge");
2672 80ddbec8 2018-04-29 stsp #endif
2673 80ddbec8 2018-04-29 stsp
2674 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2675 80ddbec8 2018-04-29 stsp switch (ch) {
2676 b672a97a 2020-01-27 stsp case 'b':
2677 b672a97a 2020-01-27 stsp log_branches = 1;
2678 b672a97a 2020-01-27 stsp break;
2679 80ddbec8 2018-04-29 stsp case 'c':
2680 80ddbec8 2018-04-29 stsp start_commit = optarg;
2681 80ddbec8 2018-04-29 stsp break;
2682 ecb28ae0 2018-07-16 stsp case 'r':
2683 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2684 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2685 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2686 9ba1d308 2019-10-21 stsp optarg);
2687 ecb28ae0 2018-07-16 stsp break;
2688 80ddbec8 2018-04-29 stsp default:
2689 17020d27 2019-03-07 stsp usage_log();
2690 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2691 80ddbec8 2018-04-29 stsp }
2692 80ddbec8 2018-04-29 stsp }
2693 80ddbec8 2018-04-29 stsp
2694 80ddbec8 2018-04-29 stsp argc -= optind;
2695 80ddbec8 2018-04-29 stsp argv += optind;
2696 80ddbec8 2018-04-29 stsp
2697 f135c941 2020-02-20 stsp if (argc > 1)
2698 f135c941 2020-02-20 stsp usage_log();
2699 f135c941 2020-02-20 stsp
2700 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2701 6962eb72 2020-02-20 stsp if (cwd == NULL)
2702 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2703 6962eb72 2020-02-20 stsp
2704 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2705 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2706 963f97a1 2019-03-18 stsp goto done;
2707 963f97a1 2019-03-18 stsp
2708 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2709 a1fbf39a 2019-08-11 stsp if (worktree)
2710 6962eb72 2020-02-20 stsp repo_path =
2711 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2712 a1fbf39a 2019-08-11 stsp else
2713 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2714 a1fbf39a 2019-08-11 stsp }
2715 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2716 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2717 963f97a1 2019-03-18 stsp goto done;
2718 ecb28ae0 2018-07-16 stsp }
2719 ecb28ae0 2018-07-16 stsp
2720 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2721 80ddbec8 2018-04-29 stsp if (error != NULL)
2722 ecb28ae0 2018-07-16 stsp goto done;
2723 80ddbec8 2018-04-29 stsp
2724 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2725 f135c941 2020-02-20 stsp repo, worktree);
2726 f135c941 2020-02-20 stsp if (error)
2727 f135c941 2020-02-20 stsp goto done;
2728 f135c941 2020-02-20 stsp
2729 f135c941 2020-02-20 stsp init_curses();
2730 f135c941 2020-02-20 stsp
2731 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2732 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2733 c02c541e 2019-03-29 stsp if (error)
2734 c02c541e 2019-03-29 stsp goto done;
2735 c02c541e 2019-03-29 stsp
2736 15a94983 2018-12-23 stsp if (start_commit == NULL)
2737 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2738 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2739 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2740 7a1d6b72 2020-01-15 stsp else
2741 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2742 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2743 80ddbec8 2018-04-29 stsp if (error != NULL)
2744 8b473291 2019-02-21 stsp goto done;
2745 8b473291 2019-02-21 stsp
2746 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2747 04cc582a 2018-08-01 stsp if (view == NULL) {
2748 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2749 04cc582a 2018-08-01 stsp goto done;
2750 04cc582a 2018-08-01 stsp }
2751 2fc00ff4 2019-08-31 stsp if (worktree) {
2752 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2753 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2754 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2755 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2756 2fc00ff4 2019-08-31 stsp goto done;
2757 2fc00ff4 2019-08-31 stsp }
2758 2fc00ff4 2019-08-31 stsp }
2759 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2760 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2761 ba4f502b 2018-08-04 stsp if (error)
2762 ba4f502b 2018-08-04 stsp goto done;
2763 2fc00ff4 2019-08-31 stsp if (worktree) {
2764 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2765 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2766 2fc00ff4 2019-08-31 stsp worktree = NULL;
2767 2fc00ff4 2019-08-31 stsp }
2768 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2769 ecb28ae0 2018-07-16 stsp done:
2770 f135c941 2020-02-20 stsp free(in_repo_path);
2771 ecb28ae0 2018-07-16 stsp free(repo_path);
2772 ecb28ae0 2018-07-16 stsp free(cwd);
2773 899d86c2 2018-05-10 stsp free(start_id);
2774 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2775 ecb28ae0 2018-07-16 stsp if (repo)
2776 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2777 ec142235 2019-03-07 stsp if (worktree)
2778 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2779 80ddbec8 2018-04-29 stsp return error;
2780 9f7d7167 2018-04-29 stsp }
2781 9f7d7167 2018-04-29 stsp
2782 4ed7e80c 2018-05-20 stsp __dead static void
2783 9f7d7167 2018-04-29 stsp usage_diff(void)
2784 9f7d7167 2018-04-29 stsp {
2785 80ddbec8 2018-04-29 stsp endwin();
2786 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2787 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2788 9f7d7167 2018-04-29 stsp exit(1);
2789 b304db33 2018-05-20 stsp }
2790 b304db33 2018-05-20 stsp
2791 b304db33 2018-05-20 stsp static char *
2792 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2793 b304db33 2018-05-20 stsp {
2794 b304db33 2018-05-20 stsp char *line;
2795 b304db33 2018-05-20 stsp size_t linelen;
2796 b304db33 2018-05-20 stsp size_t lineno;
2797 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2798 b304db33 2018-05-20 stsp
2799 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2800 b304db33 2018-05-20 stsp if (len)
2801 b304db33 2018-05-20 stsp *len = linelen;
2802 b304db33 2018-05-20 stsp return line;
2803 26ed57b2 2018-05-19 stsp }
2804 26ed57b2 2018-05-19 stsp
2805 6d17833f 2019-11-08 stsp static int
2806 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2807 41605754 2020-11-12 stsp regmatch_t *regmatch)
2808 6d17833f 2019-11-08 stsp {
2809 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2810 6d17833f 2019-11-08 stsp }
2811 6d17833f 2019-11-08 stsp
2812 f26dddb7 2019-11-08 stsp struct tog_color *
2813 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2814 6d17833f 2019-11-08 stsp {
2815 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2816 6d17833f 2019-11-08 stsp
2817 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2818 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2819 6d17833f 2019-11-08 stsp return tc;
2820 6d17833f 2019-11-08 stsp }
2821 6d17833f 2019-11-08 stsp
2822 6d17833f 2019-11-08 stsp return NULL;
2823 6d17833f 2019-11-08 stsp }
2824 6d17833f 2019-11-08 stsp
2825 4ed7e80c 2018-05-20 stsp static const struct got_error *
2826 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2827 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2828 41605754 2020-11-12 stsp {
2829 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2830 41605754 2020-11-12 stsp wchar_t *wline;
2831 41605754 2020-11-12 stsp int width;
2832 41605754 2020-11-12 stsp char *s;
2833 41605754 2020-11-12 stsp
2834 41605754 2020-11-12 stsp *wtotal = 0;
2835 41605754 2020-11-12 stsp
2836 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2837 41605754 2020-11-12 stsp if (s == NULL)
2838 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2839 41605754 2020-11-12 stsp
2840 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2841 41605754 2020-11-12 stsp if (err) {
2842 41605754 2020-11-12 stsp free(s);
2843 41605754 2020-11-12 stsp return err;
2844 41605754 2020-11-12 stsp }
2845 41605754 2020-11-12 stsp waddwstr(window, wline);
2846 41605754 2020-11-12 stsp free(wline);
2847 41605754 2020-11-12 stsp free(s);
2848 41605754 2020-11-12 stsp wlimit -= width;
2849 41605754 2020-11-12 stsp *wtotal += width;
2850 41605754 2020-11-12 stsp
2851 41605754 2020-11-12 stsp if (wlimit > 0) {
2852 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2853 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2854 41605754 2020-11-12 stsp if (s == NULL) {
2855 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2856 41605754 2020-11-12 stsp free(s);
2857 41605754 2020-11-12 stsp return err;
2858 41605754 2020-11-12 stsp }
2859 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2860 41605754 2020-11-12 stsp if (err) {
2861 41605754 2020-11-12 stsp free(s);
2862 41605754 2020-11-12 stsp return err;
2863 41605754 2020-11-12 stsp }
2864 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2865 41605754 2020-11-12 stsp waddwstr(window, wline);
2866 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2867 41605754 2020-11-12 stsp free(wline);
2868 41605754 2020-11-12 stsp free(s);
2869 41605754 2020-11-12 stsp wlimit -= width;
2870 41605754 2020-11-12 stsp *wtotal += width;
2871 41605754 2020-11-12 stsp }
2872 41605754 2020-11-12 stsp
2873 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2874 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2875 41605754 2020-11-12 stsp line + regmatch->rm_eo, wlimit, col_tab_align);
2876 41605754 2020-11-12 stsp if (err)
2877 41605754 2020-11-12 stsp return err;
2878 41605754 2020-11-12 stsp waddwstr(window, wline);
2879 41605754 2020-11-12 stsp free(wline);
2880 41605754 2020-11-12 stsp *wtotal += width;
2881 41605754 2020-11-12 stsp }
2882 41605754 2020-11-12 stsp
2883 41605754 2020-11-12 stsp return NULL;
2884 41605754 2020-11-12 stsp }
2885 41605754 2020-11-12 stsp
2886 41605754 2020-11-12 stsp static const struct got_error *
2887 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2888 26ed57b2 2018-05-19 stsp {
2889 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2890 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2891 61e69b96 2018-05-20 stsp const struct got_error *err;
2892 fe621944 2020-11-10 stsp int nprinted = 0;
2893 b304db33 2018-05-20 stsp char *line;
2894 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2895 b304db33 2018-05-20 stsp size_t len;
2896 61e69b96 2018-05-20 stsp wchar_t *wline;
2897 e0b650dd 2018-05-20 stsp int width;
2898 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2899 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2900 fe621944 2020-11-10 stsp off_t line_offset;
2901 26ed57b2 2018-05-19 stsp
2902 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2903 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2904 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2905 fe621944 2020-11-10 stsp
2906 f7d12f7e 2018-08-01 stsp werase(view->window);
2907 a3404814 2018-09-02 stsp
2908 a3404814 2018-09-02 stsp if (header) {
2909 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2910 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2911 135a2da0 2020-11-11 stsp header) == -1)
2912 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2913 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2914 135a2da0 2020-11-11 stsp free(line);
2915 135a2da0 2020-11-11 stsp if (err)
2916 a3404814 2018-09-02 stsp return err;
2917 a3404814 2018-09-02 stsp
2918 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2919 a3404814 2018-09-02 stsp wstandout(view->window);
2920 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2921 e54cc94a 2020-11-11 stsp free(wline);
2922 e54cc94a 2020-11-11 stsp wline = NULL;
2923 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2924 a3404814 2018-09-02 stsp wstandend(view->window);
2925 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2926 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2927 26ed57b2 2018-05-19 stsp
2928 a3404814 2018-09-02 stsp if (max_lines <= 1)
2929 a3404814 2018-09-02 stsp return NULL;
2930 a3404814 2018-09-02 stsp max_lines--;
2931 a3404814 2018-09-02 stsp }
2932 a3404814 2018-09-02 stsp
2933 89f1a395 2020-12-01 naddy s->eof = 0;
2934 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2935 89f1a395 2020-12-01 naddy line = parse_next_line(s->f, &len);
2936 26ed57b2 2018-05-19 stsp if (line == NULL) {
2937 89f1a395 2020-12-01 naddy s->eof = 1;
2938 26ed57b2 2018-05-19 stsp break;
2939 61e69b96 2018-05-20 stsp }
2940 6d17833f 2019-11-08 stsp
2941 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2942 f26dddb7 2019-11-08 stsp if (tc)
2943 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2944 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2945 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2946 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2948 41605754 2020-11-12 stsp view->window, regmatch);
2949 41605754 2020-11-12 stsp if (err) {
2950 41605754 2020-11-12 stsp free(line);
2951 41605754 2020-11-12 stsp return err;
2952 41605754 2020-11-12 stsp }
2953 41605754 2020-11-12 stsp } else {
2954 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2955 41605754 2020-11-12 stsp if (err) {
2956 41605754 2020-11-12 stsp free(line);
2957 41605754 2020-11-12 stsp return err;
2958 41605754 2020-11-12 stsp }
2959 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2960 41605754 2020-11-12 stsp free(wline);
2961 41605754 2020-11-12 stsp wline = NULL;
2962 41605754 2020-11-12 stsp }
2963 f26dddb7 2019-11-08 stsp if (tc)
2964 6d17833f 2019-11-08 stsp wattr_off(view->window,
2965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2966 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2967 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2968 fe621944 2020-11-10 stsp nprinted++;
2969 26ed57b2 2018-05-19 stsp free(line);
2970 26ed57b2 2018-05-19 stsp }
2971 fe621944 2020-11-10 stsp if (nprinted >= 1)
2972 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2973 89f1a395 2020-12-01 naddy (nprinted - 1);
2974 fe621944 2020-11-10 stsp else
2975 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2976 26ed57b2 2018-05-19 stsp
2977 1a57306a 2018-09-02 stsp view_vborder(view);
2978 c3e9aa98 2019-05-13 jcs
2979 89f1a395 2020-12-01 naddy if (s->eof) {
2980 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2981 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2982 c3e9aa98 2019-05-13 jcs nprinted++;
2983 c3e9aa98 2019-05-13 jcs }
2984 c3e9aa98 2019-05-13 jcs
2985 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2986 c3e9aa98 2019-05-13 jcs if (err) {
2987 c3e9aa98 2019-05-13 jcs return err;
2988 c3e9aa98 2019-05-13 jcs }
2989 26ed57b2 2018-05-19 stsp
2990 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2991 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2992 e54cc94a 2020-11-11 stsp free(wline);
2993 e54cc94a 2020-11-11 stsp wline = NULL;
2994 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2995 c3e9aa98 2019-05-13 jcs }
2996 c3e9aa98 2019-05-13 jcs
2997 26ed57b2 2018-05-19 stsp return NULL;
2998 abd2672a 2018-12-23 stsp }
2999 abd2672a 2018-12-23 stsp
3000 abd2672a 2018-12-23 stsp static char *
3001 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3002 abd2672a 2018-12-23 stsp {
3003 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3004 09867e48 2019-08-13 stsp char *p, *s;
3005 09867e48 2019-08-13 stsp
3006 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3007 09867e48 2019-08-13 stsp if (tm == NULL)
3008 09867e48 2019-08-13 stsp return NULL;
3009 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3010 09867e48 2019-08-13 stsp if (s == NULL)
3011 09867e48 2019-08-13 stsp return NULL;
3012 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3013 abd2672a 2018-12-23 stsp if (p)
3014 abd2672a 2018-12-23 stsp *p = '\0';
3015 abd2672a 2018-12-23 stsp return s;
3016 9f7d7167 2018-04-29 stsp }
3017 9f7d7167 2018-04-29 stsp
3018 4ed7e80c 2018-05-20 stsp static const struct got_error *
3019 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3020 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3021 0208f208 2020-05-05 stsp {
3022 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3023 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3026 0208f208 2020-05-05 stsp
3027 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3028 0208f208 2020-05-05 stsp if (qid != NULL) {
3029 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3030 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3031 0208f208 2020-05-05 stsp qid->id);
3032 0208f208 2020-05-05 stsp if (err)
3033 0208f208 2020-05-05 stsp return err;
3034 0208f208 2020-05-05 stsp
3035 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3037 0208f208 2020-05-05 stsp
3038 0208f208 2020-05-05 stsp }
3039 0208f208 2020-05-05 stsp
3040 0208f208 2020-05-05 stsp if (tree_id1) {
3041 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3042 0208f208 2020-05-05 stsp if (err)
3043 0208f208 2020-05-05 stsp goto done;
3044 0208f208 2020-05-05 stsp }
3045 0208f208 2020-05-05 stsp
3046 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3047 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3048 0208f208 2020-05-05 stsp if (err)
3049 0208f208 2020-05-05 stsp goto done;
3050 0208f208 2020-05-05 stsp
3051 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3052 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3053 0208f208 2020-05-05 stsp done:
3054 0208f208 2020-05-05 stsp if (tree1)
3055 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3056 0208f208 2020-05-05 stsp if (tree2)
3057 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3058 0208f208 2020-05-05 stsp return err;
3059 0208f208 2020-05-05 stsp }
3060 0208f208 2020-05-05 stsp
3061 0208f208 2020-05-05 stsp static const struct got_error *
3062 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3063 abd2672a 2018-12-23 stsp {
3064 fe621944 2020-11-10 stsp off_t *p;
3065 fe621944 2020-11-10 stsp
3066 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3067 fe621944 2020-11-10 stsp if (p == NULL)
3068 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3069 fe621944 2020-11-10 stsp *line_offsets = p;
3070 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3071 fe621944 2020-11-10 stsp (*nlines)++;
3072 fe621944 2020-11-10 stsp return NULL;
3073 fe621944 2020-11-10 stsp }
3074 fe621944 2020-11-10 stsp
3075 fe621944 2020-11-10 stsp static const struct got_error *
3076 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3077 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3079 fe621944 2020-11-10 stsp {
3080 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3081 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3082 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3083 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 45d799e2 2018-12-23 stsp time_t committer_time;
3085 45d799e2 2018-12-23 stsp const char *author, *committer;
3086 8b473291 2019-02-21 stsp char *refs_str = NULL;
3087 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3088 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3089 fe621944 2020-11-10 stsp off_t outoff = 0;
3090 fe621944 2020-11-10 stsp int n;
3091 abd2672a 2018-12-23 stsp
3092 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3093 0208f208 2020-05-05 stsp
3094 8b473291 2019-02-21 stsp if (refs) {
3095 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3096 8b473291 2019-02-21 stsp if (err)
3097 8b473291 2019-02-21 stsp return err;
3098 8b473291 2019-02-21 stsp }
3099 8b473291 2019-02-21 stsp
3100 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3101 abd2672a 2018-12-23 stsp if (err)
3102 abd2672a 2018-12-23 stsp return err;
3103 abd2672a 2018-12-23 stsp
3104 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3105 15a94983 2018-12-23 stsp if (err) {
3106 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3107 15a94983 2018-12-23 stsp goto done;
3108 15a94983 2018-12-23 stsp }
3109 abd2672a 2018-12-23 stsp
3110 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3111 fe621944 2020-11-10 stsp if (err)
3112 fe621944 2020-11-10 stsp goto done;
3113 fe621944 2020-11-10 stsp
3114 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3116 fe621944 2020-11-10 stsp if (n < 0) {
3117 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3118 abd2672a 2018-12-23 stsp goto done;
3119 abd2672a 2018-12-23 stsp }
3120 fe621944 2020-11-10 stsp outoff += n;
3121 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3122 fe621944 2020-11-10 stsp if (err)
3123 fe621944 2020-11-10 stsp goto done;
3124 fe621944 2020-11-10 stsp
3125 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3126 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3127 fe621944 2020-11-10 stsp if (n < 0) {
3128 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3129 abd2672a 2018-12-23 stsp goto done;
3130 abd2672a 2018-12-23 stsp }
3131 fe621944 2020-11-10 stsp outoff += n;
3132 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3133 fe621944 2020-11-10 stsp if (err)
3134 fe621944 2020-11-10 stsp goto done;
3135 fe621944 2020-11-10 stsp
3136 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3137 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3138 fe621944 2020-11-10 stsp if (datestr) {
3139 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3140 fe621944 2020-11-10 stsp if (n < 0) {
3141 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3142 fe621944 2020-11-10 stsp goto done;
3143 fe621944 2020-11-10 stsp }
3144 fe621944 2020-11-10 stsp outoff += n;
3145 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3146 fe621944 2020-11-10 stsp if (err)
3147 fe621944 2020-11-10 stsp goto done;
3148 abd2672a 2018-12-23 stsp }
3149 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3150 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3151 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3152 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3153 fe621944 2020-11-10 stsp if (n < 0) {
3154 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3155 fe621944 2020-11-10 stsp goto done;
3156 fe621944 2020-11-10 stsp }
3157 fe621944 2020-11-10 stsp outoff += n;
3158 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3159 fe621944 2020-11-10 stsp if (err)
3160 fe621944 2020-11-10 stsp goto done;
3161 abd2672a 2018-12-23 stsp }
3162 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3163 5943eee2 2019-08-13 stsp if (err)
3164 5943eee2 2019-08-13 stsp goto done;
3165 fe621944 2020-11-10 stsp s = logmsg;
3166 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3167 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3168 fe621944 2020-11-10 stsp if (n < 0) {
3169 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3170 fe621944 2020-11-10 stsp goto done;
3171 fe621944 2020-11-10 stsp }
3172 fe621944 2020-11-10 stsp outoff += n;
3173 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3174 fe621944 2020-11-10 stsp if (err)
3175 fe621944 2020-11-10 stsp goto done;
3176 abd2672a 2018-12-23 stsp }
3177 fe621944 2020-11-10 stsp
3178 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3179 0208f208 2020-05-05 stsp if (err)
3180 0208f208 2020-05-05 stsp goto done;
3181 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3183 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3184 fe621944 2020-11-10 stsp if (n < 0) {
3185 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3186 fe621944 2020-11-10 stsp goto done;
3187 fe621944 2020-11-10 stsp }
3188 fe621944 2020-11-10 stsp outoff += n;
3189 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3190 fe621944 2020-11-10 stsp if (err)
3191 fe621944 2020-11-10 stsp goto done;
3192 0208f208 2020-05-05 stsp free((char *)pe->path);
3193 0208f208 2020-05-05 stsp free(pe->data);
3194 0208f208 2020-05-05 stsp }
3195 fe621944 2020-11-10 stsp
3196 0208f208 2020-05-05 stsp fputc('\n', outfile);
3197 fe621944 2020-11-10 stsp outoff++;
3198 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3199 abd2672a 2018-12-23 stsp done:
3200 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3201 abd2672a 2018-12-23 stsp free(id_str);
3202 5943eee2 2019-08-13 stsp free(logmsg);
3203 8b473291 2019-02-21 stsp free(refs_str);
3204 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3205 7510f233 2020-08-09 stsp if (err) {
3206 ae6a6978 2020-08-09 stsp free(*line_offsets);
3207 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3208 ae6a6978 2020-08-09 stsp *nlines = 0;
3209 7510f233 2020-08-09 stsp }
3210 fe621944 2020-11-10 stsp return err;
3211 abd2672a 2018-12-23 stsp }
3212 abd2672a 2018-12-23 stsp
3213 abd2672a 2018-12-23 stsp static const struct got_error *
3214 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3215 26ed57b2 2018-05-19 stsp {
3216 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3217 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3218 15a94983 2018-12-23 stsp int obj_type;
3219 fe621944 2020-11-10 stsp
3220 fe621944 2020-11-10 stsp free(s->line_offsets);
3221 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3222 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3223 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3224 fe621944 2020-11-10 stsp s->nlines = 0;
3225 26ed57b2 2018-05-19 stsp
3226 511a516b 2018-05-19 stsp f = got_opentemp();
3227 48ae06ee 2018-10-18 stsp if (f == NULL) {
3228 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3229 48ae06ee 2018-10-18 stsp goto done;
3230 48ae06ee 2018-10-18 stsp }
3231 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3232 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3233 fb43ecf1 2019-02-11 stsp goto done;
3234 fb43ecf1 2019-02-11 stsp }
3235 48ae06ee 2018-10-18 stsp s->f = f;
3236 26ed57b2 2018-05-19 stsp
3237 15a94983 2018-12-23 stsp if (s->id1)
3238 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3239 15a94983 2018-12-23 stsp else
3240 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3241 15a94983 2018-12-23 stsp if (err)
3242 15a94983 2018-12-23 stsp goto done;
3243 15a94983 2018-12-23 stsp
3244 15a94983 2018-12-23 stsp switch (obj_type) {
3245 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3246 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3249 26ed57b2 2018-05-19 stsp break;
3250 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3251 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3253 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3254 26ed57b2 2018-05-19 stsp break;
3255 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3256 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3257 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3258 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3259 abd2672a 2018-12-23 stsp
3260 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3261 abd2672a 2018-12-23 stsp if (err)
3262 3ffacbe1 2020-02-02 tracey goto done;
3263 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3264 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3265 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3266 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3267 f44b1f58 2020-02-02 tracey if (err)
3268 f44b1f58 2020-02-02 tracey goto done;
3269 f44b1f58 2020-02-02 tracey } else {
3270 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3271 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 fe621944 2020-11-10 stsp err = write_commit_info(
3274 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3275 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3276 f44b1f58 2020-02-02 tracey if (err)
3277 f44b1f58 2020-02-02 tracey goto done;
3278 f5404e4e 2020-02-02 tracey break;
3279 15a087fe 2019-02-21 stsp }
3280 abd2672a 2018-12-23 stsp }
3281 abd2672a 2018-12-23 stsp }
3282 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3283 abd2672a 2018-12-23 stsp
3284 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3287 26ed57b2 2018-05-19 stsp break;
3288 abd2672a 2018-12-23 stsp }
3289 26ed57b2 2018-05-19 stsp default:
3290 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3291 48ae06ee 2018-10-18 stsp break;
3292 26ed57b2 2018-05-19 stsp }
3293 f44b1f58 2020-02-02 tracey if (err)
3294 f44b1f58 2020-02-02 tracey goto done;
3295 48ae06ee 2018-10-18 stsp done:
3296 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3297 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3298 48ae06ee 2018-10-18 stsp return err;
3299 48ae06ee 2018-10-18 stsp }
3300 26ed57b2 2018-05-19 stsp
3301 f5215bb9 2019-02-22 stsp static void
3302 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3303 f5215bb9 2019-02-22 stsp {
3304 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3305 f5215bb9 2019-02-22 stsp update_panels();
3306 f5215bb9 2019-02-22 stsp doupdate();
3307 f44b1f58 2020-02-02 tracey }
3308 f44b1f58 2020-02-02 tracey
3309 f44b1f58 2020-02-02 tracey static const struct got_error *
3310 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3311 f44b1f58 2020-02-02 tracey {
3312 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3313 f44b1f58 2020-02-02 tracey
3314 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3315 f44b1f58 2020-02-02 tracey return NULL;
3316 f44b1f58 2020-02-02 tracey }
3317 f44b1f58 2020-02-02 tracey
3318 f44b1f58 2020-02-02 tracey static const struct got_error *
3319 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3320 f44b1f58 2020-02-02 tracey {
3321 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3322 f44b1f58 2020-02-02 tracey int lineno;
3323 f44b1f58 2020-02-02 tracey
3324 f44b1f58 2020-02-02 tracey if (!view->searching) {
3325 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3326 f44b1f58 2020-02-02 tracey return NULL;
3327 f44b1f58 2020-02-02 tracey }
3328 f44b1f58 2020-02-02 tracey
3329 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3330 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3331 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3332 f44b1f58 2020-02-02 tracey else
3333 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3334 f44b1f58 2020-02-02 tracey } else {
3335 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3336 f44b1f58 2020-02-02 tracey lineno = 1;
3337 f44b1f58 2020-02-02 tracey else
3338 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3339 f44b1f58 2020-02-02 tracey }
3340 f44b1f58 2020-02-02 tracey
3341 f44b1f58 2020-02-02 tracey while (1) {
3342 f44b1f58 2020-02-02 tracey char *line = NULL;
3343 f44b1f58 2020-02-02 tracey off_t offset;
3344 f44b1f58 2020-02-02 tracey size_t len;
3345 f44b1f58 2020-02-02 tracey
3346 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3347 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3348 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3349 f44b1f58 2020-02-02 tracey free(line);
3350 f44b1f58 2020-02-02 tracey break;
3351 f44b1f58 2020-02-02 tracey }
3352 f44b1f58 2020-02-02 tracey
3353 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3354 f44b1f58 2020-02-02 tracey lineno = 1;
3355 f44b1f58 2020-02-02 tracey else
3356 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3357 f44b1f58 2020-02-02 tracey }
3358 f44b1f58 2020-02-02 tracey
3359 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3360 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3361 f44b1f58 2020-02-02 tracey free(line);
3362 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3363 f44b1f58 2020-02-02 tracey }
3364 f44b1f58 2020-02-02 tracey free(line);
3365 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3366 41605754 2020-11-12 stsp if (line &&
3367 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3368 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3369 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3370 f44b1f58 2020-02-02 tracey free(line);
3371 f44b1f58 2020-02-02 tracey break;
3372 f44b1f58 2020-02-02 tracey }
3373 f44b1f58 2020-02-02 tracey free(line);
3374 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3375 f44b1f58 2020-02-02 tracey lineno++;
3376 f44b1f58 2020-02-02 tracey else
3377 f44b1f58 2020-02-02 tracey lineno--;
3378 f44b1f58 2020-02-02 tracey }
3379 f44b1f58 2020-02-02 tracey
3380 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3381 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3382 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3383 f44b1f58 2020-02-02 tracey }
3384 f44b1f58 2020-02-02 tracey
3385 f44b1f58 2020-02-02 tracey return NULL;
3386 f5215bb9 2019-02-22 stsp }
3387 f5215bb9 2019-02-22 stsp
3388 48ae06ee 2018-10-18 stsp static const struct got_error *
3389 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3390 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3391 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3392 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3393 48ae06ee 2018-10-18 stsp {
3394 48ae06ee 2018-10-18 stsp const struct got_error *err;
3395 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3396 5dc9f4bc 2018-08-04 stsp
3397 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3398 78756c87 2020-11-24 stsp
3399 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3400 15a94983 2018-12-23 stsp int type1, type2;
3401 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3402 15a94983 2018-12-23 stsp if (err)
3403 15a94983 2018-12-23 stsp return err;
3404 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3405 15a94983 2018-12-23 stsp if (err)
3406 15a94983 2018-12-23 stsp return err;
3407 15a94983 2018-12-23 stsp
3408 15a94983 2018-12-23 stsp if (type1 != type2)
3409 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3410 15a94983 2018-12-23 stsp }
3411 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3412 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3413 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3414 f44b1f58 2020-02-02 tracey s->repo = repo;
3415 f44b1f58 2020-02-02 tracey s->id1 = id1;
3416 f44b1f58 2020-02-02 tracey s->id2 = id2;
3417 3dbaef42 2020-11-24 stsp s->label1 = label1;
3418 3dbaef42 2020-11-24 stsp s->label2 = label2;
3419 48ae06ee 2018-10-18 stsp
3420 15a94983 2018-12-23 stsp if (id1) {
3421 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3422 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3423 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3424 48ae06ee 2018-10-18 stsp } else
3425 5465d566 2020-02-01 tracey s->id1 = NULL;
3426 48ae06ee 2018-10-18 stsp
3427 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3428 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3429 5465d566 2020-02-01 tracey free(s->id1);
3430 5465d566 2020-02-01 tracey s->id1 = NULL;
3431 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3432 48ae06ee 2018-10-18 stsp }
3433 5465d566 2020-02-01 tracey s->f = NULL;
3434 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3435 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3436 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3437 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3438 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3439 5465d566 2020-02-01 tracey s->log_view = log_view;
3440 5465d566 2020-02-01 tracey s->repo = repo;
3441 6d17833f 2019-11-08 stsp
3442 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3443 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3444 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3445 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3446 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3447 6d17833f 2019-11-08 stsp if (err)
3448 6d17833f 2019-11-08 stsp return err;
3449 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3450 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3451 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3452 6d17833f 2019-11-08 stsp if (err) {
3453 5465d566 2020-02-01 tracey free_colors(&s->colors);
3454 6d17833f 2019-11-08 stsp return err;
3455 6d17833f 2019-11-08 stsp }
3456 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3457 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3458 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3459 6d17833f 2019-11-08 stsp if (err) {
3460 5465d566 2020-02-01 tracey free_colors(&s->colors);
3461 6d17833f 2019-11-08 stsp return err;
3462 6d17833f 2019-11-08 stsp }
3463 6d17833f 2019-11-08 stsp
3464 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3465 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3466 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3467 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3468 6d17833f 2019-11-08 stsp if (err) {
3469 5465d566 2020-02-01 tracey free_colors(&s->colors);
3470 6d17833f 2019-11-08 stsp return err;
3471 6d17833f 2019-11-08 stsp }
3472 11b20872 2019-11-08 stsp
3473 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3474 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3475 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3476 11b20872 2019-11-08 stsp if (err) {
3477 5465d566 2020-02-01 tracey free_colors(&s->colors);
3478 11b20872 2019-11-08 stsp return err;
3479 11b20872 2019-11-08 stsp }
3480 11b20872 2019-11-08 stsp
3481 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3482 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3483 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3484 11b20872 2019-11-08 stsp if (err) {
3485 5465d566 2020-02-01 tracey free_colors(&s->colors);
3486 11b20872 2019-11-08 stsp return err;
3487 11b20872 2019-11-08 stsp }
3488 6d17833f 2019-11-08 stsp }
3489 5dc9f4bc 2018-08-04 stsp
3490 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3491 78756c87 2020-11-24 stsp if (err) {
3492 78756c87 2020-11-24 stsp free(s->id1);
3493 78756c87 2020-11-24 stsp s->id1 = NULL;
3494 78756c87 2020-11-24 stsp free(s->id2);
3495 78756c87 2020-11-24 stsp s->id2 = NULL;
3496 78756c87 2020-11-24 stsp free_colors(&s->colors);
3497 78756c87 2020-11-24 stsp return err;
3498 78756c87 2020-11-24 stsp }
3499 78756c87 2020-11-24 stsp
3500 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3501 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3502 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3503 f5215bb9 2019-02-22 stsp
3504 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3505 fe621944 2020-11-10 stsp s->nlines = 0;
3506 5465d566 2020-02-01 tracey err = create_diff(s);
3507 48ae06ee 2018-10-18 stsp if (err) {
3508 5465d566 2020-02-01 tracey free(s->id1);
3509 5465d566 2020-02-01 tracey s->id1 = NULL;
3510 5465d566 2020-02-01 tracey free(s->id2);
3511 5465d566 2020-02-01 tracey s->id2 = NULL;
3512 78756c87 2020-11-24 stsp free_colors(&s->colors);
3513 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3514 48ae06ee 2018-10-18 stsp return err;
3515 48ae06ee 2018-10-18 stsp }
3516 48ae06ee 2018-10-18 stsp
3517 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3518 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3519 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3520 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3521 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3522 e5a0f69f 2018-08-18 stsp
3523 5dc9f4bc 2018-08-04 stsp return NULL;
3524 5dc9f4bc 2018-08-04 stsp }
3525 5dc9f4bc 2018-08-04 stsp
3526 e5a0f69f 2018-08-18 stsp static const struct got_error *
3527 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3528 5dc9f4bc 2018-08-04 stsp {
3529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3530 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3531 5465d566 2020-02-01 tracey
3532 5465d566 2020-02-01 tracey free(s->id1);
3533 5465d566 2020-02-01 tracey s->id1 = NULL;
3534 5465d566 2020-02-01 tracey free(s->id2);
3535 5465d566 2020-02-01 tracey s->id2 = NULL;
3536 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3537 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3538 5465d566 2020-02-01 tracey free_colors(&s->colors);
3539 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3540 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3541 fe621944 2020-11-10 stsp s->nlines = 0;
3542 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3543 e5a0f69f 2018-08-18 stsp return err;
3544 5dc9f4bc 2018-08-04 stsp }
3545 5dc9f4bc 2018-08-04 stsp
3546 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3547 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3548 5dc9f4bc 2018-08-04 stsp {
3549 a3404814 2018-09-02 stsp const struct got_error *err;
3550 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3551 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3552 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3553 a3404814 2018-09-02 stsp
3554 a3404814 2018-09-02 stsp if (s->id1) {
3555 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3556 a3404814 2018-09-02 stsp if (err)
3557 a3404814 2018-09-02 stsp return err;
3558 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3559 3dbaef42 2020-11-24 stsp } else
3560 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3561 3dbaef42 2020-11-24 stsp
3562 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3563 a3404814 2018-09-02 stsp if (err)
3564 a3404814 2018-09-02 stsp return err;
3565 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3566 26ed57b2 2018-05-19 stsp
3567 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3568 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3569 a3404814 2018-09-02 stsp free(id_str1);
3570 a3404814 2018-09-02 stsp free(id_str2);
3571 a3404814 2018-09-02 stsp return err;
3572 a3404814 2018-09-02 stsp }
3573 a3404814 2018-09-02 stsp free(id_str1);
3574 a3404814 2018-09-02 stsp free(id_str2);
3575 a3404814 2018-09-02 stsp
3576 89f1a395 2020-12-01 naddy return draw_file(view, header);
3577 15a087fe 2019-02-21 stsp }
3578 15a087fe 2019-02-21 stsp
3579 15a087fe 2019-02-21 stsp static const struct got_error *
3580 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3581 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3582 15a087fe 2019-02-21 stsp {
3583 d7a04538 2019-02-21 stsp const struct got_error *err;
3584 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3585 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3586 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3587 15a087fe 2019-02-21 stsp
3588 15a087fe 2019-02-21 stsp free(s->id2);
3589 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3590 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3591 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3592 15a087fe 2019-02-21 stsp
3593 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3594 d7a04538 2019-02-21 stsp if (err)
3595 d7a04538 2019-02-21 stsp return err;
3596 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3597 15a087fe 2019-02-21 stsp free(s->id1);
3598 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3599 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3600 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3601 15a087fe 2019-02-21 stsp return NULL;
3602 0cf4efb1 2018-09-29 stsp }
3603 0cf4efb1 2018-09-29 stsp
3604 0cf4efb1 2018-09-29 stsp static const struct got_error *
3605 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3606 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3607 e5a0f69f 2018-08-18 stsp {
3608 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3609 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3610 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3611 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3612 e5a0f69f 2018-08-18 stsp int i;
3613 e5a0f69f 2018-08-18 stsp
3614 e5a0f69f 2018-08-18 stsp switch (ch) {
3615 64453f7e 2020-11-21 stsp case 'a':
3616 3dbaef42 2020-11-24 stsp case 'w':
3617 3dbaef42 2020-11-24 stsp if (ch == 'a')
3618 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3619 3dbaef42 2020-11-24 stsp if (ch == 'w')
3620 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3621 64453f7e 2020-11-21 stsp wclear(view->window);
3622 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3623 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3624 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3625 64453f7e 2020-11-21 stsp err = create_diff(s);
3626 64453f7e 2020-11-21 stsp break;
3627 1e37a5c2 2019-05-12 jcs case 'k':
3628 1e37a5c2 2019-05-12 jcs case KEY_UP:
3629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3630 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3631 1e37a5c2 2019-05-12 jcs break;
3632 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3633 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3634 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3635 26ed57b2 2018-05-19 stsp break;
3636 1e37a5c2 2019-05-12 jcs i = 0;
3637 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3638 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3639 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3640 1e37a5c2 2019-05-12 jcs break;
3641 1e37a5c2 2019-05-12 jcs case 'j':
3642 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3643 1e37a5c2 2019-05-12 jcs if (!s->eof)
3644 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3645 1e37a5c2 2019-05-12 jcs break;
3646 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3647 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3648 1e37a5c2 2019-05-12 jcs case ' ':
3649 00ba99a7 2019-05-12 jcs if (s->eof)
3650 1e37a5c2 2019-05-12 jcs break;
3651 1e37a5c2 2019-05-12 jcs i = 0;
3652 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3653 1e37a5c2 2019-05-12 jcs char *line;
3654 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3655 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3656 1e37a5c2 2019-05-12 jcs if (line == NULL)
3657 34bc9ec9 2019-02-22 stsp break;
3658 1e37a5c2 2019-05-12 jcs }
3659 1e37a5c2 2019-05-12 jcs break;
3660 1e37a5c2 2019-05-12 jcs case '[':
3661 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3662 1e37a5c2 2019-05-12 jcs s->diff_context--;
3663 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3664 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3665 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3666 27829c9e 2020-11-21 stsp s->nlines) {
3667 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3668 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3669 27829c9e 2020-11-21 stsp }
3670 1e37a5c2 2019-05-12 jcs }
3671 1e37a5c2 2019-05-12 jcs break;
3672 1e37a5c2 2019-05-12 jcs case ']':
3673 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3674 1e37a5c2 2019-05-12 jcs s->diff_context++;
3675 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3676 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3677 1e37a5c2 2019-05-12 jcs }
3678 1e37a5c2 2019-05-12 jcs break;
3679 1e37a5c2 2019-05-12 jcs case '<':
3680 1e37a5c2 2019-05-12 jcs case ',':
3681 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3682 48ae06ee 2018-10-18 stsp break;
3683 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3684 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3685 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3686 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3687 48ae06ee 2018-10-18 stsp break;
3688 6524637e 2019-02-21 stsp
3689 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3690 1e37a5c2 2019-05-12 jcs KEY_UP);
3691 1e37a5c2 2019-05-12 jcs if (err)
3692 1e37a5c2 2019-05-12 jcs break;
3693 15a087fe 2019-02-21 stsp
3694 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3695 1e37a5c2 2019-05-12 jcs if (err)
3696 1e37a5c2 2019-05-12 jcs break;
3697 15a087fe 2019-02-21 stsp
3698 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3699 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3700 15a087fe 2019-02-21 stsp
3701 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3702 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3703 1e37a5c2 2019-05-12 jcs break;
3704 1e37a5c2 2019-05-12 jcs case '>':
3705 1e37a5c2 2019-05-12 jcs case '.':
3706 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3707 15a087fe 2019-02-21 stsp break;
3708 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3709 5e224a3e 2019-02-22 stsp
3710 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3711 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3712 ffe38506 2020-12-01 naddy err = trigger_log_thread(s->log_view, 1);
3713 fb872ab2 2019-02-21 stsp if (err)
3714 fb872ab2 2019-02-21 stsp break;
3715 1e37a5c2 2019-05-12 jcs }
3716 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3717 1e37a5c2 2019-05-12 jcs KEY_DOWN);
3718 1e37a5c2 2019-05-12 jcs if (err)
3719 1e37a5c2 2019-05-12 jcs break;
3720 15a087fe 2019-02-21 stsp
3721 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3722 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3723 1e37a5c2 2019-05-12 jcs break;
3724 15a087fe 2019-02-21 stsp
3725 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3726 1e37a5c2 2019-05-12 jcs if (err)
3727 1e37a5c2 2019-05-12 jcs break;
3728 15a087fe 2019-02-21 stsp
3729 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3730 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3731 1e37a5c2 2019-05-12 jcs
3732 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3733 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3734 1e37a5c2 2019-05-12 jcs break;
3735 1e37a5c2 2019-05-12 jcs default:
3736 1e37a5c2 2019-05-12 jcs break;
3737 26ed57b2 2018-05-19 stsp }
3738 e5a0f69f 2018-08-18 stsp
3739 bcbd79e2 2018-08-19 stsp return err;
3740 26ed57b2 2018-05-19 stsp }
3741 26ed57b2 2018-05-19 stsp
3742 4ed7e80c 2018-05-20 stsp static const struct got_error *
3743 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3744 9f7d7167 2018-04-29 stsp {
3745 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3746 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3747 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3748 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3749 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3750 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3751 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3752 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3753 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3754 3dbaef42 2020-11-24 stsp const char *errstr;
3755 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3756 70ac5f84 2019-03-28 stsp
3757 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3758 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3759 eb6600df 2019-01-04 stsp NULL) == -1)
3760 26ed57b2 2018-05-19 stsp err(1, "pledge");
3761 26ed57b2 2018-05-19 stsp #endif
3762 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3763 26ed57b2 2018-05-19 stsp switch (ch) {
3764 64453f7e 2020-11-21 stsp case 'a':
3765 64453f7e 2020-11-21 stsp force_text_diff = 1;
3766 3dbaef42 2020-11-24 stsp break;
3767 3dbaef42 2020-11-24 stsp case 'C':
3768 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3769 3dbaef42 2020-11-24 stsp &errstr);
3770 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3771 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3772 64453f7e 2020-11-21 stsp break;
3773 09b5bff8 2020-02-23 naddy case 'r':
3774 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3775 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3776 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3777 09b5bff8 2020-02-23 naddy optarg);
3778 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3779 09b5bff8 2020-02-23 naddy break;
3780 3dbaef42 2020-11-24 stsp case 'w':
3781 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3782 3dbaef42 2020-11-24 stsp break;
3783 26ed57b2 2018-05-19 stsp default:
3784 17020d27 2019-03-07 stsp usage_diff();
3785 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3786 26ed57b2 2018-05-19 stsp }
3787 26ed57b2 2018-05-19 stsp }
3788 26ed57b2 2018-05-19 stsp
3789 26ed57b2 2018-05-19 stsp argc -= optind;
3790 26ed57b2 2018-05-19 stsp argv += optind;
3791 26ed57b2 2018-05-19 stsp
3792 26ed57b2 2018-05-19 stsp if (argc == 0) {
3793 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3794 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3795 15a94983 2018-12-23 stsp id_str1 = argv[0];
3796 15a94983 2018-12-23 stsp id_str2 = argv[1];
3797 26ed57b2 2018-05-19 stsp } else
3798 26ed57b2 2018-05-19 stsp usage_diff();
3799 a915003a 2019-02-05 stsp
3800 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3801 a273ac94 2020-02-23 naddy if (cwd == NULL)
3802 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3803 eb6600df 2019-01-04 stsp
3804 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3805 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3806 a273ac94 2020-02-23 naddy goto done;
3807 a273ac94 2020-02-23 naddy
3808 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3809 a273ac94 2020-02-23 naddy if (worktree)
3810 a273ac94 2020-02-23 naddy repo_path =
3811 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3812 a273ac94 2020-02-23 naddy else
3813 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3814 a273ac94 2020-02-23 naddy }
3815 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3816 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3817 a273ac94 2020-02-23 naddy goto done;
3818 a273ac94 2020-02-23 naddy }
3819 a273ac94 2020-02-23 naddy
3820 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3821 eb6600df 2019-01-04 stsp if (error)
3822 eb6600df 2019-01-04 stsp goto done;
3823 26ed57b2 2018-05-19 stsp
3824 a273ac94 2020-02-23 naddy init_curses();
3825 a273ac94 2020-02-23 naddy
3826 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3827 26ed57b2 2018-05-19 stsp if (error)
3828 26ed57b2 2018-05-19 stsp goto done;
3829 26ed57b2 2018-05-19 stsp
3830 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3831 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3832 26ed57b2 2018-05-19 stsp if (error)
3833 26ed57b2 2018-05-19 stsp goto done;
3834 26ed57b2 2018-05-19 stsp
3835 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3836 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3837 26ed57b2 2018-05-19 stsp if (error)
3838 26ed57b2 2018-05-19 stsp goto done;
3839 26ed57b2 2018-05-19 stsp
3840 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3841 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3842 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3843 ea5e7bb5 2018-08-01 stsp goto done;
3844 ea5e7bb5 2018-08-01 stsp }
3845 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3846 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3847 5dc9f4bc 2018-08-04 stsp if (error)
3848 5dc9f4bc 2018-08-04 stsp goto done;
3849 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3850 26ed57b2 2018-05-19 stsp done:
3851 3dbaef42 2020-11-24 stsp free(label1);
3852 3dbaef42 2020-11-24 stsp free(label2);
3853 c02c541e 2019-03-29 stsp free(repo_path);
3854 a273ac94 2020-02-23 naddy free(cwd);
3855 921be706 2019-06-28 stsp if (repo)
3856 921be706 2019-06-28 stsp got_repo_close(repo);
3857 a273ac94 2020-02-23 naddy if (worktree)
3858 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3859 26ed57b2 2018-05-19 stsp return error;
3860 9f7d7167 2018-04-29 stsp }
3861 9f7d7167 2018-04-29 stsp
3862 4ed7e80c 2018-05-20 stsp __dead static void
3863 9f7d7167 2018-04-29 stsp usage_blame(void)
3864 9f7d7167 2018-04-29 stsp {
3865 80ddbec8 2018-04-29 stsp endwin();
3866 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3867 9f7d7167 2018-04-29 stsp getprogname());
3868 9f7d7167 2018-04-29 stsp exit(1);
3869 9f7d7167 2018-04-29 stsp }
3870 84451b3e 2018-07-10 stsp
3871 84451b3e 2018-07-10 stsp struct tog_blame_line {
3872 84451b3e 2018-07-10 stsp int annotated;
3873 84451b3e 2018-07-10 stsp struct got_object_id *id;
3874 84451b3e 2018-07-10 stsp };
3875 9f7d7167 2018-04-29 stsp
3876 4ed7e80c 2018-05-20 stsp static const struct got_error *
3877 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3878 84451b3e 2018-07-10 stsp {
3879 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3880 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3881 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3882 84451b3e 2018-07-10 stsp const struct got_error *err;
3883 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3884 84451b3e 2018-07-10 stsp char *line;
3885 84451b3e 2018-07-10 stsp size_t len;
3886 84451b3e 2018-07-10 stsp wchar_t *wline;
3887 27a741e5 2019-09-11 stsp int width;
3888 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3889 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3890 ab089a2a 2018-07-12 stsp char *id_str;
3891 11b20872 2019-11-08 stsp struct tog_color *tc;
3892 ab089a2a 2018-07-12 stsp
3893 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3894 ab089a2a 2018-07-12 stsp if (err)
3895 ab089a2a 2018-07-12 stsp return err;
3896 84451b3e 2018-07-10 stsp
3897 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3898 f7d12f7e 2018-08-01 stsp werase(view->window);
3899 84451b3e 2018-07-10 stsp
3900 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3902 ab089a2a 2018-07-12 stsp free(id_str);
3903 ab089a2a 2018-07-12 stsp return err;
3904 ab089a2a 2018-07-12 stsp }
3905 ab089a2a 2018-07-12 stsp
3906 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3907 ab089a2a 2018-07-12 stsp free(line);
3908 2550e4c3 2018-07-13 stsp line = NULL;
3909 1cae65b4 2019-09-22 stsp if (err)
3910 1cae65b4 2019-09-22 stsp return err;
3911 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3912 a3404814 2018-09-02 stsp wstandout(view->window);
3913 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3914 11b20872 2019-11-08 stsp if (tc)
3915 11b20872 2019-11-08 stsp wattr_on(view->window,
3916 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3917 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3918 11b20872 2019-11-08 stsp if (tc)
3919 11b20872 2019-11-08 stsp wattr_off(view->window,
3920 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3921 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3922 a3404814 2018-09-02 stsp wstandend(view->window);
3923 2550e4c3 2018-07-13 stsp free(wline);
3924 2550e4c3 2018-07-13 stsp wline = NULL;
3925 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3926 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3927 ab089a2a 2018-07-12 stsp
3928 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3929 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3930 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3931 ab089a2a 2018-07-12 stsp free(id_str);
3932 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3933 ab089a2a 2018-07-12 stsp }
3934 ab089a2a 2018-07-12 stsp free(id_str);
3935 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3936 3f60a8ef 2018-07-10 stsp free(line);
3937 2550e4c3 2018-07-13 stsp line = NULL;
3938 3f60a8ef 2018-07-10 stsp if (err)
3939 3f60a8ef 2018-07-10 stsp return err;
3940 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3941 2550e4c3 2018-07-13 stsp free(wline);
3942 2550e4c3 2018-07-13 stsp wline = NULL;
3943 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3944 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3945 3f60a8ef 2018-07-10 stsp
3946 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3947 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3948 4f7c3e5e 2020-12-01 naddy line = parse_next_line(blame->f, &len);
3949 84451b3e 2018-07-10 stsp if (line == NULL) {
3950 4f7c3e5e 2020-12-01 naddy s->eof = 1;
3951 84451b3e 2018-07-10 stsp break;
3952 84451b3e 2018-07-10 stsp }
3953 4f7c3e5e 2020-12-01 naddy if (++lineno < s->first_displayed_line) {
3954 84451b3e 2018-07-10 stsp free(line);
3955 84451b3e 2018-07-10 stsp continue;
3956 84451b3e 2018-07-10 stsp }
3957 84451b3e 2018-07-10 stsp
3958 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3959 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3960 b700b5d6 2018-07-10 stsp
3961 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3962 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3963 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3964 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3965 27a741e5 2019-09-11 stsp !(view->focussed &&
3966 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3967 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3968 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3969 8d0fe45a 2019-08-12 stsp char *id_str;
3970 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3971 8d0fe45a 2019-08-12 stsp if (err) {
3972 8d0fe45a 2019-08-12 stsp free(line);
3973 8d0fe45a 2019-08-12 stsp return err;
3974 8d0fe45a 2019-08-12 stsp }
3975 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3976 11b20872 2019-11-08 stsp if (tc)
3977 11b20872 2019-11-08 stsp wattr_on(view->window,
3978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3979 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3980 11b20872 2019-11-08 stsp if (tc)
3981 11b20872 2019-11-08 stsp wattr_off(view->window,
3982 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3983 8d0fe45a 2019-08-12 stsp free(id_str);
3984 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3985 8d0fe45a 2019-08-12 stsp } else {
3986 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3987 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3988 84451b3e 2018-07-10 stsp }
3989 ee41ec32 2018-07-10 stsp } else {
3990 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3991 ee41ec32 2018-07-10 stsp prev_id = NULL;
3992 ee41ec32 2018-07-10 stsp }
3993 84451b3e 2018-07-10 stsp
3994 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3995 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3996 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3997 27a741e5 2019-09-11 stsp
3998 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3999 41605754 2020-11-12 stsp width = 9;
4000 41605754 2020-11-12 stsp wline = wcsdup(L"");
4001 41605754 2020-11-12 stsp if (wline == NULL) {
4002 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4003 41605754 2020-11-12 stsp free(line);
4004 41605754 2020-11-12 stsp return err;
4005 41605754 2020-11-12 stsp }
4006 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4007 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4008 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4009 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4010 41605754 2020-11-12 stsp view->window, regmatch);
4011 41605754 2020-11-12 stsp if (err) {
4012 41605754 2020-11-12 stsp free(line);
4013 41605754 2020-11-12 stsp return err;
4014 41605754 2020-11-12 stsp }
4015 41605754 2020-11-12 stsp width += 9;
4016 41605754 2020-11-12 stsp } else {
4017 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4018 41605754 2020-11-12 stsp view->ncols - 9, 9);
4019 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4020 41605754 2020-11-12 stsp free(wline);
4021 41605754 2020-11-12 stsp wline = NULL;
4022 41605754 2020-11-12 stsp width += 9;
4023 41605754 2020-11-12 stsp }
4024 41605754 2020-11-12 stsp
4025 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4026 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4027 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4028 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4029 84451b3e 2018-07-10 stsp free(line);
4030 84451b3e 2018-07-10 stsp }
4031 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4032 84451b3e 2018-07-10 stsp
4033 1a57306a 2018-09-02 stsp view_vborder(view);
4034 84451b3e 2018-07-10 stsp
4035 84451b3e 2018-07-10 stsp return NULL;
4036 84451b3e 2018-07-10 stsp }
4037 84451b3e 2018-07-10 stsp
4038 84451b3e 2018-07-10 stsp static const struct got_error *
4039 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4040 84451b3e 2018-07-10 stsp {
4041 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4042 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4043 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4044 1a76625f 2018-10-22 stsp int errcode;
4045 84451b3e 2018-07-10 stsp
4046 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4047 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4048 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4049 84451b3e 2018-07-10 stsp
4050 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4051 1a76625f 2018-10-22 stsp if (errcode)
4052 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4053 84451b3e 2018-07-10 stsp
4054 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4055 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4056 d68a0a7d 2018-07-10 stsp goto done;
4057 d68a0a7d 2018-07-10 stsp }
4058 d68a0a7d 2018-07-10 stsp
4059 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4060 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4061 d68a0a7d 2018-07-10 stsp
4062 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4063 d68a0a7d 2018-07-10 stsp if (line->annotated)
4064 d68a0a7d 2018-07-10 stsp goto done;
4065 d68a0a7d 2018-07-10 stsp
4066 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4067 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4068 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4069 84451b3e 2018-07-10 stsp goto done;
4070 84451b3e 2018-07-10 stsp }
4071 84451b3e 2018-07-10 stsp line->annotated = 1;
4072 84451b3e 2018-07-10 stsp done:
4073 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4074 1a76625f 2018-10-22 stsp if (errcode)
4075 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4076 84451b3e 2018-07-10 stsp return err;
4077 84451b3e 2018-07-10 stsp }
4078 84451b3e 2018-07-10 stsp
4079 84451b3e 2018-07-10 stsp static void *
4080 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4081 84451b3e 2018-07-10 stsp {
4082 18430de3 2018-07-10 stsp const struct got_error *err;
4083 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4084 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4085 1a76625f 2018-10-22 stsp int errcode;
4086 18430de3 2018-07-10 stsp
4087 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4088 61266923 2020-01-14 stsp if (err)
4089 61266923 2020-01-14 stsp return (void *)err;
4090 61266923 2020-01-14 stsp
4091 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4092 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4093 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4094 fc06ba56 2019-08-22 stsp err = NULL;
4095 18430de3 2018-07-10 stsp
4096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4097 1a76625f 2018-10-22 stsp if (errcode)
4098 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4100 18430de3 2018-07-10 stsp
4101 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4102 c9beca56 2018-07-22 stsp ta->repo = NULL;
4103 c9beca56 2018-07-22 stsp *ta->complete = 1;
4104 18430de3 2018-07-10 stsp
4105 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4106 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4107 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4108 18430de3 2018-07-10 stsp
4109 18430de3 2018-07-10 stsp return (void *)err;
4110 84451b3e 2018-07-10 stsp }
4111 84451b3e 2018-07-10 stsp
4112 245d91c1 2018-07-12 stsp static struct got_object_id *
4113 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4114 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4115 245d91c1 2018-07-12 stsp {
4116 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4117 8d0fe45a 2019-08-12 stsp
4118 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4119 8d0fe45a 2019-08-12 stsp return NULL;
4120 b880a918 2018-07-10 stsp
4121 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4122 245d91c1 2018-07-12 stsp if (!line->annotated)
4123 245d91c1 2018-07-12 stsp return NULL;
4124 245d91c1 2018-07-12 stsp
4125 245d91c1 2018-07-12 stsp return line->id;
4126 b880a918 2018-07-10 stsp }
4127 245d91c1 2018-07-12 stsp
4128 b880a918 2018-07-10 stsp static const struct got_error *
4129 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4130 a70480e0 2018-06-23 stsp {
4131 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4132 245d91c1 2018-07-12 stsp int i;
4133 245d91c1 2018-07-12 stsp
4134 245d91c1 2018-07-12 stsp if (blame->thread) {
4135 1a76625f 2018-10-22 stsp int errcode;
4136 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4137 1a76625f 2018-10-22 stsp if (errcode)
4138 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4139 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4140 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4141 1a76625f 2018-10-22 stsp if (errcode)
4142 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4143 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4144 1a76625f 2018-10-22 stsp if (errcode)
4145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4146 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4147 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4148 245d91c1 2018-07-12 stsp err = NULL;
4149 245d91c1 2018-07-12 stsp blame->thread = NULL;
4150 245d91c1 2018-07-12 stsp }
4151 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4152 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4153 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4154 245d91c1 2018-07-12 stsp }
4155 245d91c1 2018-07-12 stsp if (blame->f) {
4156 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4157 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4158 245d91c1 2018-07-12 stsp blame->f = NULL;
4159 245d91c1 2018-07-12 stsp }
4160 57670559 2018-12-23 stsp if (blame->lines) {
4161 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4162 57670559 2018-12-23 stsp free(blame->lines[i].id);
4163 57670559 2018-12-23 stsp free(blame->lines);
4164 57670559 2018-12-23 stsp blame->lines = NULL;
4165 57670559 2018-12-23 stsp }
4166 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4167 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4168 245d91c1 2018-07-12 stsp
4169 245d91c1 2018-07-12 stsp return err;
4170 245d91c1 2018-07-12 stsp }
4171 245d91c1 2018-07-12 stsp
4172 245d91c1 2018-07-12 stsp static const struct got_error *
4173 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4174 fc06ba56 2019-08-22 stsp {
4175 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4176 fc06ba56 2019-08-22 stsp int *done = arg;
4177 fc06ba56 2019-08-22 stsp int errcode;
4178 fc06ba56 2019-08-22 stsp
4179 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4180 fc06ba56 2019-08-22 stsp if (errcode)
4181 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4182 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4183 fc06ba56 2019-08-22 stsp
4184 fc06ba56 2019-08-22 stsp if (*done)
4185 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4186 fc06ba56 2019-08-22 stsp
4187 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4188 fc06ba56 2019-08-22 stsp if (errcode)
4189 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4190 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4191 fc06ba56 2019-08-22 stsp
4192 fc06ba56 2019-08-22 stsp return err;
4193 fc06ba56 2019-08-22 stsp }
4194 fc06ba56 2019-08-22 stsp
4195 fc06ba56 2019-08-22 stsp static const struct got_error *
4196 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4197 245d91c1 2018-07-12 stsp {
4198 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4199 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4200 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4201 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4202 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4203 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4204 15a94983 2018-12-23 stsp int obj_type;
4205 a70480e0 2018-06-23 stsp
4206 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4207 a5388363 2020-12-01 naddy s->path);
4208 27d434c2 2018-09-15 stsp if (err)
4209 15a94983 2018-12-23 stsp return err;
4210 27d434c2 2018-09-15 stsp
4211 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4212 84451b3e 2018-07-10 stsp if (err)
4213 84451b3e 2018-07-10 stsp goto done;
4214 27d434c2 2018-09-15 stsp
4215 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4216 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4217 84451b3e 2018-07-10 stsp goto done;
4218 84451b3e 2018-07-10 stsp }
4219 a70480e0 2018-06-23 stsp
4220 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4221 a70480e0 2018-06-23 stsp if (err)
4222 a70480e0 2018-06-23 stsp goto done;
4223 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4224 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4225 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4226 84451b3e 2018-07-10 stsp goto done;
4227 84451b3e 2018-07-10 stsp }
4228 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4229 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4230 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4231 84451b3e 2018-07-10 stsp goto done;
4232 b02560ec 2019-08-19 stsp
4233 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4234 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4235 b02560ec 2019-08-19 stsp blame->nlines--;
4236 a70480e0 2018-06-23 stsp
4237 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4238 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4239 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4240 84451b3e 2018-07-10 stsp goto done;
4241 84451b3e 2018-07-10 stsp }
4242 a70480e0 2018-06-23 stsp
4243 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4244 bd24772e 2018-07-11 stsp if (err)
4245 bd24772e 2018-07-11 stsp goto done;
4246 bd24772e 2018-07-11 stsp
4247 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4248 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4249 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4250 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4251 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4253 245d91c1 2018-07-12 stsp goto done;
4254 245d91c1 2018-07-12 stsp }
4255 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4256 245d91c1 2018-07-12 stsp
4257 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4258 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4259 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4260 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4261 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4262 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4263 a5388363 2020-12-01 naddy s->blame_complete = 0;
4264 245d91c1 2018-07-12 stsp
4265 245d91c1 2018-07-12 stsp done:
4266 245d91c1 2018-07-12 stsp if (blob)
4267 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4268 27d434c2 2018-09-15 stsp free(obj_id);
4269 245d91c1 2018-07-12 stsp if (err)
4270 245d91c1 2018-07-12 stsp stop_blame(blame);
4271 245d91c1 2018-07-12 stsp return err;
4272 245d91c1 2018-07-12 stsp }
4273 245d91c1 2018-07-12 stsp
4274 245d91c1 2018-07-12 stsp static const struct got_error *
4275 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4276 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4277 245d91c1 2018-07-12 stsp {
4278 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4279 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4280 dbc6a6b6 2018-07-12 stsp
4281 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4282 245d91c1 2018-07-12 stsp
4283 c4843652 2019-08-12 stsp s->path = strdup(path);
4284 c4843652 2019-08-12 stsp if (s->path == NULL)
4285 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4286 c4843652 2019-08-12 stsp
4287 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4288 c4843652 2019-08-12 stsp if (err) {
4289 c4843652 2019-08-12 stsp free(s->path);
4290 7cbe629d 2018-08-04 stsp return err;
4291 c4843652 2019-08-12 stsp }
4292 245d91c1 2018-07-12 stsp
4293 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4294 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4295 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4296 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4297 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4298 fb2756b9 2018-08-04 stsp s->repo = repo;
4299 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4300 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4301 7cbe629d 2018-08-04 stsp
4302 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4303 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4304 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4305 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4306 11b20872 2019-11-08 stsp if (err)
4307 11b20872 2019-11-08 stsp return err;
4308 11b20872 2019-11-08 stsp }
4309 11b20872 2019-11-08 stsp
4310 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4311 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4312 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4313 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4314 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4315 e5a0f69f 2018-08-18 stsp
4316 a5388363 2020-12-01 naddy return run_blame(view);
4317 7cbe629d 2018-08-04 stsp }
4318 7cbe629d 2018-08-04 stsp
4319 e5a0f69f 2018-08-18 stsp static const struct got_error *
4320 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4321 7cbe629d 2018-08-04 stsp {
4322 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4323 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4324 7cbe629d 2018-08-04 stsp
4325 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4326 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4327 e5a0f69f 2018-08-18 stsp
4328 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4329 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4330 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4331 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4332 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4333 7cbe629d 2018-08-04 stsp }
4334 e5a0f69f 2018-08-18 stsp
4335 e5a0f69f 2018-08-18 stsp free(s->path);
4336 11b20872 2019-11-08 stsp free_colors(&s->colors);
4337 e5a0f69f 2018-08-18 stsp
4338 e5a0f69f 2018-08-18 stsp return err;
4339 7cbe629d 2018-08-04 stsp }
4340 7cbe629d 2018-08-04 stsp
4341 7cbe629d 2018-08-04 stsp static const struct got_error *
4342 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4343 6c4c42e0 2019-06-24 stsp {
4344 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4345 6c4c42e0 2019-06-24 stsp
4346 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4347 6c4c42e0 2019-06-24 stsp return NULL;
4348 6c4c42e0 2019-06-24 stsp }
4349 6c4c42e0 2019-06-24 stsp
4350 6c4c42e0 2019-06-24 stsp static const struct got_error *
4351 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4352 6c4c42e0 2019-06-24 stsp {
4353 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4354 6c4c42e0 2019-06-24 stsp int lineno;
4355 6c4c42e0 2019-06-24 stsp
4356 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4357 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4358 6c4c42e0 2019-06-24 stsp return NULL;
4359 6c4c42e0 2019-06-24 stsp }
4360 6c4c42e0 2019-06-24 stsp
4361 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4362 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4363 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4364 6c4c42e0 2019-06-24 stsp else
4365 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4366 6c4c42e0 2019-06-24 stsp } else {
4367 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4368 6c4c42e0 2019-06-24 stsp lineno = 1;
4369 6c4c42e0 2019-06-24 stsp else
4370 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4371 6c4c42e0 2019-06-24 stsp }
4372 6c4c42e0 2019-06-24 stsp
4373 6c4c42e0 2019-06-24 stsp while (1) {
4374 6c4c42e0 2019-06-24 stsp char *line = NULL;
4375 6c4c42e0 2019-06-24 stsp off_t offset;
4376 6c4c42e0 2019-06-24 stsp size_t len;
4377 6c4c42e0 2019-06-24 stsp
4378 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4379 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4380 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 6c4c42e0 2019-06-24 stsp free(line);
4382 6c4c42e0 2019-06-24 stsp break;
4383 6c4c42e0 2019-06-24 stsp }
4384 2246482e 2019-06-25 stsp
4385 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4386 6c4c42e0 2019-06-24 stsp lineno = 1;
4387 6c4c42e0 2019-06-24 stsp else
4388 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4389 6c4c42e0 2019-06-24 stsp }
4390 6c4c42e0 2019-06-24 stsp
4391 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4392 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4393 6c4c42e0 2019-06-24 stsp free(line);
4394 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4395 6c4c42e0 2019-06-24 stsp }
4396 6c4c42e0 2019-06-24 stsp free(line);
4397 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4398 41605754 2020-11-12 stsp if (line &&
4399 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4400 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4402 6c4c42e0 2019-06-24 stsp free(line);
4403 6c4c42e0 2019-06-24 stsp break;
4404 6c4c42e0 2019-06-24 stsp }
4405 6c4c42e0 2019-06-24 stsp free(line);
4406 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4407 6c4c42e0 2019-06-24 stsp lineno++;
4408 6c4c42e0 2019-06-24 stsp else
4409 6c4c42e0 2019-06-24 stsp lineno--;
4410 6c4c42e0 2019-06-24 stsp }
4411 6c4c42e0 2019-06-24 stsp
4412 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4413 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4414 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4415 6c4c42e0 2019-06-24 stsp }
4416 6c4c42e0 2019-06-24 stsp
4417 6c4c42e0 2019-06-24 stsp return NULL;
4418 6c4c42e0 2019-06-24 stsp }
4419 6c4c42e0 2019-06-24 stsp
4420 6c4c42e0 2019-06-24 stsp static const struct got_error *
4421 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4422 7cbe629d 2018-08-04 stsp {
4423 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4424 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4425 2b380cc8 2018-10-24 stsp int errcode;
4426 2b380cc8 2018-10-24 stsp
4427 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4428 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4429 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4430 2b380cc8 2018-10-24 stsp if (errcode)
4431 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4432 51fe7530 2019-08-19 stsp
4433 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4434 2b380cc8 2018-10-24 stsp }
4435 e5a0f69f 2018-08-18 stsp
4436 51fe7530 2019-08-19 stsp if (s->blame_complete)
4437 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4438 51fe7530 2019-08-19 stsp
4439 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4440 e5a0f69f 2018-08-18 stsp
4441 669b5ffa 2018-10-07 stsp view_vborder(view);
4442 e5a0f69f 2018-08-18 stsp return err;
4443 e5a0f69f 2018-08-18 stsp }
4444 e5a0f69f 2018-08-18 stsp
4445 e5a0f69f 2018-08-18 stsp static const struct got_error *
4446 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4447 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4448 e5a0f69f 2018-08-18 stsp {
4449 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4450 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4451 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4452 669b5ffa 2018-10-07 stsp int begin_x = 0;
4453 7cbe629d 2018-08-04 stsp
4454 e5a0f69f 2018-08-18 stsp switch (ch) {
4455 1e37a5c2 2019-05-12 jcs case 'q':
4456 1e37a5c2 2019-05-12 jcs s->done = 1;
4457 1e37a5c2 2019-05-12 jcs break;
4458 1e37a5c2 2019-05-12 jcs case 'k':
4459 1e37a5c2 2019-05-12 jcs case KEY_UP:
4460 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4461 1e37a5c2 2019-05-12 jcs s->selected_line--;
4462 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4463 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4464 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4465 1e37a5c2 2019-05-12 jcs break;
4466 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4467 ea025d1d 2020-02-22 naddy case CTRL('b'):
4468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4469 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4470 e5a0f69f 2018-08-18 stsp break;
4471 1e37a5c2 2019-05-12 jcs }
4472 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4473 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4474 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4475 1e37a5c2 2019-05-12 jcs else
4476 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4477 1e37a5c2 2019-05-12 jcs break;
4478 1e37a5c2 2019-05-12 jcs case 'j':
4479 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4480 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4481 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4482 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4483 1e37a5c2 2019-05-12 jcs s->selected_line++;
4484 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4485 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4486 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4487 1e37a5c2 2019-05-12 jcs break;
4488 1e37a5c2 2019-05-12 jcs case 'b':
4489 1e37a5c2 2019-05-12 jcs case 'p': {
4490 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4491 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4492 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4493 1e37a5c2 2019-05-12 jcs if (id == NULL)
4494 e5a0f69f 2018-08-18 stsp break;
4495 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4496 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4497 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4498 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4499 1e37a5c2 2019-05-12 jcs int obj_type;
4500 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4501 1e37a5c2 2019-05-12 jcs s->repo, id);
4502 e5a0f69f 2018-08-18 stsp if (err)
4503 e5a0f69f 2018-08-18 stsp break;
4504 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4505 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4506 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4507 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4508 e5a0f69f 2018-08-18 stsp break;
4509 e5a0f69f 2018-08-18 stsp }
4510 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4511 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4512 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4513 e5a0f69f 2018-08-18 stsp if (err) {
4514 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4515 1e37a5c2 2019-05-12 jcs err = NULL;
4516 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4517 e5a0f69f 2018-08-18 stsp break;
4518 e5a0f69f 2018-08-18 stsp }
4519 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4520 1e37a5c2 2019-05-12 jcs blob_id);
4521 1e37a5c2 2019-05-12 jcs free(blob_id);
4522 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4523 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4524 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4525 e5a0f69f 2018-08-18 stsp break;
4526 1e37a5c2 2019-05-12 jcs }
4527 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4528 1e37a5c2 2019-05-12 jcs pid->id);
4529 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4530 1e37a5c2 2019-05-12 jcs } else {
4531 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4532 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4533 1e37a5c2 2019-05-12 jcs break;
4534 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4535 1e37a5c2 2019-05-12 jcs id);
4536 1e37a5c2 2019-05-12 jcs }
4537 1e37a5c2 2019-05-12 jcs if (err)
4538 e5a0f69f 2018-08-18 stsp break;
4539 1e37a5c2 2019-05-12 jcs s->done = 1;
4540 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4541 1e37a5c2 2019-05-12 jcs s->done = 0;
4542 1e37a5c2 2019-05-12 jcs if (thread_err)
4543 1e37a5c2 2019-05-12 jcs break;
4544 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4545 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4546 a5388363 2020-12-01 naddy err = run_blame(view);
4547 1e37a5c2 2019-05-12 jcs if (err)
4548 1e37a5c2 2019-05-12 jcs break;
4549 1e37a5c2 2019-05-12 jcs break;
4550 1e37a5c2 2019-05-12 jcs }
4551 1e37a5c2 2019-05-12 jcs case 'B': {
4552 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4553 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4554 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4555 1e37a5c2 2019-05-12 jcs break;
4556 1e37a5c2 2019-05-12 jcs s->done = 1;
4557 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4558 1e37a5c2 2019-05-12 jcs s->done = 0;
4559 1e37a5c2 2019-05-12 jcs if (thread_err)
4560 1e37a5c2 2019-05-12 jcs break;
4561 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4562 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4563 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4564 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4565 a5388363 2020-12-01 naddy err = run_blame(view);
4566 1e37a5c2 2019-05-12 jcs if (err)
4567 1e37a5c2 2019-05-12 jcs break;
4568 1e37a5c2 2019-05-12 jcs break;
4569 1e37a5c2 2019-05-12 jcs }
4570 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4571 1e37a5c2 2019-05-12 jcs case '\r': {
4572 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4573 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4574 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4575 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4576 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4577 1e37a5c2 2019-05-12 jcs if (id == NULL)
4578 1e37a5c2 2019-05-12 jcs break;
4579 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4580 1e37a5c2 2019-05-12 jcs if (err)
4581 1e37a5c2 2019-05-12 jcs break;
4582 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4583 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4584 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4585 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4586 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4587 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4588 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4589 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4590 1e37a5c2 2019-05-12 jcs break;
4591 15a94983 2018-12-23 stsp }
4592 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4593 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4594 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4595 1e37a5c2 2019-05-12 jcs if (err) {
4596 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4597 1e37a5c2 2019-05-12 jcs break;
4598 1e37a5c2 2019-05-12 jcs }
4599 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4600 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4601 1e37a5c2 2019-05-12 jcs if (err)
4602 34bc9ec9 2019-02-22 stsp break;
4603 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4604 1e37a5c2 2019-05-12 jcs if (err) {
4605 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4606 e5a0f69f 2018-08-18 stsp break;
4607 e5a0f69f 2018-08-18 stsp }
4608 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4609 1e37a5c2 2019-05-12 jcs } else
4610 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4611 1e37a5c2 2019-05-12 jcs if (err)
4612 e5a0f69f 2018-08-18 stsp break;
4613 1e37a5c2 2019-05-12 jcs break;
4614 1e37a5c2 2019-05-12 jcs }
4615 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4616 ea025d1d 2020-02-22 naddy case CTRL('f'):
4617 1e37a5c2 2019-05-12 jcs case ' ':
4618 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4619 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4620 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4621 e5a0f69f 2018-08-18 stsp break;
4622 1e37a5c2 2019-05-12 jcs }
4623 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4624 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4625 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4626 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4627 e5a0f69f 2018-08-18 stsp break;
4628 1e37a5c2 2019-05-12 jcs }
4629 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4630 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4631 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4632 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4633 1e37a5c2 2019-05-12 jcs else
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4635 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4636 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4637 1e37a5c2 2019-05-12 jcs break;
4638 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4639 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4640 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4641 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4642 1e37a5c2 2019-05-12 jcs }
4643 1e37a5c2 2019-05-12 jcs break;
4644 1e37a5c2 2019-05-12 jcs default:
4645 1e37a5c2 2019-05-12 jcs break;
4646 a70480e0 2018-06-23 stsp }
4647 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4648 a70480e0 2018-06-23 stsp }
4649 a70480e0 2018-06-23 stsp
4650 a70480e0 2018-06-23 stsp static const struct got_error *
4651 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4652 9f7d7167 2018-04-29 stsp {
4653 a70480e0 2018-06-23 stsp const struct got_error *error;
4654 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4655 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4656 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4657 0587e10c 2020-07-23 stsp char *link_target = NULL;
4658 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4659 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4660 a70480e0 2018-06-23 stsp int ch;
4661 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4662 70ac5f84 2019-03-28 stsp
4663 a70480e0 2018-06-23 stsp #ifndef PROFILE
4664 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4665 8e94dd5b 2019-01-04 stsp NULL) == -1)
4666 a70480e0 2018-06-23 stsp err(1, "pledge");
4667 a70480e0 2018-06-23 stsp #endif
4668 a70480e0 2018-06-23 stsp
4669 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4670 a70480e0 2018-06-23 stsp switch (ch) {
4671 a70480e0 2018-06-23 stsp case 'c':
4672 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4673 a70480e0 2018-06-23 stsp break;
4674 69069811 2018-08-02 stsp case 'r':
4675 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4676 69069811 2018-08-02 stsp if (repo_path == NULL)
4677 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4678 9ba1d308 2019-10-21 stsp optarg);
4679 69069811 2018-08-02 stsp break;
4680 a70480e0 2018-06-23 stsp default:
4681 17020d27 2019-03-07 stsp usage_blame();
4682 a70480e0 2018-06-23 stsp /* NOTREACHED */
4683 a70480e0 2018-06-23 stsp }
4684 a70480e0 2018-06-23 stsp }
4685 a70480e0 2018-06-23 stsp
4686 a70480e0 2018-06-23 stsp argc -= optind;
4687 a70480e0 2018-06-23 stsp argv += optind;
4688 a70480e0 2018-06-23 stsp
4689 f135c941 2020-02-20 stsp if (argc != 1)
4690 a70480e0 2018-06-23 stsp usage_blame();
4691 69069811 2018-08-02 stsp
4692 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4693 6962eb72 2020-02-20 stsp if (cwd == NULL)
4694 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4695 6962eb72 2020-02-20 stsp
4696 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4697 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4698 f135c941 2020-02-20 stsp goto done;
4699 f135c941 2020-02-20 stsp
4700 69069811 2018-08-02 stsp if (repo_path == NULL) {
4701 f135c941 2020-02-20 stsp if (worktree)
4702 eb41ed75 2019-02-05 stsp repo_path =
4703 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4704 f135c941 2020-02-20 stsp else
4705 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4706 69069811 2018-08-02 stsp }
4707 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4708 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4709 f135c941 2020-02-20 stsp goto done;
4710 f135c941 2020-02-20 stsp }
4711 a915003a 2019-02-05 stsp
4712 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4713 c02c541e 2019-03-29 stsp if (error != NULL)
4714 8e94dd5b 2019-01-04 stsp goto done;
4715 69069811 2018-08-02 stsp
4716 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4717 f135c941 2020-02-20 stsp worktree);
4718 c02c541e 2019-03-29 stsp if (error)
4719 92205607 2019-01-04 stsp goto done;
4720 69069811 2018-08-02 stsp
4721 f135c941 2020-02-20 stsp init_curses();
4722 f135c941 2020-02-20 stsp
4723 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4724 eb41ed75 2019-02-05 stsp if (error)
4725 69069811 2018-08-02 stsp goto done;
4726 a70480e0 2018-06-23 stsp
4727 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4728 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4729 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4730 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4731 a70480e0 2018-06-23 stsp if (error != NULL)
4732 66b4983c 2018-06-23 stsp goto done;
4733 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4734 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4735 a70480e0 2018-06-23 stsp } else {
4736 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4737 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4738 a70480e0 2018-06-23 stsp }
4739 a19e88aa 2018-06-23 stsp if (error != NULL)
4740 8b473291 2019-02-21 stsp goto done;
4741 8b473291 2019-02-21 stsp
4742 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4743 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4744 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4745 e1cd8fed 2018-08-01 stsp goto done;
4746 e1cd8fed 2018-08-01 stsp }
4747 0587e10c 2020-07-23 stsp
4748 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4749 0587e10c 2020-07-23 stsp commit_id, repo);
4750 7cbe629d 2018-08-04 stsp if (error)
4751 7cbe629d 2018-08-04 stsp goto done;
4752 0587e10c 2020-07-23 stsp
4753 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4754 78756c87 2020-11-24 stsp commit_id, repo);
4755 0587e10c 2020-07-23 stsp if (error)
4756 0587e10c 2020-07-23 stsp goto done;
4757 12314ad4 2019-08-31 stsp if (worktree) {
4758 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4759 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4760 12314ad4 2019-08-31 stsp worktree = NULL;
4761 12314ad4 2019-08-31 stsp }
4762 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4763 a70480e0 2018-06-23 stsp done:
4764 69069811 2018-08-02 stsp free(repo_path);
4765 f135c941 2020-02-20 stsp free(in_repo_path);
4766 0587e10c 2020-07-23 stsp free(link_target);
4767 69069811 2018-08-02 stsp free(cwd);
4768 a70480e0 2018-06-23 stsp free(commit_id);
4769 eb41ed75 2019-02-05 stsp if (worktree)
4770 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4771 a70480e0 2018-06-23 stsp if (repo)
4772 a70480e0 2018-06-23 stsp got_repo_close(repo);
4773 a70480e0 2018-06-23 stsp return error;
4774 ffd1d5e5 2018-06-23 stsp }
4775 ffd1d5e5 2018-06-23 stsp
4776 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4777 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4778 ffd1d5e5 2018-06-23 stsp {
4779 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4780 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4781 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4782 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4783 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4784 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4785 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4786 ffd1d5e5 2018-06-23 stsp
4787 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4788 ffd1d5e5 2018-06-23 stsp
4789 f7d12f7e 2018-08-01 stsp werase(view->window);
4790 ffd1d5e5 2018-06-23 stsp
4791 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4792 ffd1d5e5 2018-06-23 stsp return NULL;
4793 ffd1d5e5 2018-06-23 stsp
4794 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4795 ffd1d5e5 2018-06-23 stsp if (err)
4796 ffd1d5e5 2018-06-23 stsp return err;
4797 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4798 a3404814 2018-09-02 stsp wstandout(view->window);
4799 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4800 11b20872 2019-11-08 stsp if (tc)
4801 11b20872 2019-11-08 stsp wattr_on(view->window,
4802 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4804 11b20872 2019-11-08 stsp if (tc)
4805 11b20872 2019-11-08 stsp wattr_off(view->window,
4806 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4807 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4808 a3404814 2018-09-02 stsp wstandend(view->window);
4809 2550e4c3 2018-07-13 stsp free(wline);
4810 2550e4c3 2018-07-13 stsp wline = NULL;
4811 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4812 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4813 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4814 ffd1d5e5 2018-06-23 stsp return NULL;
4815 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4816 ce52c690 2018-06-23 stsp if (err)
4817 ce52c690 2018-06-23 stsp return err;
4818 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4819 2550e4c3 2018-07-13 stsp free(wline);
4820 2550e4c3 2018-07-13 stsp wline = NULL;
4821 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4822 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4823 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4824 ffd1d5e5 2018-06-23 stsp return NULL;
4825 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4826 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4827 a1eca9bb 2018-06-23 stsp return NULL;
4828 ffd1d5e5 2018-06-23 stsp
4829 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4830 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4831 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4832 0cf4efb1 2018-09-29 stsp if (view->focussed)
4833 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4834 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4835 ffd1d5e5 2018-06-23 stsp }
4836 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4837 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4838 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4839 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4840 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4841 ffd1d5e5 2018-06-23 stsp return NULL;
4842 ffd1d5e5 2018-06-23 stsp n = 1;
4843 ffd1d5e5 2018-06-23 stsp } else {
4844 ffd1d5e5 2018-06-23 stsp n = 0;
4845 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4846 ffd1d5e5 2018-06-23 stsp }
4847 ffd1d5e5 2018-06-23 stsp
4848 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4849 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4850 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4851 848d6979 2019-08-12 stsp const char *modestr = "";
4852 56e0773d 2019-11-28 stsp mode_t mode;
4853 1d13200f 2018-07-12 stsp
4854 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4855 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4856 56e0773d 2019-11-28 stsp
4857 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4858 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4859 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4860 1d13200f 2018-07-12 stsp if (err)
4861 638f9024 2019-05-13 stsp return got_error_from_errno(
4862 230a42bd 2019-05-11 jcs "got_object_id_str");
4863 1d13200f 2018-07-12 stsp }
4864 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4865 63c5ca5d 2019-08-24 stsp modestr = "$";
4866 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4867 0d6c6ee3 2020-05-20 stsp int i;
4868 0d6c6ee3 2020-05-20 stsp
4869 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4870 d86d3b18 2020-12-01 naddy te, s->repo);
4871 0d6c6ee3 2020-05-20 stsp if (err) {
4872 0d6c6ee3 2020-05-20 stsp free(id_str);
4873 0d6c6ee3 2020-05-20 stsp return err;
4874 0d6c6ee3 2020-05-20 stsp }
4875 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4876 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4877 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4878 0d6c6ee3 2020-05-20 stsp }
4879 848d6979 2019-08-12 stsp modestr = "@";
4880 0d6c6ee3 2020-05-20 stsp }
4881 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4882 848d6979 2019-08-12 stsp modestr = "/";
4883 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4884 848d6979 2019-08-12 stsp modestr = "*";
4885 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4886 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4887 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4888 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4889 1d13200f 2018-07-12 stsp free(id_str);
4890 0d6c6ee3 2020-05-20 stsp free(link_target);
4891 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4892 1d13200f 2018-07-12 stsp }
4893 1d13200f 2018-07-12 stsp free(id_str);
4894 0d6c6ee3 2020-05-20 stsp free(link_target);
4895 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4896 ffd1d5e5 2018-06-23 stsp if (err) {
4897 ffd1d5e5 2018-06-23 stsp free(line);
4898 ffd1d5e5 2018-06-23 stsp break;
4899 ffd1d5e5 2018-06-23 stsp }
4900 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4901 0cf4efb1 2018-09-29 stsp if (view->focussed)
4902 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4903 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4904 ffd1d5e5 2018-06-23 stsp }
4905 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4906 f26dddb7 2019-11-08 stsp if (tc)
4907 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4908 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4909 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4910 f26dddb7 2019-11-08 stsp if (tc)
4911 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4912 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4913 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4914 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4915 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4916 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4917 ffd1d5e5 2018-06-23 stsp free(line);
4918 2550e4c3 2018-07-13 stsp free(wline);
4919 2550e4c3 2018-07-13 stsp wline = NULL;
4920 ffd1d5e5 2018-06-23 stsp n++;
4921 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4922 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4923 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4924 ffd1d5e5 2018-06-23 stsp break;
4925 ffd1d5e5 2018-06-23 stsp }
4926 ffd1d5e5 2018-06-23 stsp
4927 ffd1d5e5 2018-06-23 stsp return err;
4928 ffd1d5e5 2018-06-23 stsp }
4929 ffd1d5e5 2018-06-23 stsp
4930 ffd1d5e5 2018-06-23 stsp static void
4931 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4932 ffd1d5e5 2018-06-23 stsp {
4933 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4934 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4935 fa86c4bf 2020-11-29 stsp int i = 0;
4936 ffd1d5e5 2018-06-23 stsp
4937 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4938 ffd1d5e5 2018-06-23 stsp return;
4939 ffd1d5e5 2018-06-23 stsp
4940 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4941 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4942 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4943 fa86c4bf 2020-11-29 stsp if (!isroot)
4944 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4945 fa86c4bf 2020-11-29 stsp break;
4946 fa86c4bf 2020-11-29 stsp }
4947 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4948 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4949 ffd1d5e5 2018-06-23 stsp }
4950 ffd1d5e5 2018-06-23 stsp }
4951 ffd1d5e5 2018-06-23 stsp
4952 fa86c4bf 2020-11-29 stsp static void
4953 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4954 ffd1d5e5 2018-06-23 stsp {
4955 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4956 ffd1d5e5 2018-06-23 stsp int n = 0;
4957 ffd1d5e5 2018-06-23 stsp
4958 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4959 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4960 694d3271 2020-12-01 naddy s->first_displayed_entry);
4961 694d3271 2020-12-01 naddy else
4962 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4963 56e0773d 2019-11-28 stsp
4964 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4965 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4966 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4967 768394f3 2019-01-24 stsp if (last) {
4968 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4969 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4970 768394f3 2019-01-24 stsp }
4971 ffd1d5e5 2018-06-23 stsp }
4972 ffd1d5e5 2018-06-23 stsp }
4973 ffd1d5e5 2018-06-23 stsp
4974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4975 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4976 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4977 ffd1d5e5 2018-06-23 stsp {
4978 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4979 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4980 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4981 ffd1d5e5 2018-06-23 stsp
4982 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4983 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4984 56e0773d 2019-11-28 stsp + 1 /* slash */;
4985 ce52c690 2018-06-23 stsp if (te)
4986 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4987 ce52c690 2018-06-23 stsp
4988 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4989 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4990 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4991 ffd1d5e5 2018-06-23 stsp
4992 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4993 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4994 d9765a41 2018-06-23 stsp while (pt) {
4995 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4996 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4997 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4998 cb2ebc8a 2018-06-23 stsp goto done;
4999 cb2ebc8a 2018-06-23 stsp }
5000 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5001 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5002 cb2ebc8a 2018-06-23 stsp goto done;
5003 cb2ebc8a 2018-06-23 stsp }
5004 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5005 ffd1d5e5 2018-06-23 stsp }
5006 ce52c690 2018-06-23 stsp if (te) {
5007 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5008 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5009 ce52c690 2018-06-23 stsp goto done;
5010 ce52c690 2018-06-23 stsp }
5011 cb2ebc8a 2018-06-23 stsp }
5012 ce52c690 2018-06-23 stsp done:
5013 ce52c690 2018-06-23 stsp if (err) {
5014 ce52c690 2018-06-23 stsp free(*path);
5015 ce52c690 2018-06-23 stsp *path = NULL;
5016 ce52c690 2018-06-23 stsp }
5017 ce52c690 2018-06-23 stsp return err;
5018 ce52c690 2018-06-23 stsp }
5019 ce52c690 2018-06-23 stsp
5020 ce52c690 2018-06-23 stsp static const struct got_error *
5021 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5022 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5023 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5024 ce52c690 2018-06-23 stsp {
5025 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5026 ce52c690 2018-06-23 stsp char *path;
5027 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5028 a0de39f3 2019-08-09 stsp
5029 a0de39f3 2019-08-09 stsp *new_view = NULL;
5030 69efd4c4 2018-07-18 stsp
5031 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5032 ce52c690 2018-06-23 stsp if (err)
5033 ce52c690 2018-06-23 stsp return err;
5034 ffd1d5e5 2018-06-23 stsp
5035 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5036 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5037 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5038 83ce39e3 2019-08-12 stsp goto done;
5039 83ce39e3 2019-08-12 stsp }
5040 cdf1ee82 2018-08-01 stsp
5041 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5042 e5a0f69f 2018-08-18 stsp if (err) {
5043 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5044 fc06ba56 2019-08-22 stsp err = NULL;
5045 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5046 e5a0f69f 2018-08-18 stsp } else
5047 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5048 83ce39e3 2019-08-12 stsp done:
5049 83ce39e3 2019-08-12 stsp free(path);
5050 69efd4c4 2018-07-18 stsp return err;
5051 69efd4c4 2018-07-18 stsp }
5052 69efd4c4 2018-07-18 stsp
5053 69efd4c4 2018-07-18 stsp static const struct got_error *
5054 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
5055 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5056 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5057 69efd4c4 2018-07-18 stsp {
5058 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5059 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5060 69efd4c4 2018-07-18 stsp char *path;
5061 a0de39f3 2019-08-09 stsp
5062 a0de39f3 2019-08-09 stsp *new_view = NULL;
5063 69efd4c4 2018-07-18 stsp
5064 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5065 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5066 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5067 e5a0f69f 2018-08-18 stsp
5068 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
5069 69efd4c4 2018-07-18 stsp if (err)
5070 69efd4c4 2018-07-18 stsp return err;
5071 69efd4c4 2018-07-18 stsp
5072 78756c87 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5073 ba4f502b 2018-08-04 stsp if (err)
5074 e5a0f69f 2018-08-18 stsp view_close(log_view);
5075 e5a0f69f 2018-08-18 stsp else
5076 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5077 cb2ebc8a 2018-06-23 stsp free(path);
5078 cb2ebc8a 2018-06-23 stsp return err;
5079 ffd1d5e5 2018-06-23 stsp }
5080 ffd1d5e5 2018-06-23 stsp
5081 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5082 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5083 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5084 ffd1d5e5 2018-06-23 stsp {
5085 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5086 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5087 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5088 ffd1d5e5 2018-06-23 stsp
5089 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5090 ffd1d5e5 2018-06-23 stsp
5091 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5092 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5093 ffd1d5e5 2018-06-23 stsp goto done;
5094 ffd1d5e5 2018-06-23 stsp
5095 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5096 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5097 ffd1d5e5 2018-06-23 stsp goto done;
5098 ffd1d5e5 2018-06-23 stsp }
5099 ffd1d5e5 2018-06-23 stsp
5100 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5101 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5102 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5103 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5104 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5105 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5106 6484ec90 2018-09-29 stsp goto done;
5107 6484ec90 2018-09-29 stsp }
5108 fb2756b9 2018-08-04 stsp s->repo = repo;
5109 c0b01bdb 2019-11-08 stsp
5110 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5111 c0b01bdb 2019-11-08 stsp
5112 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5113 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5114 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5115 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5116 c0b01bdb 2019-11-08 stsp if (err)
5117 c0b01bdb 2019-11-08 stsp goto done;
5118 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5119 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5120 c0b01bdb 2019-11-08 stsp if (err) {
5121 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5122 c0b01bdb 2019-11-08 stsp goto done;
5123 c0b01bdb 2019-11-08 stsp }
5124 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5125 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5126 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5127 c0b01bdb 2019-11-08 stsp if (err) {
5128 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5129 c0b01bdb 2019-11-08 stsp goto done;
5130 c0b01bdb 2019-11-08 stsp }
5131 e5a0f69f 2018-08-18 stsp
5132 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5133 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5134 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5135 11b20872 2019-11-08 stsp if (err) {
5136 11b20872 2019-11-08 stsp free_colors(&s->colors);
5137 11b20872 2019-11-08 stsp goto done;
5138 11b20872 2019-11-08 stsp }
5139 11b20872 2019-11-08 stsp
5140 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5141 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5142 c0b01bdb 2019-11-08 stsp if (err) {
5143 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5144 c0b01bdb 2019-11-08 stsp goto done;
5145 c0b01bdb 2019-11-08 stsp }
5146 c0b01bdb 2019-11-08 stsp }
5147 c0b01bdb 2019-11-08 stsp
5148 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5149 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5150 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5151 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5152 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5153 ad80ab7b 2018-08-04 stsp done:
5154 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5155 6484ec90 2018-09-29 stsp if (err) {
5156 fb2756b9 2018-08-04 stsp free(s->tree_label);
5157 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5158 6484ec90 2018-09-29 stsp }
5159 ad80ab7b 2018-08-04 stsp return err;
5160 ad80ab7b 2018-08-04 stsp }
5161 ad80ab7b 2018-08-04 stsp
5162 e5a0f69f 2018-08-18 stsp static const struct got_error *
5163 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5164 ad80ab7b 2018-08-04 stsp {
5165 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5166 ad80ab7b 2018-08-04 stsp
5167 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5168 fb2756b9 2018-08-04 stsp free(s->tree_label);
5169 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5170 6484ec90 2018-09-29 stsp free(s->commit_id);
5171 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5172 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5173 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5174 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5175 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5176 ad80ab7b 2018-08-04 stsp free(parent);
5177 ad80ab7b 2018-08-04 stsp
5178 ad80ab7b 2018-08-04 stsp }
5179 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5180 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5181 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5182 7c32bd05 2019-06-22 stsp return NULL;
5183 7c32bd05 2019-06-22 stsp }
5184 7c32bd05 2019-06-22 stsp
5185 7c32bd05 2019-06-22 stsp static const struct got_error *
5186 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5187 7c32bd05 2019-06-22 stsp {
5188 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5189 7c32bd05 2019-06-22 stsp
5190 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5191 7c32bd05 2019-06-22 stsp return NULL;
5192 7c32bd05 2019-06-22 stsp }
5193 7c32bd05 2019-06-22 stsp
5194 7c32bd05 2019-06-22 stsp static int
5195 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5196 7c32bd05 2019-06-22 stsp {
5197 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5198 7c32bd05 2019-06-22 stsp
5199 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5200 56e0773d 2019-11-28 stsp 0) == 0;
5201 7c32bd05 2019-06-22 stsp }
5202 7c32bd05 2019-06-22 stsp
5203 7c32bd05 2019-06-22 stsp static const struct got_error *
5204 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5205 7c32bd05 2019-06-22 stsp {
5206 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5207 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5208 7c32bd05 2019-06-22 stsp
5209 7c32bd05 2019-06-22 stsp if (!view->searching) {
5210 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5211 7c32bd05 2019-06-22 stsp return NULL;
5212 7c32bd05 2019-06-22 stsp }
5213 7c32bd05 2019-06-22 stsp
5214 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5215 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5216 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5217 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5218 56e0773d 2019-11-28 stsp s->selected_entry);
5219 7c32bd05 2019-06-22 stsp else
5220 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5221 56e0773d 2019-11-28 stsp } else {
5222 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5223 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5224 56e0773d 2019-11-28 stsp else
5225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5226 56e0773d 2019-11-28 stsp s->selected_entry);
5227 7c32bd05 2019-06-22 stsp }
5228 7c32bd05 2019-06-22 stsp } else {
5229 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5230 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5231 56e0773d 2019-11-28 stsp else
5232 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5233 7c32bd05 2019-06-22 stsp }
5234 7c32bd05 2019-06-22 stsp
5235 7c32bd05 2019-06-22 stsp while (1) {
5236 56e0773d 2019-11-28 stsp if (te == NULL) {
5237 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5238 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 ac66afb8 2019-06-24 stsp return NULL;
5240 ac66afb8 2019-06-24 stsp }
5241 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5243 56e0773d 2019-11-28 stsp else
5244 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5245 7c32bd05 2019-06-22 stsp }
5246 7c32bd05 2019-06-22 stsp
5247 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5248 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 56e0773d 2019-11-28 stsp s->matched_entry = te;
5250 7c32bd05 2019-06-22 stsp break;
5251 7c32bd05 2019-06-22 stsp }
5252 7c32bd05 2019-06-22 stsp
5253 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5254 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5255 56e0773d 2019-11-28 stsp else
5256 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5257 7c32bd05 2019-06-22 stsp }
5258 e5a0f69f 2018-08-18 stsp
5259 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5260 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5261 7c32bd05 2019-06-22 stsp s->selected = 0;
5262 7c32bd05 2019-06-22 stsp }
5263 7c32bd05 2019-06-22 stsp
5264 e5a0f69f 2018-08-18 stsp return NULL;
5265 ad80ab7b 2018-08-04 stsp }
5266 ad80ab7b 2018-08-04 stsp
5267 ad80ab7b 2018-08-04 stsp static const struct got_error *
5268 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5269 ad80ab7b 2018-08-04 stsp {
5270 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5271 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5272 e5a0f69f 2018-08-18 stsp char *parent_path;
5273 ad80ab7b 2018-08-04 stsp
5274 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5275 e5a0f69f 2018-08-18 stsp if (err)
5276 e5a0f69f 2018-08-18 stsp return err;
5277 ffd1d5e5 2018-06-23 stsp
5278 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5279 e5a0f69f 2018-08-18 stsp free(parent_path);
5280 669b5ffa 2018-10-07 stsp
5281 669b5ffa 2018-10-07 stsp view_vborder(view);
5282 e5a0f69f 2018-08-18 stsp return err;
5283 e5a0f69f 2018-08-18 stsp }
5284 ce52c690 2018-06-23 stsp
5285 e5a0f69f 2018-08-18 stsp static const struct got_error *
5286 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5287 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5288 e5a0f69f 2018-08-18 stsp {
5289 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5290 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5291 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5292 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5293 ffd1d5e5 2018-06-23 stsp
5294 e5a0f69f 2018-08-18 stsp switch (ch) {
5295 1e37a5c2 2019-05-12 jcs case 'i':
5296 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5297 1e37a5c2 2019-05-12 jcs break;
5298 1e37a5c2 2019-05-12 jcs case 'l':
5299 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5300 ffd1d5e5 2018-06-23 stsp break;
5301 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5302 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5303 78756c87 2020-11-24 stsp err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5304 78756c87 2020-11-24 stsp &s->parents, s->commit_id, s->repo);
5305 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5306 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5307 1e37a5c2 2019-05-12 jcs if (err)
5308 1e37a5c2 2019-05-12 jcs return err;
5309 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5310 1e37a5c2 2019-05-12 jcs if (err) {
5311 1e37a5c2 2019-05-12 jcs view_close(log_view);
5312 669b5ffa 2018-10-07 stsp break;
5313 1e37a5c2 2019-05-12 jcs }
5314 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5315 1e37a5c2 2019-05-12 jcs } else
5316 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5317 152c1c93 2020-11-29 stsp break;
5318 152c1c93 2020-11-29 stsp case 'r':
5319 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5320 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5321 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5322 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5323 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5324 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5325 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5326 152c1c93 2020-11-29 stsp if (err) {
5327 152c1c93 2020-11-29 stsp view_close(ref_view);
5328 152c1c93 2020-11-29 stsp return err;
5329 152c1c93 2020-11-29 stsp }
5330 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5331 152c1c93 2020-11-29 stsp err = view_close_child(view);
5332 152c1c93 2020-11-29 stsp if (err)
5333 152c1c93 2020-11-29 stsp return err;
5334 152c1c93 2020-11-29 stsp err = view_set_child(view, ref_view);
5335 152c1c93 2020-11-29 stsp if (err) {
5336 152c1c93 2020-11-29 stsp view_close(ref_view);
5337 152c1c93 2020-11-29 stsp break;
5338 152c1c93 2020-11-29 stsp }
5339 152c1c93 2020-11-29 stsp *focus_view = ref_view;
5340 152c1c93 2020-11-29 stsp } else
5341 152c1c93 2020-11-29 stsp *new_view = ref_view;
5342 1e37a5c2 2019-05-12 jcs break;
5343 1e37a5c2 2019-05-12 jcs case 'k':
5344 1e37a5c2 2019-05-12 jcs case KEY_UP:
5345 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5346 1e37a5c2 2019-05-12 jcs s->selected--;
5347 fa86c4bf 2020-11-29 stsp break;
5348 1e37a5c2 2019-05-12 jcs }
5349 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5350 1e37a5c2 2019-05-12 jcs break;
5351 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5352 ea025d1d 2020-02-22 naddy case CTRL('b'):
5353 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5354 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5355 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5356 fa86c4bf 2020-11-29 stsp s->selected = 0;
5357 fa86c4bf 2020-11-29 stsp } else {
5358 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5359 fa86c4bf 2020-11-29 stsp s->selected = 0;
5360 fa86c4bf 2020-11-29 stsp }
5361 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5362 1e37a5c2 2019-05-12 jcs break;
5363 1e37a5c2 2019-05-12 jcs case 'j':
5364 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5365 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5366 1e37a5c2 2019-05-12 jcs s->selected++;
5367 1e37a5c2 2019-05-12 jcs break;
5368 1e37a5c2 2019-05-12 jcs }
5369 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5370 56e0773d 2019-11-28 stsp == NULL)
5371 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5372 1e37a5c2 2019-05-12 jcs break;
5373 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5374 1e37a5c2 2019-05-12 jcs break;
5375 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5376 ea025d1d 2020-02-22 naddy case CTRL('f'):
5377 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5378 1e37a5c2 2019-05-12 jcs == NULL) {
5379 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5380 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5381 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5382 1e37a5c2 2019-05-12 jcs break;
5383 1e37a5c2 2019-05-12 jcs }
5384 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5385 1e37a5c2 2019-05-12 jcs break;
5386 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5387 1e37a5c2 2019-05-12 jcs case '\r':
5388 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5389 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5390 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5391 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5392 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5393 1e37a5c2 2019-05-12 jcs break;
5394 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5395 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5396 1e37a5c2 2019-05-12 jcs entry);
5397 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5398 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5399 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5400 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5401 1e37a5c2 2019-05-12 jcs s->selected_entry =
5402 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5403 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5404 1e37a5c2 2019-05-12 jcs free(parent);
5405 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5406 56e0773d 2019-11-28 stsp s->selected_entry))) {
5407 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5408 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5409 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5410 1e37a5c2 2019-05-12 jcs if (err)
5411 1e37a5c2 2019-05-12 jcs break;
5412 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5413 941e9f74 2019-05-21 stsp if (err) {
5414 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5415 1e37a5c2 2019-05-12 jcs break;
5416 1e37a5c2 2019-05-12 jcs }
5417 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5418 56e0773d 2019-11-28 stsp s->selected_entry))) {
5419 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5420 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5421 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5422 1e37a5c2 2019-05-12 jcs
5423 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5424 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5425 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5426 1e37a5c2 2019-05-12 jcs if (err)
5427 1e37a5c2 2019-05-12 jcs break;
5428 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5429 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5430 669b5ffa 2018-10-07 stsp if (err)
5431 669b5ffa 2018-10-07 stsp return err;
5432 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5433 669b5ffa 2018-10-07 stsp if (err) {
5434 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5435 669b5ffa 2018-10-07 stsp break;
5436 669b5ffa 2018-10-07 stsp }
5437 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5438 669b5ffa 2018-10-07 stsp } else
5439 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5440 1e37a5c2 2019-05-12 jcs }
5441 1e37a5c2 2019-05-12 jcs break;
5442 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5443 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5444 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5445 1e37a5c2 2019-05-12 jcs break;
5446 1e37a5c2 2019-05-12 jcs default:
5447 1e37a5c2 2019-05-12 jcs break;
5448 ffd1d5e5 2018-06-23 stsp }
5449 e5a0f69f 2018-08-18 stsp
5450 ffd1d5e5 2018-06-23 stsp return err;
5451 9f7d7167 2018-04-29 stsp }
5452 9f7d7167 2018-04-29 stsp
5453 ffd1d5e5 2018-06-23 stsp __dead static void
5454 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5455 ffd1d5e5 2018-06-23 stsp {
5456 ffd1d5e5 2018-06-23 stsp endwin();
5457 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5458 ffd1d5e5 2018-06-23 stsp getprogname());
5459 ffd1d5e5 2018-06-23 stsp exit(1);
5460 ffd1d5e5 2018-06-23 stsp }
5461 ffd1d5e5 2018-06-23 stsp
5462 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5463 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5464 ffd1d5e5 2018-06-23 stsp {
5465 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5466 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5467 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5468 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5469 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5470 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5471 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5472 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5473 ffd1d5e5 2018-06-23 stsp int ch;
5474 5221c383 2018-08-01 stsp struct tog_view *view;
5475 70ac5f84 2019-03-28 stsp
5476 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5477 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5478 d188b9a6 2019-01-04 stsp NULL) == -1)
5479 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5480 ffd1d5e5 2018-06-23 stsp #endif
5481 ffd1d5e5 2018-06-23 stsp
5482 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5483 ffd1d5e5 2018-06-23 stsp switch (ch) {
5484 ffd1d5e5 2018-06-23 stsp case 'c':
5485 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5486 74283ab8 2020-02-07 stsp break;
5487 74283ab8 2020-02-07 stsp case 'r':
5488 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5489 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5490 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5491 74283ab8 2020-02-07 stsp optarg);
5492 ffd1d5e5 2018-06-23 stsp break;
5493 ffd1d5e5 2018-06-23 stsp default:
5494 e99e2d15 2020-11-24 naddy usage_tree();
5495 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5496 ffd1d5e5 2018-06-23 stsp }
5497 ffd1d5e5 2018-06-23 stsp }
5498 ffd1d5e5 2018-06-23 stsp
5499 ffd1d5e5 2018-06-23 stsp argc -= optind;
5500 ffd1d5e5 2018-06-23 stsp argv += optind;
5501 ffd1d5e5 2018-06-23 stsp
5502 55cccc34 2020-02-20 stsp if (argc > 1)
5503 e99e2d15 2020-11-24 naddy usage_tree();
5504 74283ab8 2020-02-07 stsp
5505 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5506 55cccc34 2020-02-20 stsp if (cwd == NULL)
5507 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5508 55cccc34 2020-02-20 stsp
5509 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5510 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5511 55cccc34 2020-02-20 stsp goto done;
5512 55cccc34 2020-02-20 stsp
5513 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5514 55cccc34 2020-02-20 stsp if (worktree)
5515 52185f70 2019-02-05 stsp repo_path =
5516 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5517 55cccc34 2020-02-20 stsp else
5518 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5519 74283ab8 2020-02-07 stsp }
5520 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5521 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5522 55cccc34 2020-02-20 stsp goto done;
5523 55cccc34 2020-02-20 stsp }
5524 a915003a 2019-02-05 stsp
5525 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5526 c02c541e 2019-03-29 stsp if (error != NULL)
5527 52185f70 2019-02-05 stsp goto done;
5528 d188b9a6 2019-01-04 stsp
5529 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5530 55cccc34 2020-02-20 stsp repo, worktree);
5531 55cccc34 2020-02-20 stsp if (error)
5532 55cccc34 2020-02-20 stsp goto done;
5533 55cccc34 2020-02-20 stsp
5534 55cccc34 2020-02-20 stsp init_curses();
5535 55cccc34 2020-02-20 stsp
5536 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5537 c02c541e 2019-03-29 stsp if (error)
5538 52185f70 2019-02-05 stsp goto done;
5539 ffd1d5e5 2018-06-23 stsp
5540 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5541 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5542 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5543 55cccc34 2020-02-20 stsp if (error)
5544 ffd1d5e5 2018-06-23 stsp goto done;
5545 ffd1d5e5 2018-06-23 stsp
5546 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5547 55cccc34 2020-02-20 stsp if (error)
5548 ffd1d5e5 2018-06-23 stsp goto done;
5549 ffd1d5e5 2018-06-23 stsp
5550 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5551 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5552 55cccc34 2020-02-20 stsp if (error)
5553 8b473291 2019-02-21 stsp goto done;
5554 8b473291 2019-02-21 stsp
5555 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5556 5221c383 2018-08-01 stsp if (view == NULL) {
5557 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5558 5221c383 2018-08-01 stsp goto done;
5559 5221c383 2018-08-01 stsp }
5560 78756c87 2020-11-24 stsp error = open_tree_view(view, tree, commit_id, repo);
5561 ad80ab7b 2018-08-04 stsp if (error)
5562 ad80ab7b 2018-08-04 stsp goto done;
5563 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5564 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5565 d91faf3b 2020-12-01 naddy in_repo_path);
5566 55cccc34 2020-02-20 stsp if (error)
5567 55cccc34 2020-02-20 stsp goto done;
5568 55cccc34 2020-02-20 stsp }
5569 55cccc34 2020-02-20 stsp
5570 55cccc34 2020-02-20 stsp if (worktree) {
5571 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5572 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5573 55cccc34 2020-02-20 stsp worktree = NULL;
5574 55cccc34 2020-02-20 stsp }
5575 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5576 ffd1d5e5 2018-06-23 stsp done:
5577 52185f70 2019-02-05 stsp free(repo_path);
5578 e4a0e26d 2020-02-20 stsp free(cwd);
5579 ffd1d5e5 2018-06-23 stsp free(commit_id);
5580 ffd1d5e5 2018-06-23 stsp if (commit)
5581 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5582 ffd1d5e5 2018-06-23 stsp if (tree)
5583 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5584 ffd1d5e5 2018-06-23 stsp if (repo)
5585 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5586 ffd1d5e5 2018-06-23 stsp return error;
5587 6458efa5 2020-11-24 stsp }
5588 6458efa5 2020-11-24 stsp
5589 6458efa5 2020-11-24 stsp static const struct got_error *
5590 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5591 6458efa5 2020-11-24 stsp {
5592 6458efa5 2020-11-24 stsp const struct got_error *err;
5593 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5594 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5595 6458efa5 2020-11-24 stsp
5596 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5597 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5598 6458efa5 2020-11-24 stsp if (err)
5599 6458efa5 2020-11-24 stsp return err;
5600 6458efa5 2020-11-24 stsp
5601 6458efa5 2020-11-24 stsp s->nrefs = 0;
5602 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5603 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5604 6458efa5 2020-11-24 stsp continue;
5605 6458efa5 2020-11-24 stsp
5606 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5607 6458efa5 2020-11-24 stsp if (re == NULL)
5608 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5609 6458efa5 2020-11-24 stsp
5610 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5611 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5612 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5613 6458efa5 2020-11-24 stsp }
5614 6458efa5 2020-11-24 stsp
5615 6458efa5 2020-11-24 stsp return NULL;
5616 6458efa5 2020-11-24 stsp }
5617 6458efa5 2020-11-24 stsp
5618 6458efa5 2020-11-24 stsp void
5619 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5620 6458efa5 2020-11-24 stsp {
5621 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5622 6458efa5 2020-11-24 stsp
5623 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5624 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5625 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5626 6458efa5 2020-11-24 stsp free(re);
5627 6458efa5 2020-11-24 stsp }
5628 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5629 6458efa5 2020-11-24 stsp }
5630 6458efa5 2020-11-24 stsp
5631 6458efa5 2020-11-24 stsp static const struct got_error *
5632 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5633 6458efa5 2020-11-24 stsp {
5634 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5635 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5636 6458efa5 2020-11-24 stsp
5637 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5638 6458efa5 2020-11-24 stsp s->repo = repo;
5639 6458efa5 2020-11-24 stsp
5640 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5641 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5642 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5643 6458efa5 2020-11-24 stsp
5644 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5645 6458efa5 2020-11-24 stsp if (err)
5646 6458efa5 2020-11-24 stsp return err;
5647 34ba6917 2020-11-30 stsp
5648 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5649 6458efa5 2020-11-24 stsp
5650 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5651 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5652 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5653 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5654 6458efa5 2020-11-24 stsp if (err)
5655 6458efa5 2020-11-24 stsp goto done;
5656 6458efa5 2020-11-24 stsp
5657 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5658 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5659 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5660 6458efa5 2020-11-24 stsp if (err)
5661 6458efa5 2020-11-24 stsp goto done;
5662 6458efa5 2020-11-24 stsp
5663 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5664 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5665 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5666 6458efa5 2020-11-24 stsp if (err)
5667 6458efa5 2020-11-24 stsp goto done;
5668 6458efa5 2020-11-24 stsp }
5669 6458efa5 2020-11-24 stsp
5670 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5671 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5672 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5673 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5674 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5675 6458efa5 2020-11-24 stsp done:
5676 6458efa5 2020-11-24 stsp if (err)
5677 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5678 6458efa5 2020-11-24 stsp return err;
5679 6458efa5 2020-11-24 stsp }
5680 6458efa5 2020-11-24 stsp
5681 6458efa5 2020-11-24 stsp static const struct got_error *
5682 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5683 6458efa5 2020-11-24 stsp {
5684 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5685 6458efa5 2020-11-24 stsp
5686 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5687 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5688 6458efa5 2020-11-24 stsp
5689 6458efa5 2020-11-24 stsp return NULL;
5690 9f7d7167 2018-04-29 stsp }
5691 ce5b7c56 2019-07-09 stsp
5692 6458efa5 2020-11-24 stsp static const struct got_error *
5693 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5694 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5695 6458efa5 2020-11-24 stsp {
5696 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5697 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5698 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5699 6458efa5 2020-11-24 stsp int obj_type;
5700 6458efa5 2020-11-24 stsp
5701 c42c9805 2020-11-24 stsp *commit_id = NULL;
5702 6458efa5 2020-11-24 stsp
5703 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5704 6458efa5 2020-11-24 stsp if (err)
5705 6458efa5 2020-11-24 stsp return err;
5706 6458efa5 2020-11-24 stsp
5707 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5708 6458efa5 2020-11-24 stsp if (err)
5709 6458efa5 2020-11-24 stsp goto done;
5710 6458efa5 2020-11-24 stsp
5711 6458efa5 2020-11-24 stsp switch (obj_type) {
5712 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5713 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5714 6458efa5 2020-11-24 stsp break;
5715 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5716 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5717 6458efa5 2020-11-24 stsp if (err)
5718 6458efa5 2020-11-24 stsp goto done;
5719 c42c9805 2020-11-24 stsp free(obj_id);
5720 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5721 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5722 c42c9805 2020-11-24 stsp if (err)
5723 6458efa5 2020-11-24 stsp goto done;
5724 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5725 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5726 c42c9805 2020-11-24 stsp goto done;
5727 c42c9805 2020-11-24 stsp }
5728 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5729 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5730 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5731 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5732 c42c9805 2020-11-24 stsp goto done;
5733 c42c9805 2020-11-24 stsp }
5734 6458efa5 2020-11-24 stsp break;
5735 6458efa5 2020-11-24 stsp default:
5736 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5737 c42c9805 2020-11-24 stsp break;
5738 6458efa5 2020-11-24 stsp }
5739 6458efa5 2020-11-24 stsp
5740 c42c9805 2020-11-24 stsp done:
5741 c42c9805 2020-11-24 stsp if (tag)
5742 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5743 c42c9805 2020-11-24 stsp if (err) {
5744 c42c9805 2020-11-24 stsp free(*commit_id);
5745 c42c9805 2020-11-24 stsp *commit_id = NULL;
5746 c42c9805 2020-11-24 stsp }
5747 c42c9805 2020-11-24 stsp return err;
5748 c42c9805 2020-11-24 stsp }
5749 c42c9805 2020-11-24 stsp
5750 c42c9805 2020-11-24 stsp static const struct got_error *
5751 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5752 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5753 c42c9805 2020-11-24 stsp {
5754 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5755 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5756 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5757 c42c9805 2020-11-24 stsp
5758 c42c9805 2020-11-24 stsp *new_view = NULL;
5759 c42c9805 2020-11-24 stsp
5760 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5761 c42c9805 2020-11-24 stsp if (err) {
5762 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5763 c42c9805 2020-11-24 stsp return err;
5764 c42c9805 2020-11-24 stsp else
5765 c42c9805 2020-11-24 stsp return NULL;
5766 c42c9805 2020-11-24 stsp }
5767 c42c9805 2020-11-24 stsp
5768 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5769 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5770 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5771 6458efa5 2020-11-24 stsp goto done;
5772 6458efa5 2020-11-24 stsp }
5773 6458efa5 2020-11-24 stsp
5774 6458efa5 2020-11-24 stsp err = open_log_view(log_view, commit_id, repo, NULL, "", 0);
5775 6458efa5 2020-11-24 stsp done:
5776 6458efa5 2020-11-24 stsp if (err)
5777 6458efa5 2020-11-24 stsp view_close(log_view);
5778 6458efa5 2020-11-24 stsp else
5779 6458efa5 2020-11-24 stsp *new_view = log_view;
5780 c42c9805 2020-11-24 stsp free(commit_id);
5781 6458efa5 2020-11-24 stsp return err;
5782 6458efa5 2020-11-24 stsp }
5783 6458efa5 2020-11-24 stsp
5784 ce5b7c56 2019-07-09 stsp static void
5785 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5786 6458efa5 2020-11-24 stsp {
5787 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5788 34ba6917 2020-11-30 stsp int i = 0;
5789 6458efa5 2020-11-24 stsp
5790 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5791 6458efa5 2020-11-24 stsp return;
5792 6458efa5 2020-11-24 stsp
5793 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5794 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5795 34ba6917 2020-11-30 stsp if (re == NULL)
5796 34ba6917 2020-11-30 stsp break;
5797 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5798 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5799 6458efa5 2020-11-24 stsp }
5800 6458efa5 2020-11-24 stsp }
5801 6458efa5 2020-11-24 stsp
5802 34ba6917 2020-11-30 stsp static void
5803 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5804 6458efa5 2020-11-24 stsp {
5805 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5806 6458efa5 2020-11-24 stsp int n = 0;
5807 6458efa5 2020-11-24 stsp
5808 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5809 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5810 6458efa5 2020-11-24 stsp else
5811 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5812 6458efa5 2020-11-24 stsp
5813 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5814 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5815 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5816 6458efa5 2020-11-24 stsp if (last) {
5817 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5818 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5819 6458efa5 2020-11-24 stsp }
5820 6458efa5 2020-11-24 stsp }
5821 6458efa5 2020-11-24 stsp }
5822 6458efa5 2020-11-24 stsp
5823 6458efa5 2020-11-24 stsp static const struct got_error *
5824 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5825 6458efa5 2020-11-24 stsp {
5826 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5827 6458efa5 2020-11-24 stsp
5828 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5829 6458efa5 2020-11-24 stsp return NULL;
5830 6458efa5 2020-11-24 stsp }
5831 6458efa5 2020-11-24 stsp
5832 6458efa5 2020-11-24 stsp static int
5833 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5834 6458efa5 2020-11-24 stsp {
5835 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5836 6458efa5 2020-11-24 stsp
5837 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5838 6458efa5 2020-11-24 stsp 0) == 0;
5839 6458efa5 2020-11-24 stsp }
5840 6458efa5 2020-11-24 stsp
5841 6458efa5 2020-11-24 stsp static const struct got_error *
5842 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5843 6458efa5 2020-11-24 stsp {
5844 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5845 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5846 6458efa5 2020-11-24 stsp
5847 6458efa5 2020-11-24 stsp if (!view->searching) {
5848 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5849 6458efa5 2020-11-24 stsp return NULL;
5850 6458efa5 2020-11-24 stsp }
5851 6458efa5 2020-11-24 stsp
5852 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5853 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5854 6458efa5 2020-11-24 stsp if (s->selected_entry)
5855 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5856 6458efa5 2020-11-24 stsp else
5857 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5858 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5859 6458efa5 2020-11-24 stsp } else {
5860 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5861 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5862 6458efa5 2020-11-24 stsp else
5863 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5864 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5865 6458efa5 2020-11-24 stsp }
5866 6458efa5 2020-11-24 stsp } else {
5867 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5868 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5869 6458efa5 2020-11-24 stsp else
5870 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5871 6458efa5 2020-11-24 stsp }
5872 6458efa5 2020-11-24 stsp
5873 6458efa5 2020-11-24 stsp while (1) {
5874 6458efa5 2020-11-24 stsp if (re == NULL) {
5875 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5876 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5877 6458efa5 2020-11-24 stsp return NULL;
5878 6458efa5 2020-11-24 stsp }
5879 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5880 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5881 6458efa5 2020-11-24 stsp else
5882 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5883 6458efa5 2020-11-24 stsp }
5884 6458efa5 2020-11-24 stsp
5885 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5886 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5887 6458efa5 2020-11-24 stsp s->matched_entry = re;
5888 6458efa5 2020-11-24 stsp break;
5889 6458efa5 2020-11-24 stsp }
5890 6458efa5 2020-11-24 stsp
5891 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5892 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5893 6458efa5 2020-11-24 stsp else
5894 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5895 6458efa5 2020-11-24 stsp }
5896 6458efa5 2020-11-24 stsp
5897 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5898 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5899 6458efa5 2020-11-24 stsp s->selected = 0;
5900 6458efa5 2020-11-24 stsp }
5901 6458efa5 2020-11-24 stsp
5902 6458efa5 2020-11-24 stsp return NULL;
5903 6458efa5 2020-11-24 stsp }
5904 6458efa5 2020-11-24 stsp
5905 6458efa5 2020-11-24 stsp static const struct got_error *
5906 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5907 6458efa5 2020-11-24 stsp {
5908 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5909 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5910 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5911 6458efa5 2020-11-24 stsp char *line = NULL;
5912 6458efa5 2020-11-24 stsp wchar_t *wline;
5913 6458efa5 2020-11-24 stsp struct tog_color *tc;
5914 6458efa5 2020-11-24 stsp int width, n;
5915 6458efa5 2020-11-24 stsp int limit = view->nlines;
5916 6458efa5 2020-11-24 stsp
5917 6458efa5 2020-11-24 stsp werase(view->window);
5918 6458efa5 2020-11-24 stsp
5919 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5920 6458efa5 2020-11-24 stsp
5921 6458efa5 2020-11-24 stsp if (limit == 0)
5922 6458efa5 2020-11-24 stsp return NULL;
5923 6458efa5 2020-11-24 stsp
5924 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5925 6458efa5 2020-11-24 stsp
5926 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5927 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5928 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5929 6458efa5 2020-11-24 stsp
5930 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5931 6458efa5 2020-11-24 stsp if (err) {
5932 6458efa5 2020-11-24 stsp free(line);
5933 6458efa5 2020-11-24 stsp return err;
5934 6458efa5 2020-11-24 stsp }
5935 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5936 6458efa5 2020-11-24 stsp wstandout(view->window);
5937 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5938 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5939 6458efa5 2020-11-24 stsp wstandend(view->window);
5940 6458efa5 2020-11-24 stsp free(wline);
5941 6458efa5 2020-11-24 stsp wline = NULL;
5942 6458efa5 2020-11-24 stsp free(line);
5943 6458efa5 2020-11-24 stsp line = NULL;
5944 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5945 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5946 6458efa5 2020-11-24 stsp if (--limit <= 0)
5947 6458efa5 2020-11-24 stsp return NULL;
5948 6458efa5 2020-11-24 stsp
5949 6458efa5 2020-11-24 stsp n = 0;
5950 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5951 6458efa5 2020-11-24 stsp char *line = NULL;
5952 6458efa5 2020-11-24 stsp
5953 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5954 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5955 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5956 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5957 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5958 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5959 6458efa5 2020-11-24 stsp struct got_object_id *id;
5960 6458efa5 2020-11-24 stsp char *id_str;
5961 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5962 6458efa5 2020-11-24 stsp if (err)
5963 6458efa5 2020-11-24 stsp return err;
5964 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5965 6458efa5 2020-11-24 stsp if (err) {
5966 6458efa5 2020-11-24 stsp free(id);
5967 6458efa5 2020-11-24 stsp return err;
5968 6458efa5 2020-11-24 stsp }
5969 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5970 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5971 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5972 6458efa5 2020-11-24 stsp free(id);
5973 6458efa5 2020-11-24 stsp free(id_str);
5974 6458efa5 2020-11-24 stsp return err;
5975 6458efa5 2020-11-24 stsp }
5976 6458efa5 2020-11-24 stsp free(id);
5977 6458efa5 2020-11-24 stsp free(id_str);
5978 6458efa5 2020-11-24 stsp } else {
5979 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
5980 6458efa5 2020-11-24 stsp if (line == NULL)
5981 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
5982 6458efa5 2020-11-24 stsp }
5983 6458efa5 2020-11-24 stsp
5984 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5985 6458efa5 2020-11-24 stsp if (err) {
5986 6458efa5 2020-11-24 stsp free(line);
5987 6458efa5 2020-11-24 stsp return err;
5988 6458efa5 2020-11-24 stsp }
5989 6458efa5 2020-11-24 stsp if (n == s->selected) {
5990 6458efa5 2020-11-24 stsp if (view->focussed)
5991 6458efa5 2020-11-24 stsp wstandout(view->window);
5992 6458efa5 2020-11-24 stsp s->selected_entry = re;
5993 6458efa5 2020-11-24 stsp }
5994 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
5995 6458efa5 2020-11-24 stsp if (tc)
5996 6458efa5 2020-11-24 stsp wattr_on(view->window,
5997 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
5998 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5999 6458efa5 2020-11-24 stsp if (tc)
6000 6458efa5 2020-11-24 stsp wattr_off(view->window,
6001 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6002 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6003 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6004 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6005 6458efa5 2020-11-24 stsp wstandend(view->window);
6006 6458efa5 2020-11-24 stsp free(line);
6007 6458efa5 2020-11-24 stsp free(wline);
6008 6458efa5 2020-11-24 stsp wline = NULL;
6009 6458efa5 2020-11-24 stsp n++;
6010 6458efa5 2020-11-24 stsp s->ndisplayed++;
6011 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6012 6458efa5 2020-11-24 stsp
6013 6458efa5 2020-11-24 stsp limit--;
6014 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6015 6458efa5 2020-11-24 stsp }
6016 6458efa5 2020-11-24 stsp
6017 6458efa5 2020-11-24 stsp view_vborder(view);
6018 6458efa5 2020-11-24 stsp return err;
6019 6458efa5 2020-11-24 stsp }
6020 6458efa5 2020-11-24 stsp
6021 6458efa5 2020-11-24 stsp static const struct got_error *
6022 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6023 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6024 c42c9805 2020-11-24 stsp {
6025 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6026 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6027 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6028 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6029 c42c9805 2020-11-24 stsp
6030 c42c9805 2020-11-24 stsp *new_view = NULL;
6031 c42c9805 2020-11-24 stsp
6032 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6033 c42c9805 2020-11-24 stsp if (err) {
6034 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6035 c42c9805 2020-11-24 stsp return err;
6036 c42c9805 2020-11-24 stsp else
6037 c42c9805 2020-11-24 stsp return NULL;
6038 c42c9805 2020-11-24 stsp }
6039 c42c9805 2020-11-24 stsp
6040 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6041 c42c9805 2020-11-24 stsp if (err)
6042 c42c9805 2020-11-24 stsp goto done;
6043 c42c9805 2020-11-24 stsp
6044 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6045 c42c9805 2020-11-24 stsp if (err)
6046 c42c9805 2020-11-24 stsp goto done;
6047 c42c9805 2020-11-24 stsp
6048 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6049 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6050 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6051 c42c9805 2020-11-24 stsp goto done;
6052 c42c9805 2020-11-24 stsp }
6053 c42c9805 2020-11-24 stsp
6054 c42c9805 2020-11-24 stsp err = open_tree_view(tree_view, tree, commit_id, repo);
6055 c42c9805 2020-11-24 stsp if (err)
6056 c42c9805 2020-11-24 stsp goto done;
6057 c42c9805 2020-11-24 stsp
6058 c42c9805 2020-11-24 stsp *new_view = tree_view;
6059 c42c9805 2020-11-24 stsp done:
6060 c42c9805 2020-11-24 stsp free(commit_id);
6061 c42c9805 2020-11-24 stsp free(tree_id);
6062 c42c9805 2020-11-24 stsp if (err) {
6063 c42c9805 2020-11-24 stsp if (tree)
6064 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6065 c42c9805 2020-11-24 stsp }
6066 c42c9805 2020-11-24 stsp return err;
6067 c42c9805 2020-11-24 stsp }
6068 c42c9805 2020-11-24 stsp static const struct got_error *
6069 6458efa5 2020-11-24 stsp input_ref_view(struct tog_view **new_view, struct tog_view **dead_view,
6070 6458efa5 2020-11-24 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
6071 6458efa5 2020-11-24 stsp {
6072 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6073 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6074 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6075 34ba6917 2020-11-30 stsp int begin_x = 0;
6076 6458efa5 2020-11-24 stsp
6077 6458efa5 2020-11-24 stsp switch (ch) {
6078 6458efa5 2020-11-24 stsp case 'i':
6079 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6080 6458efa5 2020-11-24 stsp break;
6081 6458efa5 2020-11-24 stsp case KEY_ENTER:
6082 6458efa5 2020-11-24 stsp case '\r':
6083 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6084 6458efa5 2020-11-24 stsp break;
6085 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6086 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6087 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6088 6458efa5 2020-11-24 stsp s->repo);
6089 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6090 6458efa5 2020-11-24 stsp err = view_close_child(view);
6091 6458efa5 2020-11-24 stsp if (err)
6092 6458efa5 2020-11-24 stsp return err;
6093 6458efa5 2020-11-24 stsp err = view_set_child(view, log_view);
6094 6458efa5 2020-11-24 stsp if (err) {
6095 6458efa5 2020-11-24 stsp view_close(log_view);
6096 6458efa5 2020-11-24 stsp break;
6097 6458efa5 2020-11-24 stsp }
6098 6458efa5 2020-11-24 stsp *focus_view = log_view;
6099 6458efa5 2020-11-24 stsp } else
6100 6458efa5 2020-11-24 stsp *new_view = log_view;
6101 6458efa5 2020-11-24 stsp break;
6102 c42c9805 2020-11-24 stsp case 't':
6103 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6104 c42c9805 2020-11-24 stsp break;
6105 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6106 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6107 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6108 c42c9805 2020-11-24 stsp s->repo);
6109 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6110 c42c9805 2020-11-24 stsp break;
6111 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6112 c42c9805 2020-11-24 stsp err = view_close_child(view);
6113 c42c9805 2020-11-24 stsp if (err)
6114 c42c9805 2020-11-24 stsp return err;
6115 c42c9805 2020-11-24 stsp err = view_set_child(view, tree_view);
6116 c42c9805 2020-11-24 stsp if (err) {
6117 c42c9805 2020-11-24 stsp view_close(tree_view);
6118 c42c9805 2020-11-24 stsp break;
6119 c42c9805 2020-11-24 stsp }
6120 c42c9805 2020-11-24 stsp *focus_view = tree_view;
6121 c42c9805 2020-11-24 stsp } else
6122 c42c9805 2020-11-24 stsp *new_view = tree_view;
6123 c42c9805 2020-11-24 stsp break;
6124 6458efa5 2020-11-24 stsp case 'k':
6125 6458efa5 2020-11-24 stsp case KEY_UP:
6126 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6127 6458efa5 2020-11-24 stsp s->selected--;
6128 6458efa5 2020-11-24 stsp break;
6129 34ba6917 2020-11-30 stsp }
6130 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6131 6458efa5 2020-11-24 stsp break;
6132 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6133 6458efa5 2020-11-24 stsp case CTRL('b'):
6134 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6135 34ba6917 2020-11-30 stsp s->selected = 0;
6136 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6137 6458efa5 2020-11-24 stsp break;
6138 6458efa5 2020-11-24 stsp case 'j':
6139 6458efa5 2020-11-24 stsp case KEY_DOWN:
6140 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6141 6458efa5 2020-11-24 stsp s->selected++;
6142 6458efa5 2020-11-24 stsp break;
6143 6458efa5 2020-11-24 stsp }
6144 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6145 6458efa5 2020-11-24 stsp /* can't scroll any further */
6146 6458efa5 2020-11-24 stsp break;
6147 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6148 6458efa5 2020-11-24 stsp break;
6149 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6150 6458efa5 2020-11-24 stsp case CTRL('f'):
6151 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6152 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6153 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6154 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6155 6458efa5 2020-11-24 stsp break;
6156 6458efa5 2020-11-24 stsp }
6157 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6158 6458efa5 2020-11-24 stsp break;
6159 6458efa5 2020-11-24 stsp case CTRL('l'):
6160 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6161 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6162 6458efa5 2020-11-24 stsp break;
6163 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6164 6458efa5 2020-11-24 stsp if (s->selected > view->nlines)
6165 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6166 6458efa5 2020-11-24 stsp break;
6167 6458efa5 2020-11-24 stsp default:
6168 6458efa5 2020-11-24 stsp break;
6169 6458efa5 2020-11-24 stsp }
6170 6458efa5 2020-11-24 stsp
6171 6458efa5 2020-11-24 stsp return err;
6172 6458efa5 2020-11-24 stsp }
6173 6458efa5 2020-11-24 stsp
6174 6458efa5 2020-11-24 stsp __dead static void
6175 6458efa5 2020-11-24 stsp usage_ref(void)
6176 6458efa5 2020-11-24 stsp {
6177 6458efa5 2020-11-24 stsp endwin();
6178 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6179 6458efa5 2020-11-24 stsp getprogname());
6180 6458efa5 2020-11-24 stsp exit(1);
6181 6458efa5 2020-11-24 stsp }
6182 6458efa5 2020-11-24 stsp
6183 6458efa5 2020-11-24 stsp static const struct got_error *
6184 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6185 6458efa5 2020-11-24 stsp {
6186 6458efa5 2020-11-24 stsp const struct got_error *error;
6187 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6188 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6189 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6190 6458efa5 2020-11-24 stsp int ch;
6191 6458efa5 2020-11-24 stsp struct tog_view *view;
6192 6458efa5 2020-11-24 stsp
6193 6458efa5 2020-11-24 stsp #ifndef PROFILE
6194 6458efa5 2020-11-24 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6195 6458efa5 2020-11-24 stsp NULL) == -1)
6196 6458efa5 2020-11-24 stsp err(1, "pledge");
6197 6458efa5 2020-11-24 stsp #endif
6198 6458efa5 2020-11-24 stsp
6199 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6200 6458efa5 2020-11-24 stsp switch (ch) {
6201 6458efa5 2020-11-24 stsp case 'r':
6202 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6203 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6204 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6205 6458efa5 2020-11-24 stsp optarg);
6206 6458efa5 2020-11-24 stsp break;
6207 6458efa5 2020-11-24 stsp default:
6208 e99e2d15 2020-11-24 naddy usage_ref();
6209 6458efa5 2020-11-24 stsp /* NOTREACHED */
6210 6458efa5 2020-11-24 stsp }
6211 6458efa5 2020-11-24 stsp }
6212 6458efa5 2020-11-24 stsp
6213 6458efa5 2020-11-24 stsp argc -= optind;
6214 6458efa5 2020-11-24 stsp argv += optind;
6215 6458efa5 2020-11-24 stsp
6216 6458efa5 2020-11-24 stsp if (argc > 1)
6217 e99e2d15 2020-11-24 naddy usage_ref();
6218 6458efa5 2020-11-24 stsp
6219 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6220 6458efa5 2020-11-24 stsp if (cwd == NULL)
6221 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6222 6458efa5 2020-11-24 stsp
6223 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6224 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6225 6458efa5 2020-11-24 stsp goto done;
6226 6458efa5 2020-11-24 stsp
6227 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6228 6458efa5 2020-11-24 stsp if (worktree)
6229 6458efa5 2020-11-24 stsp repo_path =
6230 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6231 6458efa5 2020-11-24 stsp else
6232 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6233 6458efa5 2020-11-24 stsp }
6234 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6235 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6236 6458efa5 2020-11-24 stsp goto done;
6237 6458efa5 2020-11-24 stsp }
6238 6458efa5 2020-11-24 stsp
6239 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6240 6458efa5 2020-11-24 stsp if (error != NULL)
6241 6458efa5 2020-11-24 stsp goto done;
6242 6458efa5 2020-11-24 stsp
6243 6458efa5 2020-11-24 stsp init_curses();
6244 6458efa5 2020-11-24 stsp
6245 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6246 6458efa5 2020-11-24 stsp if (error)
6247 6458efa5 2020-11-24 stsp goto done;
6248 6458efa5 2020-11-24 stsp
6249 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6250 6458efa5 2020-11-24 stsp if (view == NULL) {
6251 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6252 6458efa5 2020-11-24 stsp goto done;
6253 6458efa5 2020-11-24 stsp }
6254 6458efa5 2020-11-24 stsp
6255 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6256 6458efa5 2020-11-24 stsp if (error)
6257 6458efa5 2020-11-24 stsp goto done;
6258 6458efa5 2020-11-24 stsp
6259 6458efa5 2020-11-24 stsp if (worktree) {
6260 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6261 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6262 6458efa5 2020-11-24 stsp worktree = NULL;
6263 6458efa5 2020-11-24 stsp }
6264 6458efa5 2020-11-24 stsp error = view_loop(view);
6265 6458efa5 2020-11-24 stsp done:
6266 6458efa5 2020-11-24 stsp free(repo_path);
6267 6458efa5 2020-11-24 stsp free(cwd);
6268 6458efa5 2020-11-24 stsp if (repo)
6269 6458efa5 2020-11-24 stsp got_repo_close(repo);
6270 6458efa5 2020-11-24 stsp return error;
6271 6458efa5 2020-11-24 stsp }
6272 6458efa5 2020-11-24 stsp
6273 6458efa5 2020-11-24 stsp static void
6274 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6275 ce5b7c56 2019-07-09 stsp {
6276 ce5b7c56 2019-07-09 stsp int i;
6277 9f7d7167 2018-04-29 stsp
6278 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6279 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6280 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6281 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6282 ce5b7c56 2019-07-09 stsp }
6283 6879ba42 2020-10-01 naddy fputc('\n', fp);
6284 ce5b7c56 2019-07-09 stsp }
6285 ce5b7c56 2019-07-09 stsp
6286 4ed7e80c 2018-05-20 stsp __dead static void
6287 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6288 9f7d7167 2018-04-29 stsp {
6289 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6290 6879ba42 2020-10-01 naddy
6291 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6292 6879ba42 2020-10-01 naddy getprogname());
6293 6879ba42 2020-10-01 naddy if (hflag) {
6294 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6295 6879ba42 2020-10-01 naddy list_commands(fp);
6296 ee85c5e8 2020-02-29 stsp }
6297 6879ba42 2020-10-01 naddy exit(status);
6298 9f7d7167 2018-04-29 stsp }
6299 9f7d7167 2018-04-29 stsp
6300 c2301be8 2018-04-30 stsp static char **
6301 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6302 c2301be8 2018-04-30 stsp {
6303 ee85c5e8 2020-02-29 stsp va_list ap;
6304 c2301be8 2018-04-30 stsp char **argv;
6305 ee85c5e8 2020-02-29 stsp int i;
6306 c2301be8 2018-04-30 stsp
6307 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6308 ee85c5e8 2020-02-29 stsp
6309 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6310 c2301be8 2018-04-30 stsp if (argv == NULL)
6311 c2301be8 2018-04-30 stsp err(1, "calloc");
6312 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6313 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6314 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6315 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6316 c2301be8 2018-04-30 stsp }
6317 c2301be8 2018-04-30 stsp
6318 ee85c5e8 2020-02-29 stsp va_end(ap);
6319 c2301be8 2018-04-30 stsp return argv;
6320 ee85c5e8 2020-02-29 stsp }
6321 ee85c5e8 2020-02-29 stsp
6322 ee85c5e8 2020-02-29 stsp /*
6323 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6324 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6325 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6326 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6327 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6328 ee85c5e8 2020-02-29 stsp */
6329 ee85c5e8 2020-02-29 stsp static const struct got_error *
6330 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6331 ee85c5e8 2020-02-29 stsp {
6332 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6333 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6334 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6335 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6336 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6337 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6338 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6339 ee85c5e8 2020-02-29 stsp
6340 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6341 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6342 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6343 ee85c5e8 2020-02-29 stsp
6344 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6345 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6346 ee85c5e8 2020-02-29 stsp goto done;
6347 ee85c5e8 2020-02-29 stsp
6348 ee85c5e8 2020-02-29 stsp if (worktree)
6349 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6350 ee85c5e8 2020-02-29 stsp else
6351 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6352 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6353 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6354 ee85c5e8 2020-02-29 stsp goto done;
6355 ee85c5e8 2020-02-29 stsp }
6356 ee85c5e8 2020-02-29 stsp
6357 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6358 ee85c5e8 2020-02-29 stsp if (error != NULL)
6359 ee85c5e8 2020-02-29 stsp goto done;
6360 ee85c5e8 2020-02-29 stsp
6361 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6362 ee85c5e8 2020-02-29 stsp repo, worktree);
6363 ee85c5e8 2020-02-29 stsp if (error)
6364 ee85c5e8 2020-02-29 stsp goto done;
6365 ee85c5e8 2020-02-29 stsp
6366 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6367 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6368 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6369 ee85c5e8 2020-02-29 stsp if (error)
6370 ee85c5e8 2020-02-29 stsp goto done;
6371 ee85c5e8 2020-02-29 stsp
6372 ee85c5e8 2020-02-29 stsp if (worktree) {
6373 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6374 ee85c5e8 2020-02-29 stsp worktree = NULL;
6375 ee85c5e8 2020-02-29 stsp }
6376 ee85c5e8 2020-02-29 stsp
6377 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6378 ee85c5e8 2020-02-29 stsp if (error) {
6379 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6380 ee85c5e8 2020-02-29 stsp goto done;
6381 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6382 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6383 6879ba42 2020-10-01 naddy usage(1, 1);
6384 ee85c5e8 2020-02-29 stsp /* not reached */
6385 ee85c5e8 2020-02-29 stsp }
6386 ee85c5e8 2020-02-29 stsp
6387 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6388 ee85c5e8 2020-02-29 stsp repo = NULL;
6389 ee85c5e8 2020-02-29 stsp
6390 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6391 ee85c5e8 2020-02-29 stsp if (error)
6392 ee85c5e8 2020-02-29 stsp goto done;
6393 ee85c5e8 2020-02-29 stsp
6394 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6395 ee85c5e8 2020-02-29 stsp argc = 4;
6396 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6397 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6398 ee85c5e8 2020-02-29 stsp done:
6399 ee85c5e8 2020-02-29 stsp if (repo)
6400 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6401 ee85c5e8 2020-02-29 stsp if (worktree)
6402 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6403 ee85c5e8 2020-02-29 stsp free(id);
6404 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6405 ee85c5e8 2020-02-29 stsp free(commit_id);
6406 ee85c5e8 2020-02-29 stsp free(cwd);
6407 ee85c5e8 2020-02-29 stsp free(repo_path);
6408 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6409 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6410 ee85c5e8 2020-02-29 stsp int i;
6411 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6412 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6413 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6414 ee85c5e8 2020-02-29 stsp }
6415 ee85c5e8 2020-02-29 stsp return error;
6416 c2301be8 2018-04-30 stsp }
6417 c2301be8 2018-04-30 stsp
6418 9f7d7167 2018-04-29 stsp int
6419 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6420 9f7d7167 2018-04-29 stsp {
6421 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6422 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6423 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6424 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6425 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6426 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6427 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6428 83cd27f8 2020-01-13 stsp };
6429 9f7d7167 2018-04-29 stsp
6430 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6431 9f7d7167 2018-04-29 stsp
6432 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6433 9f7d7167 2018-04-29 stsp switch (ch) {
6434 9f7d7167 2018-04-29 stsp case 'h':
6435 9f7d7167 2018-04-29 stsp hflag = 1;
6436 9f7d7167 2018-04-29 stsp break;
6437 53ccebc2 2019-07-30 stsp case 'V':
6438 53ccebc2 2019-07-30 stsp Vflag = 1;
6439 53ccebc2 2019-07-30 stsp break;
6440 9f7d7167 2018-04-29 stsp default:
6441 6879ba42 2020-10-01 naddy usage(hflag, 1);
6442 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6443 9f7d7167 2018-04-29 stsp }
6444 9f7d7167 2018-04-29 stsp }
6445 9f7d7167 2018-04-29 stsp
6446 9f7d7167 2018-04-29 stsp argc -= optind;
6447 9f7d7167 2018-04-29 stsp argv += optind;
6448 9814e6a3 2020-09-27 naddy optind = 1;
6449 c2301be8 2018-04-30 stsp optreset = 1;
6450 9f7d7167 2018-04-29 stsp
6451 53ccebc2 2019-07-30 stsp if (Vflag) {
6452 53ccebc2 2019-07-30 stsp got_version_print_str();
6453 6879ba42 2020-10-01 naddy return 0;
6454 53ccebc2 2019-07-30 stsp }
6455 53ccebc2 2019-07-30 stsp
6456 c2301be8 2018-04-30 stsp if (argc == 0) {
6457 f29d3e89 2018-06-23 stsp if (hflag)
6458 6879ba42 2020-10-01 naddy usage(hflag, 0);
6459 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6460 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6461 c2301be8 2018-04-30 stsp argc = 1;
6462 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6463 c2301be8 2018-04-30 stsp } else {
6464 9f7d7167 2018-04-29 stsp int i;
6465 9f7d7167 2018-04-29 stsp
6466 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6467 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6468 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6469 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6470 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6471 9f7d7167 2018-04-29 stsp break;
6472 9f7d7167 2018-04-29 stsp }
6473 9f7d7167 2018-04-29 stsp }
6474 ee85c5e8 2020-02-29 stsp }
6475 3642c4c6 2019-07-09 stsp
6476 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6477 ee85c5e8 2020-02-29 stsp if (argc != 1)
6478 6879ba42 2020-10-01 naddy usage(0, 1);
6479 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6480 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6481 ee85c5e8 2020-02-29 stsp } else {
6482 ee85c5e8 2020-02-29 stsp if (hflag)
6483 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6484 ee85c5e8 2020-02-29 stsp else
6485 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6486 9f7d7167 2018-04-29 stsp }
6487 9f7d7167 2018-04-29 stsp
6488 9f7d7167 2018-04-29 stsp endwin();
6489 b46c1e04 2020-09-20 naddy putchar('\n');
6490 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6491 a2f4a359 2020-02-28 stsp int i;
6492 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6493 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6494 a2f4a359 2020-02-28 stsp free(cmd_argv);
6495 a2f4a359 2020-02-28 stsp }
6496 a2f4a359 2020-02-28 stsp
6497 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6498 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6499 9f7d7167 2018-04-29 stsp return 0;
6500 9f7d7167 2018-04-29 stsp }