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