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 1af5eddf 2022-06-23 thomas int *pack_fds;
349 1a76625f 2018-10-22 stsp int log_complete;
350 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
352 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
353 13add988 2019-10-15 stsp int *searching;
354 13add988 2019-10-15 stsp int *search_next_done;
355 13add988 2019-10-15 stsp regex_t *regex;
356 1a76625f 2018-10-22 stsp };
357 1a76625f 2018-10-22 stsp
358 1a76625f 2018-10-22 stsp struct tog_log_view_state {
359 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
363 b01e7d3b 2018-08-04 stsp int selected;
364 b01e7d3b 2018-08-04 stsp char *in_repo_path;
365 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
366 b672a97a 2020-01-27 stsp int log_branches;
367 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
368 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
369 1a76625f 2018-10-22 stsp sig_atomic_t quit;
370 1a76625f 2018-10-22 stsp pthread_t thread;
371 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
372 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
373 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
374 11b20872 2019-11-08 stsp struct tog_colors colors;
375 ba4f502b 2018-08-04 stsp };
376 11b20872 2019-11-08 stsp
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
385 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
386 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
387 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
391 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
392 ba4f502b 2018-08-04 stsp
393 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
394 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
395 e9424729 2018-08-04 stsp int nlines;
396 e9424729 2018-08-04 stsp
397 e9424729 2018-08-04 stsp struct tog_view *view;
398 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
399 e9424729 2018-08-04 stsp int *quit;
400 e9424729 2018-08-04 stsp };
401 e9424729 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
403 e9424729 2018-08-04 stsp const char *path;
404 e9424729 2018-08-04 stsp struct got_repository *repo;
405 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
406 e9424729 2018-08-04 stsp int *complete;
407 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
408 fc06ba56 2019-08-22 stsp void *cancel_arg;
409 e9424729 2018-08-04 stsp };
410 e9424729 2018-08-04 stsp
411 e9424729 2018-08-04 stsp struct tog_blame {
412 e9424729 2018-08-04 stsp FILE *f;
413 be659d10 2020-11-18 stsp off_t filesize;
414 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
415 6fcac457 2018-11-19 stsp int nlines;
416 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
417 e9424729 2018-08-04 stsp pthread_t thread;
418 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
420 e9424729 2018-08-04 stsp const char *path;
421 7cd52833 2022-06-23 thomas int *pack_fds;
422 e9424729 2018-08-04 stsp };
423 e9424729 2018-08-04 stsp
424 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
425 7cbe629d 2018-08-04 stsp int first_displayed_line;
426 7cbe629d 2018-08-04 stsp int last_displayed_line;
427 7cbe629d 2018-08-04 stsp int selected_line;
428 7cbe629d 2018-08-04 stsp int blame_complete;
429 e5a0f69f 2018-08-18 stsp int eof;
430 e5a0f69f 2018-08-18 stsp int done;
431 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
432 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
433 e5a0f69f 2018-08-18 stsp char *path;
434 7cbe629d 2018-08-04 stsp struct got_repository *repo;
435 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
436 e9424729 2018-08-04 stsp struct tog_blame blame;
437 6c4c42e0 2019-06-24 stsp int matched_line;
438 11b20872 2019-11-08 stsp struct tog_colors colors;
439 ad80ab7b 2018-08-04 stsp };
440 ad80ab7b 2018-08-04 stsp
441 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
442 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
444 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
446 ad80ab7b 2018-08-04 stsp int selected;
447 ad80ab7b 2018-08-04 stsp };
448 ad80ab7b 2018-08-04 stsp
449 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
450 ad80ab7b 2018-08-04 stsp
451 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
452 ad80ab7b 2018-08-04 stsp char *tree_label;
453 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
454 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
455 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
459 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
460 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
461 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
462 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
463 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
464 6458efa5 2020-11-24 stsp struct tog_colors colors;
465 6458efa5 2020-11-24 stsp };
466 6458efa5 2020-11-24 stsp
467 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
468 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
469 6458efa5 2020-11-24 stsp struct got_reference *ref;
470 6458efa5 2020-11-24 stsp int idx;
471 6458efa5 2020-11-24 stsp };
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
474 6458efa5 2020-11-24 stsp
475 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
476 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
480 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 6458efa5 2020-11-24 stsp struct got_repository *repo;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
483 bddb1296 2019-11-08 stsp struct tog_colors colors;
484 7cbe629d 2018-08-04 stsp };
485 7cbe629d 2018-08-04 stsp
486 669b5ffa 2018-10-07 stsp /*
487 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
488 669b5ffa 2018-10-07 stsp *
489 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
490 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
491 669b5ffa 2018-10-07 stsp * there is enough screen estate.
492 669b5ffa 2018-10-07 stsp *
493 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
494 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
495 669b5ffa 2018-10-07 stsp *
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
498 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
499 669b5ffa 2018-10-07 stsp *
500 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
501 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
502 669b5ffa 2018-10-07 stsp */
503 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
504 669b5ffa 2018-10-07 stsp
505 cc3c9aac 2018-08-01 stsp struct tog_view {
506 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
507 26ed57b2 2018-05-19 stsp WINDOW *window;
508 26ed57b2 2018-05-19 stsp PANEL *panel;
509 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
510 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
511 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
512 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
513 9970f7fc 2020-12-03 stsp int dying;
514 669b5ffa 2018-10-07 stsp struct tog_view *parent;
515 669b5ffa 2018-10-07 stsp struct tog_view *child;
516 5dc9f4bc 2018-08-04 stsp
517 e78dc838 2020-12-04 stsp /*
518 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
519 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
520 e78dc838 2020-12-04 stsp * between parent and child.
521 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
522 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
523 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
524 e78dc838 2020-12-04 stsp * situations.
525 e78dc838 2020-12-04 stsp */
526 e78dc838 2020-12-04 stsp int focus_child;
527 e78dc838 2020-12-04 stsp
528 5dc9f4bc 2018-08-04 stsp /* type-specific state */
529 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
530 5dc9f4bc 2018-08-04 stsp union {
531 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
532 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
533 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
534 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
535 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
536 5dc9f4bc 2018-08-04 stsp } state;
537 e5a0f69f 2018-08-18 stsp
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
539 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
540 e78dc838 2020-12-04 stsp struct tog_view *, int);
541 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
542 60493ae3 2019-06-20 stsp
543 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
544 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
545 c0c4acc8 2021-01-24 stsp int search_started;
546 60493ae3 2019-06-20 stsp int searching;
547 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
548 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
549 60493ae3 2019-06-20 stsp int search_next_done;
550 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
551 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
552 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
553 1803e47f 2019-06-22 stsp regex_t regex;
554 41605754 2020-11-12 stsp regmatch_t regmatch;
555 cc3c9aac 2018-08-01 stsp };
556 cd0acaa7 2018-05-20 stsp
557 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
558 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
559 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
560 78756c87 2020-11-24 stsp struct got_repository *);
561 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
562 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
563 e78dc838 2020-12-04 stsp struct tog_view *, int);
564 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
565 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
566 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
567 e5a0f69f 2018-08-18 stsp
568 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
569 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
570 78756c87 2020-11-24 stsp const char *, const char *, int);
571 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
573 e78dc838 2020-12-04 stsp struct tog_view *, int);
574 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
575 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
576 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp
578 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
579 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
580 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
582 e78dc838 2020-12-04 stsp struct tog_view *, int);
583 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
584 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
585 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp
587 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
588 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
589 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
591 e78dc838 2020-12-04 stsp struct tog_view *, int);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
593 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
594 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
595 6458efa5 2020-11-24 stsp
596 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
597 6458efa5 2020-11-24 stsp struct got_repository *);
598 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
600 e78dc838 2020-12-04 stsp struct tog_view *, int);
601 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
602 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
603 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
604 25791caa 2018-10-24 stsp
605 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
606 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
607 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
608 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
609 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
610 25791caa 2018-10-24 stsp
611 25791caa 2018-10-24 stsp static void
612 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
613 25791caa 2018-10-24 stsp {
614 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
615 83baff54 2019-08-12 stsp }
616 83baff54 2019-08-12 stsp
617 83baff54 2019-08-12 stsp static void
618 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
619 83baff54 2019-08-12 stsp {
620 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
621 25791caa 2018-10-24 stsp }
622 26ed57b2 2018-05-19 stsp
623 61266923 2020-01-14 stsp static void
624 61266923 2020-01-14 stsp tog_sigcont(int signo)
625 61266923 2020-01-14 stsp {
626 61266923 2020-01-14 stsp tog_sigcont_received = 1;
627 61266923 2020-01-14 stsp }
628 61266923 2020-01-14 stsp
629 296152d1 2022-05-31 thomas static void
630 296152d1 2022-05-31 thomas tog_sigint(int signo)
631 296152d1 2022-05-31 thomas {
632 296152d1 2022-05-31 thomas tog_sigint_received = 1;
633 296152d1 2022-05-31 thomas }
634 296152d1 2022-05-31 thomas
635 296152d1 2022-05-31 thomas static void
636 296152d1 2022-05-31 thomas tog_sigterm(int signo)
637 296152d1 2022-05-31 thomas {
638 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
639 296152d1 2022-05-31 thomas }
640 296152d1 2022-05-31 thomas
641 296152d1 2022-05-31 thomas static int
642 296152d1 2022-05-31 thomas tog_fatal_signal_received()
643 296152d1 2022-05-31 thomas {
644 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
645 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
646 296152d1 2022-05-31 thomas }
647 296152d1 2022-05-31 thomas
648 296152d1 2022-05-31 thomas
649 e5a0f69f 2018-08-18 stsp static const struct got_error *
650 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
651 ea5e7bb5 2018-08-01 stsp {
652 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
653 e5a0f69f 2018-08-18 stsp
654 669b5ffa 2018-10-07 stsp if (view->child) {
655 669b5ffa 2018-10-07 stsp view_close(view->child);
656 669b5ffa 2018-10-07 stsp view->child = NULL;
657 669b5ffa 2018-10-07 stsp }
658 e5a0f69f 2018-08-18 stsp if (view->close)
659 e5a0f69f 2018-08-18 stsp err = view->close(view);
660 ea5e7bb5 2018-08-01 stsp if (view->panel)
661 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
662 ea5e7bb5 2018-08-01 stsp if (view->window)
663 ea5e7bb5 2018-08-01 stsp delwin(view->window);
664 ea5e7bb5 2018-08-01 stsp free(view);
665 e5a0f69f 2018-08-18 stsp return err;
666 ea5e7bb5 2018-08-01 stsp }
667 ea5e7bb5 2018-08-01 stsp
668 ea5e7bb5 2018-08-01 stsp static struct tog_view *
669 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
670 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
671 ea5e7bb5 2018-08-01 stsp {
672 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
673 ea5e7bb5 2018-08-01 stsp
674 ea5e7bb5 2018-08-01 stsp if (view == NULL)
675 ea5e7bb5 2018-08-01 stsp return NULL;
676 ea5e7bb5 2018-08-01 stsp
677 d6b05b5b 2018-08-04 stsp view->type = type;
678 f7d12f7e 2018-08-01 stsp view->lines = LINES;
679 f7d12f7e 2018-08-01 stsp view->cols = COLS;
680 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
681 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
682 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
683 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
684 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
685 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
686 96a765a8 2018-08-04 stsp view_close(view);
687 ea5e7bb5 2018-08-01 stsp return NULL;
688 ea5e7bb5 2018-08-01 stsp }
689 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
690 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
691 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
692 96a765a8 2018-08-04 stsp view_close(view);
693 ea5e7bb5 2018-08-01 stsp return NULL;
694 ea5e7bb5 2018-08-01 stsp }
695 ea5e7bb5 2018-08-01 stsp
696 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
697 ea5e7bb5 2018-08-01 stsp return view;
698 cdf1ee82 2018-08-01 stsp }
699 cdf1ee82 2018-08-01 stsp
700 0cf4efb1 2018-09-29 stsp static int
701 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
702 0cf4efb1 2018-09-29 stsp {
703 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
704 0cf4efb1 2018-09-29 stsp return 0;
705 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
706 5c60c32a 2018-10-18 stsp }
707 5c60c32a 2018-10-18 stsp
708 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
709 5c60c32a 2018-10-18 stsp
710 5c60c32a 2018-10-18 stsp static const struct got_error *
711 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
712 5c60c32a 2018-10-18 stsp {
713 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
714 5c60c32a 2018-10-18 stsp
715 5c60c32a 2018-10-18 stsp view->begin_y = 0;
716 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
717 5c60c32a 2018-10-18 stsp view->nlines = LINES;
718 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
719 5c60c32a 2018-10-18 stsp view->lines = LINES;
720 5c60c32a 2018-10-18 stsp view->cols = COLS;
721 5c60c32a 2018-10-18 stsp err = view_resize(view);
722 5c60c32a 2018-10-18 stsp if (err)
723 5c60c32a 2018-10-18 stsp return err;
724 5c60c32a 2018-10-18 stsp
725 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
727 5c60c32a 2018-10-18 stsp
728 5c60c32a 2018-10-18 stsp return NULL;
729 5c60c32a 2018-10-18 stsp }
730 5c60c32a 2018-10-18 stsp
731 5c60c32a 2018-10-18 stsp static const struct got_error *
732 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
733 5c60c32a 2018-10-18 stsp {
734 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
735 5c60c32a 2018-10-18 stsp
736 5c60c32a 2018-10-18 stsp view->begin_x = 0;
737 5c60c32a 2018-10-18 stsp view->begin_y = 0;
738 5c60c32a 2018-10-18 stsp view->nlines = LINES;
739 5c60c32a 2018-10-18 stsp view->ncols = COLS;
740 5c60c32a 2018-10-18 stsp view->lines = LINES;
741 5c60c32a 2018-10-18 stsp view->cols = COLS;
742 5c60c32a 2018-10-18 stsp err = view_resize(view);
743 5c60c32a 2018-10-18 stsp if (err)
744 5c60c32a 2018-10-18 stsp return err;
745 5c60c32a 2018-10-18 stsp
746 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
748 5c60c32a 2018-10-18 stsp
749 5c60c32a 2018-10-18 stsp return NULL;
750 0cf4efb1 2018-09-29 stsp }
751 0cf4efb1 2018-09-29 stsp
752 5c60c32a 2018-10-18 stsp static int
753 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
754 5c60c32a 2018-10-18 stsp {
755 5c60c32a 2018-10-18 stsp return view->parent == NULL;
756 5c60c32a 2018-10-18 stsp }
757 5c60c32a 2018-10-18 stsp
758 4d8c2215 2018-08-19 stsp static const struct got_error *
759 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
760 f7d12f7e 2018-08-01 stsp {
761 f7d12f7e 2018-08-01 stsp int nlines, ncols;
762 f7d12f7e 2018-08-01 stsp
763 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
764 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
765 0cf4efb1 2018-09-29 stsp else
766 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
767 f7d12f7e 2018-08-01 stsp
768 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
769 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
770 0cf4efb1 2018-09-29 stsp else
771 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
772 f7d12f7e 2018-08-01 stsp
773 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
774 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
775 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
776 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
777 25791caa 2018-10-24 stsp wclear(view->window);
778 f7d12f7e 2018-08-01 stsp
779 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
780 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
781 0cf4efb1 2018-09-29 stsp view->lines = LINES;
782 0cf4efb1 2018-09-29 stsp view->cols = COLS;
783 6d0fee91 2018-08-01 stsp
784 6e3e5d9c 2018-10-18 stsp if (view->child) {
785 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
786 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
787 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
788 5c60c32a 2018-10-18 stsp if (view->child->focussed)
789 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
790 5c60c32a 2018-10-18 stsp else
791 5c60c32a 2018-10-18 stsp show_panel(view->panel);
792 5c60c32a 2018-10-18 stsp } else {
793 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
794 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
795 5c60c32a 2018-10-18 stsp }
796 5c60c32a 2018-10-18 stsp }
797 669b5ffa 2018-10-07 stsp
798 5c60c32a 2018-10-18 stsp return NULL;
799 669b5ffa 2018-10-07 stsp }
800 669b5ffa 2018-10-07 stsp
801 669b5ffa 2018-10-07 stsp static const struct got_error *
802 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
803 669b5ffa 2018-10-07 stsp {
804 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
805 669b5ffa 2018-10-07 stsp
806 669b5ffa 2018-10-07 stsp if (view->child == NULL)
807 669b5ffa 2018-10-07 stsp return NULL;
808 669b5ffa 2018-10-07 stsp
809 669b5ffa 2018-10-07 stsp err = view_close(view->child);
810 669b5ffa 2018-10-07 stsp view->child = NULL;
811 669b5ffa 2018-10-07 stsp return err;
812 669b5ffa 2018-10-07 stsp }
813 669b5ffa 2018-10-07 stsp
814 72a9cb46 2020-12-03 stsp static void
815 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
816 669b5ffa 2018-10-07 stsp {
817 669b5ffa 2018-10-07 stsp view->child = child;
818 669b5ffa 2018-10-07 stsp child->parent = view;
819 bfddd0d9 2018-09-29 stsp }
820 bfddd0d9 2018-09-29 stsp
821 bfddd0d9 2018-09-29 stsp static int
822 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
823 bfddd0d9 2018-09-29 stsp {
824 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
825 34bc9ec9 2019-02-22 stsp }
826 34bc9ec9 2019-02-22 stsp
827 34bc9ec9 2019-02-22 stsp static void
828 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
829 25791caa 2018-10-24 stsp {
830 25791caa 2018-10-24 stsp int cols, lines;
831 25791caa 2018-10-24 stsp struct winsize size;
832 25791caa 2018-10-24 stsp
833 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
834 25791caa 2018-10-24 stsp cols = 80; /* Default */
835 25791caa 2018-10-24 stsp lines = 24;
836 25791caa 2018-10-24 stsp } else {
837 25791caa 2018-10-24 stsp cols = size.ws_col;
838 25791caa 2018-10-24 stsp lines = size.ws_row;
839 25791caa 2018-10-24 stsp }
840 25791caa 2018-10-24 stsp resize_term(lines, cols);
841 2b49a8ae 2019-06-22 stsp }
842 2b49a8ae 2019-06-22 stsp
843 2b49a8ae 2019-06-22 stsp static const struct got_error *
844 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
845 2b49a8ae 2019-06-22 stsp {
846 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
847 2b49a8ae 2019-06-22 stsp char pattern[1024];
848 2b49a8ae 2019-06-22 stsp int ret;
849 c0c4acc8 2021-01-24 stsp
850 c0c4acc8 2021-01-24 stsp if (view->search_started) {
851 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
852 c0c4acc8 2021-01-24 stsp view->searching = 0;
853 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
854 c0c4acc8 2021-01-24 stsp }
855 c0c4acc8 2021-01-24 stsp view->search_started = 0;
856 2b49a8ae 2019-06-22 stsp
857 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
858 2b49a8ae 2019-06-22 stsp return NULL;
859 2b49a8ae 2019-06-22 stsp
860 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
861 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
862 2b49a8ae 2019-06-22 stsp
863 2b49a8ae 2019-06-22 stsp nocbreak();
864 2b49a8ae 2019-06-22 stsp echo();
865 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
866 2b49a8ae 2019-06-22 stsp cbreak();
867 2b49a8ae 2019-06-22 stsp noecho();
868 2b49a8ae 2019-06-22 stsp if (ret == ERR)
869 2b49a8ae 2019-06-22 stsp return NULL;
870 2b49a8ae 2019-06-22 stsp
871 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
872 7c32bd05 2019-06-22 stsp err = view->search_start(view);
873 7c32bd05 2019-06-22 stsp if (err) {
874 7c32bd05 2019-06-22 stsp regfree(&view->regex);
875 7c32bd05 2019-06-22 stsp return err;
876 7c32bd05 2019-06-22 stsp }
877 c0c4acc8 2021-01-24 stsp view->search_started = 1;
878 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
879 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
880 2b49a8ae 2019-06-22 stsp view->search_next(view);
881 2b49a8ae 2019-06-22 stsp }
882 2b49a8ae 2019-06-22 stsp
883 2b49a8ae 2019-06-22 stsp return NULL;
884 0cf4efb1 2018-09-29 stsp }
885 6d0fee91 2018-08-01 stsp
886 0cf4efb1 2018-09-29 stsp static const struct got_error *
887 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
888 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
889 e5a0f69f 2018-08-18 stsp {
890 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
891 669b5ffa 2018-10-07 stsp struct tog_view *v;
892 1a76625f 2018-10-22 stsp int ch, errcode;
893 e5a0f69f 2018-08-18 stsp
894 e5a0f69f 2018-08-18 stsp *new = NULL;
895 8f4ed634 2020-03-26 stsp
896 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
897 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
898 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
899 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
900 e5a0f69f 2018-08-18 stsp
901 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
902 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
903 82954512 2020-02-03 stsp if (errcode)
904 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
905 82954512 2020-02-03 stsp "pthread_mutex_unlock");
906 3da8ef85 2021-09-21 stsp sched_yield();
907 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
908 82954512 2020-02-03 stsp if (errcode)
909 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
910 82954512 2020-02-03 stsp "pthread_mutex_lock");
911 60493ae3 2019-06-20 stsp view->search_next(view);
912 60493ae3 2019-06-20 stsp return NULL;
913 60493ae3 2019-06-20 stsp }
914 60493ae3 2019-06-20 stsp
915 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
916 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
917 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
918 1a76625f 2018-10-22 stsp if (errcode)
919 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
920 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
921 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
922 1a76625f 2018-10-22 stsp if (errcode)
923 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
924 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
925 25791caa 2018-10-24 stsp
926 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
927 25791caa 2018-10-24 stsp tog_resizeterm();
928 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
929 61266923 2020-01-14 stsp tog_sigcont_received = 0;
930 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
931 25791caa 2018-10-24 stsp err = view_resize(v);
932 25791caa 2018-10-24 stsp if (err)
933 25791caa 2018-10-24 stsp return err;
934 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
935 25791caa 2018-10-24 stsp if (err)
936 25791caa 2018-10-24 stsp return err;
937 cdfcfb03 2020-12-06 stsp if (v->child) {
938 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
939 cdfcfb03 2020-12-06 stsp if (err)
940 cdfcfb03 2020-12-06 stsp return err;
941 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
942 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
943 cdfcfb03 2020-12-06 stsp if (err)
944 cdfcfb03 2020-12-06 stsp return err;
945 cdfcfb03 2020-12-06 stsp }
946 25791caa 2018-10-24 stsp }
947 25791caa 2018-10-24 stsp }
948 25791caa 2018-10-24 stsp
949 e5a0f69f 2018-08-18 stsp switch (ch) {
950 1e37a5c2 2019-05-12 jcs case '\t':
951 1e37a5c2 2019-05-12 jcs if (view->child) {
952 e78dc838 2020-12-04 stsp view->focussed = 0;
953 e78dc838 2020-12-04 stsp view->child->focussed = 1;
954 e78dc838 2020-12-04 stsp view->focus_child = 1;
955 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
956 e78dc838 2020-12-04 stsp view->focussed = 0;
957 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
958 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
959 1e37a5c2 2019-05-12 jcs }
960 1e37a5c2 2019-05-12 jcs break;
961 1e37a5c2 2019-05-12 jcs case 'q':
962 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
963 9970f7fc 2020-12-03 stsp view->dying = 1;
964 1e37a5c2 2019-05-12 jcs break;
965 1e37a5c2 2019-05-12 jcs case 'Q':
966 1e37a5c2 2019-05-12 jcs *done = 1;
967 1e37a5c2 2019-05-12 jcs break;
968 1e37a5c2 2019-05-12 jcs case 'f':
969 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
970 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
971 1e37a5c2 2019-05-12 jcs break;
972 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
973 e78dc838 2020-12-04 stsp view->focussed = 0;
974 e78dc838 2020-12-04 stsp view->child->focussed = 1;
975 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
976 1e37a5c2 2019-05-12 jcs } else
977 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
978 1e37a5c2 2019-05-12 jcs if (err)
979 1e37a5c2 2019-05-12 jcs break;
980 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
981 9970f7fc 2020-12-03 stsp KEY_RESIZE);
982 1e37a5c2 2019-05-12 jcs } else {
983 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
984 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
985 e78dc838 2020-12-04 stsp view->focussed = 1;
986 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
987 1e37a5c2 2019-05-12 jcs } else {
988 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
989 669b5ffa 2018-10-07 stsp }
990 1e37a5c2 2019-05-12 jcs if (err)
991 1e37a5c2 2019-05-12 jcs break;
992 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
993 1e37a5c2 2019-05-12 jcs }
994 1e37a5c2 2019-05-12 jcs break;
995 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
996 60493ae3 2019-06-20 stsp break;
997 60493ae3 2019-06-20 stsp case '/':
998 60493ae3 2019-06-20 stsp if (view->search_start)
999 2b49a8ae 2019-06-22 stsp view_search_start(view);
1000 60493ae3 2019-06-20 stsp else
1001 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1002 1e37a5c2 2019-05-12 jcs break;
1003 b1bf1435 2019-06-21 stsp case 'N':
1004 60493ae3 2019-06-20 stsp case 'n':
1005 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1006 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1007 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1008 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1009 60493ae3 2019-06-20 stsp view->search_next(view);
1010 60493ae3 2019-06-20 stsp } else
1011 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1012 60493ae3 2019-06-20 stsp break;
1013 1e37a5c2 2019-05-12 jcs default:
1014 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1015 1e37a5c2 2019-05-12 jcs break;
1016 e5a0f69f 2018-08-18 stsp }
1017 e5a0f69f 2018-08-18 stsp
1018 e5a0f69f 2018-08-18 stsp return err;
1019 e5a0f69f 2018-08-18 stsp }
1020 e5a0f69f 2018-08-18 stsp
1021 1a57306a 2018-09-02 stsp void
1022 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1023 1a57306a 2018-09-02 stsp {
1024 0cf4efb1 2018-09-29 stsp PANEL *panel;
1025 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1026 0cf4efb1 2018-09-29 stsp
1027 669b5ffa 2018-10-07 stsp if (view->parent)
1028 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1029 669b5ffa 2018-10-07 stsp
1030 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1031 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1032 1a57306a 2018-09-02 stsp return;
1033 1a57306a 2018-09-02 stsp
1034 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1035 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1036 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1037 bcbd79e2 2018-08-19 stsp }
1038 bcbd79e2 2018-08-19 stsp
1039 a3404814 2018-09-02 stsp int
1040 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1041 a3404814 2018-09-02 stsp {
1042 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1043 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1044 669b5ffa 2018-10-07 stsp return 0;
1045 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1046 669b5ffa 2018-10-07 stsp return 0;
1047 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1048 a3404814 2018-09-02 stsp return 0;
1049 a3404814 2018-09-02 stsp
1050 669b5ffa 2018-10-07 stsp return view->focussed;
1051 a3404814 2018-09-02 stsp }
1052 a3404814 2018-09-02 stsp
1053 bcbd79e2 2018-08-19 stsp static const struct got_error *
1054 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1055 e5a0f69f 2018-08-18 stsp {
1056 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1057 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1058 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1059 fd823528 2018-10-22 stsp int fast_refresh = 10;
1060 1a76625f 2018-10-22 stsp int done = 0, errcode;
1061 e5a0f69f 2018-08-18 stsp
1062 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1063 1a76625f 2018-10-22 stsp if (errcode)
1064 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1065 1a76625f 2018-10-22 stsp
1066 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1067 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1068 e5a0f69f 2018-08-18 stsp
1069 1004088d 2018-09-29 stsp view->focussed = 1;
1070 878940b7 2018-09-29 stsp err = view->show(view);
1071 0cf4efb1 2018-09-29 stsp if (err)
1072 0cf4efb1 2018-09-29 stsp return err;
1073 0cf4efb1 2018-09-29 stsp update_panels();
1074 0cf4efb1 2018-09-29 stsp doupdate();
1075 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1076 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1077 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1078 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1079 fd823528 2018-10-22 stsp
1080 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1081 e5a0f69f 2018-08-18 stsp if (err)
1082 e5a0f69f 2018-08-18 stsp break;
1083 9970f7fc 2020-12-03 stsp if (view->dying) {
1084 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1085 669b5ffa 2018-10-07 stsp
1086 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1087 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1088 9970f7fc 2020-12-03 stsp entry);
1089 e78dc838 2020-12-04 stsp else if (view->parent)
1090 669b5ffa 2018-10-07 stsp prev = view->parent;
1091 669b5ffa 2018-10-07 stsp
1092 e78dc838 2020-12-04 stsp if (view->parent) {
1093 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1094 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1095 e78dc838 2020-12-04 stsp } else
1096 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1097 669b5ffa 2018-10-07 stsp
1098 9970f7fc 2020-12-03 stsp err = view_close(view);
1099 fb59748f 2020-12-05 stsp if (err)
1100 e5a0f69f 2018-08-18 stsp goto done;
1101 669b5ffa 2018-10-07 stsp
1102 e78dc838 2020-12-04 stsp view = NULL;
1103 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1104 e78dc838 2020-12-04 stsp if (v->focussed)
1105 e78dc838 2020-12-04 stsp break;
1106 0cf4efb1 2018-09-29 stsp }
1107 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1108 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1109 e78dc838 2020-12-04 stsp if (prev)
1110 e78dc838 2020-12-04 stsp view = prev;
1111 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1112 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1113 e78dc838 2020-12-04 stsp tog_view_list_head);
1114 e78dc838 2020-12-04 stsp }
1115 e78dc838 2020-12-04 stsp if (view) {
1116 e78dc838 2020-12-04 stsp if (view->focus_child) {
1117 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1118 e78dc838 2020-12-04 stsp view = view->child;
1119 e78dc838 2020-12-04 stsp } else
1120 e78dc838 2020-12-04 stsp view->focussed = 1;
1121 e78dc838 2020-12-04 stsp }
1122 e78dc838 2020-12-04 stsp }
1123 e5a0f69f 2018-08-18 stsp }
1124 bcbd79e2 2018-08-19 stsp if (new_view) {
1125 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1126 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1127 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1128 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1129 86c66b02 2018-10-18 stsp continue;
1130 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1131 86c66b02 2018-10-18 stsp err = view_close(v);
1132 86c66b02 2018-10-18 stsp if (err)
1133 86c66b02 2018-10-18 stsp goto done;
1134 86c66b02 2018-10-18 stsp break;
1135 86c66b02 2018-10-18 stsp }
1136 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1137 fed7eaa8 2018-10-24 stsp view = new_view;
1138 7cd52833 2022-06-23 thomas }
1139 669b5ffa 2018-10-07 stsp if (view) {
1140 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1141 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1142 e78dc838 2020-12-04 stsp view = view->child;
1143 e78dc838 2020-12-04 stsp } else {
1144 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1145 e78dc838 2020-12-04 stsp view = view->parent;
1146 1a76625f 2018-10-22 stsp }
1147 e78dc838 2020-12-04 stsp show_panel(view->panel);
1148 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1149 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1150 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1151 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1152 669b5ffa 2018-10-07 stsp if (err)
1153 1a76625f 2018-10-22 stsp goto done;
1154 669b5ffa 2018-10-07 stsp }
1155 669b5ffa 2018-10-07 stsp err = view->show(view);
1156 0cf4efb1 2018-09-29 stsp if (err)
1157 1a76625f 2018-10-22 stsp goto done;
1158 669b5ffa 2018-10-07 stsp if (view->child) {
1159 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1160 669b5ffa 2018-10-07 stsp if (err)
1161 1a76625f 2018-10-22 stsp goto done;
1162 669b5ffa 2018-10-07 stsp }
1163 1a76625f 2018-10-22 stsp update_panels();
1164 1a76625f 2018-10-22 stsp doupdate();
1165 0cf4efb1 2018-09-29 stsp }
1166 e5a0f69f 2018-08-18 stsp }
1167 e5a0f69f 2018-08-18 stsp done:
1168 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1169 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1170 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1171 e5a0f69f 2018-08-18 stsp view_close(view);
1172 e5a0f69f 2018-08-18 stsp }
1173 1a76625f 2018-10-22 stsp
1174 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1175 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1176 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1177 1a76625f 2018-10-22 stsp
1178 e5a0f69f 2018-08-18 stsp return err;
1179 ea5e7bb5 2018-08-01 stsp }
1180 ea5e7bb5 2018-08-01 stsp
1181 4ed7e80c 2018-05-20 stsp __dead static void
1182 9f7d7167 2018-04-29 stsp usage_log(void)
1183 9f7d7167 2018-04-29 stsp {
1184 80ddbec8 2018-04-29 stsp endwin();
1185 c70c5802 2018-08-01 stsp fprintf(stderr,
1186 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1187 9f7d7167 2018-04-29 stsp getprogname());
1188 9f7d7167 2018-04-29 stsp exit(1);
1189 80ddbec8 2018-04-29 stsp }
1190 80ddbec8 2018-04-29 stsp
1191 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1192 80ddbec8 2018-04-29 stsp static const struct got_error *
1193 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1194 963b370f 2018-05-20 stsp {
1195 00dfcb92 2018-06-11 stsp char *vis = NULL;
1196 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1197 963b370f 2018-05-20 stsp
1198 963b370f 2018-05-20 stsp *ws = NULL;
1199 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1200 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1201 00dfcb92 2018-06-11 stsp int vislen;
1202 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1203 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1204 00dfcb92 2018-06-11 stsp
1205 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1206 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1207 00dfcb92 2018-06-11 stsp if (err)
1208 00dfcb92 2018-06-11 stsp return err;
1209 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1210 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1211 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1212 a7f50699 2018-06-11 stsp goto done;
1213 a7f50699 2018-06-11 stsp }
1214 00dfcb92 2018-06-11 stsp }
1215 963b370f 2018-05-20 stsp
1216 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1217 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1218 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1219 a7f50699 2018-06-11 stsp goto done;
1220 a7f50699 2018-06-11 stsp }
1221 963b370f 2018-05-20 stsp
1222 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1223 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1224 a7f50699 2018-06-11 stsp done:
1225 00dfcb92 2018-06-11 stsp free(vis);
1226 963b370f 2018-05-20 stsp if (err) {
1227 963b370f 2018-05-20 stsp free(*ws);
1228 963b370f 2018-05-20 stsp *ws = NULL;
1229 963b370f 2018-05-20 stsp *wlen = 0;
1230 963b370f 2018-05-20 stsp }
1231 963b370f 2018-05-20 stsp return err;
1232 05171be4 2022-06-23 thomas }
1233 05171be4 2022-06-23 thomas
1234 05171be4 2022-06-23 thomas static const struct got_error *
1235 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1236 05171be4 2022-06-23 thomas {
1237 05171be4 2022-06-23 thomas char *dst;
1238 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1239 05171be4 2022-06-23 thomas
1240 05171be4 2022-06-23 thomas *ptr = NULL;
1241 05171be4 2022-06-23 thomas n = len = strlen(src);
1242 83316834 2022-06-23 thomas dst = malloc(n + 1);
1243 05171be4 2022-06-23 thomas if (dst == NULL)
1244 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1245 05171be4 2022-06-23 thomas
1246 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1247 05171be4 2022-06-23 thomas const char c = src[idx];
1248 05171be4 2022-06-23 thomas
1249 05171be4 2022-06-23 thomas if (c == '\t') {
1250 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1251 b235ad5d 2022-06-23 thomas char *p;
1252 b235ad5d 2022-06-23 thomas
1253 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1254 83316834 2022-06-23 thomas if (p == NULL) {
1255 83316834 2022-06-23 thomas free(dst);
1256 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1257 83316834 2022-06-23 thomas
1258 83316834 2022-06-23 thomas }
1259 83316834 2022-06-23 thomas dst = p;
1260 05171be4 2022-06-23 thomas n += nb;
1261 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1262 05171be4 2022-06-23 thomas sz += nb;
1263 05171be4 2022-06-23 thomas } else
1264 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1265 05171be4 2022-06-23 thomas ++idx;
1266 05171be4 2022-06-23 thomas }
1267 05171be4 2022-06-23 thomas
1268 05171be4 2022-06-23 thomas dst[sz] = '\0';
1269 05171be4 2022-06-23 thomas *ptr = dst;
1270 05171be4 2022-06-23 thomas return NULL;
1271 963b370f 2018-05-20 stsp }
1272 963b370f 2018-05-20 stsp
1273 f91a2b48 2022-06-23 thomas /*
1274 f91a2b48 2022-06-23 thomas * Skip leading nscroll columns of a wide character string.
1275 f91a2b48 2022-06-23 thomas * Returns the index to the first character of the scrolled string.
1276 f91a2b48 2022-06-23 thomas */
1277 f91a2b48 2022-06-23 thomas static const struct got_error *
1278 f91a2b48 2022-06-23 thomas scroll_wline(int *scrollx , wchar_t *wline, int nscroll,
1279 f91a2b48 2022-06-23 thomas int col_tab_align)
1280 963b370f 2018-05-20 stsp {
1281 f91a2b48 2022-06-23 thomas int cols = 0;
1282 f91a2b48 2022-06-23 thomas size_t wlen = wcslen(wline);
1283 f91a2b48 2022-06-23 thomas int i = 0;
1284 f91a2b48 2022-06-23 thomas
1285 f91a2b48 2022-06-23 thomas *scrollx = 0;
1286 f91a2b48 2022-06-23 thomas
1287 f91a2b48 2022-06-23 thomas while (i < wlen && cols < nscroll) {
1288 f91a2b48 2022-06-23 thomas int width = wcwidth(wline[i]);
1289 f91a2b48 2022-06-23 thomas
1290 f91a2b48 2022-06-23 thomas if (width == 0) {
1291 f91a2b48 2022-06-23 thomas i++;
1292 f91a2b48 2022-06-23 thomas continue;
1293 f91a2b48 2022-06-23 thomas }
1294 f91a2b48 2022-06-23 thomas
1295 f91a2b48 2022-06-23 thomas if (width == 1 || width == 2) {
1296 f91a2b48 2022-06-23 thomas if (cols + width > nscroll)
1297 f91a2b48 2022-06-23 thomas break;
1298 f91a2b48 2022-06-23 thomas cols += width;
1299 f91a2b48 2022-06-23 thomas i++;
1300 f91a2b48 2022-06-23 thomas } else if (width == -1) {
1301 f91a2b48 2022-06-23 thomas if (wline[i] == L'\t') {
1302 f91a2b48 2022-06-23 thomas width = TABSIZE -
1303 f91a2b48 2022-06-23 thomas ((cols + col_tab_align) % TABSIZE);
1304 f91a2b48 2022-06-23 thomas } else {
1305 f91a2b48 2022-06-23 thomas width = 1;
1306 f91a2b48 2022-06-23 thomas wline[i] = L'.';
1307 f91a2b48 2022-06-23 thomas }
1308 f91a2b48 2022-06-23 thomas if (cols + width > nscroll)
1309 f91a2b48 2022-06-23 thomas break;
1310 f91a2b48 2022-06-23 thomas cols += width;
1311 f91a2b48 2022-06-23 thomas i++;
1312 f91a2b48 2022-06-23 thomas } else
1313 f91a2b48 2022-06-23 thomas return got_error_from_errno("wcwidth");
1314 f91a2b48 2022-06-23 thomas }
1315 f91a2b48 2022-06-23 thomas
1316 f91a2b48 2022-06-23 thomas *scrollx = i;
1317 f91a2b48 2022-06-23 thomas return NULL;
1318 f91a2b48 2022-06-23 thomas }
1319 f91a2b48 2022-06-23 thomas
1320 f91a2b48 2022-06-23 thomas /*
1321 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1322 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1323 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1324 f91a2b48 2022-06-23 thomas */
1325 f91a2b48 2022-06-23 thomas static const struct got_error *
1326 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1328 f91a2b48 2022-06-23 thomas {
1329 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1330 963b370f 2018-05-20 stsp int cols = 0;
1331 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1332 05171be4 2022-06-23 thomas char *exstr = NULL;
1333 963b370f 2018-05-20 stsp size_t wlen;
1334 f91a2b48 2022-06-23 thomas int i, scrollx = 0;
1335 963b370f 2018-05-20 stsp
1336 963b370f 2018-05-20 stsp *wlinep = NULL;
1337 b700b5d6 2018-07-10 stsp *widthp = 0;
1338 963b370f 2018-05-20 stsp
1339 05171be4 2022-06-23 thomas if (expand) {
1340 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1341 05171be4 2022-06-23 thomas if (err)
1342 05171be4 2022-06-23 thomas return err;
1343 05171be4 2022-06-23 thomas }
1344 05171be4 2022-06-23 thomas
1345 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1346 05171be4 2022-06-23 thomas free(exstr);
1347 963b370f 2018-05-20 stsp if (err)
1348 963b370f 2018-05-20 stsp return err;
1349 963b370f 2018-05-20 stsp
1350 f91a2b48 2022-06-23 thomas err = scroll_wline(&scrollx, wline, nscroll, col_tab_align);
1351 f91a2b48 2022-06-23 thomas if (err)
1352 f91a2b48 2022-06-23 thomas goto done;
1353 f91a2b48 2022-06-23 thomas
1354 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1355 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1356 3f670bfb 2020-12-10 stsp wlen--;
1357 3f670bfb 2020-12-10 stsp }
1358 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1359 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1360 3f670bfb 2020-12-10 stsp wlen--;
1361 3f670bfb 2020-12-10 stsp }
1362 3f670bfb 2020-12-10 stsp
1363 f91a2b48 2022-06-23 thomas i = scrollx;
1364 27a741e5 2019-09-11 stsp while (i < wlen) {
1365 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1366 27a741e5 2019-09-11 stsp
1367 27a741e5 2019-09-11 stsp if (width == 0) {
1368 b700b5d6 2018-07-10 stsp i++;
1369 27a741e5 2019-09-11 stsp continue;
1370 27a741e5 2019-09-11 stsp }
1371 27a741e5 2019-09-11 stsp
1372 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1373 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1374 27a741e5 2019-09-11 stsp break;
1375 27a741e5 2019-09-11 stsp cols += width;
1376 3c1f04f1 2018-09-13 stsp i++;
1377 27a741e5 2019-09-11 stsp } else if (width == -1) {
1378 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1379 27a741e5 2019-09-11 stsp width = TABSIZE -
1380 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1381 748d5cab 2020-12-14 naddy } else {
1382 748d5cab 2020-12-14 naddy width = 1;
1383 748d5cab 2020-12-14 naddy wline[i] = L'.';
1384 27a741e5 2019-09-11 stsp }
1385 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1386 748d5cab 2020-12-14 naddy break;
1387 748d5cab 2020-12-14 naddy cols += width;
1388 b700b5d6 2018-07-10 stsp i++;
1389 27a741e5 2019-09-11 stsp } else {
1390 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1391 963b370f 2018-05-20 stsp goto done;
1392 963b370f 2018-05-20 stsp }
1393 963b370f 2018-05-20 stsp }
1394 963b370f 2018-05-20 stsp wline[i] = L'\0';
1395 b700b5d6 2018-07-10 stsp if (widthp)
1396 b700b5d6 2018-07-10 stsp *widthp = cols;
1397 f91a2b48 2022-06-23 thomas if (scrollxp)
1398 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1399 963b370f 2018-05-20 stsp done:
1400 963b370f 2018-05-20 stsp if (err)
1401 963b370f 2018-05-20 stsp free(wline);
1402 963b370f 2018-05-20 stsp else
1403 963b370f 2018-05-20 stsp *wlinep = wline;
1404 963b370f 2018-05-20 stsp return err;
1405 963b370f 2018-05-20 stsp }
1406 963b370f 2018-05-20 stsp
1407 8b473291 2019-02-21 stsp static const struct got_error*
1408 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1409 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1410 8b473291 2019-02-21 stsp {
1411 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1412 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1413 8b473291 2019-02-21 stsp char *s;
1414 8b473291 2019-02-21 stsp const char *name;
1415 8b473291 2019-02-21 stsp
1416 8b473291 2019-02-21 stsp *refs_str = NULL;
1417 8b473291 2019-02-21 stsp
1418 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1419 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1420 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1421 52b5abe1 2019-08-13 stsp int cmp;
1422 52b5abe1 2019-08-13 stsp
1423 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1424 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1425 8b473291 2019-02-21 stsp continue;
1426 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1427 8b473291 2019-02-21 stsp name += 5;
1428 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1429 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1430 7143d404 2019-03-12 stsp continue;
1431 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1432 8b473291 2019-02-21 stsp name += 6;
1433 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1434 8b473291 2019-02-21 stsp name += 8;
1435 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1436 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1437 79cc719f 2020-04-24 stsp continue;
1438 79cc719f 2020-04-24 stsp }
1439 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1440 48cae60d 2020-09-22 stsp if (err)
1441 48cae60d 2020-09-22 stsp break;
1442 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1443 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1444 5d844a1e 2019-08-13 stsp if (err) {
1445 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1446 48cae60d 2020-09-22 stsp free(ref_id);
1447 5d844a1e 2019-08-13 stsp break;
1448 48cae60d 2020-09-22 stsp }
1449 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1450 5d844a1e 2019-08-13 stsp err = NULL;
1451 5d844a1e 2019-08-13 stsp tag = NULL;
1452 5d844a1e 2019-08-13 stsp }
1453 52b5abe1 2019-08-13 stsp }
1454 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1455 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1456 48cae60d 2020-09-22 stsp free(ref_id);
1457 52b5abe1 2019-08-13 stsp if (tag)
1458 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1459 52b5abe1 2019-08-13 stsp if (cmp != 0)
1460 52b5abe1 2019-08-13 stsp continue;
1461 8b473291 2019-02-21 stsp s = *refs_str;
1462 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1463 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1464 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1465 8b473291 2019-02-21 stsp free(s);
1466 8b473291 2019-02-21 stsp *refs_str = NULL;
1467 8b473291 2019-02-21 stsp break;
1468 8b473291 2019-02-21 stsp }
1469 8b473291 2019-02-21 stsp free(s);
1470 8b473291 2019-02-21 stsp }
1471 8b473291 2019-02-21 stsp
1472 8b473291 2019-02-21 stsp return err;
1473 8b473291 2019-02-21 stsp }
1474 8b473291 2019-02-21 stsp
1475 963b370f 2018-05-20 stsp static const struct got_error *
1476 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1477 27a741e5 2019-09-11 stsp int col_tab_align)
1478 5813d178 2019-03-09 stsp {
1479 e6b8b890 2020-12-29 naddy char *smallerthan;
1480 5813d178 2019-03-09 stsp
1481 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1482 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1483 5813d178 2019-03-09 stsp author = smallerthan + 1;
1484 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1485 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1486 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1487 5813d178 2019-03-09 stsp }
1488 5813d178 2019-03-09 stsp
1489 5813d178 2019-03-09 stsp static const struct got_error *
1490 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1491 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1492 8fdc79fe 2020-12-01 naddy int author_display_cols)
1493 80ddbec8 2018-04-29 stsp {
1494 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1495 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1496 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1497 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1498 5813d178 2019-03-09 stsp char *author = NULL;
1499 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1500 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1501 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1502 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1503 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1504 ccb26ccd 2018-11-05 stsp struct tm tm;
1505 45d799e2 2018-12-23 stsp time_t committer_time;
1506 11b20872 2019-11-08 stsp struct tog_color *tc;
1507 80ddbec8 2018-04-29 stsp
1508 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1509 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1510 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1511 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1512 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1513 b39d25c7 2018-07-10 stsp
1514 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1515 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1516 b39d25c7 2018-07-10 stsp else
1517 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1518 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1519 11b20872 2019-11-08 stsp if (tc)
1520 11b20872 2019-11-08 stsp wattr_on(view->window,
1521 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1522 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1523 11b20872 2019-11-08 stsp if (tc)
1524 11b20872 2019-11-08 stsp wattr_off(view->window,
1525 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1526 27a741e5 2019-09-11 stsp col = limit;
1527 b39d25c7 2018-07-10 stsp if (col > avail)
1528 b39d25c7 2018-07-10 stsp goto done;
1529 6570a66d 2019-11-08 stsp
1530 6570a66d 2019-11-08 stsp if (avail >= 120) {
1531 6570a66d 2019-11-08 stsp char *id_str;
1532 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1533 6570a66d 2019-11-08 stsp if (err)
1534 6570a66d 2019-11-08 stsp goto done;
1535 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1536 11b20872 2019-11-08 stsp if (tc)
1537 11b20872 2019-11-08 stsp wattr_on(view->window,
1538 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1539 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1540 11b20872 2019-11-08 stsp if (tc)
1541 11b20872 2019-11-08 stsp wattr_off(view->window,
1542 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1543 6570a66d 2019-11-08 stsp free(id_str);
1544 6570a66d 2019-11-08 stsp col += 9;
1545 6570a66d 2019-11-08 stsp if (col > avail)
1546 6570a66d 2019-11-08 stsp goto done;
1547 6570a66d 2019-11-08 stsp }
1548 b39d25c7 2018-07-10 stsp
1549 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1550 5813d178 2019-03-09 stsp if (author == NULL) {
1551 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1552 80ddbec8 2018-04-29 stsp goto done;
1553 80ddbec8 2018-04-29 stsp }
1554 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1555 bb737323 2018-05-20 stsp if (err)
1556 bb737323 2018-05-20 stsp goto done;
1557 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1558 11b20872 2019-11-08 stsp if (tc)
1559 11b20872 2019-11-08 stsp wattr_on(view->window,
1560 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1561 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1562 11b20872 2019-11-08 stsp if (tc)
1563 11b20872 2019-11-08 stsp wattr_off(view->window,
1564 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1565 bb737323 2018-05-20 stsp col += author_width;
1566 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1567 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1568 bb737323 2018-05-20 stsp col++;
1569 bb737323 2018-05-20 stsp author_width++;
1570 bb737323 2018-05-20 stsp }
1571 9c2eaf34 2018-05-20 stsp if (col > avail)
1572 9c2eaf34 2018-05-20 stsp goto done;
1573 80ddbec8 2018-04-29 stsp
1574 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1575 5943eee2 2019-08-13 stsp if (err)
1576 6d9fbc00 2018-04-29 stsp goto done;
1577 bb737323 2018-05-20 stsp logmsg = logmsg0;
1578 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1579 bb737323 2018-05-20 stsp logmsg++;
1580 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1581 bb737323 2018-05-20 stsp if (newline)
1582 bb737323 2018-05-20 stsp *newline = '\0';
1583 f91a2b48 2022-06-23 thomas limit = avail - col;
1584 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1585 f91a2b48 2022-06-23 thomas limit, col, 1);
1586 331b1a16 2022-06-23 thomas if (err)
1587 331b1a16 2022-06-23 thomas goto done;
1588 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
1589 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
1590 27a741e5 2019-09-11 stsp while (col < avail) {
1591 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1592 bb737323 2018-05-20 stsp col++;
1593 881b2d3e 2018-04-30 stsp }
1594 80ddbec8 2018-04-29 stsp done:
1595 80ddbec8 2018-04-29 stsp free(logmsg0);
1596 bb737323 2018-05-20 stsp free(wlogmsg);
1597 5813d178 2019-03-09 stsp free(author);
1598 bb737323 2018-05-20 stsp free(wauthor);
1599 80ddbec8 2018-04-29 stsp free(line);
1600 80ddbec8 2018-04-29 stsp return err;
1601 80ddbec8 2018-04-29 stsp }
1602 26ed57b2 2018-05-19 stsp
1603 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1604 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1605 899d86c2 2018-05-10 stsp struct got_object_id *id)
1606 80ddbec8 2018-04-29 stsp {
1607 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1608 80ddbec8 2018-04-29 stsp
1609 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1610 80ddbec8 2018-04-29 stsp if (entry == NULL)
1611 899d86c2 2018-05-10 stsp return NULL;
1612 99db9666 2018-05-07 stsp
1613 899d86c2 2018-05-10 stsp entry->id = id;
1614 99db9666 2018-05-07 stsp entry->commit = commit;
1615 899d86c2 2018-05-10 stsp return entry;
1616 99db9666 2018-05-07 stsp }
1617 80ddbec8 2018-04-29 stsp
1618 99db9666 2018-05-07 stsp static void
1619 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1620 99db9666 2018-05-07 stsp {
1621 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1622 99db9666 2018-05-07 stsp
1623 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1624 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1625 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1626 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1627 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1628 99db9666 2018-05-07 stsp free(entry);
1629 99db9666 2018-05-07 stsp }
1630 99db9666 2018-05-07 stsp
1631 99db9666 2018-05-07 stsp static void
1632 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1633 99db9666 2018-05-07 stsp {
1634 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1635 99db9666 2018-05-07 stsp pop_commit(commits);
1636 c4972b91 2018-05-07 stsp }
1637 c4972b91 2018-05-07 stsp
1638 c4972b91 2018-05-07 stsp static const struct got_error *
1639 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1640 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1641 13add988 2019-10-15 stsp {
1642 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1643 13add988 2019-10-15 stsp regmatch_t regmatch;
1644 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1645 13add988 2019-10-15 stsp
1646 13add988 2019-10-15 stsp *have_match = 0;
1647 13add988 2019-10-15 stsp
1648 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1649 13add988 2019-10-15 stsp if (err)
1650 13add988 2019-10-15 stsp return err;
1651 13add988 2019-10-15 stsp
1652 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1653 13add988 2019-10-15 stsp if (err)
1654 13add988 2019-10-15 stsp goto done;
1655 13add988 2019-10-15 stsp
1656 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1657 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1658 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1659 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1660 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1661 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1662 13add988 2019-10-15 stsp *have_match = 1;
1663 13add988 2019-10-15 stsp done:
1664 13add988 2019-10-15 stsp free(id_str);
1665 13add988 2019-10-15 stsp free(logmsg);
1666 13add988 2019-10-15 stsp return err;
1667 13add988 2019-10-15 stsp }
1668 13add988 2019-10-15 stsp
1669 13add988 2019-10-15 stsp static const struct got_error *
1670 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1671 c4972b91 2018-05-07 stsp {
1672 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1673 9ba79e04 2018-06-11 stsp
1674 1a76625f 2018-10-22 stsp /*
1675 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1676 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1677 1a76625f 2018-10-22 stsp * while updating the display.
1678 1a76625f 2018-10-22 stsp */
1679 4e0d2870 2020-12-07 naddy do {
1680 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1681 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1682 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1683 1a76625f 2018-10-22 stsp int errcode;
1684 899d86c2 2018-05-10 stsp
1685 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1686 4e0d2870 2020-12-07 naddy NULL, NULL);
1687 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1688 ecb28ae0 2018-07-16 stsp break;
1689 899d86c2 2018-05-10 stsp
1690 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1691 9ba79e04 2018-06-11 stsp if (err)
1692 9ba79e04 2018-06-11 stsp break;
1693 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1694 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1695 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1696 9ba79e04 2018-06-11 stsp break;
1697 9ba79e04 2018-06-11 stsp }
1698 93e45b7c 2018-09-24 stsp
1699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1700 1a76625f 2018-10-22 stsp if (errcode) {
1701 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1702 13add988 2019-10-15 stsp "pthread_mutex_lock");
1703 1a76625f 2018-10-22 stsp break;
1704 1a76625f 2018-10-22 stsp }
1705 1a76625f 2018-10-22 stsp
1706 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1707 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1708 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1709 1a76625f 2018-10-22 stsp
1710 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1711 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1712 7c1452c1 2020-03-26 stsp int have_match;
1713 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1714 7c1452c1 2020-03-26 stsp if (err)
1715 7c1452c1 2020-03-26 stsp break;
1716 7c1452c1 2020-03-26 stsp if (have_match)
1717 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1718 13add988 2019-10-15 stsp }
1719 13add988 2019-10-15 stsp
1720 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1721 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1722 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1723 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1724 7c1452c1 2020-03-26 stsp if (err)
1725 13add988 2019-10-15 stsp break;
1726 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1727 899d86c2 2018-05-10 stsp
1728 9ba79e04 2018-06-11 stsp return err;
1729 0553a4e3 2018-04-30 stsp }
1730 0553a4e3 2018-04-30 stsp
1731 2b779855 2020-12-05 naddy static void
1732 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1733 2b779855 2020-12-05 naddy {
1734 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1735 2b779855 2020-12-05 naddy int ncommits = 0;
1736 2b779855 2020-12-05 naddy
1737 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1738 2b779855 2020-12-05 naddy while (entry) {
1739 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1740 2b779855 2020-12-05 naddy s->selected_entry = entry;
1741 2b779855 2020-12-05 naddy break;
1742 2b779855 2020-12-05 naddy }
1743 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1744 2b779855 2020-12-05 naddy ncommits++;
1745 2b779855 2020-12-05 naddy }
1746 2b779855 2020-12-05 naddy }
1747 2b779855 2020-12-05 naddy
1748 0553a4e3 2018-04-30 stsp static const struct got_error *
1749 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1750 0553a4e3 2018-04-30 stsp {
1751 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1752 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1753 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1754 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1755 60493ae3 2019-06-20 stsp int width;
1756 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1757 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1758 8b473291 2019-02-21 stsp char *refs_str = NULL;
1759 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1760 11b20872 2019-11-08 stsp struct tog_color *tc;
1761 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1762 0553a4e3 2018-04-30 stsp
1763 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1764 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1765 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1766 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1767 1a76625f 2018-10-22 stsp if (err)
1768 ecb28ae0 2018-07-16 stsp return err;
1769 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1770 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1771 d2075bf3 2020-12-25 stsp if (refs) {
1772 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1773 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1774 d2075bf3 2020-12-25 stsp if (err)
1775 d2075bf3 2020-12-25 stsp goto done;
1776 d2075bf3 2020-12-25 stsp }
1777 867c6645 2018-07-10 stsp }
1778 359bfafd 2019-02-22 stsp
1779 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1780 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1781 1a76625f 2018-10-22 stsp
1782 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1783 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1784 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1785 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1786 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1787 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1788 8f4ed634 2020-03-26 stsp goto done;
1789 8f4ed634 2020-03-26 stsp }
1790 8f4ed634 2020-03-26 stsp } else {
1791 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1792 f9686aa5 2020-03-27 stsp
1793 f9686aa5 2020-03-27 stsp if (view->searching) {
1794 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1795 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1796 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1797 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1798 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1799 f9686aa5 2020-03-27 stsp search_str = "searching...";
1800 f9686aa5 2020-03-27 stsp }
1801 f9686aa5 2020-03-27 stsp
1802 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1803 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1804 f9686aa5 2020-03-27 stsp search_str ? search_str :
1805 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1806 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1807 8f4ed634 2020-03-26 stsp goto done;
1808 8f4ed634 2020-03-26 stsp }
1809 8b473291 2019-02-21 stsp }
1810 1a76625f 2018-10-22 stsp
1811 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1812 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1813 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1814 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1815 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1816 1a76625f 2018-10-22 stsp header = NULL;
1817 1a76625f 2018-10-22 stsp goto done;
1818 1a76625f 2018-10-22 stsp }
1819 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1820 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1821 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1822 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1823 1a76625f 2018-10-22 stsp header = NULL;
1824 1a76625f 2018-10-22 stsp goto done;
1825 ecb28ae0 2018-07-16 stsp }
1826 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1827 1a76625f 2018-10-22 stsp if (err)
1828 1a76625f 2018-10-22 stsp goto done;
1829 867c6645 2018-07-10 stsp
1830 2814baeb 2018-08-01 stsp werase(view->window);
1831 867c6645 2018-07-10 stsp
1832 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1833 a3404814 2018-09-02 stsp wstandout(view->window);
1834 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1835 11b20872 2019-11-08 stsp if (tc)
1836 11b20872 2019-11-08 stsp wattr_on(view->window,
1837 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1838 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1839 11b20872 2019-11-08 stsp if (tc)
1840 11b20872 2019-11-08 stsp wattr_off(view->window,
1841 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1842 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1843 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1844 1a76625f 2018-10-22 stsp width++;
1845 1a76625f 2018-10-22 stsp }
1846 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1847 a3404814 2018-09-02 stsp wstandend(view->window);
1848 ecb28ae0 2018-07-16 stsp free(wline);
1849 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1850 1a76625f 2018-10-22 stsp goto done;
1851 0553a4e3 2018-04-30 stsp
1852 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
1853 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1854 5813d178 2019-03-09 stsp ncommits = 0;
1855 05171be4 2022-06-23 thomas view->maxx = 0;
1856 5813d178 2019-03-09 stsp while (entry) {
1857 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
1858 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
1859 5813d178 2019-03-09 stsp int width;
1860 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1861 5813d178 2019-03-09 stsp break;
1862 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1863 5813d178 2019-03-09 stsp if (author == NULL) {
1864 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1865 5813d178 2019-03-09 stsp goto done;
1866 5813d178 2019-03-09 stsp }
1867 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1868 27a741e5 2019-09-11 stsp date_display_cols);
1869 5813d178 2019-03-09 stsp if (author_cols < width)
1870 5813d178 2019-03-09 stsp author_cols = width;
1871 5813d178 2019-03-09 stsp free(wauthor);
1872 5813d178 2019-03-09 stsp free(author);
1873 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
1874 05171be4 2022-06-23 thomas if (err)
1875 05171be4 2022-06-23 thomas goto done;
1876 05171be4 2022-06-23 thomas msg = msg0;
1877 05171be4 2022-06-23 thomas while (*msg == '\n')
1878 05171be4 2022-06-23 thomas ++msg;
1879 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
1880 331b1a16 2022-06-23 thomas *eol = '\0';
1881 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1882 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
1883 331b1a16 2022-06-23 thomas if (err)
1884 331b1a16 2022-06-23 thomas goto done;
1885 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
1886 05171be4 2022-06-23 thomas free(msg0);
1887 331b1a16 2022-06-23 thomas free(wmsg);
1888 7ca04879 2019-10-19 stsp ncommits++;
1889 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1890 5813d178 2019-03-09 stsp }
1891 5813d178 2019-03-09 stsp
1892 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1893 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1894 867c6645 2018-07-10 stsp ncommits = 0;
1895 899d86c2 2018-05-10 stsp while (entry) {
1896 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1897 899d86c2 2018-05-10 stsp break;
1898 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1899 2814baeb 2018-08-01 stsp wstandout(view->window);
1900 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1901 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1902 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1903 2814baeb 2018-08-01 stsp wstandend(view->window);
1904 0553a4e3 2018-04-30 stsp if (err)
1905 60493ae3 2019-06-20 stsp goto done;
1906 0553a4e3 2018-04-30 stsp ncommits++;
1907 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1908 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1909 80ddbec8 2018-04-29 stsp }
1910 80ddbec8 2018-04-29 stsp
1911 1a57306a 2018-09-02 stsp view_vborder(view);
1912 dd038bc6 2021-09-21 thomas.ad update_panels();
1913 dd038bc6 2021-09-21 thomas.ad doupdate();
1914 1a76625f 2018-10-22 stsp done:
1915 1a76625f 2018-10-22 stsp free(id_str);
1916 8b473291 2019-02-21 stsp free(refs_str);
1917 1a76625f 2018-10-22 stsp free(ncommits_str);
1918 1a76625f 2018-10-22 stsp free(header);
1919 80ddbec8 2018-04-29 stsp return err;
1920 9f7d7167 2018-04-29 stsp }
1921 07b55e75 2018-05-10 stsp
1922 07b55e75 2018-05-10 stsp static void
1923 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1924 07b55e75 2018-05-10 stsp {
1925 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1926 07b55e75 2018-05-10 stsp int nscrolled = 0;
1927 07b55e75 2018-05-10 stsp
1928 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1929 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1930 07b55e75 2018-05-10 stsp return;
1931 9f7d7167 2018-04-29 stsp
1932 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1933 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1934 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1935 07b55e75 2018-05-10 stsp if (entry) {
1936 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1937 07b55e75 2018-05-10 stsp nscrolled++;
1938 07b55e75 2018-05-10 stsp }
1939 07b55e75 2018-05-10 stsp }
1940 aa075928 2018-05-10 stsp }
1941 aa075928 2018-05-10 stsp
1942 aa075928 2018-05-10 stsp static const struct got_error *
1943 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1944 aa075928 2018-05-10 stsp {
1945 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1946 5e224a3e 2019-02-22 stsp int errcode;
1947 8a42fca8 2019-02-22 stsp
1948 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1949 aa075928 2018-05-10 stsp
1950 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1951 ffe38506 2020-12-01 naddy if (ta->log_complete)
1952 5e224a3e 2019-02-22 stsp break;
1953 b295e71b 2019-02-22 stsp
1954 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1955 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1956 7aafa0d1 2019-02-22 stsp if (errcode)
1957 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1958 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1959 7c1452c1 2020-03-26 stsp
1960 7c1452c1 2020-03-26 stsp /*
1961 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1962 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1963 7c1452c1 2020-03-26 stsp */
1964 7c1452c1 2020-03-26 stsp if (!wait)
1965 7c1452c1 2020-03-26 stsp break;
1966 7c1452c1 2020-03-26 stsp
1967 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1968 ffe38506 2020-12-01 naddy show_log_view(view);
1969 7c1452c1 2020-03-26 stsp update_panels();
1970 7c1452c1 2020-03-26 stsp doupdate();
1971 7c1452c1 2020-03-26 stsp
1972 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1973 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1974 82954512 2020-02-03 stsp if (errcode)
1975 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1976 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1977 82954512 2020-02-03 stsp
1978 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1979 ffe38506 2020-12-01 naddy show_log_view(view);
1980 7c1452c1 2020-03-26 stsp update_panels();
1981 7c1452c1 2020-03-26 stsp doupdate();
1982 5e224a3e 2019-02-22 stsp }
1983 5e224a3e 2019-02-22 stsp
1984 5e224a3e 2019-02-22 stsp return NULL;
1985 5e224a3e 2019-02-22 stsp }
1986 5e224a3e 2019-02-22 stsp
1987 5e224a3e 2019-02-22 stsp static const struct got_error *
1988 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1989 5e224a3e 2019-02-22 stsp {
1990 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1991 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1992 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1993 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1994 5e224a3e 2019-02-22 stsp
1995 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1996 5e224a3e 2019-02-22 stsp return NULL;
1997 5e224a3e 2019-02-22 stsp
1998 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1999 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2000 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2001 08ebd0a9 2019-02-22 stsp /*
2002 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2003 08ebd0a9 2019-02-22 stsp */
2004 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2005 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2006 5e224a3e 2019-02-22 stsp if (err)
2007 5e224a3e 2019-02-22 stsp return err;
2008 7aafa0d1 2019-02-22 stsp }
2009 b295e71b 2019-02-22 stsp
2010 7aafa0d1 2019-02-22 stsp do {
2011 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2012 00ba99a7 2019-05-12 jcs if (pentry == NULL)
2013 88048b54 2019-02-21 stsp break;
2014 88048b54 2019-02-21 stsp
2015 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
2016 aa075928 2018-05-10 stsp
2017 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2018 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2019 dd0a52c1 2018-05-20 stsp break;
2020 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2021 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2022 aa075928 2018-05-10 stsp
2023 dd0a52c1 2018-05-20 stsp return err;
2024 07b55e75 2018-05-10 stsp }
2025 4a7f7875 2018-05-10 stsp
2026 cd0acaa7 2018-05-20 stsp static const struct got_error *
2027 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2028 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2029 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2030 cd0acaa7 2018-05-20 stsp {
2031 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2032 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2033 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2034 cd0acaa7 2018-05-20 stsp
2035 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2036 15a94983 2018-12-23 stsp if (diff_view == NULL)
2037 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2038 ea5e7bb5 2018-08-01 stsp
2039 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2040 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2041 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2042 e5a0f69f 2018-08-18 stsp if (err == NULL)
2043 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2044 cd0acaa7 2018-05-20 stsp return err;
2045 4a7f7875 2018-05-10 stsp }
2046 4a7f7875 2018-05-10 stsp
2047 80ddbec8 2018-04-29 stsp static const struct got_error *
2048 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2049 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2050 9343a5fb 2018-06-23 stsp {
2051 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2052 941e9f74 2019-05-21 stsp
2053 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2054 941e9f74 2019-05-21 stsp if (parent == NULL)
2055 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2056 941e9f74 2019-05-21 stsp
2057 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2058 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2059 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2060 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2061 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2062 941e9f74 2019-05-21 stsp s->tree = subtree;
2063 941e9f74 2019-05-21 stsp s->selected = 0;
2064 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2065 941e9f74 2019-05-21 stsp return NULL;
2066 941e9f74 2019-05-21 stsp }
2067 941e9f74 2019-05-21 stsp
2068 941e9f74 2019-05-21 stsp static const struct got_error *
2069 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2070 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2071 941e9f74 2019-05-21 stsp {
2072 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2073 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2074 941e9f74 2019-05-21 stsp const char *p;
2075 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2076 9343a5fb 2018-06-23 stsp
2077 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2078 941e9f74 2019-05-21 stsp p = path;
2079 941e9f74 2019-05-21 stsp while (*p) {
2080 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2081 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2082 56e0773d 2019-11-28 stsp char *te_name;
2083 33cbf02b 2020-01-12 stsp
2084 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2085 33cbf02b 2020-01-12 stsp p++;
2086 941e9f74 2019-05-21 stsp
2087 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2088 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2089 941e9f74 2019-05-21 stsp if (slash == NULL)
2090 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2091 33cbf02b 2020-01-12 stsp else
2092 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2093 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2094 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2095 56e0773d 2019-11-28 stsp break;
2096 941e9f74 2019-05-21 stsp }
2097 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2098 56e0773d 2019-11-28 stsp if (te == NULL) {
2099 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2100 56e0773d 2019-11-28 stsp free(te_name);
2101 941e9f74 2019-05-21 stsp break;
2102 941e9f74 2019-05-21 stsp }
2103 56e0773d 2019-11-28 stsp free(te_name);
2104 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2105 941e9f74 2019-05-21 stsp
2106 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2107 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2108 b03c880f 2019-05-21 stsp
2109 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2110 941e9f74 2019-05-21 stsp if (slash)
2111 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2112 941e9f74 2019-05-21 stsp else
2113 941e9f74 2019-05-21 stsp subpath = strdup(path);
2114 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2115 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2116 941e9f74 2019-05-21 stsp break;
2117 941e9f74 2019-05-21 stsp }
2118 941e9f74 2019-05-21 stsp
2119 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2120 941e9f74 2019-05-21 stsp subpath);
2121 941e9f74 2019-05-21 stsp if (err)
2122 941e9f74 2019-05-21 stsp break;
2123 941e9f74 2019-05-21 stsp
2124 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2125 941e9f74 2019-05-21 stsp free(tree_id);
2126 941e9f74 2019-05-21 stsp if (err)
2127 941e9f74 2019-05-21 stsp break;
2128 941e9f74 2019-05-21 stsp
2129 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2130 941e9f74 2019-05-21 stsp if (err) {
2131 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2132 941e9f74 2019-05-21 stsp break;
2133 941e9f74 2019-05-21 stsp }
2134 941e9f74 2019-05-21 stsp if (slash == NULL)
2135 941e9f74 2019-05-21 stsp break;
2136 941e9f74 2019-05-21 stsp free(subpath);
2137 941e9f74 2019-05-21 stsp subpath = NULL;
2138 941e9f74 2019-05-21 stsp p = slash;
2139 941e9f74 2019-05-21 stsp }
2140 941e9f74 2019-05-21 stsp
2141 941e9f74 2019-05-21 stsp free(subpath);
2142 1a76625f 2018-10-22 stsp return err;
2143 61266923 2020-01-14 stsp }
2144 61266923 2020-01-14 stsp
2145 61266923 2020-01-14 stsp static const struct got_error *
2146 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2147 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2148 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2149 55cccc34 2020-02-20 stsp {
2150 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2151 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2152 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2153 55cccc34 2020-02-20 stsp
2154 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2155 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2156 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2157 55cccc34 2020-02-20 stsp
2158 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2159 bc573f3b 2021-07-10 stsp if (err)
2160 55cccc34 2020-02-20 stsp return err;
2161 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2162 55cccc34 2020-02-20 stsp
2163 55cccc34 2020-02-20 stsp *new_view = tree_view;
2164 55cccc34 2020-02-20 stsp
2165 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2166 55cccc34 2020-02-20 stsp return NULL;
2167 55cccc34 2020-02-20 stsp
2168 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2169 55cccc34 2020-02-20 stsp }
2170 55cccc34 2020-02-20 stsp
2171 55cccc34 2020-02-20 stsp static const struct got_error *
2172 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2173 61266923 2020-01-14 stsp {
2174 61266923 2020-01-14 stsp sigset_t sigset;
2175 61266923 2020-01-14 stsp int errcode;
2176 61266923 2020-01-14 stsp
2177 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2178 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2179 61266923 2020-01-14 stsp
2180 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2181 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2182 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2183 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2184 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2185 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2186 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2187 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2188 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2189 61266923 2020-01-14 stsp
2190 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2191 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2192 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2193 61266923 2020-01-14 stsp
2194 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2195 61266923 2020-01-14 stsp if (errcode)
2196 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2197 61266923 2020-01-14 stsp
2198 61266923 2020-01-14 stsp return NULL;
2199 1a76625f 2018-10-22 stsp }
2200 1a76625f 2018-10-22 stsp
2201 1a76625f 2018-10-22 stsp static void *
2202 1a76625f 2018-10-22 stsp log_thread(void *arg)
2203 1a76625f 2018-10-22 stsp {
2204 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2205 1a76625f 2018-10-22 stsp int errcode = 0;
2206 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2207 1a76625f 2018-10-22 stsp int done = 0;
2208 61266923 2020-01-14 stsp
2209 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2210 61266923 2020-01-14 stsp if (err)
2211 61266923 2020-01-14 stsp return (void *)err;
2212 1a76625f 2018-10-22 stsp
2213 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2214 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2215 1a76625f 2018-10-22 stsp if (err) {
2216 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2217 1a76625f 2018-10-22 stsp return (void *)err;
2218 1a76625f 2018-10-22 stsp err = NULL;
2219 1a76625f 2018-10-22 stsp done = 1;
2220 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2221 1a76625f 2018-10-22 stsp a->commits_needed--;
2222 1a76625f 2018-10-22 stsp
2223 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2224 3abe8080 2019-04-10 stsp if (errcode) {
2225 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2226 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2227 3abe8080 2019-04-10 stsp break;
2228 3abe8080 2019-04-10 stsp } else if (*a->quit)
2229 1a76625f 2018-10-22 stsp done = 1;
2230 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2231 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2232 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2233 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2234 1a76625f 2018-10-22 stsp }
2235 1a76625f 2018-10-22 stsp
2236 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2237 7c1452c1 2020-03-26 stsp if (errcode) {
2238 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2239 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2240 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2241 7c1452c1 2020-03-26 stsp break;
2242 7c1452c1 2020-03-26 stsp }
2243 7c1452c1 2020-03-26 stsp
2244 1a76625f 2018-10-22 stsp if (done)
2245 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2246 7c1452c1 2020-03-26 stsp else {
2247 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2248 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2249 7c1452c1 2020-03-26 stsp &tog_mutex);
2250 7c1452c1 2020-03-26 stsp if (errcode)
2251 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2252 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2253 21355643 2020-12-06 stsp if (*a->quit)
2254 21355643 2020-12-06 stsp done = 1;
2255 7c1452c1 2020-03-26 stsp }
2256 1a76625f 2018-10-22 stsp }
2257 1a76625f 2018-10-22 stsp
2258 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2259 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2260 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2261 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2262 1a76625f 2018-10-22 stsp }
2263 3abe8080 2019-04-10 stsp a->log_complete = 1;
2264 1a76625f 2018-10-22 stsp return (void *)err;
2265 1a76625f 2018-10-22 stsp }
2266 1a76625f 2018-10-22 stsp
2267 1a76625f 2018-10-22 stsp static const struct got_error *
2268 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2269 1a76625f 2018-10-22 stsp {
2270 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2271 1a76625f 2018-10-22 stsp int errcode;
2272 1a76625f 2018-10-22 stsp
2273 1a76625f 2018-10-22 stsp if (s->thread) {
2274 1a76625f 2018-10-22 stsp s->quit = 1;
2275 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2276 1a76625f 2018-10-22 stsp if (errcode)
2277 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2278 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2279 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2280 1a76625f 2018-10-22 stsp if (errcode)
2281 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2282 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2283 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2284 1a76625f 2018-10-22 stsp if (errcode)
2285 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2286 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2287 1a76625f 2018-10-22 stsp if (errcode)
2288 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2289 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2290 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2291 1a76625f 2018-10-22 stsp }
2292 1a76625f 2018-10-22 stsp
2293 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2294 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2295 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2296 1af5eddf 2022-06-23 thomas }
2297 1af5eddf 2022-06-23 thomas
2298 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2299 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2300 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2301 1af5eddf 2022-06-23 thomas if (err == NULL)
2302 1af5eddf 2022-06-23 thomas err = pack_err;
2303 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2304 1a76625f 2018-10-22 stsp }
2305 1a76625f 2018-10-22 stsp
2306 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2307 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2308 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2309 1a76625f 2018-10-22 stsp }
2310 1a76625f 2018-10-22 stsp
2311 9343a5fb 2018-06-23 stsp return err;
2312 9343a5fb 2018-06-23 stsp }
2313 9343a5fb 2018-06-23 stsp
2314 9343a5fb 2018-06-23 stsp static const struct got_error *
2315 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2316 1a76625f 2018-10-22 stsp {
2317 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2318 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2319 276b94a1 2020-11-13 naddy int errcode;
2320 1a76625f 2018-10-22 stsp
2321 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2322 276b94a1 2020-11-13 naddy
2323 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2324 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2325 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2326 276b94a1 2020-11-13 naddy
2327 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2328 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2329 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2330 276b94a1 2020-11-13 naddy
2331 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2332 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2333 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2334 1a76625f 2018-10-22 stsp free(s->start_id);
2335 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2336 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2337 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2338 1a76625f 2018-10-22 stsp return err;
2339 1a76625f 2018-10-22 stsp }
2340 1a76625f 2018-10-22 stsp
2341 1a76625f 2018-10-22 stsp static const struct got_error *
2342 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2343 60493ae3 2019-06-20 stsp {
2344 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2345 60493ae3 2019-06-20 stsp
2346 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2347 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2348 60493ae3 2019-06-20 stsp return NULL;
2349 60493ae3 2019-06-20 stsp }
2350 60493ae3 2019-06-20 stsp
2351 60493ae3 2019-06-20 stsp static const struct got_error *
2352 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2353 60493ae3 2019-06-20 stsp {
2354 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2355 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2356 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2357 60493ae3 2019-06-20 stsp
2358 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2359 f9686aa5 2020-03-27 stsp show_log_view(view);
2360 f9686aa5 2020-03-27 stsp update_panels();
2361 f9686aa5 2020-03-27 stsp doupdate();
2362 f9686aa5 2020-03-27 stsp
2363 96e2b566 2019-07-08 stsp if (s->search_entry) {
2364 13add988 2019-10-15 stsp int errcode, ch;
2365 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2366 13add988 2019-10-15 stsp if (errcode)
2367 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2368 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2369 13add988 2019-10-15 stsp ch = wgetch(view->window);
2370 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2371 13add988 2019-10-15 stsp if (errcode)
2372 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2373 13add988 2019-10-15 stsp "pthread_mutex_lock");
2374 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2375 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2376 678cbce5 2019-07-28 stsp return NULL;
2377 678cbce5 2019-07-28 stsp }
2378 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2379 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2380 96e2b566 2019-07-08 stsp else
2381 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2382 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2383 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2384 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2385 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2386 b1bf1435 2019-06-21 stsp else
2387 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2388 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2389 20be8d96 2019-06-21 stsp } else {
2390 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2391 20be8d96 2019-06-21 stsp }
2392 60493ae3 2019-06-20 stsp
2393 60493ae3 2019-06-20 stsp while (1) {
2394 13add988 2019-10-15 stsp int have_match = 0;
2395 13add988 2019-10-15 stsp
2396 60493ae3 2019-06-20 stsp if (entry == NULL) {
2397 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2398 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2399 f9967bca 2020-03-27 stsp view->search_next_done =
2400 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2401 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2402 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2403 f801134a 2019-06-25 stsp return NULL;
2404 60493ae3 2019-06-20 stsp }
2405 96e2b566 2019-07-08 stsp /*
2406 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2407 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2408 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2409 96e2b566 2019-07-08 stsp */
2410 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2411 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2412 60493ae3 2019-06-20 stsp }
2413 60493ae3 2019-06-20 stsp
2414 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2415 13add988 2019-10-15 stsp &view->regex);
2416 5943eee2 2019-08-13 stsp if (err)
2417 13add988 2019-10-15 stsp break;
2418 13add988 2019-10-15 stsp if (have_match) {
2419 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2420 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2421 60493ae3 2019-06-20 stsp break;
2422 60493ae3 2019-06-20 stsp }
2423 13add988 2019-10-15 stsp
2424 96e2b566 2019-07-08 stsp s->search_entry = entry;
2425 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2426 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2427 b1bf1435 2019-06-21 stsp else
2428 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2429 60493ae3 2019-06-20 stsp }
2430 60493ae3 2019-06-20 stsp
2431 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2432 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2433 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2434 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2435 60493ae3 2019-06-20 stsp if (err)
2436 60493ae3 2019-06-20 stsp return err;
2437 ead14cbe 2019-06-21 stsp cur++;
2438 ead14cbe 2019-06-21 stsp }
2439 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2440 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2441 60493ae3 2019-06-20 stsp if (err)
2442 60493ae3 2019-06-20 stsp return err;
2443 ead14cbe 2019-06-21 stsp cur--;
2444 60493ae3 2019-06-20 stsp }
2445 60493ae3 2019-06-20 stsp }
2446 60493ae3 2019-06-20 stsp
2447 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2448 96e2b566 2019-07-08 stsp
2449 60493ae3 2019-06-20 stsp return NULL;
2450 60493ae3 2019-06-20 stsp }
2451 60493ae3 2019-06-20 stsp
2452 60493ae3 2019-06-20 stsp static const struct got_error *
2453 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2454 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2455 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2456 80ddbec8 2018-04-29 stsp {
2457 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2458 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2459 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2460 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2461 1a76625f 2018-10-22 stsp int errcode;
2462 80ddbec8 2018-04-29 stsp
2463 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2464 f135c941 2020-02-20 stsp free(s->in_repo_path);
2465 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2466 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2467 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2468 f135c941 2020-02-20 stsp }
2469 ecb28ae0 2018-07-16 stsp
2470 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2471 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2472 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2473 78756c87 2020-11-24 stsp
2474 fb2756b9 2018-08-04 stsp s->repo = repo;
2475 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2476 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2477 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2478 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2479 9cd7cbd1 2020-12-07 stsp goto done;
2480 9cd7cbd1 2020-12-07 stsp }
2481 9cd7cbd1 2020-12-07 stsp }
2482 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2483 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2484 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2485 5036bf37 2018-09-24 stsp goto done;
2486 5036bf37 2018-09-24 stsp }
2487 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2488 e5a0f69f 2018-08-18 stsp
2489 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2490 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2491 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2492 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2493 11b20872 2019-11-08 stsp if (err)
2494 11b20872 2019-11-08 stsp goto done;
2495 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2496 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2497 11b20872 2019-11-08 stsp if (err) {
2498 11b20872 2019-11-08 stsp free_colors(&s->colors);
2499 11b20872 2019-11-08 stsp goto done;
2500 11b20872 2019-11-08 stsp }
2501 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2502 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2503 11b20872 2019-11-08 stsp if (err) {
2504 11b20872 2019-11-08 stsp free_colors(&s->colors);
2505 11b20872 2019-11-08 stsp goto done;
2506 11b20872 2019-11-08 stsp }
2507 11b20872 2019-11-08 stsp }
2508 11b20872 2019-11-08 stsp
2509 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2510 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2511 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2512 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2513 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2514 1a76625f 2018-10-22 stsp
2515 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2516 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2517 1af5eddf 2022-06-23 thomas if (err)
2518 1af5eddf 2022-06-23 thomas goto done;
2519 1af5eddf 2022-06-23 thomas }
2520 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2521 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2522 7cd52833 2022-06-23 thomas if (err)
2523 7cd52833 2022-06-23 thomas goto done;
2524 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2525 b672a97a 2020-01-27 stsp !s->log_branches);
2526 1a76625f 2018-10-22 stsp if (err)
2527 1a76625f 2018-10-22 stsp goto done;
2528 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2529 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2530 c5b78334 2020-01-12 stsp if (err)
2531 c5b78334 2020-01-12 stsp goto done;
2532 1a76625f 2018-10-22 stsp
2533 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2534 1a76625f 2018-10-22 stsp if (errcode) {
2535 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2536 1a76625f 2018-10-22 stsp goto done;
2537 1a76625f 2018-10-22 stsp }
2538 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2539 7c1452c1 2020-03-26 stsp if (errcode) {
2540 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2541 7c1452c1 2020-03-26 stsp goto done;
2542 7c1452c1 2020-03-26 stsp }
2543 1a76625f 2018-10-22 stsp
2544 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2545 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2546 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2547 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2548 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2549 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2550 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2551 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2552 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2553 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2554 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2555 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2556 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2557 ba4f502b 2018-08-04 stsp done:
2558 1a76625f 2018-10-22 stsp if (err)
2559 1a76625f 2018-10-22 stsp close_log_view(view);
2560 ba4f502b 2018-08-04 stsp return err;
2561 ba4f502b 2018-08-04 stsp }
2562 ba4f502b 2018-08-04 stsp
2563 e5a0f69f 2018-08-18 stsp static const struct got_error *
2564 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2565 ba4f502b 2018-08-04 stsp {
2566 f2f6d207 2020-11-24 stsp const struct got_error *err;
2567 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2568 ba4f502b 2018-08-04 stsp
2569 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2570 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2571 2b380cc8 2018-10-24 stsp &s->thread_args);
2572 2b380cc8 2018-10-24 stsp if (errcode)
2573 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2574 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2575 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2576 f2f6d207 2020-11-24 stsp if (err)
2577 f2f6d207 2020-11-24 stsp return err;
2578 f2f6d207 2020-11-24 stsp }
2579 2b380cc8 2018-10-24 stsp }
2580 2b380cc8 2018-10-24 stsp
2581 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2582 e5a0f69f 2018-08-18 stsp }
2583 04cc582a 2018-08-01 stsp
2584 e5a0f69f 2018-08-18 stsp static const struct got_error *
2585 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2586 e5a0f69f 2018-08-18 stsp {
2587 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2588 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2589 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2590 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2591 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2592 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2593 80ddbec8 2018-04-29 stsp
2594 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2595 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2596 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2597 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2598 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2599 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2600 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2601 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2602 fb280deb 2021-08-30 stsp select_commit(s);
2603 fb280deb 2021-08-30 stsp }
2604 528dedf3 2021-08-30 stsp return NULL;
2605 528dedf3 2021-08-30 stsp }
2606 528dedf3 2021-08-30 stsp
2607 528dedf3 2021-08-30 stsp switch (ch) {
2608 1e37a5c2 2019-05-12 jcs case 'q':
2609 1e37a5c2 2019-05-12 jcs s->quit = 1;
2610 05171be4 2022-06-23 thomas break;
2611 05171be4 2022-06-23 thomas case '0':
2612 05171be4 2022-06-23 thomas view->x = 0;
2613 05171be4 2022-06-23 thomas break;
2614 05171be4 2022-06-23 thomas case '$':
2615 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
2616 05171be4 2022-06-23 thomas break;
2617 05171be4 2022-06-23 thomas case KEY_RIGHT:
2618 05171be4 2022-06-23 thomas case 'l':
2619 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
2620 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
2621 1e37a5c2 2019-05-12 jcs break;
2622 05171be4 2022-06-23 thomas case KEY_LEFT:
2623 05171be4 2022-06-23 thomas case 'h':
2624 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
2625 05171be4 2022-06-23 thomas break;
2626 1e37a5c2 2019-05-12 jcs case 'k':
2627 1e37a5c2 2019-05-12 jcs case KEY_UP:
2628 1e37a5c2 2019-05-12 jcs case '<':
2629 1e37a5c2 2019-05-12 jcs case ',':
2630 f7140bf5 2021-10-17 thomas case CTRL('p'):
2631 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2632 1a76625f 2018-10-22 stsp break;
2633 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2634 1e37a5c2 2019-05-12 jcs s->selected--;
2635 1144d21a 2019-06-21 stsp else
2636 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2637 912a3f79 2021-08-30 j select_commit(s);
2638 912a3f79 2021-08-30 j break;
2639 912a3f79 2021-08-30 j case 'g':
2640 912a3f79 2021-08-30 j case KEY_HOME:
2641 912a3f79 2021-08-30 j s->selected = 0;
2642 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2643 2b779855 2020-12-05 naddy select_commit(s);
2644 1e37a5c2 2019-05-12 jcs break;
2645 70f17a53 2022-06-13 thomas case CTRL('u'):
2646 23427b14 2022-06-23 thomas case 'u':
2647 70f17a53 2022-06-13 thomas nscroll /= 2;
2648 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2649 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2650 a4292ac5 2019-05-12 jcs case CTRL('b'):
2651 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2652 e5a0f69f 2018-08-18 stsp break;
2653 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2654 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2655 2b779855 2020-12-05 naddy else
2656 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2657 2b779855 2020-12-05 naddy select_commit(s);
2658 1e37a5c2 2019-05-12 jcs break;
2659 1e37a5c2 2019-05-12 jcs case 'j':
2660 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2661 1e37a5c2 2019-05-12 jcs case '>':
2662 1e37a5c2 2019-05-12 jcs case '.':
2663 f7140bf5 2021-10-17 thomas case CTRL('n'):
2664 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2665 e5a0f69f 2018-08-18 stsp break;
2666 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2667 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2668 1e37a5c2 2019-05-12 jcs s->selected++;
2669 2b779855 2020-12-05 naddy else {
2670 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2671 2b779855 2020-12-05 naddy if (err)
2672 2b779855 2020-12-05 naddy break;
2673 912a3f79 2021-08-30 j }
2674 912a3f79 2021-08-30 j select_commit(s);
2675 912a3f79 2021-08-30 j break;
2676 912a3f79 2021-08-30 j case 'G':
2677 912a3f79 2021-08-30 j case KEY_END: {
2678 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2679 912a3f79 2021-08-30 j * traverse them all. */
2680 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2681 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2682 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2683 80ddbec8 2018-04-29 stsp }
2684 912a3f79 2021-08-30 j
2685 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2686 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2687 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2688 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2689 f3bc9f1d 2021-09-05 naddy break;
2690 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2691 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2692 f3bc9f1d 2021-09-05 naddy }
2693 f3bc9f1d 2021-09-05 naddy if (n > 0)
2694 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2695 2b779855 2020-12-05 naddy select_commit(s);
2696 1e37a5c2 2019-05-12 jcs break;
2697 912a3f79 2021-08-30 j }
2698 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2699 23427b14 2022-06-23 thomas case 'd':
2700 70f17a53 2022-06-13 thomas nscroll /= 2;
2701 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2702 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2703 70f17a53 2022-06-13 thomas case CTRL('f'): {
2704 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2705 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2706 1e37a5c2 2019-05-12 jcs if (first == NULL)
2707 e5a0f69f 2018-08-18 stsp break;
2708 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2709 1cae65b4 2019-09-22 stsp if (err)
2710 1cae65b4 2019-09-22 stsp break;
2711 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2712 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2713 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2714 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2715 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2716 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2717 1e37a5c2 2019-05-12 jcs }
2718 2b779855 2020-12-05 naddy select_commit(s);
2719 1e37a5c2 2019-05-12 jcs break;
2720 1e37a5c2 2019-05-12 jcs }
2721 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2722 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2723 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2724 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2725 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2726 2b779855 2020-12-05 naddy select_commit(s);
2727 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2728 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2729 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2730 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2731 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2732 0bf7f153 2020-12-02 naddy }
2733 1e37a5c2 2019-05-12 jcs break;
2734 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2735 87c7274c 2019-05-12 jcs case ' ':
2736 1e37a5c2 2019-05-12 jcs case '\r':
2737 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2738 e5a0f69f 2018-08-18 stsp break;
2739 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2740 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2741 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2742 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2743 78756c87 2020-11-24 stsp view, s->repo);
2744 1e37a5c2 2019-05-12 jcs if (err)
2745 1e37a5c2 2019-05-12 jcs break;
2746 e78dc838 2020-12-04 stsp view->focussed = 0;
2747 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2748 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2749 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2750 f7013a22 2018-10-24 stsp if (err)
2751 1e37a5c2 2019-05-12 jcs return err;
2752 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2753 e78dc838 2020-12-04 stsp view->focus_child = 1;
2754 1e37a5c2 2019-05-12 jcs } else
2755 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2756 1e37a5c2 2019-05-12 jcs break;
2757 1e37a5c2 2019-05-12 jcs case 't':
2758 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2759 5036bf37 2018-09-24 stsp break;
2760 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2761 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2762 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2763 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2764 4e97c21c 2020-12-06 stsp s->repo);
2765 1e37a5c2 2019-05-12 jcs if (err)
2766 e5a0f69f 2018-08-18 stsp break;
2767 e78dc838 2020-12-04 stsp view->focussed = 0;
2768 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2769 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2770 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2771 1e37a5c2 2019-05-12 jcs if (err)
2772 1e37a5c2 2019-05-12 jcs return err;
2773 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2774 e78dc838 2020-12-04 stsp view->focus_child = 1;
2775 1e37a5c2 2019-05-12 jcs } else
2776 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2777 1e37a5c2 2019-05-12 jcs break;
2778 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2779 21355643 2020-12-06 stsp case CTRL('l'):
2780 21355643 2020-12-06 stsp case 'B':
2781 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2782 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2783 1e37a5c2 2019-05-12 jcs break;
2784 21355643 2020-12-06 stsp err = stop_log_thread(s);
2785 74cfe85e 2020-10-20 stsp if (err)
2786 74cfe85e 2020-10-20 stsp return err;
2787 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2788 21355643 2020-12-06 stsp char *parent_path;
2789 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2790 21355643 2020-12-06 stsp if (err)
2791 21355643 2020-12-06 stsp return err;
2792 21355643 2020-12-06 stsp free(s->in_repo_path);
2793 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2794 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2795 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2796 21355643 2020-12-06 stsp struct got_object_id *start_id;
2797 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2798 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2799 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2800 21355643 2020-12-06 stsp if (err)
2801 21355643 2020-12-06 stsp return err;
2802 21355643 2020-12-06 stsp free(s->start_id);
2803 21355643 2020-12-06 stsp s->start_id = start_id;
2804 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2805 21355643 2020-12-06 stsp } else /* 'B' */
2806 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2807 21355643 2020-12-06 stsp
2808 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2809 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2810 bf7e79b3 2022-06-23 thomas if (err)
2811 bf7e79b3 2022-06-23 thomas return err;
2812 bf7e79b3 2022-06-23 thomas }
2813 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
2814 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
2815 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2816 74cfe85e 2020-10-20 stsp if (err)
2817 21355643 2020-12-06 stsp return err;
2818 51a10b52 2020-12-26 stsp tog_free_refs();
2819 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2820 ca51c541 2020-12-07 stsp if (err)
2821 ca51c541 2020-12-07 stsp return err;
2822 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2823 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2824 d01904d4 2019-06-25 stsp if (err)
2825 d01904d4 2019-06-25 stsp return err;
2826 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2827 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2828 b672a97a 2020-01-27 stsp if (err)
2829 b672a97a 2020-01-27 stsp return err;
2830 21355643 2020-12-06 stsp free_commits(&s->commits);
2831 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2832 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2833 21355643 2020-12-06 stsp s->selected_entry = NULL;
2834 21355643 2020-12-06 stsp s->selected = 0;
2835 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2836 21355643 2020-12-06 stsp s->quit = 0;
2837 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2838 d01904d4 2019-06-25 stsp break;
2839 6458efa5 2020-11-24 stsp case 'r':
2840 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2841 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2842 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2843 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2844 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2845 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2846 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2847 6458efa5 2020-11-24 stsp if (err) {
2848 6458efa5 2020-11-24 stsp view_close(ref_view);
2849 6458efa5 2020-11-24 stsp return err;
2850 6458efa5 2020-11-24 stsp }
2851 e78dc838 2020-12-04 stsp view->focussed = 0;
2852 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2853 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2854 6458efa5 2020-11-24 stsp err = view_close_child(view);
2855 6458efa5 2020-11-24 stsp if (err)
2856 6458efa5 2020-11-24 stsp return err;
2857 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2858 e78dc838 2020-12-04 stsp view->focus_child = 1;
2859 6458efa5 2020-11-24 stsp } else
2860 6458efa5 2020-11-24 stsp *new_view = ref_view;
2861 6458efa5 2020-11-24 stsp break;
2862 1e37a5c2 2019-05-12 jcs default:
2863 1e37a5c2 2019-05-12 jcs break;
2864 899d86c2 2018-05-10 stsp }
2865 e5a0f69f 2018-08-18 stsp
2866 80ddbec8 2018-04-29 stsp return err;
2867 80ddbec8 2018-04-29 stsp }
2868 80ddbec8 2018-04-29 stsp
2869 4ed7e80c 2018-05-20 stsp static const struct got_error *
2870 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2871 c2db6724 2019-01-04 stsp {
2872 c2db6724 2019-01-04 stsp const struct got_error *error;
2873 c2db6724 2019-01-04 stsp
2874 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2875 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2876 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2877 37c06ea4 2019-07-15 stsp #endif
2878 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2879 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2880 c2db6724 2019-01-04 stsp
2881 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2882 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2883 c2db6724 2019-01-04 stsp
2884 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2885 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2886 c2db6724 2019-01-04 stsp
2887 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2888 c2db6724 2019-01-04 stsp if (error != NULL)
2889 c2db6724 2019-01-04 stsp return error;
2890 c2db6724 2019-01-04 stsp
2891 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2892 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2893 c2db6724 2019-01-04 stsp
2894 c2db6724 2019-01-04 stsp return NULL;
2895 c2db6724 2019-01-04 stsp }
2896 c2db6724 2019-01-04 stsp
2897 a915003a 2019-02-05 stsp static void
2898 a915003a 2019-02-05 stsp init_curses(void)
2899 a915003a 2019-02-05 stsp {
2900 296152d1 2022-05-31 thomas /*
2901 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2902 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2903 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2904 296152d1 2022-05-31 thomas */
2905 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2906 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2907 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2908 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2909 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2910 296152d1 2022-05-31 thomas
2911 a915003a 2019-02-05 stsp initscr();
2912 a915003a 2019-02-05 stsp cbreak();
2913 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2914 a915003a 2019-02-05 stsp noecho();
2915 a915003a 2019-02-05 stsp nonl();
2916 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2917 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2918 a915003a 2019-02-05 stsp curs_set(0);
2919 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2920 6d17833f 2019-11-08 stsp start_color();
2921 6d17833f 2019-11-08 stsp use_default_colors();
2922 6d17833f 2019-11-08 stsp }
2923 a915003a 2019-02-05 stsp }
2924 a915003a 2019-02-05 stsp
2925 c2db6724 2019-01-04 stsp static const struct got_error *
2926 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2927 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2928 f135c941 2020-02-20 stsp {
2929 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2930 f135c941 2020-02-20 stsp
2931 f135c941 2020-02-20 stsp if (argc == 0) {
2932 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2933 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2934 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2935 f135c941 2020-02-20 stsp return NULL;
2936 f135c941 2020-02-20 stsp }
2937 f135c941 2020-02-20 stsp
2938 f135c941 2020-02-20 stsp if (worktree) {
2939 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2940 bfd61697 2020-11-14 stsp char *p;
2941 f135c941 2020-02-20 stsp
2942 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2943 f135c941 2020-02-20 stsp if (err)
2944 f135c941 2020-02-20 stsp return err;
2945 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2946 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2947 bfd61697 2020-11-14 stsp p) == -1) {
2948 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2949 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2950 f135c941 2020-02-20 stsp }
2951 f135c941 2020-02-20 stsp free(p);
2952 f135c941 2020-02-20 stsp } else
2953 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2954 f135c941 2020-02-20 stsp
2955 f135c941 2020-02-20 stsp return err;
2956 f135c941 2020-02-20 stsp }
2957 f135c941 2020-02-20 stsp
2958 f135c941 2020-02-20 stsp static const struct got_error *
2959 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2960 9f7d7167 2018-04-29 stsp {
2961 80ddbec8 2018-04-29 stsp const struct got_error *error;
2962 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2963 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2964 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2965 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2966 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2967 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2968 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2969 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2970 04cc582a 2018-08-01 stsp struct tog_view *view;
2971 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
2972 80ddbec8 2018-04-29 stsp
2973 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2974 80ddbec8 2018-04-29 stsp switch (ch) {
2975 b672a97a 2020-01-27 stsp case 'b':
2976 b672a97a 2020-01-27 stsp log_branches = 1;
2977 b672a97a 2020-01-27 stsp break;
2978 80ddbec8 2018-04-29 stsp case 'c':
2979 80ddbec8 2018-04-29 stsp start_commit = optarg;
2980 80ddbec8 2018-04-29 stsp break;
2981 ecb28ae0 2018-07-16 stsp case 'r':
2982 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2983 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2984 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2985 9ba1d308 2019-10-21 stsp optarg);
2986 ecb28ae0 2018-07-16 stsp break;
2987 80ddbec8 2018-04-29 stsp default:
2988 17020d27 2019-03-07 stsp usage_log();
2989 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2990 80ddbec8 2018-04-29 stsp }
2991 80ddbec8 2018-04-29 stsp }
2992 80ddbec8 2018-04-29 stsp
2993 80ddbec8 2018-04-29 stsp argc -= optind;
2994 80ddbec8 2018-04-29 stsp argv += optind;
2995 80ddbec8 2018-04-29 stsp
2996 f135c941 2020-02-20 stsp if (argc > 1)
2997 f135c941 2020-02-20 stsp usage_log();
2998 963f97a1 2019-03-18 stsp
2999 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3000 7cd52833 2022-06-23 thomas if (error != NULL)
3001 7cd52833 2022-06-23 thomas goto done;
3002 7cd52833 2022-06-23 thomas
3003 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3004 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3005 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3006 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3007 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3008 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3009 c156c7a4 2020-12-18 stsp goto done;
3010 a1fbf39a 2019-08-11 stsp if (worktree)
3011 6962eb72 2020-02-20 stsp repo_path =
3012 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3013 a1fbf39a 2019-08-11 stsp else
3014 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3015 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3016 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3017 c156c7a4 2020-12-18 stsp goto done;
3018 c156c7a4 2020-12-18 stsp }
3019 ecb28ae0 2018-07-16 stsp }
3020 ecb28ae0 2018-07-16 stsp
3021 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3022 80ddbec8 2018-04-29 stsp if (error != NULL)
3023 ecb28ae0 2018-07-16 stsp goto done;
3024 80ddbec8 2018-04-29 stsp
3025 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3026 f135c941 2020-02-20 stsp repo, worktree);
3027 f135c941 2020-02-20 stsp if (error)
3028 f135c941 2020-02-20 stsp goto done;
3029 f135c941 2020-02-20 stsp
3030 f135c941 2020-02-20 stsp init_curses();
3031 f135c941 2020-02-20 stsp
3032 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3033 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3034 c02c541e 2019-03-29 stsp if (error)
3035 c02c541e 2019-03-29 stsp goto done;
3036 c02c541e 2019-03-29 stsp
3037 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3038 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3039 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3040 87670572 2020-12-26 naddy if (error)
3041 87670572 2020-12-26 naddy goto done;
3042 87670572 2020-12-26 naddy }
3043 51a10b52 2020-12-26 stsp
3044 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3045 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3046 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3047 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3048 d8f38dc4 2020-12-05 stsp if (error)
3049 d8f38dc4 2020-12-05 stsp goto done;
3050 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3051 d8f38dc4 2020-12-05 stsp } else {
3052 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3053 d8f38dc4 2020-12-05 stsp if (error == NULL)
3054 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3055 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3056 d8f38dc4 2020-12-05 stsp goto done;
3057 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3058 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3059 d8f38dc4 2020-12-05 stsp if (error)
3060 d8f38dc4 2020-12-05 stsp goto done;
3061 d8f38dc4 2020-12-05 stsp }
3062 8b473291 2019-02-21 stsp
3063 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3064 04cc582a 2018-08-01 stsp if (view == NULL) {
3065 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3066 04cc582a 2018-08-01 stsp goto done;
3067 04cc582a 2018-08-01 stsp }
3068 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3069 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3070 ba4f502b 2018-08-04 stsp if (error)
3071 ba4f502b 2018-08-04 stsp goto done;
3072 2fc00ff4 2019-08-31 stsp if (worktree) {
3073 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3074 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3075 2fc00ff4 2019-08-31 stsp worktree = NULL;
3076 2fc00ff4 2019-08-31 stsp }
3077 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3078 ecb28ae0 2018-07-16 stsp done:
3079 f135c941 2020-02-20 stsp free(in_repo_path);
3080 ecb28ae0 2018-07-16 stsp free(repo_path);
3081 ecb28ae0 2018-07-16 stsp free(cwd);
3082 899d86c2 2018-05-10 stsp free(start_id);
3083 d8f38dc4 2020-12-05 stsp free(label);
3084 d8f38dc4 2020-12-05 stsp if (ref)
3085 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3086 1d0f4054 2021-06-17 stsp if (repo) {
3087 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3088 1d0f4054 2021-06-17 stsp if (error == NULL)
3089 1d0f4054 2021-06-17 stsp error = close_err;
3090 1d0f4054 2021-06-17 stsp }
3091 ec142235 2019-03-07 stsp if (worktree)
3092 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3093 7cd52833 2022-06-23 thomas if (pack_fds) {
3094 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3095 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3096 7cd52833 2022-06-23 thomas if (error == NULL)
3097 7cd52833 2022-06-23 thomas error = pack_err;
3098 7cd52833 2022-06-23 thomas }
3099 51a10b52 2020-12-26 stsp tog_free_refs();
3100 80ddbec8 2018-04-29 stsp return error;
3101 9f7d7167 2018-04-29 stsp }
3102 9f7d7167 2018-04-29 stsp
3103 4ed7e80c 2018-05-20 stsp __dead static void
3104 9f7d7167 2018-04-29 stsp usage_diff(void)
3105 9f7d7167 2018-04-29 stsp {
3106 80ddbec8 2018-04-29 stsp endwin();
3107 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3108 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3109 9f7d7167 2018-04-29 stsp exit(1);
3110 b304db33 2018-05-20 stsp }
3111 b304db33 2018-05-20 stsp
3112 6d17833f 2019-11-08 stsp static int
3113 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3114 41605754 2020-11-12 stsp regmatch_t *regmatch)
3115 6d17833f 2019-11-08 stsp {
3116 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3117 6d17833f 2019-11-08 stsp }
3118 6d17833f 2019-11-08 stsp
3119 f26dddb7 2019-11-08 stsp struct tog_color *
3120 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3121 6d17833f 2019-11-08 stsp {
3122 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3123 6d17833f 2019-11-08 stsp
3124 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3125 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3126 6d17833f 2019-11-08 stsp return tc;
3127 6d17833f 2019-11-08 stsp }
3128 6d17833f 2019-11-08 stsp
3129 6d17833f 2019-11-08 stsp return NULL;
3130 6d17833f 2019-11-08 stsp }
3131 6d17833f 2019-11-08 stsp
3132 4ed7e80c 2018-05-20 stsp static const struct got_error *
3133 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3134 05171be4 2022-06-23 thomas WINDOW *window, int skip, regmatch_t *regmatch)
3135 41605754 2020-11-12 stsp {
3136 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3137 41605754 2020-11-12 stsp wchar_t *wline;
3138 05171be4 2022-06-23 thomas int rme, rms, n, width;
3139 41605754 2020-11-12 stsp
3140 41605754 2020-11-12 stsp *wtotal = 0;
3141 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3142 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3143 41605754 2020-11-12 stsp
3144 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, wlimit + skip,
3145 05171be4 2022-06-23 thomas col_tab_align, 1);
3146 05171be4 2022-06-23 thomas if (err)
3147 41605754 2020-11-12 stsp return err;
3148 05171be4 2022-06-23 thomas
3149 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3150 05171be4 2022-06-23 thomas n = MAX(rms - skip, 0);
3151 05171be4 2022-06-23 thomas if (n) {
3152 05171be4 2022-06-23 thomas waddnwstr(window, wline + skip, n);
3153 05171be4 2022-06-23 thomas wlimit -= n;
3154 05171be4 2022-06-23 thomas *wtotal += n;
3155 41605754 2020-11-12 stsp }
3156 41605754 2020-11-12 stsp
3157 41605754 2020-11-12 stsp if (wlimit > 0) {
3158 05171be4 2022-06-23 thomas int len = rme - rms;
3159 05171be4 2022-06-23 thomas n = 0;
3160 05171be4 2022-06-23 thomas if (skip > rms) {
3161 05171be4 2022-06-23 thomas n = skip - rms;
3162 05171be4 2022-06-23 thomas len = MAX(len - n, 0);
3163 41605754 2020-11-12 stsp }
3164 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3165 05171be4 2022-06-23 thomas if (len) {
3166 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3167 05171be4 2022-06-23 thomas waddnwstr(window, wline + rms + n, len);
3168 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3169 05171be4 2022-06-23 thomas wlimit -= len;
3170 05171be4 2022-06-23 thomas *wtotal += len;
3171 41605754 2020-11-12 stsp }
3172 41605754 2020-11-12 stsp }
3173 41605754 2020-11-12 stsp
3174 05171be4 2022-06-23 thomas if (wlimit > 0 && skip < width) { /* draw rest of line */
3175 05171be4 2022-06-23 thomas n = 0;
3176 05171be4 2022-06-23 thomas if (skip > rme)
3177 05171be4 2022-06-23 thomas n = MIN(skip - rme, width - rme);
3178 05171be4 2022-06-23 thomas waddnwstr(window, wline + rme + n, wlimit);
3179 41605754 2020-11-12 stsp }
3180 41605754 2020-11-12 stsp
3181 05171be4 2022-06-23 thomas *wtotal = width;
3182 05171be4 2022-06-23 thomas free(wline);
3183 41605754 2020-11-12 stsp return NULL;
3184 41605754 2020-11-12 stsp }
3185 41605754 2020-11-12 stsp
3186 41605754 2020-11-12 stsp static const struct got_error *
3187 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3188 26ed57b2 2018-05-19 stsp {
3189 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3190 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3191 61e69b96 2018-05-20 stsp const struct got_error *err;
3192 fe621944 2020-11-10 stsp int nprinted = 0;
3193 b304db33 2018-05-20 stsp char *line;
3194 826082fe 2020-12-10 stsp size_t linesize = 0;
3195 826082fe 2020-12-10 stsp ssize_t linelen;
3196 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3197 61e69b96 2018-05-20 stsp wchar_t *wline;
3198 e0b650dd 2018-05-20 stsp int width;
3199 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3200 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3201 fe621944 2020-11-10 stsp off_t line_offset;
3202 26ed57b2 2018-05-19 stsp
3203 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3204 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3205 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3206 fe621944 2020-11-10 stsp
3207 f7d12f7e 2018-08-01 stsp werase(view->window);
3208 a3404814 2018-09-02 stsp
3209 a3404814 2018-09-02 stsp if (header) {
3210 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3211 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3212 135a2da0 2020-11-11 stsp header) == -1)
3213 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3214 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3215 f91a2b48 2022-06-23 thomas 0, 0);
3216 135a2da0 2020-11-11 stsp free(line);
3217 135a2da0 2020-11-11 stsp if (err)
3218 a3404814 2018-09-02 stsp return err;
3219 a3404814 2018-09-02 stsp
3220 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3221 a3404814 2018-09-02 stsp wstandout(view->window);
3222 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3223 e54cc94a 2020-11-11 stsp free(wline);
3224 e54cc94a 2020-11-11 stsp wline = NULL;
3225 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3226 a3404814 2018-09-02 stsp wstandend(view->window);
3227 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3228 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3229 26ed57b2 2018-05-19 stsp
3230 a3404814 2018-09-02 stsp if (max_lines <= 1)
3231 a3404814 2018-09-02 stsp return NULL;
3232 a3404814 2018-09-02 stsp max_lines--;
3233 a3404814 2018-09-02 stsp }
3234 a3404814 2018-09-02 stsp
3235 89f1a395 2020-12-01 naddy s->eof = 0;
3236 05171be4 2022-06-23 thomas view->maxx = 0;
3237 826082fe 2020-12-10 stsp line = NULL;
3238 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3239 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3240 826082fe 2020-12-10 stsp if (linelen == -1) {
3241 826082fe 2020-12-10 stsp if (feof(s->f)) {
3242 826082fe 2020-12-10 stsp s->eof = 1;
3243 826082fe 2020-12-10 stsp break;
3244 826082fe 2020-12-10 stsp }
3245 826082fe 2020-12-10 stsp free(line);
3246 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3247 61e69b96 2018-05-20 stsp }
3248 6d17833f 2019-11-08 stsp
3249 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
3250 05171be4 2022-06-23 thomas
3251 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3252 f26dddb7 2019-11-08 stsp if (tc)
3253 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3254 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3255 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3256 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3257 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3258 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3259 41605754 2020-11-12 stsp if (err) {
3260 41605754 2020-11-12 stsp free(line);
3261 41605754 2020-11-12 stsp return err;
3262 41605754 2020-11-12 stsp }
3263 41605754 2020-11-12 stsp } else {
3264 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0,
3265 05171be4 2022-06-23 thomas view->x + view->ncols, 0, view->x ? 1 : 0);
3266 41605754 2020-11-12 stsp if (err) {
3267 41605754 2020-11-12 stsp free(line);
3268 41605754 2020-11-12 stsp return err;
3269 41605754 2020-11-12 stsp }
3270 4122562d 2022-06-23 thomas if (view->x < width)
3271 05171be4 2022-06-23 thomas waddwstr(view->window, wline + view->x);
3272 41605754 2020-11-12 stsp free(wline);
3273 41605754 2020-11-12 stsp wline = NULL;
3274 41605754 2020-11-12 stsp }
3275 f26dddb7 2019-11-08 stsp if (tc)
3276 6d17833f 2019-11-08 stsp wattr_off(view->window,
3277 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3278 05171be4 2022-06-23 thomas if (width - view->x <= view->ncols - 1)
3279 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3280 fe621944 2020-11-10 stsp nprinted++;
3281 826082fe 2020-12-10 stsp }
3282 826082fe 2020-12-10 stsp free(line);
3283 fe621944 2020-11-10 stsp if (nprinted >= 1)
3284 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3285 89f1a395 2020-12-01 naddy (nprinted - 1);
3286 fe621944 2020-11-10 stsp else
3287 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3288 26ed57b2 2018-05-19 stsp
3289 1a57306a 2018-09-02 stsp view_vborder(view);
3290 c3e9aa98 2019-05-13 jcs
3291 89f1a395 2020-12-01 naddy if (s->eof) {
3292 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3293 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3294 c3e9aa98 2019-05-13 jcs nprinted++;
3295 c3e9aa98 2019-05-13 jcs }
3296 c3e9aa98 2019-05-13 jcs
3297 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3298 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3299 c3e9aa98 2019-05-13 jcs if (err) {
3300 c3e9aa98 2019-05-13 jcs return err;
3301 c3e9aa98 2019-05-13 jcs }
3302 26ed57b2 2018-05-19 stsp
3303 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3304 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3305 e54cc94a 2020-11-11 stsp free(wline);
3306 e54cc94a 2020-11-11 stsp wline = NULL;
3307 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3308 c3e9aa98 2019-05-13 jcs }
3309 c3e9aa98 2019-05-13 jcs
3310 26ed57b2 2018-05-19 stsp return NULL;
3311 abd2672a 2018-12-23 stsp }
3312 abd2672a 2018-12-23 stsp
3313 abd2672a 2018-12-23 stsp static char *
3314 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3315 abd2672a 2018-12-23 stsp {
3316 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3317 09867e48 2019-08-13 stsp char *p, *s;
3318 09867e48 2019-08-13 stsp
3319 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3320 09867e48 2019-08-13 stsp if (tm == NULL)
3321 09867e48 2019-08-13 stsp return NULL;
3322 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3323 09867e48 2019-08-13 stsp if (s == NULL)
3324 09867e48 2019-08-13 stsp return NULL;
3325 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3326 abd2672a 2018-12-23 stsp if (p)
3327 abd2672a 2018-12-23 stsp *p = '\0';
3328 abd2672a 2018-12-23 stsp return s;
3329 9f7d7167 2018-04-29 stsp }
3330 9f7d7167 2018-04-29 stsp
3331 4ed7e80c 2018-05-20 stsp static const struct got_error *
3332 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3333 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3334 0208f208 2020-05-05 stsp {
3335 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3336 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3337 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3338 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3339 0208f208 2020-05-05 stsp
3340 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3341 0208f208 2020-05-05 stsp if (qid != NULL) {
3342 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3343 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3344 ec242592 2022-04-22 thomas &qid->id);
3345 0208f208 2020-05-05 stsp if (err)
3346 0208f208 2020-05-05 stsp return err;
3347 0208f208 2020-05-05 stsp
3348 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3349 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3350 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3351 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3352 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3353 aa8b5dd0 2021-08-01 stsp }
3354 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3355 0208f208 2020-05-05 stsp
3356 0208f208 2020-05-05 stsp }
3357 0208f208 2020-05-05 stsp
3358 0208f208 2020-05-05 stsp if (tree_id1) {
3359 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3360 0208f208 2020-05-05 stsp if (err)
3361 0208f208 2020-05-05 stsp goto done;
3362 0208f208 2020-05-05 stsp }
3363 0208f208 2020-05-05 stsp
3364 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3365 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3366 0208f208 2020-05-05 stsp if (err)
3367 0208f208 2020-05-05 stsp goto done;
3368 0208f208 2020-05-05 stsp
3369 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3370 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3371 0208f208 2020-05-05 stsp done:
3372 0208f208 2020-05-05 stsp if (tree1)
3373 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3374 0208f208 2020-05-05 stsp if (tree2)
3375 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3376 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3377 0208f208 2020-05-05 stsp return err;
3378 0208f208 2020-05-05 stsp }
3379 0208f208 2020-05-05 stsp
3380 0208f208 2020-05-05 stsp static const struct got_error *
3381 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3382 abd2672a 2018-12-23 stsp {
3383 fe621944 2020-11-10 stsp off_t *p;
3384 fe621944 2020-11-10 stsp
3385 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3386 fe621944 2020-11-10 stsp if (p == NULL)
3387 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3388 fe621944 2020-11-10 stsp *line_offsets = p;
3389 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3390 fe621944 2020-11-10 stsp (*nlines)++;
3391 fe621944 2020-11-10 stsp return NULL;
3392 fe621944 2020-11-10 stsp }
3393 fe621944 2020-11-10 stsp
3394 fe621944 2020-11-10 stsp static const struct got_error *
3395 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3396 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3397 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3398 fe621944 2020-11-10 stsp {
3399 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3400 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3401 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3402 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3403 45d799e2 2018-12-23 stsp time_t committer_time;
3404 45d799e2 2018-12-23 stsp const char *author, *committer;
3405 8b473291 2019-02-21 stsp char *refs_str = NULL;
3406 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3407 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3408 fe621944 2020-11-10 stsp off_t outoff = 0;
3409 fe621944 2020-11-10 stsp int n;
3410 abd2672a 2018-12-23 stsp
3411 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3412 0208f208 2020-05-05 stsp
3413 8b473291 2019-02-21 stsp if (refs) {
3414 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3415 8b473291 2019-02-21 stsp if (err)
3416 8b473291 2019-02-21 stsp return err;
3417 8b473291 2019-02-21 stsp }
3418 8b473291 2019-02-21 stsp
3419 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3420 abd2672a 2018-12-23 stsp if (err)
3421 abd2672a 2018-12-23 stsp return err;
3422 abd2672a 2018-12-23 stsp
3423 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3424 15a94983 2018-12-23 stsp if (err) {
3425 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3426 15a94983 2018-12-23 stsp goto done;
3427 15a94983 2018-12-23 stsp }
3428 abd2672a 2018-12-23 stsp
3429 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3430 fe621944 2020-11-10 stsp if (err)
3431 fe621944 2020-11-10 stsp goto done;
3432 fe621944 2020-11-10 stsp
3433 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3434 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3435 fe621944 2020-11-10 stsp if (n < 0) {
3436 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3437 abd2672a 2018-12-23 stsp goto done;
3438 abd2672a 2018-12-23 stsp }
3439 fe621944 2020-11-10 stsp outoff += n;
3440 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3441 fe621944 2020-11-10 stsp if (err)
3442 fe621944 2020-11-10 stsp goto done;
3443 fe621944 2020-11-10 stsp
3444 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3445 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3446 fe621944 2020-11-10 stsp if (n < 0) {
3447 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3448 abd2672a 2018-12-23 stsp goto done;
3449 abd2672a 2018-12-23 stsp }
3450 fe621944 2020-11-10 stsp outoff += n;
3451 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3452 fe621944 2020-11-10 stsp if (err)
3453 fe621944 2020-11-10 stsp goto done;
3454 fe621944 2020-11-10 stsp
3455 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3456 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3457 fe621944 2020-11-10 stsp if (datestr) {
3458 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3459 fe621944 2020-11-10 stsp if (n < 0) {
3460 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3461 fe621944 2020-11-10 stsp goto done;
3462 fe621944 2020-11-10 stsp }
3463 fe621944 2020-11-10 stsp outoff += n;
3464 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3465 fe621944 2020-11-10 stsp if (err)
3466 fe621944 2020-11-10 stsp goto done;
3467 abd2672a 2018-12-23 stsp }
3468 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3469 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3470 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3471 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3472 fe621944 2020-11-10 stsp if (n < 0) {
3473 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3474 fe621944 2020-11-10 stsp goto done;
3475 fe621944 2020-11-10 stsp }
3476 fe621944 2020-11-10 stsp outoff += n;
3477 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3478 fe621944 2020-11-10 stsp if (err)
3479 fe621944 2020-11-10 stsp goto done;
3480 abd2672a 2018-12-23 stsp }
3481 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3482 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3483 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3484 ac4dc263 2021-09-24 thomas int pn = 1;
3485 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3486 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3487 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3488 ac4dc263 2021-09-24 thomas if (err)
3489 ac4dc263 2021-09-24 thomas goto done;
3490 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3491 ac4dc263 2021-09-24 thomas if (n < 0) {
3492 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3493 ac4dc263 2021-09-24 thomas goto done;
3494 ac4dc263 2021-09-24 thomas }
3495 ac4dc263 2021-09-24 thomas outoff += n;
3496 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3497 ac4dc263 2021-09-24 thomas if (err)
3498 ac4dc263 2021-09-24 thomas goto done;
3499 ac4dc263 2021-09-24 thomas free(id_str);
3500 ac4dc263 2021-09-24 thomas id_str = NULL;
3501 ac4dc263 2021-09-24 thomas }
3502 ac4dc263 2021-09-24 thomas }
3503 ac4dc263 2021-09-24 thomas
3504 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3505 5943eee2 2019-08-13 stsp if (err)
3506 5943eee2 2019-08-13 stsp goto done;
3507 fe621944 2020-11-10 stsp s = logmsg;
3508 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3509 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3510 fe621944 2020-11-10 stsp if (n < 0) {
3511 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3512 fe621944 2020-11-10 stsp goto done;
3513 fe621944 2020-11-10 stsp }
3514 fe621944 2020-11-10 stsp outoff += n;
3515 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3516 fe621944 2020-11-10 stsp if (err)
3517 fe621944 2020-11-10 stsp goto done;
3518 abd2672a 2018-12-23 stsp }
3519 fe621944 2020-11-10 stsp
3520 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3521 0208f208 2020-05-05 stsp if (err)
3522 0208f208 2020-05-05 stsp goto done;
3523 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3524 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3525 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3526 fe621944 2020-11-10 stsp if (n < 0) {
3527 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3528 fe621944 2020-11-10 stsp goto done;
3529 fe621944 2020-11-10 stsp }
3530 fe621944 2020-11-10 stsp outoff += n;
3531 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3532 fe621944 2020-11-10 stsp if (err)
3533 fe621944 2020-11-10 stsp goto done;
3534 0208f208 2020-05-05 stsp free((char *)pe->path);
3535 0208f208 2020-05-05 stsp free(pe->data);
3536 0208f208 2020-05-05 stsp }
3537 fe621944 2020-11-10 stsp
3538 0208f208 2020-05-05 stsp fputc('\n', outfile);
3539 fe621944 2020-11-10 stsp outoff++;
3540 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3541 abd2672a 2018-12-23 stsp done:
3542 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3543 abd2672a 2018-12-23 stsp free(id_str);
3544 5943eee2 2019-08-13 stsp free(logmsg);
3545 8b473291 2019-02-21 stsp free(refs_str);
3546 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3547 7510f233 2020-08-09 stsp if (err) {
3548 ae6a6978 2020-08-09 stsp free(*line_offsets);
3549 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3550 ae6a6978 2020-08-09 stsp *nlines = 0;
3551 7510f233 2020-08-09 stsp }
3552 fe621944 2020-11-10 stsp return err;
3553 abd2672a 2018-12-23 stsp }
3554 abd2672a 2018-12-23 stsp
3555 abd2672a 2018-12-23 stsp static const struct got_error *
3556 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3557 26ed57b2 2018-05-19 stsp {
3558 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3559 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3560 15a94983 2018-12-23 stsp int obj_type;
3561 fe621944 2020-11-10 stsp
3562 fe621944 2020-11-10 stsp free(s->line_offsets);
3563 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3564 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3565 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3566 fe621944 2020-11-10 stsp s->nlines = 0;
3567 26ed57b2 2018-05-19 stsp
3568 511a516b 2018-05-19 stsp f = got_opentemp();
3569 48ae06ee 2018-10-18 stsp if (f == NULL) {
3570 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3571 48ae06ee 2018-10-18 stsp goto done;
3572 48ae06ee 2018-10-18 stsp }
3573 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3574 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3575 fb43ecf1 2019-02-11 stsp goto done;
3576 fb43ecf1 2019-02-11 stsp }
3577 48ae06ee 2018-10-18 stsp s->f = f;
3578 26ed57b2 2018-05-19 stsp
3579 15a94983 2018-12-23 stsp if (s->id1)
3580 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3581 15a94983 2018-12-23 stsp else
3582 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3583 15a94983 2018-12-23 stsp if (err)
3584 15a94983 2018-12-23 stsp goto done;
3585 15a94983 2018-12-23 stsp
3586 15a94983 2018-12-23 stsp switch (obj_type) {
3587 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3588 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3589 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3590 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3591 a0f32f33 2022-06-13 thomas s->repo, s->f);
3592 26ed57b2 2018-05-19 stsp break;
3593 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3594 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3595 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3596 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3597 26ed57b2 2018-05-19 stsp break;
3598 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3599 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3600 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3601 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3602 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3603 abd2672a 2018-12-23 stsp
3604 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3605 abd2672a 2018-12-23 stsp if (err)
3606 3ffacbe1 2020-02-02 tracey goto done;
3607 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3608 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3609 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3610 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3611 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3612 f44b1f58 2020-02-02 tracey if (err)
3613 f44b1f58 2020-02-02 tracey goto done;
3614 f44b1f58 2020-02-02 tracey } else {
3615 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3616 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3617 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3618 fe621944 2020-11-10 stsp err = write_commit_info(
3619 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3620 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3621 f44b1f58 2020-02-02 tracey if (err)
3622 f44b1f58 2020-02-02 tracey goto done;
3623 f5404e4e 2020-02-02 tracey break;
3624 15a087fe 2019-02-21 stsp }
3625 abd2672a 2018-12-23 stsp }
3626 abd2672a 2018-12-23 stsp }
3627 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3628 abd2672a 2018-12-23 stsp
3629 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3630 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3631 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3632 26ed57b2 2018-05-19 stsp break;
3633 abd2672a 2018-12-23 stsp }
3634 26ed57b2 2018-05-19 stsp default:
3635 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3636 48ae06ee 2018-10-18 stsp break;
3637 26ed57b2 2018-05-19 stsp }
3638 f44b1f58 2020-02-02 tracey if (err)
3639 f44b1f58 2020-02-02 tracey goto done;
3640 48ae06ee 2018-10-18 stsp done:
3641 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3642 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3643 48ae06ee 2018-10-18 stsp return err;
3644 48ae06ee 2018-10-18 stsp }
3645 26ed57b2 2018-05-19 stsp
3646 f5215bb9 2019-02-22 stsp static void
3647 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3648 f5215bb9 2019-02-22 stsp {
3649 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3650 f5215bb9 2019-02-22 stsp update_panels();
3651 f5215bb9 2019-02-22 stsp doupdate();
3652 f44b1f58 2020-02-02 tracey }
3653 f44b1f58 2020-02-02 tracey
3654 f44b1f58 2020-02-02 tracey static const struct got_error *
3655 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3656 f44b1f58 2020-02-02 tracey {
3657 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3658 f44b1f58 2020-02-02 tracey
3659 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3660 f44b1f58 2020-02-02 tracey return NULL;
3661 f44b1f58 2020-02-02 tracey }
3662 f44b1f58 2020-02-02 tracey
3663 f44b1f58 2020-02-02 tracey static const struct got_error *
3664 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3665 f44b1f58 2020-02-02 tracey {
3666 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3667 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
3668 f44b1f58 2020-02-02 tracey int lineno;
3669 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
3670 826082fe 2020-12-10 stsp size_t linesize = 0;
3671 826082fe 2020-12-10 stsp ssize_t linelen;
3672 f44b1f58 2020-02-02 tracey
3673 f44b1f58 2020-02-02 tracey if (!view->searching) {
3674 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3675 f44b1f58 2020-02-02 tracey return NULL;
3676 f44b1f58 2020-02-02 tracey }
3677 f44b1f58 2020-02-02 tracey
3678 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3679 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3680 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3681 f44b1f58 2020-02-02 tracey else
3682 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3683 4b3f9dac 2021-12-17 thomas } else
3684 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3685 f44b1f58 2020-02-02 tracey
3686 f44b1f58 2020-02-02 tracey while (1) {
3687 f44b1f58 2020-02-02 tracey off_t offset;
3688 f44b1f58 2020-02-02 tracey
3689 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3690 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3691 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3692 f44b1f58 2020-02-02 tracey break;
3693 f44b1f58 2020-02-02 tracey }
3694 f44b1f58 2020-02-02 tracey
3695 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3696 f44b1f58 2020-02-02 tracey lineno = 1;
3697 f44b1f58 2020-02-02 tracey else
3698 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3699 f44b1f58 2020-02-02 tracey }
3700 f44b1f58 2020-02-02 tracey
3701 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3702 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3703 f44b1f58 2020-02-02 tracey free(line);
3704 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3705 f44b1f58 2020-02-02 tracey }
3706 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3707 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
3708 05171be4 2022-06-23 thomas if (err)
3709 05171be4 2022-06-23 thomas break;
3710 826082fe 2020-12-10 stsp if (linelen != -1 &&
3711 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
3712 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3713 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3714 f44b1f58 2020-02-02 tracey break;
3715 f44b1f58 2020-02-02 tracey }
3716 05171be4 2022-06-23 thomas free(exstr);
3717 05171be4 2022-06-23 thomas exstr = NULL;
3718 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3719 f44b1f58 2020-02-02 tracey lineno++;
3720 f44b1f58 2020-02-02 tracey else
3721 f44b1f58 2020-02-02 tracey lineno--;
3722 f44b1f58 2020-02-02 tracey }
3723 826082fe 2020-12-10 stsp free(line);
3724 05171be4 2022-06-23 thomas free(exstr);
3725 f44b1f58 2020-02-02 tracey
3726 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3727 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3728 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3729 f44b1f58 2020-02-02 tracey }
3730 f44b1f58 2020-02-02 tracey
3731 05171be4 2022-06-23 thomas return err;
3732 f5215bb9 2019-02-22 stsp }
3733 f5215bb9 2019-02-22 stsp
3734 48ae06ee 2018-10-18 stsp static const struct got_error *
3735 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3736 a0f32f33 2022-06-13 thomas {
3737 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3738 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3739 a0f32f33 2022-06-13 thomas
3740 a0f32f33 2022-06-13 thomas free(s->id1);
3741 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3742 a0f32f33 2022-06-13 thomas free(s->id2);
3743 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3744 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3745 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3746 a0f32f33 2022-06-13 thomas s->f = NULL;
3747 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3748 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3749 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3750 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3751 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3752 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3753 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3754 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3755 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3756 a0f32f33 2022-06-13 thomas s->nlines = 0;
3757 a0f32f33 2022-06-13 thomas return err;
3758 a0f32f33 2022-06-13 thomas }
3759 a0f32f33 2022-06-13 thomas
3760 a0f32f33 2022-06-13 thomas static const struct got_error *
3761 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3762 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3763 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3764 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3765 48ae06ee 2018-10-18 stsp {
3766 48ae06ee 2018-10-18 stsp const struct got_error *err;
3767 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3768 5dc9f4bc 2018-08-04 stsp
3769 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3770 a0f32f33 2022-06-13 thomas
3771 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3772 15a94983 2018-12-23 stsp int type1, type2;
3773 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3774 15a94983 2018-12-23 stsp if (err)
3775 15a94983 2018-12-23 stsp return err;
3776 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3777 15a94983 2018-12-23 stsp if (err)
3778 15a94983 2018-12-23 stsp return err;
3779 15a94983 2018-12-23 stsp
3780 15a94983 2018-12-23 stsp if (type1 != type2)
3781 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3782 15a94983 2018-12-23 stsp }
3783 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3784 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3785 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3786 f44b1f58 2020-02-02 tracey s->repo = repo;
3787 f44b1f58 2020-02-02 tracey s->id1 = id1;
3788 f44b1f58 2020-02-02 tracey s->id2 = id2;
3789 3dbaef42 2020-11-24 stsp s->label1 = label1;
3790 3dbaef42 2020-11-24 stsp s->label2 = label2;
3791 48ae06ee 2018-10-18 stsp
3792 15a94983 2018-12-23 stsp if (id1) {
3793 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3794 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3795 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3796 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3797 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3798 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3799 a0f32f33 2022-06-13 thomas goto done;
3800 a0f32f33 2022-06-13 thomas }
3801 48ae06ee 2018-10-18 stsp } else
3802 5465d566 2020-02-01 tracey s->id1 = NULL;
3803 48ae06ee 2018-10-18 stsp
3804 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3805 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3806 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3807 a0f32f33 2022-06-13 thomas goto done;
3808 48ae06ee 2018-10-18 stsp }
3809 a0f32f33 2022-06-13 thomas
3810 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3811 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3812 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3813 a0f32f33 2022-06-13 thomas goto done;
3814 a0f32f33 2022-06-13 thomas }
3815 a0f32f33 2022-06-13 thomas
3816 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3817 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3818 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3819 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3820 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3821 5465d566 2020-02-01 tracey s->log_view = log_view;
3822 5465d566 2020-02-01 tracey s->repo = repo;
3823 6d17833f 2019-11-08 stsp
3824 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3825 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3826 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3827 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3828 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3829 6d17833f 2019-11-08 stsp if (err)
3830 a0f32f33 2022-06-13 thomas goto done;
3831 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3832 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3833 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3834 a0f32f33 2022-06-13 thomas if (err)
3835 a0f32f33 2022-06-13 thomas goto done;
3836 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3837 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3838 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3839 a0f32f33 2022-06-13 thomas if (err)
3840 a0f32f33 2022-06-13 thomas goto done;
3841 6d17833f 2019-11-08 stsp
3842 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3843 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3844 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3845 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3846 a0f32f33 2022-06-13 thomas if (err)
3847 a0f32f33 2022-06-13 thomas goto done;
3848 11b20872 2019-11-08 stsp
3849 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3850 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3851 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3852 a0f32f33 2022-06-13 thomas if (err)
3853 a0f32f33 2022-06-13 thomas goto done;
3854 11b20872 2019-11-08 stsp
3855 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3856 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3857 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3858 a0f32f33 2022-06-13 thomas if (err)
3859 a0f32f33 2022-06-13 thomas goto done;
3860 6d17833f 2019-11-08 stsp }
3861 5dc9f4bc 2018-08-04 stsp
3862 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3863 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3864 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3865 f5215bb9 2019-02-22 stsp
3866 5465d566 2020-02-01 tracey err = create_diff(s);
3867 48ae06ee 2018-10-18 stsp
3868 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3869 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3870 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3871 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3872 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3873 a0f32f33 2022-06-13 thomas done:
3874 a0f32f33 2022-06-13 thomas if (err)
3875 a0f32f33 2022-06-13 thomas close_diff_view(view);
3876 e5a0f69f 2018-08-18 stsp return err;
3877 5dc9f4bc 2018-08-04 stsp }
3878 5dc9f4bc 2018-08-04 stsp
3879 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3880 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3881 5dc9f4bc 2018-08-04 stsp {
3882 a3404814 2018-09-02 stsp const struct got_error *err;
3883 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3884 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3885 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3886 a3404814 2018-09-02 stsp
3887 a3404814 2018-09-02 stsp if (s->id1) {
3888 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3889 a3404814 2018-09-02 stsp if (err)
3890 a3404814 2018-09-02 stsp return err;
3891 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3892 3dbaef42 2020-11-24 stsp } else
3893 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3894 3dbaef42 2020-11-24 stsp
3895 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3896 a3404814 2018-09-02 stsp if (err)
3897 a3404814 2018-09-02 stsp return err;
3898 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3899 26ed57b2 2018-05-19 stsp
3900 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3901 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3902 a3404814 2018-09-02 stsp free(id_str1);
3903 a3404814 2018-09-02 stsp free(id_str2);
3904 a3404814 2018-09-02 stsp return err;
3905 a3404814 2018-09-02 stsp }
3906 a3404814 2018-09-02 stsp free(id_str1);
3907 a3404814 2018-09-02 stsp free(id_str2);
3908 a3404814 2018-09-02 stsp
3909 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3910 267bb3b8 2021-08-01 stsp free(header);
3911 267bb3b8 2021-08-01 stsp return err;
3912 15a087fe 2019-02-21 stsp }
3913 15a087fe 2019-02-21 stsp
3914 15a087fe 2019-02-21 stsp static const struct got_error *
3915 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3916 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3917 15a087fe 2019-02-21 stsp {
3918 d7a04538 2019-02-21 stsp const struct got_error *err;
3919 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3920 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3921 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3922 15a087fe 2019-02-21 stsp
3923 15a087fe 2019-02-21 stsp free(s->id2);
3924 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3925 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3926 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3927 15a087fe 2019-02-21 stsp
3928 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3929 d7a04538 2019-02-21 stsp if (err)
3930 d7a04538 2019-02-21 stsp return err;
3931 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3932 15a087fe 2019-02-21 stsp free(s->id1);
3933 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3934 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3935 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3936 15a087fe 2019-02-21 stsp return NULL;
3937 0cf4efb1 2018-09-29 stsp }
3938 0cf4efb1 2018-09-29 stsp
3939 0cf4efb1 2018-09-29 stsp static const struct got_error *
3940 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3941 e5a0f69f 2018-08-18 stsp {
3942 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3943 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3944 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3945 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3946 826082fe 2020-12-10 stsp char *line = NULL;
3947 826082fe 2020-12-10 stsp size_t linesize = 0;
3948 826082fe 2020-12-10 stsp ssize_t linelen;
3949 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3950 e5a0f69f 2018-08-18 stsp
3951 e5a0f69f 2018-08-18 stsp switch (ch) {
3952 05171be4 2022-06-23 thomas case '0':
3953 05171be4 2022-06-23 thomas view->x = 0;
3954 05171be4 2022-06-23 thomas break;
3955 05171be4 2022-06-23 thomas case '$':
3956 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
3957 05171be4 2022-06-23 thomas break;
3958 05171be4 2022-06-23 thomas case KEY_RIGHT:
3959 05171be4 2022-06-23 thomas case 'l':
3960 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
3961 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3962 05171be4 2022-06-23 thomas break;
3963 05171be4 2022-06-23 thomas case KEY_LEFT:
3964 05171be4 2022-06-23 thomas case 'h':
3965 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3966 05171be4 2022-06-23 thomas break;
3967 64453f7e 2020-11-21 stsp case 'a':
3968 3dbaef42 2020-11-24 stsp case 'w':
3969 3dbaef42 2020-11-24 stsp if (ch == 'a')
3970 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3971 3dbaef42 2020-11-24 stsp if (ch == 'w')
3972 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3973 64453f7e 2020-11-21 stsp wclear(view->window);
3974 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3975 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3976 aa61903a 2021-12-31 thomas s->matched_line = 0;
3977 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3978 64453f7e 2020-11-21 stsp err = create_diff(s);
3979 912a3f79 2021-08-30 j break;
3980 912a3f79 2021-08-30 j case 'g':
3981 912a3f79 2021-08-30 j case KEY_HOME:
3982 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3983 64453f7e 2020-11-21 stsp break;
3984 912a3f79 2021-08-30 j case 'G':
3985 912a3f79 2021-08-30 j case KEY_END:
3986 912a3f79 2021-08-30 j if (s->eof)
3987 912a3f79 2021-08-30 j break;
3988 912a3f79 2021-08-30 j
3989 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3990 912a3f79 2021-08-30 j s->eof = 1;
3991 912a3f79 2021-08-30 j break;
3992 1e37a5c2 2019-05-12 jcs case 'k':
3993 1e37a5c2 2019-05-12 jcs case KEY_UP:
3994 f7140bf5 2021-10-17 thomas case CTRL('p'):
3995 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3996 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3997 1e37a5c2 2019-05-12 jcs break;
3998 70f17a53 2022-06-13 thomas case CTRL('u'):
3999 23427b14 2022-06-23 thomas case 'u':
4000 70f17a53 2022-06-13 thomas nscroll /= 2;
4001 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4002 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4003 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4004 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
4005 26ed57b2 2018-05-19 stsp break;
4006 1e37a5c2 2019-05-12 jcs i = 0;
4007 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4008 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4009 1e37a5c2 2019-05-12 jcs break;
4010 1e37a5c2 2019-05-12 jcs case 'j':
4011 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4012 f7140bf5 2021-10-17 thomas case CTRL('n'):
4013 1e37a5c2 2019-05-12 jcs if (!s->eof)
4014 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4015 1e37a5c2 2019-05-12 jcs break;
4016 70f17a53 2022-06-13 thomas case CTRL('d'):
4017 23427b14 2022-06-23 thomas case 'd':
4018 70f17a53 2022-06-13 thomas nscroll /= 2;
4019 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4020 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4021 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4022 1e37a5c2 2019-05-12 jcs case ' ':
4023 00ba99a7 2019-05-12 jcs if (s->eof)
4024 1e37a5c2 2019-05-12 jcs break;
4025 1e37a5c2 2019-05-12 jcs i = 0;
4026 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4027 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4028 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4029 826082fe 2020-12-10 stsp if (linelen == -1) {
4030 826082fe 2020-12-10 stsp if (feof(s->f)) {
4031 826082fe 2020-12-10 stsp s->eof = 1;
4032 826082fe 2020-12-10 stsp } else
4033 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4034 34bc9ec9 2019-02-22 stsp break;
4035 826082fe 2020-12-10 stsp }
4036 1e37a5c2 2019-05-12 jcs }
4037 826082fe 2020-12-10 stsp free(line);
4038 1e37a5c2 2019-05-12 jcs break;
4039 1e37a5c2 2019-05-12 jcs case '[':
4040 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4041 1e37a5c2 2019-05-12 jcs s->diff_context--;
4042 aa61903a 2021-12-31 thomas s->matched_line = 0;
4043 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4044 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4045 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4046 27829c9e 2020-11-21 stsp s->nlines) {
4047 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4048 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4049 27829c9e 2020-11-21 stsp }
4050 1e37a5c2 2019-05-12 jcs }
4051 1e37a5c2 2019-05-12 jcs break;
4052 1e37a5c2 2019-05-12 jcs case ']':
4053 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4054 1e37a5c2 2019-05-12 jcs s->diff_context++;
4055 aa61903a 2021-12-31 thomas s->matched_line = 0;
4056 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4057 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4058 1e37a5c2 2019-05-12 jcs }
4059 1e37a5c2 2019-05-12 jcs break;
4060 1e37a5c2 2019-05-12 jcs case '<':
4061 1e37a5c2 2019-05-12 jcs case ',':
4062 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4063 48ae06ee 2018-10-18 stsp break;
4064 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4065 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4066 6524637e 2019-02-21 stsp
4067 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4068 1e37a5c2 2019-05-12 jcs if (err)
4069 1e37a5c2 2019-05-12 jcs break;
4070 15a087fe 2019-02-21 stsp
4071 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4072 5a8b5076 2020-12-05 stsp break;
4073 5a8b5076 2020-12-05 stsp
4074 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4075 1e37a5c2 2019-05-12 jcs if (err)
4076 1e37a5c2 2019-05-12 jcs break;
4077 15a087fe 2019-02-21 stsp
4078 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4079 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4080 aa61903a 2021-12-31 thomas s->matched_line = 0;
4081 05171be4 2022-06-23 thomas view->x = 0;
4082 15a087fe 2019-02-21 stsp
4083 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4084 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4085 1e37a5c2 2019-05-12 jcs break;
4086 1e37a5c2 2019-05-12 jcs case '>':
4087 1e37a5c2 2019-05-12 jcs case '.':
4088 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
4089 15a087fe 2019-02-21 stsp break;
4090 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4091 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4092 5e224a3e 2019-02-22 stsp
4093 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4094 1e37a5c2 2019-05-12 jcs if (err)
4095 5a8b5076 2020-12-05 stsp break;
4096 5a8b5076 2020-12-05 stsp
4097 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4098 1e37a5c2 2019-05-12 jcs break;
4099 15a087fe 2019-02-21 stsp
4100 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4101 1e37a5c2 2019-05-12 jcs if (err)
4102 1e37a5c2 2019-05-12 jcs break;
4103 15a087fe 2019-02-21 stsp
4104 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4105 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4106 aa61903a 2021-12-31 thomas s->matched_line = 0;
4107 05171be4 2022-06-23 thomas view->x = 0;
4108 1e37a5c2 2019-05-12 jcs
4109 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4110 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4111 1e37a5c2 2019-05-12 jcs break;
4112 1e37a5c2 2019-05-12 jcs default:
4113 1e37a5c2 2019-05-12 jcs break;
4114 26ed57b2 2018-05-19 stsp }
4115 e5a0f69f 2018-08-18 stsp
4116 bcbd79e2 2018-08-19 stsp return err;
4117 26ed57b2 2018-05-19 stsp }
4118 26ed57b2 2018-05-19 stsp
4119 4ed7e80c 2018-05-20 stsp static const struct got_error *
4120 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4121 9f7d7167 2018-04-29 stsp {
4122 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4123 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4124 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4125 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4126 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4127 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4128 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4129 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4130 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4131 3dbaef42 2020-11-24 stsp const char *errstr;
4132 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4133 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4134 70ac5f84 2019-03-28 stsp
4135 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4136 26ed57b2 2018-05-19 stsp switch (ch) {
4137 64453f7e 2020-11-21 stsp case 'a':
4138 64453f7e 2020-11-21 stsp force_text_diff = 1;
4139 3dbaef42 2020-11-24 stsp break;
4140 3dbaef42 2020-11-24 stsp case 'C':
4141 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4142 3dbaef42 2020-11-24 stsp &errstr);
4143 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4144 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4145 8f666e67 2022-02-12 thomas errstr, errstr);
4146 64453f7e 2020-11-21 stsp break;
4147 09b5bff8 2020-02-23 naddy case 'r':
4148 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4149 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4150 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4151 09b5bff8 2020-02-23 naddy optarg);
4152 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4153 09b5bff8 2020-02-23 naddy break;
4154 3dbaef42 2020-11-24 stsp case 'w':
4155 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4156 3dbaef42 2020-11-24 stsp break;
4157 26ed57b2 2018-05-19 stsp default:
4158 17020d27 2019-03-07 stsp usage_diff();
4159 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4160 26ed57b2 2018-05-19 stsp }
4161 26ed57b2 2018-05-19 stsp }
4162 26ed57b2 2018-05-19 stsp
4163 26ed57b2 2018-05-19 stsp argc -= optind;
4164 26ed57b2 2018-05-19 stsp argv += optind;
4165 26ed57b2 2018-05-19 stsp
4166 26ed57b2 2018-05-19 stsp if (argc == 0) {
4167 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4168 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4169 15a94983 2018-12-23 stsp id_str1 = argv[0];
4170 15a94983 2018-12-23 stsp id_str2 = argv[1];
4171 26ed57b2 2018-05-19 stsp } else
4172 26ed57b2 2018-05-19 stsp usage_diff();
4173 eb6600df 2019-01-04 stsp
4174 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4175 7cd52833 2022-06-23 thomas if (error)
4176 7cd52833 2022-06-23 thomas goto done;
4177 7cd52833 2022-06-23 thomas
4178 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4179 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4180 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4181 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4182 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4183 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4184 c156c7a4 2020-12-18 stsp goto done;
4185 a273ac94 2020-02-23 naddy if (worktree)
4186 a273ac94 2020-02-23 naddy repo_path =
4187 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4188 a273ac94 2020-02-23 naddy else
4189 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4190 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4191 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4192 c156c7a4 2020-12-18 stsp goto done;
4193 c156c7a4 2020-12-18 stsp }
4194 a273ac94 2020-02-23 naddy }
4195 a273ac94 2020-02-23 naddy
4196 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4197 eb6600df 2019-01-04 stsp if (error)
4198 eb6600df 2019-01-04 stsp goto done;
4199 26ed57b2 2018-05-19 stsp
4200 a273ac94 2020-02-23 naddy init_curses();
4201 a273ac94 2020-02-23 naddy
4202 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4203 51a10b52 2020-12-26 stsp if (error)
4204 51a10b52 2020-12-26 stsp goto done;
4205 51a10b52 2020-12-26 stsp
4206 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4207 26ed57b2 2018-05-19 stsp if (error)
4208 26ed57b2 2018-05-19 stsp goto done;
4209 26ed57b2 2018-05-19 stsp
4210 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4211 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4212 26ed57b2 2018-05-19 stsp if (error)
4213 26ed57b2 2018-05-19 stsp goto done;
4214 26ed57b2 2018-05-19 stsp
4215 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4216 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4217 26ed57b2 2018-05-19 stsp if (error)
4218 26ed57b2 2018-05-19 stsp goto done;
4219 26ed57b2 2018-05-19 stsp
4220 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4221 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4222 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4223 ea5e7bb5 2018-08-01 stsp goto done;
4224 ea5e7bb5 2018-08-01 stsp }
4225 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4226 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4227 5dc9f4bc 2018-08-04 stsp if (error)
4228 5dc9f4bc 2018-08-04 stsp goto done;
4229 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4230 26ed57b2 2018-05-19 stsp done:
4231 3dbaef42 2020-11-24 stsp free(label1);
4232 3dbaef42 2020-11-24 stsp free(label2);
4233 c02c541e 2019-03-29 stsp free(repo_path);
4234 a273ac94 2020-02-23 naddy free(cwd);
4235 1d0f4054 2021-06-17 stsp if (repo) {
4236 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4237 1d0f4054 2021-06-17 stsp if (error == NULL)
4238 1d0f4054 2021-06-17 stsp error = close_err;
4239 1d0f4054 2021-06-17 stsp }
4240 a273ac94 2020-02-23 naddy if (worktree)
4241 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4242 7cd52833 2022-06-23 thomas if (pack_fds) {
4243 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4244 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4245 7cd52833 2022-06-23 thomas if (error == NULL)
4246 7cd52833 2022-06-23 thomas error = pack_err;
4247 7cd52833 2022-06-23 thomas }
4248 51a10b52 2020-12-26 stsp tog_free_refs();
4249 26ed57b2 2018-05-19 stsp return error;
4250 9f7d7167 2018-04-29 stsp }
4251 9f7d7167 2018-04-29 stsp
4252 4ed7e80c 2018-05-20 stsp __dead static void
4253 9f7d7167 2018-04-29 stsp usage_blame(void)
4254 9f7d7167 2018-04-29 stsp {
4255 80ddbec8 2018-04-29 stsp endwin();
4256 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4257 9f7d7167 2018-04-29 stsp getprogname());
4258 9f7d7167 2018-04-29 stsp exit(1);
4259 9f7d7167 2018-04-29 stsp }
4260 84451b3e 2018-07-10 stsp
4261 84451b3e 2018-07-10 stsp struct tog_blame_line {
4262 84451b3e 2018-07-10 stsp int annotated;
4263 84451b3e 2018-07-10 stsp struct got_object_id *id;
4264 84451b3e 2018-07-10 stsp };
4265 9f7d7167 2018-04-29 stsp
4266 4ed7e80c 2018-05-20 stsp static const struct got_error *
4267 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4268 84451b3e 2018-07-10 stsp {
4269 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4270 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4271 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4272 84451b3e 2018-07-10 stsp const struct got_error *err;
4273 02bce7e2 2022-06-23 thomas int lineno = 0, nprinted = 0, i;
4274 826082fe 2020-12-10 stsp char *line = NULL;
4275 826082fe 2020-12-10 stsp size_t linesize = 0;
4276 826082fe 2020-12-10 stsp ssize_t linelen;
4277 84451b3e 2018-07-10 stsp wchar_t *wline;
4278 27a741e5 2019-09-11 stsp int width;
4279 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4280 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4281 ab089a2a 2018-07-12 stsp char *id_str;
4282 11b20872 2019-11-08 stsp struct tog_color *tc;
4283 ab089a2a 2018-07-12 stsp
4284 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4285 ab089a2a 2018-07-12 stsp if (err)
4286 ab089a2a 2018-07-12 stsp return err;
4287 84451b3e 2018-07-10 stsp
4288 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4289 f7d12f7e 2018-08-01 stsp werase(view->window);
4290 84451b3e 2018-07-10 stsp
4291 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4292 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4293 ab089a2a 2018-07-12 stsp free(id_str);
4294 ab089a2a 2018-07-12 stsp return err;
4295 ab089a2a 2018-07-12 stsp }
4296 ab089a2a 2018-07-12 stsp
4297 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4298 ab089a2a 2018-07-12 stsp free(line);
4299 2550e4c3 2018-07-13 stsp line = NULL;
4300 1cae65b4 2019-09-22 stsp if (err)
4301 1cae65b4 2019-09-22 stsp return err;
4302 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4303 a3404814 2018-09-02 stsp wstandout(view->window);
4304 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4305 11b20872 2019-11-08 stsp if (tc)
4306 11b20872 2019-11-08 stsp wattr_on(view->window,
4307 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4308 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4309 11b20872 2019-11-08 stsp if (tc)
4310 11b20872 2019-11-08 stsp wattr_off(view->window,
4311 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4312 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4313 a3404814 2018-09-02 stsp wstandend(view->window);
4314 2550e4c3 2018-07-13 stsp free(wline);
4315 2550e4c3 2018-07-13 stsp wline = NULL;
4316 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4317 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4318 ab089a2a 2018-07-12 stsp
4319 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4320 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4321 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4322 ab089a2a 2018-07-12 stsp free(id_str);
4323 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4324 ab089a2a 2018-07-12 stsp }
4325 ab089a2a 2018-07-12 stsp free(id_str);
4326 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4327 3f60a8ef 2018-07-10 stsp free(line);
4328 2550e4c3 2018-07-13 stsp line = NULL;
4329 3f60a8ef 2018-07-10 stsp if (err)
4330 3f60a8ef 2018-07-10 stsp return err;
4331 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4332 2550e4c3 2018-07-13 stsp free(wline);
4333 2550e4c3 2018-07-13 stsp wline = NULL;
4334 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4335 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4336 3f60a8ef 2018-07-10 stsp
4337 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4338 05171be4 2022-06-23 thomas view->maxx = 0;
4339 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4340 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4341 826082fe 2020-12-10 stsp if (linelen == -1) {
4342 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4343 826082fe 2020-12-10 stsp s->eof = 1;
4344 826082fe 2020-12-10 stsp break;
4345 826082fe 2020-12-10 stsp }
4346 84451b3e 2018-07-10 stsp free(line);
4347 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4348 84451b3e 2018-07-10 stsp }
4349 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4350 826082fe 2020-12-10 stsp continue;
4351 84451b3e 2018-07-10 stsp
4352 05171be4 2022-06-23 thomas view->maxx = MAX(view->maxx, linelen);
4353 05171be4 2022-06-23 thomas
4354 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4355 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4356 b700b5d6 2018-07-10 stsp
4357 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4358 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4359 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4360 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4361 27a741e5 2019-09-11 stsp !(view->focussed &&
4362 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4363 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4364 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4365 8d0fe45a 2019-08-12 stsp char *id_str;
4366 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4367 8d0fe45a 2019-08-12 stsp if (err) {
4368 8d0fe45a 2019-08-12 stsp free(line);
4369 8d0fe45a 2019-08-12 stsp return err;
4370 8d0fe45a 2019-08-12 stsp }
4371 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4372 11b20872 2019-11-08 stsp if (tc)
4373 11b20872 2019-11-08 stsp wattr_on(view->window,
4374 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4375 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4376 11b20872 2019-11-08 stsp if (tc)
4377 11b20872 2019-11-08 stsp wattr_off(view->window,
4378 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4379 8d0fe45a 2019-08-12 stsp free(id_str);
4380 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4381 8d0fe45a 2019-08-12 stsp } else {
4382 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4383 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4384 84451b3e 2018-07-10 stsp }
4385 ee41ec32 2018-07-10 stsp } else {
4386 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4387 ee41ec32 2018-07-10 stsp prev_id = NULL;
4388 ee41ec32 2018-07-10 stsp }
4389 84451b3e 2018-07-10 stsp
4390 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4391 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4392 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4393 27a741e5 2019-09-11 stsp
4394 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4395 41605754 2020-11-12 stsp width = 9;
4396 41605754 2020-11-12 stsp wline = wcsdup(L"");
4397 41605754 2020-11-12 stsp if (wline == NULL) {
4398 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4399 41605754 2020-11-12 stsp free(line);
4400 41605754 2020-11-12 stsp return err;
4401 41605754 2020-11-12 stsp }
4402 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4403 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4404 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4405 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4406 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4407 41605754 2020-11-12 stsp if (err) {
4408 41605754 2020-11-12 stsp free(line);
4409 41605754 2020-11-12 stsp return err;
4410 41605754 2020-11-12 stsp }
4411 41605754 2020-11-12 stsp width += 9;
4412 41605754 2020-11-12 stsp } else {
4413 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0,
4414 05171be4 2022-06-23 thomas view->x + view->ncols - 9, 9, 1);
4415 02bce7e2 2022-06-23 thomas if (err) {
4416 02bce7e2 2022-06-23 thomas free(line);
4417 02bce7e2 2022-06-23 thomas return err;
4418 02bce7e2 2022-06-23 thomas }
4419 02bce7e2 2022-06-23 thomas if (view->x < width) {
4420 02bce7e2 2022-06-23 thomas waddwstr(view->window, wline + view->x);
4421 02bce7e2 2022-06-23 thomas for (i = 0; i < view->x; i++)
4422 02bce7e2 2022-06-23 thomas width -= wcwidth(wline[i]);
4423 05171be4 2022-06-23 thomas }
4424 02bce7e2 2022-06-23 thomas width += 9;
4425 41605754 2020-11-12 stsp free(wline);
4426 41605754 2020-11-12 stsp wline = NULL;
4427 41605754 2020-11-12 stsp }
4428 41605754 2020-11-12 stsp
4429 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4430 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4431 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4432 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4433 84451b3e 2018-07-10 stsp }
4434 826082fe 2020-12-10 stsp free(line);
4435 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4436 84451b3e 2018-07-10 stsp
4437 1a57306a 2018-09-02 stsp view_vborder(view);
4438 84451b3e 2018-07-10 stsp
4439 84451b3e 2018-07-10 stsp return NULL;
4440 84451b3e 2018-07-10 stsp }
4441 84451b3e 2018-07-10 stsp
4442 84451b3e 2018-07-10 stsp static const struct got_error *
4443 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4444 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4445 84451b3e 2018-07-10 stsp {
4446 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4447 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4448 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4449 1a76625f 2018-10-22 stsp int errcode;
4450 84451b3e 2018-07-10 stsp
4451 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4452 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4453 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4454 84451b3e 2018-07-10 stsp
4455 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4456 1a76625f 2018-10-22 stsp if (errcode)
4457 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4458 84451b3e 2018-07-10 stsp
4459 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4460 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4461 d68a0a7d 2018-07-10 stsp goto done;
4462 d68a0a7d 2018-07-10 stsp }
4463 d68a0a7d 2018-07-10 stsp
4464 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4465 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4466 d68a0a7d 2018-07-10 stsp
4467 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4468 d68a0a7d 2018-07-10 stsp if (line->annotated)
4469 d68a0a7d 2018-07-10 stsp goto done;
4470 d68a0a7d 2018-07-10 stsp
4471 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4472 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4473 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4474 84451b3e 2018-07-10 stsp goto done;
4475 84451b3e 2018-07-10 stsp }
4476 84451b3e 2018-07-10 stsp line->annotated = 1;
4477 84451b3e 2018-07-10 stsp done:
4478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4479 1a76625f 2018-10-22 stsp if (errcode)
4480 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4481 84451b3e 2018-07-10 stsp return err;
4482 84451b3e 2018-07-10 stsp }
4483 84451b3e 2018-07-10 stsp
4484 84451b3e 2018-07-10 stsp static void *
4485 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4486 84451b3e 2018-07-10 stsp {
4487 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4488 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4489 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4490 1a76625f 2018-10-22 stsp int errcode;
4491 18430de3 2018-07-10 stsp
4492 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4493 61266923 2020-01-14 stsp if (err)
4494 61266923 2020-01-14 stsp return (void *)err;
4495 61266923 2020-01-14 stsp
4496 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4497 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4498 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4499 fc06ba56 2019-08-22 stsp err = NULL;
4500 18430de3 2018-07-10 stsp
4501 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4502 1a76625f 2018-10-22 stsp if (errcode)
4503 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4504 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4505 18430de3 2018-07-10 stsp
4506 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4507 1d0f4054 2021-06-17 stsp if (err == NULL)
4508 1d0f4054 2021-06-17 stsp err = close_err;
4509 c9beca56 2018-07-22 stsp ta->repo = NULL;
4510 c9beca56 2018-07-22 stsp *ta->complete = 1;
4511 18430de3 2018-07-10 stsp
4512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4515 18430de3 2018-07-10 stsp
4516 18430de3 2018-07-10 stsp return (void *)err;
4517 84451b3e 2018-07-10 stsp }
4518 84451b3e 2018-07-10 stsp
4519 245d91c1 2018-07-12 stsp static struct got_object_id *
4520 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4521 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4522 245d91c1 2018-07-12 stsp {
4523 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4524 8d0fe45a 2019-08-12 stsp
4525 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4526 8d0fe45a 2019-08-12 stsp return NULL;
4527 b880a918 2018-07-10 stsp
4528 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4529 245d91c1 2018-07-12 stsp if (!line->annotated)
4530 245d91c1 2018-07-12 stsp return NULL;
4531 245d91c1 2018-07-12 stsp
4532 245d91c1 2018-07-12 stsp return line->id;
4533 b880a918 2018-07-10 stsp }
4534 245d91c1 2018-07-12 stsp
4535 b880a918 2018-07-10 stsp static const struct got_error *
4536 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4537 a70480e0 2018-06-23 stsp {
4538 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4539 245d91c1 2018-07-12 stsp int i;
4540 245d91c1 2018-07-12 stsp
4541 245d91c1 2018-07-12 stsp if (blame->thread) {
4542 1a76625f 2018-10-22 stsp int errcode;
4543 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4544 1a76625f 2018-10-22 stsp if (errcode)
4545 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4546 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4547 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4548 1a76625f 2018-10-22 stsp if (errcode)
4549 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4550 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4551 1a76625f 2018-10-22 stsp if (errcode)
4552 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4553 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4554 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4555 245d91c1 2018-07-12 stsp err = NULL;
4556 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4557 245d91c1 2018-07-12 stsp }
4558 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4559 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4560 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4561 1d0f4054 2021-06-17 stsp if (err == NULL)
4562 1d0f4054 2021-06-17 stsp err = close_err;
4563 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4564 245d91c1 2018-07-12 stsp }
4565 245d91c1 2018-07-12 stsp if (blame->f) {
4566 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4567 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4568 245d91c1 2018-07-12 stsp blame->f = NULL;
4569 245d91c1 2018-07-12 stsp }
4570 57670559 2018-12-23 stsp if (blame->lines) {
4571 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4572 57670559 2018-12-23 stsp free(blame->lines[i].id);
4573 57670559 2018-12-23 stsp free(blame->lines);
4574 57670559 2018-12-23 stsp blame->lines = NULL;
4575 57670559 2018-12-23 stsp }
4576 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4577 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4578 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
4579 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4580 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
4581 7cd52833 2022-06-23 thomas if (err == NULL)
4582 7cd52833 2022-06-23 thomas err = pack_err;
4583 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
4584 7cd52833 2022-06-23 thomas }
4585 245d91c1 2018-07-12 stsp return err;
4586 245d91c1 2018-07-12 stsp }
4587 245d91c1 2018-07-12 stsp
4588 245d91c1 2018-07-12 stsp static const struct got_error *
4589 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4590 fc06ba56 2019-08-22 stsp {
4591 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4592 fc06ba56 2019-08-22 stsp int *done = arg;
4593 fc06ba56 2019-08-22 stsp int errcode;
4594 fc06ba56 2019-08-22 stsp
4595 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4596 fc06ba56 2019-08-22 stsp if (errcode)
4597 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4598 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4599 fc06ba56 2019-08-22 stsp
4600 fc06ba56 2019-08-22 stsp if (*done)
4601 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4602 fc06ba56 2019-08-22 stsp
4603 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4604 fc06ba56 2019-08-22 stsp if (errcode)
4605 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4606 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4607 fc06ba56 2019-08-22 stsp
4608 fc06ba56 2019-08-22 stsp return err;
4609 fc06ba56 2019-08-22 stsp }
4610 fc06ba56 2019-08-22 stsp
4611 fc06ba56 2019-08-22 stsp static const struct got_error *
4612 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4613 245d91c1 2018-07-12 stsp {
4614 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4615 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4616 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4617 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4618 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4619 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4620 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4621 15a94983 2018-12-23 stsp int obj_type;
4622 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4623 a70480e0 2018-06-23 stsp
4624 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4625 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4626 27d434c2 2018-09-15 stsp if (err)
4627 15a94983 2018-12-23 stsp return err;
4628 945f9229 2022-04-16 thomas
4629 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4630 945f9229 2022-04-16 thomas if (err)
4631 945f9229 2022-04-16 thomas goto done;
4632 27d434c2 2018-09-15 stsp
4633 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4634 84451b3e 2018-07-10 stsp if (err)
4635 84451b3e 2018-07-10 stsp goto done;
4636 27d434c2 2018-09-15 stsp
4637 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4638 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4639 84451b3e 2018-07-10 stsp goto done;
4640 84451b3e 2018-07-10 stsp }
4641 a70480e0 2018-06-23 stsp
4642 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4643 a70480e0 2018-06-23 stsp if (err)
4644 a70480e0 2018-06-23 stsp goto done;
4645 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4646 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4647 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4648 84451b3e 2018-07-10 stsp goto done;
4649 84451b3e 2018-07-10 stsp }
4650 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4651 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4652 1fddf795 2021-01-20 stsp if (err)
4653 1fddf795 2021-01-20 stsp goto done;
4654 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4655 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4656 84451b3e 2018-07-10 stsp goto done;
4657 1fddf795 2021-01-20 stsp }
4658 b02560ec 2019-08-19 stsp
4659 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4660 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4661 b02560ec 2019-08-19 stsp blame->nlines--;
4662 a70480e0 2018-06-23 stsp
4663 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4664 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4665 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4666 84451b3e 2018-07-10 stsp goto done;
4667 84451b3e 2018-07-10 stsp }
4668 a70480e0 2018-06-23 stsp
4669 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
4670 bd24772e 2018-07-11 stsp if (err)
4671 bd24772e 2018-07-11 stsp goto done;
4672 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4673 7cd52833 2022-06-23 thomas pack_fds);
4674 7cd52833 2022-06-23 thomas if (err)
4675 7cd52833 2022-06-23 thomas goto done;
4676 bd24772e 2018-07-11 stsp
4677 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
4678 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4679 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4680 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4681 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4682 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4683 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4684 245d91c1 2018-07-12 stsp goto done;
4685 245d91c1 2018-07-12 stsp }
4686 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4687 245d91c1 2018-07-12 stsp
4688 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4689 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4690 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4691 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4692 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4693 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4694 a5388363 2020-12-01 naddy s->blame_complete = 0;
4695 f5a09613 2020-12-13 naddy
4696 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4697 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4698 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4699 f5a09613 2020-12-13 naddy s->selected_line = 1;
4700 f5a09613 2020-12-13 naddy }
4701 aa61903a 2021-12-31 thomas s->matched_line = 0;
4702 245d91c1 2018-07-12 stsp
4703 245d91c1 2018-07-12 stsp done:
4704 945f9229 2022-04-16 thomas if (commit)
4705 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4706 245d91c1 2018-07-12 stsp if (blob)
4707 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4708 27d434c2 2018-09-15 stsp free(obj_id);
4709 245d91c1 2018-07-12 stsp if (err)
4710 245d91c1 2018-07-12 stsp stop_blame(blame);
4711 245d91c1 2018-07-12 stsp return err;
4712 245d91c1 2018-07-12 stsp }
4713 245d91c1 2018-07-12 stsp
4714 245d91c1 2018-07-12 stsp static const struct got_error *
4715 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4716 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4717 245d91c1 2018-07-12 stsp {
4718 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4719 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4720 dbc6a6b6 2018-07-12 stsp
4721 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4722 245d91c1 2018-07-12 stsp
4723 c4843652 2019-08-12 stsp s->path = strdup(path);
4724 c4843652 2019-08-12 stsp if (s->path == NULL)
4725 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4726 c4843652 2019-08-12 stsp
4727 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4728 c4843652 2019-08-12 stsp if (err) {
4729 c4843652 2019-08-12 stsp free(s->path);
4730 7cbe629d 2018-08-04 stsp return err;
4731 c4843652 2019-08-12 stsp }
4732 245d91c1 2018-07-12 stsp
4733 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4734 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4735 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4736 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4737 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4738 fb2756b9 2018-08-04 stsp s->repo = repo;
4739 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4740 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4741 7cbe629d 2018-08-04 stsp
4742 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4743 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4744 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4745 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4746 11b20872 2019-11-08 stsp if (err)
4747 11b20872 2019-11-08 stsp return err;
4748 11b20872 2019-11-08 stsp }
4749 11b20872 2019-11-08 stsp
4750 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4751 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4752 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4753 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4754 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4755 e5a0f69f 2018-08-18 stsp
4756 a5388363 2020-12-01 naddy return run_blame(view);
4757 7cbe629d 2018-08-04 stsp }
4758 7cbe629d 2018-08-04 stsp
4759 e5a0f69f 2018-08-18 stsp static const struct got_error *
4760 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4761 7cbe629d 2018-08-04 stsp {
4762 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4763 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4764 7cbe629d 2018-08-04 stsp
4765 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4766 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4767 e5a0f69f 2018-08-18 stsp
4768 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4769 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4770 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4771 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4772 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4773 7cbe629d 2018-08-04 stsp }
4774 e5a0f69f 2018-08-18 stsp
4775 e5a0f69f 2018-08-18 stsp free(s->path);
4776 11b20872 2019-11-08 stsp free_colors(&s->colors);
4777 e5a0f69f 2018-08-18 stsp return err;
4778 7cbe629d 2018-08-04 stsp }
4779 7cbe629d 2018-08-04 stsp
4780 7cbe629d 2018-08-04 stsp static const struct got_error *
4781 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4782 6c4c42e0 2019-06-24 stsp {
4783 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4784 6c4c42e0 2019-06-24 stsp
4785 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4786 6c4c42e0 2019-06-24 stsp return NULL;
4787 6c4c42e0 2019-06-24 stsp }
4788 6c4c42e0 2019-06-24 stsp
4789 6c4c42e0 2019-06-24 stsp static const struct got_error *
4790 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4791 6c4c42e0 2019-06-24 stsp {
4792 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4793 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4794 6c4c42e0 2019-06-24 stsp int lineno;
4795 05171be4 2022-06-23 thomas char *exstr = NULL, *line = NULL;
4796 826082fe 2020-12-10 stsp size_t linesize = 0;
4797 826082fe 2020-12-10 stsp ssize_t linelen;
4798 6c4c42e0 2019-06-24 stsp
4799 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4800 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4801 6c4c42e0 2019-06-24 stsp return NULL;
4802 6c4c42e0 2019-06-24 stsp }
4803 6c4c42e0 2019-06-24 stsp
4804 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4805 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4806 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4807 6c4c42e0 2019-06-24 stsp else
4808 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4809 4b3f9dac 2021-12-17 thomas } else
4810 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4811 6c4c42e0 2019-06-24 stsp
4812 6c4c42e0 2019-06-24 stsp while (1) {
4813 6c4c42e0 2019-06-24 stsp off_t offset;
4814 6c4c42e0 2019-06-24 stsp
4815 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4816 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4817 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4818 6c4c42e0 2019-06-24 stsp break;
4819 6c4c42e0 2019-06-24 stsp }
4820 2246482e 2019-06-25 stsp
4821 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4822 6c4c42e0 2019-06-24 stsp lineno = 1;
4823 6c4c42e0 2019-06-24 stsp else
4824 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4825 6c4c42e0 2019-06-24 stsp }
4826 6c4c42e0 2019-06-24 stsp
4827 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4828 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4829 6c4c42e0 2019-06-24 stsp free(line);
4830 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4831 6c4c42e0 2019-06-24 stsp }
4832 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4833 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
4834 05171be4 2022-06-23 thomas if (err)
4835 05171be4 2022-06-23 thomas break;
4836 826082fe 2020-12-10 stsp if (linelen != -1 &&
4837 05171be4 2022-06-23 thomas match_line(exstr, &view->regex, 1, &view->regmatch)) {
4838 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4839 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4840 6c4c42e0 2019-06-24 stsp break;
4841 6c4c42e0 2019-06-24 stsp }
4842 05171be4 2022-06-23 thomas free(exstr);
4843 05171be4 2022-06-23 thomas exstr = NULL;
4844 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4845 6c4c42e0 2019-06-24 stsp lineno++;
4846 6c4c42e0 2019-06-24 stsp else
4847 6c4c42e0 2019-06-24 stsp lineno--;
4848 6c4c42e0 2019-06-24 stsp }
4849 826082fe 2020-12-10 stsp free(line);
4850 05171be4 2022-06-23 thomas free(exstr);
4851 6c4c42e0 2019-06-24 stsp
4852 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4853 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4854 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4855 6c4c42e0 2019-06-24 stsp }
4856 6c4c42e0 2019-06-24 stsp
4857 05171be4 2022-06-23 thomas return err;
4858 6c4c42e0 2019-06-24 stsp }
4859 6c4c42e0 2019-06-24 stsp
4860 6c4c42e0 2019-06-24 stsp static const struct got_error *
4861 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4862 7cbe629d 2018-08-04 stsp {
4863 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4864 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4865 2b380cc8 2018-10-24 stsp int errcode;
4866 2b380cc8 2018-10-24 stsp
4867 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4868 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4869 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4870 2b380cc8 2018-10-24 stsp if (errcode)
4871 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4872 51fe7530 2019-08-19 stsp
4873 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4874 2b380cc8 2018-10-24 stsp }
4875 e5a0f69f 2018-08-18 stsp
4876 51fe7530 2019-08-19 stsp if (s->blame_complete)
4877 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4878 51fe7530 2019-08-19 stsp
4879 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4880 e5a0f69f 2018-08-18 stsp
4881 669b5ffa 2018-10-07 stsp view_vborder(view);
4882 e5a0f69f 2018-08-18 stsp return err;
4883 e5a0f69f 2018-08-18 stsp }
4884 e5a0f69f 2018-08-18 stsp
4885 e5a0f69f 2018-08-18 stsp static const struct got_error *
4886 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4887 e5a0f69f 2018-08-18 stsp {
4888 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4889 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4890 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4891 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4892 7cbe629d 2018-08-04 stsp
4893 e5a0f69f 2018-08-18 stsp switch (ch) {
4894 05171be4 2022-06-23 thomas case '0':
4895 05171be4 2022-06-23 thomas view->x = 0;
4896 05171be4 2022-06-23 thomas break;
4897 05171be4 2022-06-23 thomas case '$':
4898 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4899 05171be4 2022-06-23 thomas break;
4900 05171be4 2022-06-23 thomas case KEY_RIGHT:
4901 05171be4 2022-06-23 thomas case 'l':
4902 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4903 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4904 05171be4 2022-06-23 thomas break;
4905 05171be4 2022-06-23 thomas case KEY_LEFT:
4906 05171be4 2022-06-23 thomas case 'h':
4907 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4908 05171be4 2022-06-23 thomas break;
4909 1e37a5c2 2019-05-12 jcs case 'q':
4910 1e37a5c2 2019-05-12 jcs s->done = 1;
4911 4deef56f 2021-09-02 naddy break;
4912 4deef56f 2021-09-02 naddy case 'g':
4913 4deef56f 2021-09-02 naddy case KEY_HOME:
4914 4deef56f 2021-09-02 naddy s->selected_line = 1;
4915 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4916 4deef56f 2021-09-02 naddy break;
4917 4deef56f 2021-09-02 naddy case 'G':
4918 4deef56f 2021-09-02 naddy case KEY_END:
4919 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4920 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4921 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4922 4deef56f 2021-09-02 naddy } else {
4923 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4924 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4925 4deef56f 2021-09-02 naddy (view->nlines - 3);
4926 4deef56f 2021-09-02 naddy }
4927 1e37a5c2 2019-05-12 jcs break;
4928 1e37a5c2 2019-05-12 jcs case 'k':
4929 1e37a5c2 2019-05-12 jcs case KEY_UP:
4930 f7140bf5 2021-10-17 thomas case CTRL('p'):
4931 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4932 1e37a5c2 2019-05-12 jcs s->selected_line--;
4933 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4934 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4935 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4936 1e37a5c2 2019-05-12 jcs break;
4937 70f17a53 2022-06-13 thomas case CTRL('u'):
4938 23427b14 2022-06-23 thomas case 'u':
4939 70f17a53 2022-06-13 thomas nscroll /= 2;
4940 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4941 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4942 ea025d1d 2020-02-22 naddy case CTRL('b'):
4943 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4944 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4945 e5a0f69f 2018-08-18 stsp break;
4946 1e37a5c2 2019-05-12 jcs }
4947 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4948 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4949 1e37a5c2 2019-05-12 jcs else
4950 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4951 1e37a5c2 2019-05-12 jcs break;
4952 1e37a5c2 2019-05-12 jcs case 'j':
4953 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4954 f7140bf5 2021-10-17 thomas case CTRL('n'):
4955 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4956 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4957 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4958 1e37a5c2 2019-05-12 jcs s->selected_line++;
4959 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4960 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4961 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4962 1e37a5c2 2019-05-12 jcs break;
4963 1e37a5c2 2019-05-12 jcs case 'b':
4964 1e37a5c2 2019-05-12 jcs case 'p': {
4965 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4966 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4967 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4968 1e37a5c2 2019-05-12 jcs if (id == NULL)
4969 e5a0f69f 2018-08-18 stsp break;
4970 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4971 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4972 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4973 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4974 1e37a5c2 2019-05-12 jcs int obj_type;
4975 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4976 1e37a5c2 2019-05-12 jcs s->repo, id);
4977 e5a0f69f 2018-08-18 stsp if (err)
4978 e5a0f69f 2018-08-18 stsp break;
4979 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4980 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4981 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4982 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4983 e5a0f69f 2018-08-18 stsp break;
4984 e5a0f69f 2018-08-18 stsp }
4985 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4986 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4987 ec242592 2022-04-22 thomas s->repo, &pid->id);
4988 945f9229 2022-04-16 thomas if (err)
4989 945f9229 2022-04-16 thomas break;
4990 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4991 945f9229 2022-04-16 thomas pcommit, s->path);
4992 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4993 e5a0f69f 2018-08-18 stsp if (err) {
4994 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4995 1e37a5c2 2019-05-12 jcs err = NULL;
4996 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4997 e5a0f69f 2018-08-18 stsp break;
4998 e5a0f69f 2018-08-18 stsp }
4999 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5000 1e37a5c2 2019-05-12 jcs blob_id);
5001 1e37a5c2 2019-05-12 jcs free(blob_id);
5002 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5003 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5004 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5005 e5a0f69f 2018-08-18 stsp break;
5006 1e37a5c2 2019-05-12 jcs }
5007 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5008 ec242592 2022-04-22 thomas &pid->id);
5009 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5010 1e37a5c2 2019-05-12 jcs } else {
5011 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5012 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5013 1e37a5c2 2019-05-12 jcs break;
5014 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5015 1e37a5c2 2019-05-12 jcs id);
5016 1e37a5c2 2019-05-12 jcs }
5017 1e37a5c2 2019-05-12 jcs if (err)
5018 e5a0f69f 2018-08-18 stsp break;
5019 1e37a5c2 2019-05-12 jcs s->done = 1;
5020 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5021 1e37a5c2 2019-05-12 jcs s->done = 0;
5022 1e37a5c2 2019-05-12 jcs if (thread_err)
5023 1e37a5c2 2019-05-12 jcs break;
5024 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5025 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5026 a5388363 2020-12-01 naddy err = run_blame(view);
5027 1e37a5c2 2019-05-12 jcs if (err)
5028 1e37a5c2 2019-05-12 jcs break;
5029 1e37a5c2 2019-05-12 jcs break;
5030 1e37a5c2 2019-05-12 jcs }
5031 1e37a5c2 2019-05-12 jcs case 'B': {
5032 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5033 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5034 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5035 1e37a5c2 2019-05-12 jcs break;
5036 1e37a5c2 2019-05-12 jcs s->done = 1;
5037 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5038 1e37a5c2 2019-05-12 jcs s->done = 0;
5039 1e37a5c2 2019-05-12 jcs if (thread_err)
5040 1e37a5c2 2019-05-12 jcs break;
5041 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5042 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5043 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5044 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5045 a5388363 2020-12-01 naddy err = run_blame(view);
5046 1e37a5c2 2019-05-12 jcs if (err)
5047 1e37a5c2 2019-05-12 jcs break;
5048 1e37a5c2 2019-05-12 jcs break;
5049 1e37a5c2 2019-05-12 jcs }
5050 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5051 1e37a5c2 2019-05-12 jcs case '\r': {
5052 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5053 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5054 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5055 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5056 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5057 1e37a5c2 2019-05-12 jcs if (id == NULL)
5058 1e37a5c2 2019-05-12 jcs break;
5059 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5060 1e37a5c2 2019-05-12 jcs if (err)
5061 1e37a5c2 2019-05-12 jcs break;
5062 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5063 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
5064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5065 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5066 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5067 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5068 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5069 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5070 1e37a5c2 2019-05-12 jcs break;
5071 15a94983 2018-12-23 stsp }
5072 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5073 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5074 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5075 1e37a5c2 2019-05-12 jcs if (err) {
5076 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5077 1e37a5c2 2019-05-12 jcs break;
5078 1e37a5c2 2019-05-12 jcs }
5079 e78dc838 2020-12-04 stsp view->focussed = 0;
5080 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5081 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5082 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5083 1e37a5c2 2019-05-12 jcs if (err)
5084 34bc9ec9 2019-02-22 stsp break;
5085 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
5086 e78dc838 2020-12-04 stsp view->focus_child = 1;
5087 1e37a5c2 2019-05-12 jcs } else
5088 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5089 1e37a5c2 2019-05-12 jcs if (err)
5090 e5a0f69f 2018-08-18 stsp break;
5091 1e37a5c2 2019-05-12 jcs break;
5092 1e37a5c2 2019-05-12 jcs }
5093 70f17a53 2022-06-13 thomas case CTRL('d'):
5094 23427b14 2022-06-23 thomas case 'd':
5095 70f17a53 2022-06-13 thomas nscroll /= 2;
5096 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5097 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5098 ea025d1d 2020-02-22 naddy case CTRL('f'):
5099 1e37a5c2 2019-05-12 jcs case ' ':
5100 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5101 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5102 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5103 e5a0f69f 2018-08-18 stsp break;
5104 1e37a5c2 2019-05-12 jcs }
5105 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5106 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5107 70f17a53 2022-06-13 thomas s->selected_line +=
5108 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5109 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5110 1e37a5c2 2019-05-12 jcs }
5111 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5112 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5113 1e37a5c2 2019-05-12 jcs else
5114 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5115 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5116 1e37a5c2 2019-05-12 jcs break;
5117 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5118 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5119 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5120 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5121 1e37a5c2 2019-05-12 jcs }
5122 1e37a5c2 2019-05-12 jcs break;
5123 1e37a5c2 2019-05-12 jcs default:
5124 1e37a5c2 2019-05-12 jcs break;
5125 a70480e0 2018-06-23 stsp }
5126 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5127 a70480e0 2018-06-23 stsp }
5128 a70480e0 2018-06-23 stsp
5129 a70480e0 2018-06-23 stsp static const struct got_error *
5130 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5131 9f7d7167 2018-04-29 stsp {
5132 a70480e0 2018-06-23 stsp const struct got_error *error;
5133 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5134 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5135 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5136 0587e10c 2020-07-23 stsp char *link_target = NULL;
5137 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5138 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5139 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5140 a70480e0 2018-06-23 stsp int ch;
5141 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5142 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5143 a70480e0 2018-06-23 stsp
5144 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5145 a70480e0 2018-06-23 stsp switch (ch) {
5146 a70480e0 2018-06-23 stsp case 'c':
5147 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5148 a70480e0 2018-06-23 stsp break;
5149 69069811 2018-08-02 stsp case 'r':
5150 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5151 69069811 2018-08-02 stsp if (repo_path == NULL)
5152 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5153 9ba1d308 2019-10-21 stsp optarg);
5154 69069811 2018-08-02 stsp break;
5155 a70480e0 2018-06-23 stsp default:
5156 17020d27 2019-03-07 stsp usage_blame();
5157 a70480e0 2018-06-23 stsp /* NOTREACHED */
5158 a70480e0 2018-06-23 stsp }
5159 a70480e0 2018-06-23 stsp }
5160 a70480e0 2018-06-23 stsp
5161 a70480e0 2018-06-23 stsp argc -= optind;
5162 a70480e0 2018-06-23 stsp argv += optind;
5163 a70480e0 2018-06-23 stsp
5164 f135c941 2020-02-20 stsp if (argc != 1)
5165 a70480e0 2018-06-23 stsp usage_blame();
5166 6962eb72 2020-02-20 stsp
5167 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5168 7cd52833 2022-06-23 thomas if (error != NULL)
5169 7cd52833 2022-06-23 thomas goto done;
5170 7cd52833 2022-06-23 thomas
5171 69069811 2018-08-02 stsp if (repo_path == NULL) {
5172 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5173 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5174 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5175 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5176 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5177 c156c7a4 2020-12-18 stsp goto done;
5178 f135c941 2020-02-20 stsp if (worktree)
5179 eb41ed75 2019-02-05 stsp repo_path =
5180 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5181 f135c941 2020-02-20 stsp else
5182 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5183 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5184 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5185 c156c7a4 2020-12-18 stsp goto done;
5186 c156c7a4 2020-12-18 stsp }
5187 f135c941 2020-02-20 stsp }
5188 a915003a 2019-02-05 stsp
5189 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5190 c02c541e 2019-03-29 stsp if (error != NULL)
5191 8e94dd5b 2019-01-04 stsp goto done;
5192 69069811 2018-08-02 stsp
5193 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5194 f135c941 2020-02-20 stsp worktree);
5195 c02c541e 2019-03-29 stsp if (error)
5196 92205607 2019-01-04 stsp goto done;
5197 69069811 2018-08-02 stsp
5198 f135c941 2020-02-20 stsp init_curses();
5199 f135c941 2020-02-20 stsp
5200 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5201 51a10b52 2020-12-26 stsp if (error)
5202 51a10b52 2020-12-26 stsp goto done;
5203 51a10b52 2020-12-26 stsp
5204 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5205 eb41ed75 2019-02-05 stsp if (error)
5206 69069811 2018-08-02 stsp goto done;
5207 a70480e0 2018-06-23 stsp
5208 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5209 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5210 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5211 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5212 a70480e0 2018-06-23 stsp if (error != NULL)
5213 66b4983c 2018-06-23 stsp goto done;
5214 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5215 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5216 a70480e0 2018-06-23 stsp } else {
5217 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5218 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5219 a70480e0 2018-06-23 stsp }
5220 a19e88aa 2018-06-23 stsp if (error != NULL)
5221 8b473291 2019-02-21 stsp goto done;
5222 8b473291 2019-02-21 stsp
5223 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5224 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5225 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5226 e1cd8fed 2018-08-01 stsp goto done;
5227 e1cd8fed 2018-08-01 stsp }
5228 0587e10c 2020-07-23 stsp
5229 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5230 945f9229 2022-04-16 thomas if (error)
5231 945f9229 2022-04-16 thomas goto done;
5232 945f9229 2022-04-16 thomas
5233 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5234 945f9229 2022-04-16 thomas commit, repo);
5235 7cbe629d 2018-08-04 stsp if (error)
5236 7cbe629d 2018-08-04 stsp goto done;
5237 0587e10c 2020-07-23 stsp
5238 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5239 78756c87 2020-11-24 stsp commit_id, repo);
5240 0587e10c 2020-07-23 stsp if (error)
5241 0587e10c 2020-07-23 stsp goto done;
5242 12314ad4 2019-08-31 stsp if (worktree) {
5243 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5244 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5245 12314ad4 2019-08-31 stsp worktree = NULL;
5246 12314ad4 2019-08-31 stsp }
5247 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5248 a70480e0 2018-06-23 stsp done:
5249 69069811 2018-08-02 stsp free(repo_path);
5250 f135c941 2020-02-20 stsp free(in_repo_path);
5251 0587e10c 2020-07-23 stsp free(link_target);
5252 69069811 2018-08-02 stsp free(cwd);
5253 a70480e0 2018-06-23 stsp free(commit_id);
5254 945f9229 2022-04-16 thomas if (commit)
5255 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5256 eb41ed75 2019-02-05 stsp if (worktree)
5257 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5258 1d0f4054 2021-06-17 stsp if (repo) {
5259 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5260 1d0f4054 2021-06-17 stsp if (error == NULL)
5261 1d0f4054 2021-06-17 stsp error = close_err;
5262 1d0f4054 2021-06-17 stsp }
5263 7cd52833 2022-06-23 thomas if (pack_fds) {
5264 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5265 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5266 7cd52833 2022-06-23 thomas if (error == NULL)
5267 7cd52833 2022-06-23 thomas error = pack_err;
5268 7cd52833 2022-06-23 thomas }
5269 51a10b52 2020-12-26 stsp tog_free_refs();
5270 a70480e0 2018-06-23 stsp return error;
5271 ffd1d5e5 2018-06-23 stsp }
5272 ffd1d5e5 2018-06-23 stsp
5273 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5274 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5275 ffd1d5e5 2018-06-23 stsp {
5276 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5277 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5278 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5279 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5280 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5281 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5282 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5283 ffd1d5e5 2018-06-23 stsp
5284 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5285 ffd1d5e5 2018-06-23 stsp
5286 f7d12f7e 2018-08-01 stsp werase(view->window);
5287 ffd1d5e5 2018-06-23 stsp
5288 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5289 ffd1d5e5 2018-06-23 stsp return NULL;
5290 ffd1d5e5 2018-06-23 stsp
5291 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5292 f91a2b48 2022-06-23 thomas 0, 0);
5293 ffd1d5e5 2018-06-23 stsp if (err)
5294 ffd1d5e5 2018-06-23 stsp return err;
5295 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5296 a3404814 2018-09-02 stsp wstandout(view->window);
5297 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5298 11b20872 2019-11-08 stsp if (tc)
5299 11b20872 2019-11-08 stsp wattr_on(view->window,
5300 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5301 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5302 11b20872 2019-11-08 stsp if (tc)
5303 11b20872 2019-11-08 stsp wattr_off(view->window,
5304 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5305 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5306 a3404814 2018-09-02 stsp wstandend(view->window);
5307 2550e4c3 2018-07-13 stsp free(wline);
5308 2550e4c3 2018-07-13 stsp wline = NULL;
5309 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5310 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5311 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5312 ffd1d5e5 2018-06-23 stsp return NULL;
5313 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5314 f91a2b48 2022-06-23 thomas 0, 0);
5315 ce52c690 2018-06-23 stsp if (err)
5316 ce52c690 2018-06-23 stsp return err;
5317 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5318 2550e4c3 2018-07-13 stsp free(wline);
5319 2550e4c3 2018-07-13 stsp wline = NULL;
5320 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5321 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5322 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5323 ffd1d5e5 2018-06-23 stsp return NULL;
5324 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5325 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5326 a1eca9bb 2018-06-23 stsp return NULL;
5327 ffd1d5e5 2018-06-23 stsp
5328 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5329 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5330 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5331 0cf4efb1 2018-09-29 stsp if (view->focussed)
5332 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5333 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5334 ffd1d5e5 2018-06-23 stsp }
5335 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5336 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5337 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5338 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5339 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5340 ffd1d5e5 2018-06-23 stsp return NULL;
5341 ffd1d5e5 2018-06-23 stsp n = 1;
5342 ffd1d5e5 2018-06-23 stsp } else {
5343 ffd1d5e5 2018-06-23 stsp n = 0;
5344 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5345 ffd1d5e5 2018-06-23 stsp }
5346 ffd1d5e5 2018-06-23 stsp
5347 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5348 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5349 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5350 848d6979 2019-08-12 stsp const char *modestr = "";
5351 56e0773d 2019-11-28 stsp mode_t mode;
5352 1d13200f 2018-07-12 stsp
5353 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5354 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5355 56e0773d 2019-11-28 stsp
5356 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5357 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5358 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5359 1d13200f 2018-07-12 stsp if (err)
5360 638f9024 2019-05-13 stsp return got_error_from_errno(
5361 230a42bd 2019-05-11 jcs "got_object_id_str");
5362 1d13200f 2018-07-12 stsp }
5363 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5364 63c5ca5d 2019-08-24 stsp modestr = "$";
5365 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5366 0d6c6ee3 2020-05-20 stsp int i;
5367 0d6c6ee3 2020-05-20 stsp
5368 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5369 d86d3b18 2020-12-01 naddy te, s->repo);
5370 0d6c6ee3 2020-05-20 stsp if (err) {
5371 0d6c6ee3 2020-05-20 stsp free(id_str);
5372 0d6c6ee3 2020-05-20 stsp return err;
5373 0d6c6ee3 2020-05-20 stsp }
5374 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5375 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5376 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5377 0d6c6ee3 2020-05-20 stsp }
5378 848d6979 2019-08-12 stsp modestr = "@";
5379 0d6c6ee3 2020-05-20 stsp }
5380 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5381 848d6979 2019-08-12 stsp modestr = "/";
5382 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5383 848d6979 2019-08-12 stsp modestr = "*";
5384 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5385 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5386 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5387 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5388 1d13200f 2018-07-12 stsp free(id_str);
5389 0d6c6ee3 2020-05-20 stsp free(link_target);
5390 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5391 1d13200f 2018-07-12 stsp }
5392 1d13200f 2018-07-12 stsp free(id_str);
5393 0d6c6ee3 2020-05-20 stsp free(link_target);
5394 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5395 f91a2b48 2022-06-23 thomas 0, 0);
5396 ffd1d5e5 2018-06-23 stsp if (err) {
5397 ffd1d5e5 2018-06-23 stsp free(line);
5398 ffd1d5e5 2018-06-23 stsp break;
5399 ffd1d5e5 2018-06-23 stsp }
5400 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5401 0cf4efb1 2018-09-29 stsp if (view->focussed)
5402 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5403 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5404 ffd1d5e5 2018-06-23 stsp }
5405 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5406 f26dddb7 2019-11-08 stsp if (tc)
5407 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5408 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5409 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5410 f26dddb7 2019-11-08 stsp if (tc)
5411 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5412 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5413 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5414 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5415 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5416 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5417 ffd1d5e5 2018-06-23 stsp free(line);
5418 2550e4c3 2018-07-13 stsp free(wline);
5419 2550e4c3 2018-07-13 stsp wline = NULL;
5420 ffd1d5e5 2018-06-23 stsp n++;
5421 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5422 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5423 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5424 ffd1d5e5 2018-06-23 stsp break;
5425 ffd1d5e5 2018-06-23 stsp }
5426 ffd1d5e5 2018-06-23 stsp
5427 ffd1d5e5 2018-06-23 stsp return err;
5428 ffd1d5e5 2018-06-23 stsp }
5429 ffd1d5e5 2018-06-23 stsp
5430 ffd1d5e5 2018-06-23 stsp static void
5431 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5432 ffd1d5e5 2018-06-23 stsp {
5433 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5434 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5435 fa86c4bf 2020-11-29 stsp int i = 0;
5436 ffd1d5e5 2018-06-23 stsp
5437 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5438 ffd1d5e5 2018-06-23 stsp return;
5439 ffd1d5e5 2018-06-23 stsp
5440 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5441 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5442 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5443 fa86c4bf 2020-11-29 stsp if (!isroot)
5444 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5445 fa86c4bf 2020-11-29 stsp break;
5446 fa86c4bf 2020-11-29 stsp }
5447 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5448 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5449 ffd1d5e5 2018-06-23 stsp }
5450 ffd1d5e5 2018-06-23 stsp }
5451 ffd1d5e5 2018-06-23 stsp
5452 fa86c4bf 2020-11-29 stsp static void
5453 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5454 ffd1d5e5 2018-06-23 stsp {
5455 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5456 ffd1d5e5 2018-06-23 stsp int n = 0;
5457 ffd1d5e5 2018-06-23 stsp
5458 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5459 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5460 694d3271 2020-12-01 naddy s->first_displayed_entry);
5461 694d3271 2020-12-01 naddy else
5462 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5463 56e0773d 2019-11-28 stsp
5464 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5465 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5466 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5467 768394f3 2019-01-24 stsp if (last) {
5468 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5469 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5470 768394f3 2019-01-24 stsp }
5471 ffd1d5e5 2018-06-23 stsp }
5472 ffd1d5e5 2018-06-23 stsp }
5473 ffd1d5e5 2018-06-23 stsp
5474 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5475 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5476 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5477 ffd1d5e5 2018-06-23 stsp {
5478 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5479 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5480 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5481 ffd1d5e5 2018-06-23 stsp
5482 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5483 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5484 56e0773d 2019-11-28 stsp + 1 /* slash */;
5485 ce52c690 2018-06-23 stsp if (te)
5486 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5487 ce52c690 2018-06-23 stsp
5488 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5489 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5490 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5491 ffd1d5e5 2018-06-23 stsp
5492 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5493 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5494 d9765a41 2018-06-23 stsp while (pt) {
5495 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5496 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5497 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5498 cb2ebc8a 2018-06-23 stsp goto done;
5499 cb2ebc8a 2018-06-23 stsp }
5500 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5501 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5502 cb2ebc8a 2018-06-23 stsp goto done;
5503 cb2ebc8a 2018-06-23 stsp }
5504 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5505 ffd1d5e5 2018-06-23 stsp }
5506 ce52c690 2018-06-23 stsp if (te) {
5507 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5508 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5509 ce52c690 2018-06-23 stsp goto done;
5510 ce52c690 2018-06-23 stsp }
5511 cb2ebc8a 2018-06-23 stsp }
5512 ce52c690 2018-06-23 stsp done:
5513 ce52c690 2018-06-23 stsp if (err) {
5514 ce52c690 2018-06-23 stsp free(*path);
5515 ce52c690 2018-06-23 stsp *path = NULL;
5516 ce52c690 2018-06-23 stsp }
5517 ce52c690 2018-06-23 stsp return err;
5518 ce52c690 2018-06-23 stsp }
5519 ce52c690 2018-06-23 stsp
5520 ce52c690 2018-06-23 stsp static const struct got_error *
5521 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5522 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5523 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5524 ce52c690 2018-06-23 stsp {
5525 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5526 ce52c690 2018-06-23 stsp char *path;
5527 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5528 a0de39f3 2019-08-09 stsp
5529 a0de39f3 2019-08-09 stsp *new_view = NULL;
5530 69efd4c4 2018-07-18 stsp
5531 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5532 ce52c690 2018-06-23 stsp if (err)
5533 ce52c690 2018-06-23 stsp return err;
5534 ffd1d5e5 2018-06-23 stsp
5535 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5536 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5537 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5538 83ce39e3 2019-08-12 stsp goto done;
5539 83ce39e3 2019-08-12 stsp }
5540 cdf1ee82 2018-08-01 stsp
5541 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5542 e5a0f69f 2018-08-18 stsp if (err) {
5543 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5544 fc06ba56 2019-08-22 stsp err = NULL;
5545 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5546 e5a0f69f 2018-08-18 stsp } else
5547 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5548 83ce39e3 2019-08-12 stsp done:
5549 83ce39e3 2019-08-12 stsp free(path);
5550 69efd4c4 2018-07-18 stsp return err;
5551 69efd4c4 2018-07-18 stsp }
5552 69efd4c4 2018-07-18 stsp
5553 69efd4c4 2018-07-18 stsp static const struct got_error *
5554 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5555 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5556 69efd4c4 2018-07-18 stsp {
5557 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5558 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5559 69efd4c4 2018-07-18 stsp char *path;
5560 a0de39f3 2019-08-09 stsp
5561 a0de39f3 2019-08-09 stsp *new_view = NULL;
5562 69efd4c4 2018-07-18 stsp
5563 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5564 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5565 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5566 e5a0f69f 2018-08-18 stsp
5567 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5568 69efd4c4 2018-07-18 stsp if (err)
5569 69efd4c4 2018-07-18 stsp return err;
5570 69efd4c4 2018-07-18 stsp
5571 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5572 4e97c21c 2020-12-06 stsp path, 0);
5573 ba4f502b 2018-08-04 stsp if (err)
5574 e5a0f69f 2018-08-18 stsp view_close(log_view);
5575 e5a0f69f 2018-08-18 stsp else
5576 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5577 cb2ebc8a 2018-06-23 stsp free(path);
5578 cb2ebc8a 2018-06-23 stsp return err;
5579 ffd1d5e5 2018-06-23 stsp }
5580 ffd1d5e5 2018-06-23 stsp
5581 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5582 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5583 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5584 ffd1d5e5 2018-06-23 stsp {
5585 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5586 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5587 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5588 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5589 ffd1d5e5 2018-06-23 stsp
5590 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5591 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5592 bc573f3b 2021-07-10 stsp
5593 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5594 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5595 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5596 ffd1d5e5 2018-06-23 stsp
5597 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5598 bc573f3b 2021-07-10 stsp if (err)
5599 bc573f3b 2021-07-10 stsp goto done;
5600 bc573f3b 2021-07-10 stsp
5601 bc573f3b 2021-07-10 stsp /*
5602 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5603 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5604 bc573f3b 2021-07-10 stsp * closed on demand.
5605 bc573f3b 2021-07-10 stsp */
5606 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5607 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5608 bc573f3b 2021-07-10 stsp if (err)
5609 bc573f3b 2021-07-10 stsp goto done;
5610 bc573f3b 2021-07-10 stsp s->tree = s->root;
5611 bc573f3b 2021-07-10 stsp
5612 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5613 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5614 ffd1d5e5 2018-06-23 stsp goto done;
5615 ffd1d5e5 2018-06-23 stsp
5616 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5617 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5618 ffd1d5e5 2018-06-23 stsp goto done;
5619 ffd1d5e5 2018-06-23 stsp }
5620 ffd1d5e5 2018-06-23 stsp
5621 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5622 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5623 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5624 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5625 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5626 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5627 9cd7cbd1 2020-12-07 stsp goto done;
5628 9cd7cbd1 2020-12-07 stsp }
5629 9cd7cbd1 2020-12-07 stsp }
5630 fb2756b9 2018-08-04 stsp s->repo = repo;
5631 c0b01bdb 2019-11-08 stsp
5632 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5633 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5634 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5635 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5636 c0b01bdb 2019-11-08 stsp if (err)
5637 c0b01bdb 2019-11-08 stsp goto done;
5638 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5639 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5640 bc573f3b 2021-07-10 stsp if (err)
5641 c0b01bdb 2019-11-08 stsp goto done;
5642 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5643 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5644 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5645 bc573f3b 2021-07-10 stsp if (err)
5646 c0b01bdb 2019-11-08 stsp goto done;
5647 e5a0f69f 2018-08-18 stsp
5648 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5649 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5650 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5651 bc573f3b 2021-07-10 stsp if (err)
5652 11b20872 2019-11-08 stsp goto done;
5653 11b20872 2019-11-08 stsp
5654 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5655 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5656 bc573f3b 2021-07-10 stsp if (err)
5657 c0b01bdb 2019-11-08 stsp goto done;
5658 c0b01bdb 2019-11-08 stsp }
5659 c0b01bdb 2019-11-08 stsp
5660 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5661 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5662 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5663 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5664 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5665 ad80ab7b 2018-08-04 stsp done:
5666 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5667 bc573f3b 2021-07-10 stsp if (commit)
5668 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5669 bc573f3b 2021-07-10 stsp if (err)
5670 bc573f3b 2021-07-10 stsp close_tree_view(view);
5671 ad80ab7b 2018-08-04 stsp return err;
5672 ad80ab7b 2018-08-04 stsp }
5673 ad80ab7b 2018-08-04 stsp
5674 e5a0f69f 2018-08-18 stsp static const struct got_error *
5675 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5676 ad80ab7b 2018-08-04 stsp {
5677 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5678 ad80ab7b 2018-08-04 stsp
5679 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5680 fb2756b9 2018-08-04 stsp free(s->tree_label);
5681 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5682 6484ec90 2018-09-29 stsp free(s->commit_id);
5683 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5684 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5685 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5686 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5687 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5688 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5689 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5690 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5691 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5692 ad80ab7b 2018-08-04 stsp free(parent);
5693 ad80ab7b 2018-08-04 stsp
5694 ad80ab7b 2018-08-04 stsp }
5695 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5696 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5697 bc573f3b 2021-07-10 stsp if (s->root)
5698 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5699 7c32bd05 2019-06-22 stsp return NULL;
5700 7c32bd05 2019-06-22 stsp }
5701 7c32bd05 2019-06-22 stsp
5702 7c32bd05 2019-06-22 stsp static const struct got_error *
5703 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5704 7c32bd05 2019-06-22 stsp {
5705 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5706 7c32bd05 2019-06-22 stsp
5707 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5708 7c32bd05 2019-06-22 stsp return NULL;
5709 7c32bd05 2019-06-22 stsp }
5710 7c32bd05 2019-06-22 stsp
5711 7c32bd05 2019-06-22 stsp static int
5712 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5713 7c32bd05 2019-06-22 stsp {
5714 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5715 7c32bd05 2019-06-22 stsp
5716 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5717 56e0773d 2019-11-28 stsp 0) == 0;
5718 7c32bd05 2019-06-22 stsp }
5719 7c32bd05 2019-06-22 stsp
5720 7c32bd05 2019-06-22 stsp static const struct got_error *
5721 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5722 7c32bd05 2019-06-22 stsp {
5723 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5724 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5725 7c32bd05 2019-06-22 stsp
5726 7c32bd05 2019-06-22 stsp if (!view->searching) {
5727 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5728 7c32bd05 2019-06-22 stsp return NULL;
5729 7c32bd05 2019-06-22 stsp }
5730 7c32bd05 2019-06-22 stsp
5731 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5732 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5733 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5734 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5735 56e0773d 2019-11-28 stsp s->selected_entry);
5736 7c32bd05 2019-06-22 stsp else
5737 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5738 56e0773d 2019-11-28 stsp } else {
5739 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5740 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5741 56e0773d 2019-11-28 stsp else
5742 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5743 56e0773d 2019-11-28 stsp s->selected_entry);
5744 7c32bd05 2019-06-22 stsp }
5745 7c32bd05 2019-06-22 stsp } else {
5746 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5747 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5748 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5749 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5750 56e0773d 2019-11-28 stsp else
5751 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5752 7c32bd05 2019-06-22 stsp }
5753 7c32bd05 2019-06-22 stsp
5754 7c32bd05 2019-06-22 stsp while (1) {
5755 56e0773d 2019-11-28 stsp if (te == NULL) {
5756 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5757 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5758 ac66afb8 2019-06-24 stsp return NULL;
5759 ac66afb8 2019-06-24 stsp }
5760 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5761 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5762 56e0773d 2019-11-28 stsp else
5763 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5764 7c32bd05 2019-06-22 stsp }
5765 7c32bd05 2019-06-22 stsp
5766 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5767 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5768 56e0773d 2019-11-28 stsp s->matched_entry = te;
5769 7c32bd05 2019-06-22 stsp break;
5770 7c32bd05 2019-06-22 stsp }
5771 7c32bd05 2019-06-22 stsp
5772 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5773 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5774 56e0773d 2019-11-28 stsp else
5775 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5776 7c32bd05 2019-06-22 stsp }
5777 e5a0f69f 2018-08-18 stsp
5778 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5779 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5780 7c32bd05 2019-06-22 stsp s->selected = 0;
5781 7c32bd05 2019-06-22 stsp }
5782 7c32bd05 2019-06-22 stsp
5783 e5a0f69f 2018-08-18 stsp return NULL;
5784 ad80ab7b 2018-08-04 stsp }
5785 ad80ab7b 2018-08-04 stsp
5786 ad80ab7b 2018-08-04 stsp static const struct got_error *
5787 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5788 ad80ab7b 2018-08-04 stsp {
5789 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5790 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5791 e5a0f69f 2018-08-18 stsp char *parent_path;
5792 ad80ab7b 2018-08-04 stsp
5793 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5794 e5a0f69f 2018-08-18 stsp if (err)
5795 e5a0f69f 2018-08-18 stsp return err;
5796 ffd1d5e5 2018-06-23 stsp
5797 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5798 e5a0f69f 2018-08-18 stsp free(parent_path);
5799 669b5ffa 2018-10-07 stsp
5800 669b5ffa 2018-10-07 stsp view_vborder(view);
5801 e5a0f69f 2018-08-18 stsp return err;
5802 e5a0f69f 2018-08-18 stsp }
5803 ce52c690 2018-06-23 stsp
5804 e5a0f69f 2018-08-18 stsp static const struct got_error *
5805 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5806 e5a0f69f 2018-08-18 stsp {
5807 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5808 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5809 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5810 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5811 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5812 ffd1d5e5 2018-06-23 stsp
5813 e5a0f69f 2018-08-18 stsp switch (ch) {
5814 1e37a5c2 2019-05-12 jcs case 'i':
5815 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5816 1e37a5c2 2019-05-12 jcs break;
5817 1e37a5c2 2019-05-12 jcs case 'l':
5818 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5819 ffd1d5e5 2018-06-23 stsp break;
5820 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5821 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5822 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5823 e78dc838 2020-12-04 stsp view->focussed = 0;
5824 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5825 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5826 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5827 1e37a5c2 2019-05-12 jcs if (err)
5828 1e37a5c2 2019-05-12 jcs return err;
5829 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5830 e78dc838 2020-12-04 stsp view->focus_child = 1;
5831 1e37a5c2 2019-05-12 jcs } else
5832 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5833 152c1c93 2020-11-29 stsp break;
5834 152c1c93 2020-11-29 stsp case 'r':
5835 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5836 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5837 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5838 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5839 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5840 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5841 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5842 152c1c93 2020-11-29 stsp if (err) {
5843 152c1c93 2020-11-29 stsp view_close(ref_view);
5844 152c1c93 2020-11-29 stsp return err;
5845 152c1c93 2020-11-29 stsp }
5846 e78dc838 2020-12-04 stsp view->focussed = 0;
5847 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5848 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5849 152c1c93 2020-11-29 stsp err = view_close_child(view);
5850 152c1c93 2020-11-29 stsp if (err)
5851 152c1c93 2020-11-29 stsp return err;
5852 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5853 e78dc838 2020-12-04 stsp view->focus_child = 1;
5854 152c1c93 2020-11-29 stsp } else
5855 152c1c93 2020-11-29 stsp *new_view = ref_view;
5856 e4526bf5 2021-09-03 naddy break;
5857 e4526bf5 2021-09-03 naddy case 'g':
5858 e4526bf5 2021-09-03 naddy case KEY_HOME:
5859 e4526bf5 2021-09-03 naddy s->selected = 0;
5860 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5861 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5862 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5863 e4526bf5 2021-09-03 naddy else
5864 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5865 1e37a5c2 2019-05-12 jcs break;
5866 e4526bf5 2021-09-03 naddy case 'G':
5867 e4526bf5 2021-09-03 naddy case KEY_END:
5868 e4526bf5 2021-09-03 naddy s->selected = 0;
5869 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5870 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5871 e4526bf5 2021-09-03 naddy if (te == NULL) {
5872 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5873 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5874 e4526bf5 2021-09-03 naddy n++;
5875 e4526bf5 2021-09-03 naddy }
5876 e4526bf5 2021-09-03 naddy break;
5877 e4526bf5 2021-09-03 naddy }
5878 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5879 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5880 e4526bf5 2021-09-03 naddy }
5881 e4526bf5 2021-09-03 naddy if (n > 0)
5882 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5883 e4526bf5 2021-09-03 naddy break;
5884 1e37a5c2 2019-05-12 jcs case 'k':
5885 1e37a5c2 2019-05-12 jcs case KEY_UP:
5886 f7140bf5 2021-10-17 thomas case CTRL('p'):
5887 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5888 1e37a5c2 2019-05-12 jcs s->selected--;
5889 fa86c4bf 2020-11-29 stsp break;
5890 1e37a5c2 2019-05-12 jcs }
5891 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5892 1e37a5c2 2019-05-12 jcs break;
5893 70f17a53 2022-06-13 thomas case CTRL('u'):
5894 23427b14 2022-06-23 thomas case 'u':
5895 70f17a53 2022-06-13 thomas nscroll /= 2;
5896 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5897 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5898 ea025d1d 2020-02-22 naddy case CTRL('b'):
5899 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5900 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5901 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5902 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5903 fa86c4bf 2020-11-29 stsp } else {
5904 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5905 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5906 fa86c4bf 2020-11-29 stsp }
5907 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5908 1e37a5c2 2019-05-12 jcs break;
5909 1e37a5c2 2019-05-12 jcs case 'j':
5910 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5911 f7140bf5 2021-10-17 thomas case CTRL('n'):
5912 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5913 1e37a5c2 2019-05-12 jcs s->selected++;
5914 1e37a5c2 2019-05-12 jcs break;
5915 1e37a5c2 2019-05-12 jcs }
5916 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5917 56e0773d 2019-11-28 stsp == NULL)
5918 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5919 1e37a5c2 2019-05-12 jcs break;
5920 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5921 1e37a5c2 2019-05-12 jcs break;
5922 70f17a53 2022-06-13 thomas case CTRL('d'):
5923 23427b14 2022-06-23 thomas case 'd':
5924 70f17a53 2022-06-13 thomas nscroll /= 2;
5925 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5926 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5927 ea025d1d 2020-02-22 naddy case CTRL('f'):
5928 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5929 1e37a5c2 2019-05-12 jcs == NULL) {
5930 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5931 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5932 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5933 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5934 1e37a5c2 2019-05-12 jcs break;
5935 1e37a5c2 2019-05-12 jcs }
5936 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5937 1e37a5c2 2019-05-12 jcs break;
5938 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5939 1e37a5c2 2019-05-12 jcs case '\r':
5940 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5941 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5942 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5943 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5944 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5945 1e37a5c2 2019-05-12 jcs break;
5946 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5947 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5948 1e37a5c2 2019-05-12 jcs entry);
5949 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5950 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5951 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5952 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5953 1e37a5c2 2019-05-12 jcs s->selected_entry =
5954 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5955 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5956 1e37a5c2 2019-05-12 jcs free(parent);
5957 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5958 56e0773d 2019-11-28 stsp s->selected_entry))) {
5959 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5960 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5961 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5962 1e37a5c2 2019-05-12 jcs if (err)
5963 1e37a5c2 2019-05-12 jcs break;
5964 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5965 941e9f74 2019-05-21 stsp if (err) {
5966 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5967 1e37a5c2 2019-05-12 jcs break;
5968 1e37a5c2 2019-05-12 jcs }
5969 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5970 56e0773d 2019-11-28 stsp s->selected_entry))) {
5971 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5972 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5973 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5974 1e37a5c2 2019-05-12 jcs
5975 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5976 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5977 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5978 1e37a5c2 2019-05-12 jcs if (err)
5979 1e37a5c2 2019-05-12 jcs break;
5980 e78dc838 2020-12-04 stsp view->focussed = 0;
5981 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5982 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5983 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5984 669b5ffa 2018-10-07 stsp if (err)
5985 669b5ffa 2018-10-07 stsp return err;
5986 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5987 e78dc838 2020-12-04 stsp view->focus_child = 1;
5988 669b5ffa 2018-10-07 stsp } else
5989 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5990 1e37a5c2 2019-05-12 jcs }
5991 1e37a5c2 2019-05-12 jcs break;
5992 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5993 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5994 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5995 1e37a5c2 2019-05-12 jcs break;
5996 1e37a5c2 2019-05-12 jcs default:
5997 1e37a5c2 2019-05-12 jcs break;
5998 ffd1d5e5 2018-06-23 stsp }
5999 e5a0f69f 2018-08-18 stsp
6000 ffd1d5e5 2018-06-23 stsp return err;
6001 9f7d7167 2018-04-29 stsp }
6002 9f7d7167 2018-04-29 stsp
6003 ffd1d5e5 2018-06-23 stsp __dead static void
6004 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6005 ffd1d5e5 2018-06-23 stsp {
6006 ffd1d5e5 2018-06-23 stsp endwin();
6007 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6008 ffd1d5e5 2018-06-23 stsp getprogname());
6009 ffd1d5e5 2018-06-23 stsp exit(1);
6010 ffd1d5e5 2018-06-23 stsp }
6011 ffd1d5e5 2018-06-23 stsp
6012 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6013 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6014 ffd1d5e5 2018-06-23 stsp {
6015 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6016 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6017 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6018 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6019 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6020 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6021 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6022 4e97c21c 2020-12-06 stsp char *label = NULL;
6023 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6024 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6025 ffd1d5e5 2018-06-23 stsp int ch;
6026 5221c383 2018-08-01 stsp struct tog_view *view;
6027 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6028 70ac5f84 2019-03-28 stsp
6029 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 ffd1d5e5 2018-06-23 stsp switch (ch) {
6031 ffd1d5e5 2018-06-23 stsp case 'c':
6032 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6033 74283ab8 2020-02-07 stsp break;
6034 74283ab8 2020-02-07 stsp case 'r':
6035 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6036 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6037 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6038 74283ab8 2020-02-07 stsp optarg);
6039 ffd1d5e5 2018-06-23 stsp break;
6040 ffd1d5e5 2018-06-23 stsp default:
6041 e99e2d15 2020-11-24 naddy usage_tree();
6042 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6043 ffd1d5e5 2018-06-23 stsp }
6044 ffd1d5e5 2018-06-23 stsp }
6045 ffd1d5e5 2018-06-23 stsp
6046 ffd1d5e5 2018-06-23 stsp argc -= optind;
6047 ffd1d5e5 2018-06-23 stsp argv += optind;
6048 ffd1d5e5 2018-06-23 stsp
6049 55cccc34 2020-02-20 stsp if (argc > 1)
6050 e99e2d15 2020-11-24 naddy usage_tree();
6051 7cd52833 2022-06-23 thomas
6052 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6053 7cd52833 2022-06-23 thomas if (error != NULL)
6054 7cd52833 2022-06-23 thomas goto done;
6055 74283ab8 2020-02-07 stsp
6056 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6057 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6058 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6059 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6060 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6061 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 c156c7a4 2020-12-18 stsp goto done;
6063 55cccc34 2020-02-20 stsp if (worktree)
6064 52185f70 2019-02-05 stsp repo_path =
6065 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6066 55cccc34 2020-02-20 stsp else
6067 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6068 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6069 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6070 c156c7a4 2020-12-18 stsp goto done;
6071 c156c7a4 2020-12-18 stsp }
6072 55cccc34 2020-02-20 stsp }
6073 a915003a 2019-02-05 stsp
6074 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 c02c541e 2019-03-29 stsp if (error != NULL)
6076 52185f70 2019-02-05 stsp goto done;
6077 d188b9a6 2019-01-04 stsp
6078 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6079 55cccc34 2020-02-20 stsp repo, worktree);
6080 55cccc34 2020-02-20 stsp if (error)
6081 55cccc34 2020-02-20 stsp goto done;
6082 55cccc34 2020-02-20 stsp
6083 55cccc34 2020-02-20 stsp init_curses();
6084 55cccc34 2020-02-20 stsp
6085 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6086 c02c541e 2019-03-29 stsp if (error)
6087 52185f70 2019-02-05 stsp goto done;
6088 ffd1d5e5 2018-06-23 stsp
6089 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6090 51a10b52 2020-12-26 stsp if (error)
6091 51a10b52 2020-12-26 stsp goto done;
6092 51a10b52 2020-12-26 stsp
6093 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6094 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6095 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6096 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6097 4e97c21c 2020-12-06 stsp if (error)
6098 4e97c21c 2020-12-06 stsp goto done;
6099 4e97c21c 2020-12-06 stsp head_ref_name = label;
6100 4e97c21c 2020-12-06 stsp } else {
6101 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6102 4e97c21c 2020-12-06 stsp if (error == NULL)
6103 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6104 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6105 4e97c21c 2020-12-06 stsp goto done;
6106 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6107 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6108 4e97c21c 2020-12-06 stsp if (error)
6109 4e97c21c 2020-12-06 stsp goto done;
6110 4e97c21c 2020-12-06 stsp }
6111 ffd1d5e5 2018-06-23 stsp
6112 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6113 945f9229 2022-04-16 thomas if (error)
6114 945f9229 2022-04-16 thomas goto done;
6115 945f9229 2022-04-16 thomas
6116 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6117 5221c383 2018-08-01 stsp if (view == NULL) {
6118 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6119 5221c383 2018-08-01 stsp goto done;
6120 5221c383 2018-08-01 stsp }
6121 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6122 ad80ab7b 2018-08-04 stsp if (error)
6123 ad80ab7b 2018-08-04 stsp goto done;
6124 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6125 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
6126 d91faf3b 2020-12-01 naddy in_repo_path);
6127 55cccc34 2020-02-20 stsp if (error)
6128 55cccc34 2020-02-20 stsp goto done;
6129 55cccc34 2020-02-20 stsp }
6130 55cccc34 2020-02-20 stsp
6131 55cccc34 2020-02-20 stsp if (worktree) {
6132 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6133 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6134 55cccc34 2020-02-20 stsp worktree = NULL;
6135 55cccc34 2020-02-20 stsp }
6136 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6137 ffd1d5e5 2018-06-23 stsp done:
6138 52185f70 2019-02-05 stsp free(repo_path);
6139 e4a0e26d 2020-02-20 stsp free(cwd);
6140 ffd1d5e5 2018-06-23 stsp free(commit_id);
6141 4e97c21c 2020-12-06 stsp free(label);
6142 486cd271 2020-12-06 stsp if (ref)
6143 486cd271 2020-12-06 stsp got_ref_close(ref);
6144 1d0f4054 2021-06-17 stsp if (repo) {
6145 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6146 1d0f4054 2021-06-17 stsp if (error == NULL)
6147 1d0f4054 2021-06-17 stsp error = close_err;
6148 1d0f4054 2021-06-17 stsp }
6149 7cd52833 2022-06-23 thomas if (pack_fds) {
6150 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6151 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6152 7cd52833 2022-06-23 thomas if (error == NULL)
6153 7cd52833 2022-06-23 thomas error = pack_err;
6154 7cd52833 2022-06-23 thomas }
6155 51a10b52 2020-12-26 stsp tog_free_refs();
6156 ffd1d5e5 2018-06-23 stsp return error;
6157 6458efa5 2020-11-24 stsp }
6158 6458efa5 2020-11-24 stsp
6159 6458efa5 2020-11-24 stsp static const struct got_error *
6160 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6161 6458efa5 2020-11-24 stsp {
6162 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6163 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6164 6458efa5 2020-11-24 stsp
6165 6458efa5 2020-11-24 stsp s->nrefs = 0;
6166 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6167 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
6168 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
6169 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
6170 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
6171 6458efa5 2020-11-24 stsp continue;
6172 6458efa5 2020-11-24 stsp
6173 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6174 6458efa5 2020-11-24 stsp if (re == NULL)
6175 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6176 6458efa5 2020-11-24 stsp
6177 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6178 8924d611 2020-12-26 stsp if (re->ref == NULL)
6179 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6180 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6181 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6182 6458efa5 2020-11-24 stsp }
6183 6458efa5 2020-11-24 stsp
6184 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6185 6458efa5 2020-11-24 stsp return NULL;
6186 6458efa5 2020-11-24 stsp }
6187 6458efa5 2020-11-24 stsp
6188 6458efa5 2020-11-24 stsp void
6189 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6190 6458efa5 2020-11-24 stsp {
6191 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6192 6458efa5 2020-11-24 stsp
6193 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6194 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6195 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6196 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6197 6458efa5 2020-11-24 stsp free(re);
6198 6458efa5 2020-11-24 stsp }
6199 6458efa5 2020-11-24 stsp }
6200 6458efa5 2020-11-24 stsp
6201 6458efa5 2020-11-24 stsp static const struct got_error *
6202 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6203 6458efa5 2020-11-24 stsp {
6204 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6205 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6206 6458efa5 2020-11-24 stsp
6207 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6208 6458efa5 2020-11-24 stsp s->repo = repo;
6209 6458efa5 2020-11-24 stsp
6210 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6211 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6212 6458efa5 2020-11-24 stsp
6213 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6214 6458efa5 2020-11-24 stsp if (err)
6215 6458efa5 2020-11-24 stsp return err;
6216 34ba6917 2020-11-30 stsp
6217 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6218 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6219 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6220 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6221 6458efa5 2020-11-24 stsp if (err)
6222 6458efa5 2020-11-24 stsp goto done;
6223 6458efa5 2020-11-24 stsp
6224 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6225 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6226 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6227 6458efa5 2020-11-24 stsp if (err)
6228 6458efa5 2020-11-24 stsp goto done;
6229 6458efa5 2020-11-24 stsp
6230 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6231 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6232 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6233 2183bbf6 2022-01-23 thomas if (err)
6234 2183bbf6 2022-01-23 thomas goto done;
6235 2183bbf6 2022-01-23 thomas
6236 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6237 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6238 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6239 6458efa5 2020-11-24 stsp if (err)
6240 6458efa5 2020-11-24 stsp goto done;
6241 6458efa5 2020-11-24 stsp }
6242 6458efa5 2020-11-24 stsp
6243 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6244 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6245 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6246 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6247 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6248 6458efa5 2020-11-24 stsp done:
6249 6458efa5 2020-11-24 stsp if (err)
6250 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6251 6458efa5 2020-11-24 stsp return err;
6252 6458efa5 2020-11-24 stsp }
6253 6458efa5 2020-11-24 stsp
6254 6458efa5 2020-11-24 stsp static const struct got_error *
6255 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6256 6458efa5 2020-11-24 stsp {
6257 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6258 6458efa5 2020-11-24 stsp
6259 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6260 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6261 6458efa5 2020-11-24 stsp
6262 6458efa5 2020-11-24 stsp return NULL;
6263 9f7d7167 2018-04-29 stsp }
6264 ce5b7c56 2019-07-09 stsp
6265 6458efa5 2020-11-24 stsp static const struct got_error *
6266 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6267 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6268 6458efa5 2020-11-24 stsp {
6269 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6270 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6271 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6272 6458efa5 2020-11-24 stsp int obj_type;
6273 6458efa5 2020-11-24 stsp
6274 c42c9805 2020-11-24 stsp *commit_id = NULL;
6275 6458efa5 2020-11-24 stsp
6276 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6277 6458efa5 2020-11-24 stsp if (err)
6278 6458efa5 2020-11-24 stsp return err;
6279 6458efa5 2020-11-24 stsp
6280 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6281 6458efa5 2020-11-24 stsp if (err)
6282 6458efa5 2020-11-24 stsp goto done;
6283 6458efa5 2020-11-24 stsp
6284 6458efa5 2020-11-24 stsp switch (obj_type) {
6285 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6286 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6287 6458efa5 2020-11-24 stsp break;
6288 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6289 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6290 6458efa5 2020-11-24 stsp if (err)
6291 6458efa5 2020-11-24 stsp goto done;
6292 c42c9805 2020-11-24 stsp free(obj_id);
6293 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6294 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6295 c42c9805 2020-11-24 stsp if (err)
6296 6458efa5 2020-11-24 stsp goto done;
6297 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6298 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6299 c42c9805 2020-11-24 stsp goto done;
6300 c42c9805 2020-11-24 stsp }
6301 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6302 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6303 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6304 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6305 c42c9805 2020-11-24 stsp goto done;
6306 c42c9805 2020-11-24 stsp }
6307 6458efa5 2020-11-24 stsp break;
6308 6458efa5 2020-11-24 stsp default:
6309 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6310 c42c9805 2020-11-24 stsp break;
6311 6458efa5 2020-11-24 stsp }
6312 6458efa5 2020-11-24 stsp
6313 c42c9805 2020-11-24 stsp done:
6314 c42c9805 2020-11-24 stsp if (tag)
6315 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6316 c42c9805 2020-11-24 stsp if (err) {
6317 c42c9805 2020-11-24 stsp free(*commit_id);
6318 c42c9805 2020-11-24 stsp *commit_id = NULL;
6319 c42c9805 2020-11-24 stsp }
6320 c42c9805 2020-11-24 stsp return err;
6321 c42c9805 2020-11-24 stsp }
6322 c42c9805 2020-11-24 stsp
6323 c42c9805 2020-11-24 stsp static const struct got_error *
6324 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6325 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6326 c42c9805 2020-11-24 stsp {
6327 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6328 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6329 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6330 c42c9805 2020-11-24 stsp
6331 c42c9805 2020-11-24 stsp *new_view = NULL;
6332 c42c9805 2020-11-24 stsp
6333 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6334 c42c9805 2020-11-24 stsp if (err) {
6335 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6336 c42c9805 2020-11-24 stsp return err;
6337 c42c9805 2020-11-24 stsp else
6338 c42c9805 2020-11-24 stsp return NULL;
6339 c42c9805 2020-11-24 stsp }
6340 c42c9805 2020-11-24 stsp
6341 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6342 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6343 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6344 6458efa5 2020-11-24 stsp goto done;
6345 6458efa5 2020-11-24 stsp }
6346 6458efa5 2020-11-24 stsp
6347 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6348 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6349 6458efa5 2020-11-24 stsp done:
6350 6458efa5 2020-11-24 stsp if (err)
6351 6458efa5 2020-11-24 stsp view_close(log_view);
6352 6458efa5 2020-11-24 stsp else
6353 6458efa5 2020-11-24 stsp *new_view = log_view;
6354 c42c9805 2020-11-24 stsp free(commit_id);
6355 6458efa5 2020-11-24 stsp return err;
6356 6458efa5 2020-11-24 stsp }
6357 6458efa5 2020-11-24 stsp
6358 ce5b7c56 2019-07-09 stsp static void
6359 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6360 6458efa5 2020-11-24 stsp {
6361 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6362 34ba6917 2020-11-30 stsp int i = 0;
6363 6458efa5 2020-11-24 stsp
6364 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6365 6458efa5 2020-11-24 stsp return;
6366 6458efa5 2020-11-24 stsp
6367 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6368 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6369 34ba6917 2020-11-30 stsp if (re == NULL)
6370 34ba6917 2020-11-30 stsp break;
6371 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6372 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6373 6458efa5 2020-11-24 stsp }
6374 6458efa5 2020-11-24 stsp }
6375 6458efa5 2020-11-24 stsp
6376 34ba6917 2020-11-30 stsp static void
6377 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6378 6458efa5 2020-11-24 stsp {
6379 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6380 6458efa5 2020-11-24 stsp int n = 0;
6381 6458efa5 2020-11-24 stsp
6382 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6383 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6384 6458efa5 2020-11-24 stsp else
6385 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6386 6458efa5 2020-11-24 stsp
6387 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6388 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6389 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6390 6458efa5 2020-11-24 stsp if (last) {
6391 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6392 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6393 6458efa5 2020-11-24 stsp }
6394 6458efa5 2020-11-24 stsp }
6395 6458efa5 2020-11-24 stsp }
6396 6458efa5 2020-11-24 stsp
6397 6458efa5 2020-11-24 stsp static const struct got_error *
6398 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6399 6458efa5 2020-11-24 stsp {
6400 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6401 6458efa5 2020-11-24 stsp
6402 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6403 6458efa5 2020-11-24 stsp return NULL;
6404 6458efa5 2020-11-24 stsp }
6405 6458efa5 2020-11-24 stsp
6406 6458efa5 2020-11-24 stsp static int
6407 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6408 6458efa5 2020-11-24 stsp {
6409 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6410 6458efa5 2020-11-24 stsp
6411 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6412 6458efa5 2020-11-24 stsp 0) == 0;
6413 6458efa5 2020-11-24 stsp }
6414 6458efa5 2020-11-24 stsp
6415 6458efa5 2020-11-24 stsp static const struct got_error *
6416 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6417 6458efa5 2020-11-24 stsp {
6418 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6419 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6420 6458efa5 2020-11-24 stsp
6421 6458efa5 2020-11-24 stsp if (!view->searching) {
6422 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6423 6458efa5 2020-11-24 stsp return NULL;
6424 6458efa5 2020-11-24 stsp }
6425 6458efa5 2020-11-24 stsp
6426 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6427 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6428 6458efa5 2020-11-24 stsp if (s->selected_entry)
6429 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6430 6458efa5 2020-11-24 stsp else
6431 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6432 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6433 6458efa5 2020-11-24 stsp } else {
6434 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6435 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6436 6458efa5 2020-11-24 stsp else
6437 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6438 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6439 6458efa5 2020-11-24 stsp }
6440 6458efa5 2020-11-24 stsp } else {
6441 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6442 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6443 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6444 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6445 6458efa5 2020-11-24 stsp else
6446 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6447 6458efa5 2020-11-24 stsp }
6448 6458efa5 2020-11-24 stsp
6449 6458efa5 2020-11-24 stsp while (1) {
6450 6458efa5 2020-11-24 stsp if (re == NULL) {
6451 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6452 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6453 6458efa5 2020-11-24 stsp return NULL;
6454 6458efa5 2020-11-24 stsp }
6455 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6456 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6457 6458efa5 2020-11-24 stsp else
6458 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6459 6458efa5 2020-11-24 stsp }
6460 6458efa5 2020-11-24 stsp
6461 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6462 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6463 6458efa5 2020-11-24 stsp s->matched_entry = re;
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 if (view->searching == TOG_SEARCH_FORWARD)
6468 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6469 6458efa5 2020-11-24 stsp else
6470 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6471 6458efa5 2020-11-24 stsp }
6472 6458efa5 2020-11-24 stsp
6473 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6474 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6475 6458efa5 2020-11-24 stsp s->selected = 0;
6476 6458efa5 2020-11-24 stsp }
6477 6458efa5 2020-11-24 stsp
6478 6458efa5 2020-11-24 stsp return NULL;
6479 6458efa5 2020-11-24 stsp }
6480 6458efa5 2020-11-24 stsp
6481 6458efa5 2020-11-24 stsp static const struct got_error *
6482 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6483 6458efa5 2020-11-24 stsp {
6484 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6485 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6487 6458efa5 2020-11-24 stsp char *line = NULL;
6488 6458efa5 2020-11-24 stsp wchar_t *wline;
6489 6458efa5 2020-11-24 stsp struct tog_color *tc;
6490 6458efa5 2020-11-24 stsp int width, n;
6491 6458efa5 2020-11-24 stsp int limit = view->nlines;
6492 6458efa5 2020-11-24 stsp
6493 6458efa5 2020-11-24 stsp werase(view->window);
6494 6458efa5 2020-11-24 stsp
6495 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6496 6458efa5 2020-11-24 stsp
6497 6458efa5 2020-11-24 stsp if (limit == 0)
6498 6458efa5 2020-11-24 stsp return NULL;
6499 6458efa5 2020-11-24 stsp
6500 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6501 6458efa5 2020-11-24 stsp
6502 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6503 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6504 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6505 6458efa5 2020-11-24 stsp
6506 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6507 6458efa5 2020-11-24 stsp if (err) {
6508 6458efa5 2020-11-24 stsp free(line);
6509 6458efa5 2020-11-24 stsp return err;
6510 6458efa5 2020-11-24 stsp }
6511 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6512 6458efa5 2020-11-24 stsp wstandout(view->window);
6513 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6514 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6515 6458efa5 2020-11-24 stsp wstandend(view->window);
6516 6458efa5 2020-11-24 stsp free(wline);
6517 6458efa5 2020-11-24 stsp wline = NULL;
6518 6458efa5 2020-11-24 stsp free(line);
6519 6458efa5 2020-11-24 stsp line = NULL;
6520 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6521 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6522 6458efa5 2020-11-24 stsp if (--limit <= 0)
6523 6458efa5 2020-11-24 stsp return NULL;
6524 6458efa5 2020-11-24 stsp
6525 6458efa5 2020-11-24 stsp n = 0;
6526 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6527 6458efa5 2020-11-24 stsp char *line = NULL;
6528 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6529 6458efa5 2020-11-24 stsp
6530 84227eb1 2022-06-23 thomas if (s->show_date) {
6531 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
6532 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
6533 84227eb1 2022-06-23 thomas struct got_object_id *id;
6534 84227eb1 2022-06-23 thomas struct tm tm;
6535 84227eb1 2022-06-23 thomas time_t t;
6536 84227eb1 2022-06-23 thomas
6537 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
6538 84227eb1 2022-06-23 thomas if (err)
6539 84227eb1 2022-06-23 thomas return err;
6540 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
6541 84227eb1 2022-06-23 thomas if (err) {
6542 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
6543 84227eb1 2022-06-23 thomas free(id);
6544 84227eb1 2022-06-23 thomas return err;
6545 84227eb1 2022-06-23 thomas }
6546 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
6547 84227eb1 2022-06-23 thomas id);
6548 84227eb1 2022-06-23 thomas if (err) {
6549 84227eb1 2022-06-23 thomas free(id);
6550 84227eb1 2022-06-23 thomas return err;
6551 84227eb1 2022-06-23 thomas }
6552 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
6553 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
6554 84227eb1 2022-06-23 thomas } else {
6555 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
6556 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
6557 84227eb1 2022-06-23 thomas }
6558 84227eb1 2022-06-23 thomas free(id);
6559 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
6560 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
6561 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6562 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
6563 84227eb1 2022-06-23 thomas }
6564 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6565 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
6566 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
6567 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6568 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6569 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6570 6458efa5 2020-11-24 stsp struct got_object_id *id;
6571 6458efa5 2020-11-24 stsp char *id_str;
6572 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6573 6458efa5 2020-11-24 stsp if (err)
6574 6458efa5 2020-11-24 stsp return err;
6575 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6576 6458efa5 2020-11-24 stsp if (err) {
6577 6458efa5 2020-11-24 stsp free(id);
6578 6458efa5 2020-11-24 stsp return err;
6579 6458efa5 2020-11-24 stsp }
6580 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6581 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6582 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6583 6458efa5 2020-11-24 stsp free(id);
6584 6458efa5 2020-11-24 stsp free(id_str);
6585 6458efa5 2020-11-24 stsp return err;
6586 6458efa5 2020-11-24 stsp }
6587 6458efa5 2020-11-24 stsp free(id);
6588 6458efa5 2020-11-24 stsp free(id_str);
6589 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6590 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
6591 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
6592 6458efa5 2020-11-24 stsp
6593 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6594 f91a2b48 2022-06-23 thomas 0, 0);
6595 6458efa5 2020-11-24 stsp if (err) {
6596 6458efa5 2020-11-24 stsp free(line);
6597 6458efa5 2020-11-24 stsp return err;
6598 6458efa5 2020-11-24 stsp }
6599 6458efa5 2020-11-24 stsp if (n == s->selected) {
6600 6458efa5 2020-11-24 stsp if (view->focussed)
6601 6458efa5 2020-11-24 stsp wstandout(view->window);
6602 6458efa5 2020-11-24 stsp s->selected_entry = re;
6603 6458efa5 2020-11-24 stsp }
6604 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6605 6458efa5 2020-11-24 stsp if (tc)
6606 6458efa5 2020-11-24 stsp wattr_on(view->window,
6607 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6608 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6609 6458efa5 2020-11-24 stsp if (tc)
6610 6458efa5 2020-11-24 stsp wattr_off(view->window,
6611 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6612 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6613 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6614 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6615 6458efa5 2020-11-24 stsp wstandend(view->window);
6616 6458efa5 2020-11-24 stsp free(line);
6617 6458efa5 2020-11-24 stsp free(wline);
6618 6458efa5 2020-11-24 stsp wline = NULL;
6619 6458efa5 2020-11-24 stsp n++;
6620 6458efa5 2020-11-24 stsp s->ndisplayed++;
6621 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6622 6458efa5 2020-11-24 stsp
6623 6458efa5 2020-11-24 stsp limit--;
6624 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6625 6458efa5 2020-11-24 stsp }
6626 6458efa5 2020-11-24 stsp
6627 6458efa5 2020-11-24 stsp view_vborder(view);
6628 6458efa5 2020-11-24 stsp return err;
6629 6458efa5 2020-11-24 stsp }
6630 6458efa5 2020-11-24 stsp
6631 6458efa5 2020-11-24 stsp static const struct got_error *
6632 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6633 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6634 c42c9805 2020-11-24 stsp {
6635 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6636 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6637 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6638 c42c9805 2020-11-24 stsp
6639 c42c9805 2020-11-24 stsp *new_view = NULL;
6640 c42c9805 2020-11-24 stsp
6641 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6642 c42c9805 2020-11-24 stsp if (err) {
6643 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6644 c42c9805 2020-11-24 stsp return err;
6645 c42c9805 2020-11-24 stsp else
6646 c42c9805 2020-11-24 stsp return NULL;
6647 c42c9805 2020-11-24 stsp }
6648 c42c9805 2020-11-24 stsp
6649 c42c9805 2020-11-24 stsp
6650 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6651 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6652 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6653 c42c9805 2020-11-24 stsp goto done;
6654 c42c9805 2020-11-24 stsp }
6655 c42c9805 2020-11-24 stsp
6656 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6657 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6658 c42c9805 2020-11-24 stsp if (err)
6659 c42c9805 2020-11-24 stsp goto done;
6660 c42c9805 2020-11-24 stsp
6661 c42c9805 2020-11-24 stsp *new_view = tree_view;
6662 c42c9805 2020-11-24 stsp done:
6663 c42c9805 2020-11-24 stsp free(commit_id);
6664 c42c9805 2020-11-24 stsp return err;
6665 c42c9805 2020-11-24 stsp }
6666 c42c9805 2020-11-24 stsp static const struct got_error *
6667 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6668 6458efa5 2020-11-24 stsp {
6669 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6670 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6671 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6672 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6673 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6674 6458efa5 2020-11-24 stsp
6675 6458efa5 2020-11-24 stsp switch (ch) {
6676 6458efa5 2020-11-24 stsp case 'i':
6677 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6678 3bfadbd4 2021-11-20 thomas break;
6679 84227eb1 2022-06-23 thomas case 'm':
6680 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
6681 84227eb1 2022-06-23 thomas break;
6682 98182bd0 2021-11-20 thomas case 'o':
6683 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6684 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6685 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6686 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6687 c591440f 2021-11-20 thomas if (err)
6688 c591440f 2021-11-20 thomas break;
6689 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6690 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6691 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6692 3bfadbd4 2021-11-20 thomas if (err)
6693 3bfadbd4 2021-11-20 thomas break;
6694 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6695 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6696 6458efa5 2020-11-24 stsp break;
6697 6458efa5 2020-11-24 stsp case KEY_ENTER:
6698 6458efa5 2020-11-24 stsp case '\r':
6699 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6700 6458efa5 2020-11-24 stsp break;
6701 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6702 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6703 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6704 6458efa5 2020-11-24 stsp s->repo);
6705 e78dc838 2020-12-04 stsp view->focussed = 0;
6706 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6707 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6708 6458efa5 2020-11-24 stsp err = view_close_child(view);
6709 6458efa5 2020-11-24 stsp if (err)
6710 6458efa5 2020-11-24 stsp return err;
6711 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6712 e78dc838 2020-12-04 stsp view->focus_child = 1;
6713 6458efa5 2020-11-24 stsp } else
6714 6458efa5 2020-11-24 stsp *new_view = log_view;
6715 6458efa5 2020-11-24 stsp break;
6716 c42c9805 2020-11-24 stsp case 't':
6717 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6718 c42c9805 2020-11-24 stsp break;
6719 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6720 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6721 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6722 c42c9805 2020-11-24 stsp s->repo);
6723 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6724 c42c9805 2020-11-24 stsp break;
6725 e78dc838 2020-12-04 stsp view->focussed = 0;
6726 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6727 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6728 c42c9805 2020-11-24 stsp err = view_close_child(view);
6729 c42c9805 2020-11-24 stsp if (err)
6730 c42c9805 2020-11-24 stsp return err;
6731 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6732 e78dc838 2020-12-04 stsp view->focus_child = 1;
6733 c42c9805 2020-11-24 stsp } else
6734 c42c9805 2020-11-24 stsp *new_view = tree_view;
6735 c42c9805 2020-11-24 stsp break;
6736 e4526bf5 2021-09-03 naddy case 'g':
6737 e4526bf5 2021-09-03 naddy case KEY_HOME:
6738 e4526bf5 2021-09-03 naddy s->selected = 0;
6739 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6740 e4526bf5 2021-09-03 naddy break;
6741 e4526bf5 2021-09-03 naddy case 'G':
6742 e4526bf5 2021-09-03 naddy case KEY_END:
6743 e4526bf5 2021-09-03 naddy s->selected = 0;
6744 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6745 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6746 e4526bf5 2021-09-03 naddy if (re == NULL)
6747 e4526bf5 2021-09-03 naddy break;
6748 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6749 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6750 e4526bf5 2021-09-03 naddy }
6751 e4526bf5 2021-09-03 naddy if (n > 0)
6752 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6753 e4526bf5 2021-09-03 naddy break;
6754 6458efa5 2020-11-24 stsp case 'k':
6755 6458efa5 2020-11-24 stsp case KEY_UP:
6756 f7140bf5 2021-10-17 thomas case CTRL('p'):
6757 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6758 6458efa5 2020-11-24 stsp s->selected--;
6759 6458efa5 2020-11-24 stsp break;
6760 34ba6917 2020-11-30 stsp }
6761 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6762 6458efa5 2020-11-24 stsp break;
6763 70f17a53 2022-06-13 thomas case CTRL('u'):
6764 23427b14 2022-06-23 thomas case 'u':
6765 70f17a53 2022-06-13 thomas nscroll /= 2;
6766 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6767 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6768 6458efa5 2020-11-24 stsp case CTRL('b'):
6769 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6770 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6771 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6772 6458efa5 2020-11-24 stsp break;
6773 6458efa5 2020-11-24 stsp case 'j':
6774 6458efa5 2020-11-24 stsp case KEY_DOWN:
6775 f7140bf5 2021-10-17 thomas case CTRL('n'):
6776 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6777 6458efa5 2020-11-24 stsp s->selected++;
6778 6458efa5 2020-11-24 stsp break;
6779 6458efa5 2020-11-24 stsp }
6780 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6781 6458efa5 2020-11-24 stsp /* can't scroll any further */
6782 6458efa5 2020-11-24 stsp break;
6783 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6784 6458efa5 2020-11-24 stsp break;
6785 70f17a53 2022-06-13 thomas case CTRL('d'):
6786 23427b14 2022-06-23 thomas case 'd':
6787 70f17a53 2022-06-13 thomas nscroll /= 2;
6788 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6789 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6790 6458efa5 2020-11-24 stsp case CTRL('f'):
6791 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6792 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6793 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6794 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6795 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6796 6458efa5 2020-11-24 stsp break;
6797 6458efa5 2020-11-24 stsp }
6798 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6799 6458efa5 2020-11-24 stsp break;
6800 6458efa5 2020-11-24 stsp case CTRL('l'):
6801 8924d611 2020-12-26 stsp tog_free_refs();
6802 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6803 8924d611 2020-12-26 stsp if (err)
6804 8924d611 2020-12-26 stsp break;
6805 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6806 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6807 6458efa5 2020-11-24 stsp break;
6808 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6809 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6810 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6811 6458efa5 2020-11-24 stsp break;
6812 6458efa5 2020-11-24 stsp default:
6813 6458efa5 2020-11-24 stsp break;
6814 6458efa5 2020-11-24 stsp }
6815 6458efa5 2020-11-24 stsp
6816 6458efa5 2020-11-24 stsp return err;
6817 6458efa5 2020-11-24 stsp }
6818 6458efa5 2020-11-24 stsp
6819 6458efa5 2020-11-24 stsp __dead static void
6820 6458efa5 2020-11-24 stsp usage_ref(void)
6821 6458efa5 2020-11-24 stsp {
6822 6458efa5 2020-11-24 stsp endwin();
6823 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6824 6458efa5 2020-11-24 stsp getprogname());
6825 6458efa5 2020-11-24 stsp exit(1);
6826 6458efa5 2020-11-24 stsp }
6827 6458efa5 2020-11-24 stsp
6828 6458efa5 2020-11-24 stsp static const struct got_error *
6829 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6830 6458efa5 2020-11-24 stsp {
6831 6458efa5 2020-11-24 stsp const struct got_error *error;
6832 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6833 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6834 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6835 6458efa5 2020-11-24 stsp int ch;
6836 6458efa5 2020-11-24 stsp struct tog_view *view;
6837 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6838 6458efa5 2020-11-24 stsp
6839 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6840 6458efa5 2020-11-24 stsp switch (ch) {
6841 6458efa5 2020-11-24 stsp case 'r':
6842 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6843 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6844 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6845 6458efa5 2020-11-24 stsp optarg);
6846 6458efa5 2020-11-24 stsp break;
6847 6458efa5 2020-11-24 stsp default:
6848 e99e2d15 2020-11-24 naddy usage_ref();
6849 6458efa5 2020-11-24 stsp /* NOTREACHED */
6850 6458efa5 2020-11-24 stsp }
6851 6458efa5 2020-11-24 stsp }
6852 6458efa5 2020-11-24 stsp
6853 6458efa5 2020-11-24 stsp argc -= optind;
6854 6458efa5 2020-11-24 stsp argv += optind;
6855 6458efa5 2020-11-24 stsp
6856 6458efa5 2020-11-24 stsp if (argc > 1)
6857 e99e2d15 2020-11-24 naddy usage_ref();
6858 7cd52833 2022-06-23 thomas
6859 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6860 7cd52833 2022-06-23 thomas if (error != NULL)
6861 7cd52833 2022-06-23 thomas goto done;
6862 6458efa5 2020-11-24 stsp
6863 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6864 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6865 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6866 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6867 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6868 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6869 c156c7a4 2020-12-18 stsp goto done;
6870 6458efa5 2020-11-24 stsp if (worktree)
6871 6458efa5 2020-11-24 stsp repo_path =
6872 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6873 6458efa5 2020-11-24 stsp else
6874 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6875 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6876 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6877 c156c7a4 2020-12-18 stsp goto done;
6878 c156c7a4 2020-12-18 stsp }
6879 6458efa5 2020-11-24 stsp }
6880 6458efa5 2020-11-24 stsp
6881 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6882 6458efa5 2020-11-24 stsp if (error != NULL)
6883 6458efa5 2020-11-24 stsp goto done;
6884 6458efa5 2020-11-24 stsp
6885 6458efa5 2020-11-24 stsp init_curses();
6886 6458efa5 2020-11-24 stsp
6887 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6888 51a10b52 2020-12-26 stsp if (error)
6889 51a10b52 2020-12-26 stsp goto done;
6890 51a10b52 2020-12-26 stsp
6891 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6892 6458efa5 2020-11-24 stsp if (error)
6893 6458efa5 2020-11-24 stsp goto done;
6894 6458efa5 2020-11-24 stsp
6895 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6896 6458efa5 2020-11-24 stsp if (view == NULL) {
6897 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6898 6458efa5 2020-11-24 stsp goto done;
6899 6458efa5 2020-11-24 stsp }
6900 6458efa5 2020-11-24 stsp
6901 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6902 6458efa5 2020-11-24 stsp if (error)
6903 6458efa5 2020-11-24 stsp goto done;
6904 6458efa5 2020-11-24 stsp
6905 6458efa5 2020-11-24 stsp if (worktree) {
6906 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6907 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6908 6458efa5 2020-11-24 stsp worktree = NULL;
6909 6458efa5 2020-11-24 stsp }
6910 6458efa5 2020-11-24 stsp error = view_loop(view);
6911 6458efa5 2020-11-24 stsp done:
6912 6458efa5 2020-11-24 stsp free(repo_path);
6913 6458efa5 2020-11-24 stsp free(cwd);
6914 1d0f4054 2021-06-17 stsp if (repo) {
6915 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6916 1d0f4054 2021-06-17 stsp if (close_err)
6917 1d0f4054 2021-06-17 stsp error = close_err;
6918 1d0f4054 2021-06-17 stsp }
6919 7cd52833 2022-06-23 thomas if (pack_fds) {
6920 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6921 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6922 7cd52833 2022-06-23 thomas if (error == NULL)
6923 7cd52833 2022-06-23 thomas error = pack_err;
6924 7cd52833 2022-06-23 thomas }
6925 51a10b52 2020-12-26 stsp tog_free_refs();
6926 6458efa5 2020-11-24 stsp return error;
6927 6458efa5 2020-11-24 stsp }
6928 6458efa5 2020-11-24 stsp
6929 6458efa5 2020-11-24 stsp static void
6930 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6931 ce5b7c56 2019-07-09 stsp {
6932 6059809a 2020-12-17 stsp size_t i;
6933 9f7d7167 2018-04-29 stsp
6934 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6935 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6936 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6937 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6938 ce5b7c56 2019-07-09 stsp }
6939 6879ba42 2020-10-01 naddy fputc('\n', fp);
6940 ce5b7c56 2019-07-09 stsp }
6941 ce5b7c56 2019-07-09 stsp
6942 4ed7e80c 2018-05-20 stsp __dead static void
6943 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6944 9f7d7167 2018-04-29 stsp {
6945 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6946 6879ba42 2020-10-01 naddy
6947 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6948 6879ba42 2020-10-01 naddy getprogname());
6949 6879ba42 2020-10-01 naddy if (hflag) {
6950 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6951 6879ba42 2020-10-01 naddy list_commands(fp);
6952 ee85c5e8 2020-02-29 stsp }
6953 6879ba42 2020-10-01 naddy exit(status);
6954 9f7d7167 2018-04-29 stsp }
6955 9f7d7167 2018-04-29 stsp
6956 c2301be8 2018-04-30 stsp static char **
6957 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6958 c2301be8 2018-04-30 stsp {
6959 ee85c5e8 2020-02-29 stsp va_list ap;
6960 c2301be8 2018-04-30 stsp char **argv;
6961 ee85c5e8 2020-02-29 stsp int i;
6962 c2301be8 2018-04-30 stsp
6963 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6964 ee85c5e8 2020-02-29 stsp
6965 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6966 c2301be8 2018-04-30 stsp if (argv == NULL)
6967 c2301be8 2018-04-30 stsp err(1, "calloc");
6968 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6969 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6970 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6971 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6972 c2301be8 2018-04-30 stsp }
6973 c2301be8 2018-04-30 stsp
6974 ee85c5e8 2020-02-29 stsp va_end(ap);
6975 c2301be8 2018-04-30 stsp return argv;
6976 ee85c5e8 2020-02-29 stsp }
6977 ee85c5e8 2020-02-29 stsp
6978 ee85c5e8 2020-02-29 stsp /*
6979 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6980 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6981 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6982 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6983 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6984 ee85c5e8 2020-02-29 stsp */
6985 ee85c5e8 2020-02-29 stsp static const struct got_error *
6986 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6987 ee85c5e8 2020-02-29 stsp {
6988 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6989 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6990 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6991 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6992 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6993 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6994 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6995 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6996 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6997 84de9106 2020-12-26 stsp
6998 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6999 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7000 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7001 ee85c5e8 2020-02-29 stsp
7002 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7003 7cd52833 2022-06-23 thomas if (error != NULL)
7004 7cd52833 2022-06-23 thomas goto done;
7005 7cd52833 2022-06-23 thomas
7006 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7007 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7008 ee85c5e8 2020-02-29 stsp goto done;
7009 ee85c5e8 2020-02-29 stsp
7010 ee85c5e8 2020-02-29 stsp if (worktree)
7011 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7012 ee85c5e8 2020-02-29 stsp else
7013 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7014 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7015 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7016 ee85c5e8 2020-02-29 stsp goto done;
7017 ee85c5e8 2020-02-29 stsp }
7018 ee85c5e8 2020-02-29 stsp
7019 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7020 ee85c5e8 2020-02-29 stsp if (error != NULL)
7021 ee85c5e8 2020-02-29 stsp goto done;
7022 ee85c5e8 2020-02-29 stsp
7023 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7024 ee85c5e8 2020-02-29 stsp repo, worktree);
7025 ee85c5e8 2020-02-29 stsp if (error)
7026 ee85c5e8 2020-02-29 stsp goto done;
7027 ee85c5e8 2020-02-29 stsp
7028 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7029 84de9106 2020-12-26 stsp if (error)
7030 84de9106 2020-12-26 stsp goto done;
7031 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7032 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7033 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7034 ee85c5e8 2020-02-29 stsp if (error)
7035 ee85c5e8 2020-02-29 stsp goto done;
7036 ee85c5e8 2020-02-29 stsp
7037 ee85c5e8 2020-02-29 stsp if (worktree) {
7038 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7039 ee85c5e8 2020-02-29 stsp worktree = NULL;
7040 ee85c5e8 2020-02-29 stsp }
7041 ee85c5e8 2020-02-29 stsp
7042 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7043 945f9229 2022-04-16 thomas if (error)
7044 945f9229 2022-04-16 thomas goto done;
7045 945f9229 2022-04-16 thomas
7046 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7047 ee85c5e8 2020-02-29 stsp if (error) {
7048 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7049 ee85c5e8 2020-02-29 stsp goto done;
7050 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7051 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7052 6879ba42 2020-10-01 naddy usage(1, 1);
7053 ee85c5e8 2020-02-29 stsp /* not reached */
7054 ee85c5e8 2020-02-29 stsp }
7055 ee85c5e8 2020-02-29 stsp
7056 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7057 1d0f4054 2021-06-17 stsp if (error == NULL)
7058 1d0f4054 2021-06-17 stsp error = close_err;
7059 ee85c5e8 2020-02-29 stsp repo = NULL;
7060 ee85c5e8 2020-02-29 stsp
7061 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7062 ee85c5e8 2020-02-29 stsp if (error)
7063 ee85c5e8 2020-02-29 stsp goto done;
7064 ee85c5e8 2020-02-29 stsp
7065 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7066 ee85c5e8 2020-02-29 stsp argc = 4;
7067 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7068 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7069 ee85c5e8 2020-02-29 stsp done:
7070 1d0f4054 2021-06-17 stsp if (repo) {
7071 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7072 1d0f4054 2021-06-17 stsp if (error == NULL)
7073 1d0f4054 2021-06-17 stsp error = close_err;
7074 1d0f4054 2021-06-17 stsp }
7075 945f9229 2022-04-16 thomas if (commit)
7076 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7077 ee85c5e8 2020-02-29 stsp if (worktree)
7078 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7079 7cd52833 2022-06-23 thomas if (pack_fds) {
7080 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7081 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7082 7cd52833 2022-06-23 thomas if (error == NULL)
7083 7cd52833 2022-06-23 thomas error = pack_err;
7084 7cd52833 2022-06-23 thomas }
7085 ee85c5e8 2020-02-29 stsp free(id);
7086 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7087 ee85c5e8 2020-02-29 stsp free(commit_id);
7088 ee85c5e8 2020-02-29 stsp free(cwd);
7089 ee85c5e8 2020-02-29 stsp free(repo_path);
7090 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7091 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7092 ee85c5e8 2020-02-29 stsp int i;
7093 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7094 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7095 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7096 ee85c5e8 2020-02-29 stsp }
7097 87670572 2020-12-26 naddy tog_free_refs();
7098 ee85c5e8 2020-02-29 stsp return error;
7099 c2301be8 2018-04-30 stsp }
7100 c2301be8 2018-04-30 stsp
7101 9f7d7167 2018-04-29 stsp int
7102 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7103 9f7d7167 2018-04-29 stsp {
7104 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7105 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7106 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7107 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7108 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
7109 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7110 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7111 83cd27f8 2020-01-13 stsp };
7112 9f7d7167 2018-04-29 stsp
7113 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7114 9f7d7167 2018-04-29 stsp
7115 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7116 9f7d7167 2018-04-29 stsp switch (ch) {
7117 9f7d7167 2018-04-29 stsp case 'h':
7118 9f7d7167 2018-04-29 stsp hflag = 1;
7119 9f7d7167 2018-04-29 stsp break;
7120 53ccebc2 2019-07-30 stsp case 'V':
7121 53ccebc2 2019-07-30 stsp Vflag = 1;
7122 53ccebc2 2019-07-30 stsp break;
7123 9f7d7167 2018-04-29 stsp default:
7124 6879ba42 2020-10-01 naddy usage(hflag, 1);
7125 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7126 9f7d7167 2018-04-29 stsp }
7127 9f7d7167 2018-04-29 stsp }
7128 9f7d7167 2018-04-29 stsp
7129 9f7d7167 2018-04-29 stsp argc -= optind;
7130 9f7d7167 2018-04-29 stsp argv += optind;
7131 9814e6a3 2020-09-27 naddy optind = 1;
7132 c2301be8 2018-04-30 stsp optreset = 1;
7133 9f7d7167 2018-04-29 stsp
7134 53ccebc2 2019-07-30 stsp if (Vflag) {
7135 53ccebc2 2019-07-30 stsp got_version_print_str();
7136 6879ba42 2020-10-01 naddy return 0;
7137 53ccebc2 2019-07-30 stsp }
7138 4010e238 2020-12-04 stsp
7139 4010e238 2020-12-04 stsp #ifndef PROFILE
7140 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7141 4010e238 2020-12-04 stsp NULL) == -1)
7142 4010e238 2020-12-04 stsp err(1, "pledge");
7143 4010e238 2020-12-04 stsp #endif
7144 53ccebc2 2019-07-30 stsp
7145 c2301be8 2018-04-30 stsp if (argc == 0) {
7146 f29d3e89 2018-06-23 stsp if (hflag)
7147 6879ba42 2020-10-01 naddy usage(hflag, 0);
7148 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7149 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7150 c2301be8 2018-04-30 stsp argc = 1;
7151 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7152 c2301be8 2018-04-30 stsp } else {
7153 6059809a 2020-12-17 stsp size_t i;
7154 9f7d7167 2018-04-29 stsp
7155 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7156 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7157 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7158 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7159 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7160 9f7d7167 2018-04-29 stsp break;
7161 9f7d7167 2018-04-29 stsp }
7162 9f7d7167 2018-04-29 stsp }
7163 ee85c5e8 2020-02-29 stsp }
7164 3642c4c6 2019-07-09 stsp
7165 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7166 ee85c5e8 2020-02-29 stsp if (argc != 1)
7167 6879ba42 2020-10-01 naddy usage(0, 1);
7168 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7169 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7170 ee85c5e8 2020-02-29 stsp } else {
7171 ee85c5e8 2020-02-29 stsp if (hflag)
7172 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7173 ee85c5e8 2020-02-29 stsp else
7174 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7175 9f7d7167 2018-04-29 stsp }
7176 9f7d7167 2018-04-29 stsp
7177 9f7d7167 2018-04-29 stsp endwin();
7178 b46c1e04 2020-09-20 naddy putchar('\n');
7179 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7180 a2f4a359 2020-02-28 stsp int i;
7181 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7182 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7183 a2f4a359 2020-02-28 stsp free(cmd_argv);
7184 a2f4a359 2020-02-28 stsp }
7185 a2f4a359 2020-02-28 stsp
7186 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7187 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7188 9f7d7167 2018-04-29 stsp return 0;
7189 9f7d7167 2018-04-29 stsp }