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