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