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