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