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