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