Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
19 80ddbec8 2018-04-29 stsp
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 def2f970 2021-09-28 thomas #endif
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
45 dd038bc6 2021-09-21 thomas.ad
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
60 9f7d7167 2018-04-29 stsp
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
63 dd038bc6 2021-09-21 thomas.ad
64 881b2d3e 2018-04-30 stsp #ifndef MIN
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 881b2d3e 2018-04-30 stsp #endif
67 881b2d3e 2018-04-30 stsp
68 2bd27830 2018-10-22 stsp #ifndef MAX
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 2bd27830 2018-10-22 stsp #endif
71 2bd27830 2018-10-22 stsp
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
74 acf52a76 2021-09-21 thomas.ad #endif
75 2bd27830 2018-10-22 stsp
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
78 9f7d7167 2018-04-29 stsp #endif
79 9f7d7167 2018-04-29 stsp
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
84 9f7d7167 2018-04-29 stsp };
85 9f7d7167 2018-04-29 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
92 9f7d7167 2018-04-29 stsp
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
98 9f7d7167 2018-04-29 stsp
99 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
105 9f7d7167 2018-04-29 stsp };
106 9f7d7167 2018-04-29 stsp
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
113 d6b05b5b 2018-08-04 stsp };
114 c3e9aa98 2019-05-13 jcs
115 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
116 d6b05b5b 2018-08-04 stsp
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
121 1a76625f 2018-10-22 stsp int idx;
122 ba4f502b 2018-08-04 stsp };
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
127 6d17833f 2019-11-08 stsp };
128 6d17833f 2019-11-08 stsp
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
133 15a087fe 2019-02-21 stsp };
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
135 11b20872 2019-11-08 stsp
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
138 2183bbf6 2022-01-23 thomas
139 2183bbf6 2022-01-23 thomas static const struct got_error *
140 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 2183bbf6 2022-01-23 thomas struct got_reference* re2)
142 2183bbf6 2022-01-23 thomas {
143 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
144 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
145 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
146 2183bbf6 2022-01-23 thomas
147 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
148 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
151 2183bbf6 2022-01-23 thomas *cmp = -1;
152 2183bbf6 2022-01-23 thomas return NULL;
153 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
154 2183bbf6 2022-01-23 thomas *cmp = 1;
155 2183bbf6 2022-01-23 thomas return NULL;
156 2183bbf6 2022-01-23 thomas }
157 2183bbf6 2022-01-23 thomas
158 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 2183bbf6 2022-01-23 thomas return NULL;
160 2183bbf6 2022-01-23 thomas }
161 51a10b52 2020-12-26 stsp
162 11b20872 2019-11-08 stsp static const struct got_error *
163 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
164 51a10b52 2020-12-26 stsp {
165 51a10b52 2020-12-26 stsp const struct got_error *err;
166 51a10b52 2020-12-26 stsp
167 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
169 3bfadbd4 2021-11-20 thomas repo);
170 51a10b52 2020-12-26 stsp if (err)
171 51a10b52 2020-12-26 stsp return err;
172 51a10b52 2020-12-26 stsp
173 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
174 51a10b52 2020-12-26 stsp repo);
175 51a10b52 2020-12-26 stsp }
176 51a10b52 2020-12-26 stsp
177 51a10b52 2020-12-26 stsp static void
178 51a10b52 2020-12-26 stsp tog_free_refs(void)
179 51a10b52 2020-12-26 stsp {
180 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
181 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
182 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
183 51a10b52 2020-12-26 stsp }
184 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp
187 51a10b52 2020-12-26 stsp static const struct got_error *
188 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
189 11b20872 2019-11-08 stsp int idx, short color)
190 11b20872 2019-11-08 stsp {
191 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
192 11b20872 2019-11-08 stsp struct tog_color *tc;
193 11b20872 2019-11-08 stsp int regerr = 0;
194 11b20872 2019-11-08 stsp
195 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
196 11b20872 2019-11-08 stsp return NULL;
197 11b20872 2019-11-08 stsp
198 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
201 11b20872 2019-11-08 stsp if (tc == NULL)
202 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
203 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
204 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 11b20872 2019-11-08 stsp if (regerr) {
206 11b20872 2019-11-08 stsp static char regerr_msg[512];
207 11b20872 2019-11-08 stsp static char err_msg[512];
208 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
209 11b20872 2019-11-08 stsp sizeof(regerr_msg));
210 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 11b20872 2019-11-08 stsp regerr_msg);
212 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
213 11b20872 2019-11-08 stsp free(tc);
214 11b20872 2019-11-08 stsp return err;
215 11b20872 2019-11-08 stsp }
216 11b20872 2019-11-08 stsp tc->colorpair = idx;
217 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
218 11b20872 2019-11-08 stsp return NULL;
219 11b20872 2019-11-08 stsp }
220 11b20872 2019-11-08 stsp
221 11b20872 2019-11-08 stsp static void
222 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
223 11b20872 2019-11-08 stsp {
224 11b20872 2019-11-08 stsp struct tog_color *tc;
225 11b20872 2019-11-08 stsp
226 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
227 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
228 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
229 11b20872 2019-11-08 stsp regfree(&tc->regex);
230 11b20872 2019-11-08 stsp free(tc);
231 11b20872 2019-11-08 stsp }
232 11b20872 2019-11-08 stsp }
233 11b20872 2019-11-08 stsp
234 11b20872 2019-11-08 stsp struct tog_color *
235 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
236 11b20872 2019-11-08 stsp {
237 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
238 11b20872 2019-11-08 stsp
239 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
240 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
241 11b20872 2019-11-08 stsp return tc;
242 11b20872 2019-11-08 stsp }
243 11b20872 2019-11-08 stsp
244 11b20872 2019-11-08 stsp return NULL;
245 11b20872 2019-11-08 stsp }
246 11b20872 2019-11-08 stsp
247 11b20872 2019-11-08 stsp static int
248 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
249 11b20872 2019-11-08 stsp {
250 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_CYAN;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 11b20872 2019-11-08 stsp return COLOR_YELLOW;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 11b20872 2019-11-08 stsp return COLOR_GREEN;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_CYAN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_GREEN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 6458efa5 2020-11-24 stsp return COLOR_GREEN;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
278 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
280 11b20872 2019-11-08 stsp
281 11b20872 2019-11-08 stsp return -1;
282 11b20872 2019-11-08 stsp }
283 11b20872 2019-11-08 stsp
284 11b20872 2019-11-08 stsp static int
285 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
286 11b20872 2019-11-08 stsp {
287 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
288 11b20872 2019-11-08 stsp
289 11b20872 2019-11-08 stsp if (val == NULL)
290 11b20872 2019-11-08 stsp return default_color_value(envvar);
291 15a087fe 2019-02-21 stsp
292 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
293 11b20872 2019-11-08 stsp return COLOR_BLACK;
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
295 11b20872 2019-11-08 stsp return COLOR_RED;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
297 11b20872 2019-11-08 stsp return COLOR_GREEN;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
299 11b20872 2019-11-08 stsp return COLOR_YELLOW;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
301 11b20872 2019-11-08 stsp return COLOR_BLUE;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
303 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
305 11b20872 2019-11-08 stsp return COLOR_CYAN;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
307 11b20872 2019-11-08 stsp return COLOR_WHITE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
309 11b20872 2019-11-08 stsp return -1;
310 11b20872 2019-11-08 stsp
311 11b20872 2019-11-08 stsp return default_color_value(envvar);
312 11b20872 2019-11-08 stsp }
313 11b20872 2019-11-08 stsp
314 11b20872 2019-11-08 stsp
315 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
316 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
317 3dbaef42 2020-11-24 stsp const char *label1, *label2;
318 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
319 15a087fe 2019-02-21 stsp int first_displayed_line;
320 15a087fe 2019-02-21 stsp int last_displayed_line;
321 15a087fe 2019-02-21 stsp int eof;
322 15a087fe 2019-02-21 stsp int diff_context;
323 3dbaef42 2020-11-24 stsp int ignore_whitespace;
324 64453f7e 2020-11-21 stsp int force_text_diff;
325 15a087fe 2019-02-21 stsp struct got_repository *repo;
326 bddb1296 2019-11-08 stsp struct tog_colors colors;
327 fe621944 2020-11-10 stsp size_t nlines;
328 f44b1f58 2020-02-02 tracey off_t *line_offsets;
329 f44b1f58 2020-02-02 tracey int matched_line;
330 f44b1f58 2020-02-02 tracey int selected_line;
331 15a087fe 2019-02-21 stsp
332 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
333 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
334 b01e7d3b 2018-08-04 stsp };
335 b01e7d3b 2018-08-04 stsp
336 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
337 1a76625f 2018-10-22 stsp
338 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
339 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
340 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
341 1a76625f 2018-10-22 stsp int commits_needed;
342 fb280deb 2021-08-30 stsp int load_all;
343 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
344 1a76625f 2018-10-22 stsp struct commit_queue *commits;
345 1a76625f 2018-10-22 stsp const char *in_repo_path;
346 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
347 1a76625f 2018-10-22 stsp struct got_repository *repo;
348 1a76625f 2018-10-22 stsp int log_complete;
349 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
350 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
352 13add988 2019-10-15 stsp int *searching;
353 13add988 2019-10-15 stsp int *search_next_done;
354 13add988 2019-10-15 stsp regex_t *regex;
355 1a76625f 2018-10-22 stsp };
356 1a76625f 2018-10-22 stsp
357 1a76625f 2018-10-22 stsp struct tog_log_view_state {
358 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
359 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
362 b01e7d3b 2018-08-04 stsp int selected;
363 b01e7d3b 2018-08-04 stsp char *in_repo_path;
364 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
365 b672a97a 2020-01-27 stsp int log_branches;
366 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
367 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp sig_atomic_t quit;
369 1a76625f 2018-10-22 stsp pthread_t thread;
370 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
371 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
372 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
373 11b20872 2019-11-08 stsp struct tog_colors colors;
374 ba4f502b 2018-08-04 stsp };
375 11b20872 2019-11-08 stsp
376 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
380 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
384 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
385 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
386 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
387 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
390 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
391 ba4f502b 2018-08-04 stsp
392 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
393 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
394 e9424729 2018-08-04 stsp int nlines;
395 e9424729 2018-08-04 stsp
396 e9424729 2018-08-04 stsp struct tog_view *view;
397 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
398 e9424729 2018-08-04 stsp int *quit;
399 e9424729 2018-08-04 stsp };
400 e9424729 2018-08-04 stsp
401 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
402 e9424729 2018-08-04 stsp const char *path;
403 e9424729 2018-08-04 stsp struct got_repository *repo;
404 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
405 e9424729 2018-08-04 stsp int *complete;
406 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
407 fc06ba56 2019-08-22 stsp void *cancel_arg;
408 e9424729 2018-08-04 stsp };
409 e9424729 2018-08-04 stsp
410 e9424729 2018-08-04 stsp struct tog_blame {
411 e9424729 2018-08-04 stsp FILE *f;
412 be659d10 2020-11-18 stsp off_t filesize;
413 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
414 6fcac457 2018-11-19 stsp int nlines;
415 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
416 e9424729 2018-08-04 stsp pthread_t thread;
417 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
419 e9424729 2018-08-04 stsp const char *path;
420 e9424729 2018-08-04 stsp };
421 e9424729 2018-08-04 stsp
422 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
423 7cbe629d 2018-08-04 stsp int first_displayed_line;
424 7cbe629d 2018-08-04 stsp int last_displayed_line;
425 7cbe629d 2018-08-04 stsp int selected_line;
426 7cbe629d 2018-08-04 stsp int blame_complete;
427 e5a0f69f 2018-08-18 stsp int eof;
428 e5a0f69f 2018-08-18 stsp int done;
429 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
430 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
431 e5a0f69f 2018-08-18 stsp char *path;
432 7cbe629d 2018-08-04 stsp struct got_repository *repo;
433 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
434 e9424729 2018-08-04 stsp struct tog_blame blame;
435 6c4c42e0 2019-06-24 stsp int matched_line;
436 11b20872 2019-11-08 stsp struct tog_colors colors;
437 ad80ab7b 2018-08-04 stsp };
438 ad80ab7b 2018-08-04 stsp
439 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
440 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
441 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
442 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
444 ad80ab7b 2018-08-04 stsp int selected;
445 ad80ab7b 2018-08-04 stsp };
446 ad80ab7b 2018-08-04 stsp
447 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
448 ad80ab7b 2018-08-04 stsp
449 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
450 ad80ab7b 2018-08-04 stsp char *tree_label;
451 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
452 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
453 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
458 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
459 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
460 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
461 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
462 6458efa5 2020-11-24 stsp struct tog_colors colors;
463 6458efa5 2020-11-24 stsp };
464 6458efa5 2020-11-24 stsp
465 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
466 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
467 6458efa5 2020-11-24 stsp struct got_reference *ref;
468 6458efa5 2020-11-24 stsp int idx;
469 6458efa5 2020-11-24 stsp };
470 6458efa5 2020-11-24 stsp
471 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
474 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
478 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 6458efa5 2020-11-24 stsp struct got_repository *repo;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
481 bddb1296 2019-11-08 stsp struct tog_colors colors;
482 7cbe629d 2018-08-04 stsp };
483 7cbe629d 2018-08-04 stsp
484 669b5ffa 2018-10-07 stsp /*
485 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
486 669b5ffa 2018-10-07 stsp *
487 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
488 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
489 669b5ffa 2018-10-07 stsp * there is enough screen estate.
490 669b5ffa 2018-10-07 stsp *
491 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
492 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
493 669b5ffa 2018-10-07 stsp *
494 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
495 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
496 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
497 669b5ffa 2018-10-07 stsp *
498 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
499 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
500 669b5ffa 2018-10-07 stsp */
501 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
502 669b5ffa 2018-10-07 stsp
503 cc3c9aac 2018-08-01 stsp struct tog_view {
504 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
505 26ed57b2 2018-05-19 stsp WINDOW *window;
506 26ed57b2 2018-05-19 stsp PANEL *panel;
507 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
508 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
509 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
510 9970f7fc 2020-12-03 stsp int dying;
511 669b5ffa 2018-10-07 stsp struct tog_view *parent;
512 669b5ffa 2018-10-07 stsp struct tog_view *child;
513 5dc9f4bc 2018-08-04 stsp
514 e78dc838 2020-12-04 stsp /*
515 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
516 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
517 e78dc838 2020-12-04 stsp * between parent and child.
518 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
519 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
520 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
521 e78dc838 2020-12-04 stsp * situations.
522 e78dc838 2020-12-04 stsp */
523 e78dc838 2020-12-04 stsp int focus_child;
524 e78dc838 2020-12-04 stsp
525 5dc9f4bc 2018-08-04 stsp /* type-specific state */
526 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
527 5dc9f4bc 2018-08-04 stsp union {
528 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
529 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
530 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
531 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
532 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
533 5dc9f4bc 2018-08-04 stsp } state;
534 e5a0f69f 2018-08-18 stsp
535 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
536 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
537 e78dc838 2020-12-04 stsp struct tog_view *, int);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
539 60493ae3 2019-06-20 stsp
540 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
541 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
542 c0c4acc8 2021-01-24 stsp int search_started;
543 60493ae3 2019-06-20 stsp int searching;
544 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
545 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
546 60493ae3 2019-06-20 stsp int search_next_done;
547 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
548 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
549 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
550 1803e47f 2019-06-22 stsp regex_t regex;
551 41605754 2020-11-12 stsp regmatch_t regmatch;
552 cc3c9aac 2018-08-01 stsp };
553 cd0acaa7 2018-05-20 stsp
554 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
555 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
556 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
557 78756c87 2020-11-24 stsp struct got_repository *);
558 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
562 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
563 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp
565 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
566 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
567 78756c87 2020-11-24 stsp const char *, const char *, int);
568 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
572 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
573 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
574 e5a0f69f 2018-08-18 stsp
575 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
576 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
577 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
579 e78dc838 2020-12-04 stsp struct tog_view *, int);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
581 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
582 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp
584 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
585 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
586 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
590 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
591 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
592 6458efa5 2020-11-24 stsp
593 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
594 6458efa5 2020-11-24 stsp struct got_repository *);
595 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
601 25791caa 2018-10-24 stsp
602 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
603 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
604 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
605 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
606 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
607 25791caa 2018-10-24 stsp
608 25791caa 2018-10-24 stsp static void
609 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
610 25791caa 2018-10-24 stsp {
611 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
612 83baff54 2019-08-12 stsp }
613 83baff54 2019-08-12 stsp
614 83baff54 2019-08-12 stsp static void
615 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
616 83baff54 2019-08-12 stsp {
617 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
618 25791caa 2018-10-24 stsp }
619 26ed57b2 2018-05-19 stsp
620 61266923 2020-01-14 stsp static void
621 61266923 2020-01-14 stsp tog_sigcont(int signo)
622 61266923 2020-01-14 stsp {
623 61266923 2020-01-14 stsp tog_sigcont_received = 1;
624 61266923 2020-01-14 stsp }
625 61266923 2020-01-14 stsp
626 296152d1 2022-05-31 thomas static void
627 296152d1 2022-05-31 thomas tog_sigint(int signo)
628 296152d1 2022-05-31 thomas {
629 296152d1 2022-05-31 thomas tog_sigint_received = 1;
630 296152d1 2022-05-31 thomas }
631 296152d1 2022-05-31 thomas
632 296152d1 2022-05-31 thomas static void
633 296152d1 2022-05-31 thomas tog_sigterm(int signo)
634 296152d1 2022-05-31 thomas {
635 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
636 296152d1 2022-05-31 thomas }
637 296152d1 2022-05-31 thomas
638 296152d1 2022-05-31 thomas static int
639 296152d1 2022-05-31 thomas tog_fatal_signal_received()
640 296152d1 2022-05-31 thomas {
641 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
642 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
643 296152d1 2022-05-31 thomas }
644 296152d1 2022-05-31 thomas
645 296152d1 2022-05-31 thomas
646 e5a0f69f 2018-08-18 stsp static const struct got_error *
647 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
648 ea5e7bb5 2018-08-01 stsp {
649 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
650 e5a0f69f 2018-08-18 stsp
651 669b5ffa 2018-10-07 stsp if (view->child) {
652 669b5ffa 2018-10-07 stsp view_close(view->child);
653 669b5ffa 2018-10-07 stsp view->child = NULL;
654 669b5ffa 2018-10-07 stsp }
655 e5a0f69f 2018-08-18 stsp if (view->close)
656 e5a0f69f 2018-08-18 stsp err = view->close(view);
657 ea5e7bb5 2018-08-01 stsp if (view->panel)
658 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
659 ea5e7bb5 2018-08-01 stsp if (view->window)
660 ea5e7bb5 2018-08-01 stsp delwin(view->window);
661 ea5e7bb5 2018-08-01 stsp free(view);
662 e5a0f69f 2018-08-18 stsp return err;
663 ea5e7bb5 2018-08-01 stsp }
664 ea5e7bb5 2018-08-01 stsp
665 ea5e7bb5 2018-08-01 stsp static struct tog_view *
666 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
667 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
668 ea5e7bb5 2018-08-01 stsp {
669 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
670 ea5e7bb5 2018-08-01 stsp
671 ea5e7bb5 2018-08-01 stsp if (view == NULL)
672 ea5e7bb5 2018-08-01 stsp return NULL;
673 ea5e7bb5 2018-08-01 stsp
674 d6b05b5b 2018-08-04 stsp view->type = type;
675 f7d12f7e 2018-08-01 stsp view->lines = LINES;
676 f7d12f7e 2018-08-01 stsp view->cols = COLS;
677 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
678 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
679 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
680 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
681 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
682 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
683 96a765a8 2018-08-04 stsp view_close(view);
684 ea5e7bb5 2018-08-01 stsp return NULL;
685 ea5e7bb5 2018-08-01 stsp }
686 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
687 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
688 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
689 96a765a8 2018-08-04 stsp view_close(view);
690 ea5e7bb5 2018-08-01 stsp return NULL;
691 ea5e7bb5 2018-08-01 stsp }
692 ea5e7bb5 2018-08-01 stsp
693 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
694 ea5e7bb5 2018-08-01 stsp return view;
695 cdf1ee82 2018-08-01 stsp }
696 cdf1ee82 2018-08-01 stsp
697 0cf4efb1 2018-09-29 stsp static int
698 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
699 0cf4efb1 2018-09-29 stsp {
700 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
701 0cf4efb1 2018-09-29 stsp return 0;
702 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
703 5c60c32a 2018-10-18 stsp }
704 5c60c32a 2018-10-18 stsp
705 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
706 5c60c32a 2018-10-18 stsp
707 5c60c32a 2018-10-18 stsp static const struct got_error *
708 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
709 5c60c32a 2018-10-18 stsp {
710 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
711 5c60c32a 2018-10-18 stsp
712 5c60c32a 2018-10-18 stsp view->begin_y = 0;
713 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
714 5c60c32a 2018-10-18 stsp view->nlines = LINES;
715 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
716 5c60c32a 2018-10-18 stsp view->lines = LINES;
717 5c60c32a 2018-10-18 stsp view->cols = COLS;
718 5c60c32a 2018-10-18 stsp err = view_resize(view);
719 5c60c32a 2018-10-18 stsp if (err)
720 5c60c32a 2018-10-18 stsp return err;
721 5c60c32a 2018-10-18 stsp
722 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
724 5c60c32a 2018-10-18 stsp
725 5c60c32a 2018-10-18 stsp return NULL;
726 5c60c32a 2018-10-18 stsp }
727 5c60c32a 2018-10-18 stsp
728 5c60c32a 2018-10-18 stsp static const struct got_error *
729 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
730 5c60c32a 2018-10-18 stsp {
731 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
732 5c60c32a 2018-10-18 stsp
733 5c60c32a 2018-10-18 stsp view->begin_x = 0;
734 5c60c32a 2018-10-18 stsp view->begin_y = 0;
735 5c60c32a 2018-10-18 stsp view->nlines = LINES;
736 5c60c32a 2018-10-18 stsp view->ncols = COLS;
737 5c60c32a 2018-10-18 stsp view->lines = LINES;
738 5c60c32a 2018-10-18 stsp view->cols = COLS;
739 5c60c32a 2018-10-18 stsp err = view_resize(view);
740 5c60c32a 2018-10-18 stsp if (err)
741 5c60c32a 2018-10-18 stsp return err;
742 5c60c32a 2018-10-18 stsp
743 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
744 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
745 5c60c32a 2018-10-18 stsp
746 5c60c32a 2018-10-18 stsp return NULL;
747 0cf4efb1 2018-09-29 stsp }
748 0cf4efb1 2018-09-29 stsp
749 5c60c32a 2018-10-18 stsp static int
750 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
751 5c60c32a 2018-10-18 stsp {
752 5c60c32a 2018-10-18 stsp return view->parent == NULL;
753 5c60c32a 2018-10-18 stsp }
754 5c60c32a 2018-10-18 stsp
755 4d8c2215 2018-08-19 stsp static const struct got_error *
756 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
757 f7d12f7e 2018-08-01 stsp {
758 f7d12f7e 2018-08-01 stsp int nlines, ncols;
759 f7d12f7e 2018-08-01 stsp
760 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
761 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
762 0cf4efb1 2018-09-29 stsp else
763 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
764 f7d12f7e 2018-08-01 stsp
765 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
766 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
767 0cf4efb1 2018-09-29 stsp else
768 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
769 f7d12f7e 2018-08-01 stsp
770 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
771 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
772 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
773 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
774 25791caa 2018-10-24 stsp wclear(view->window);
775 f7d12f7e 2018-08-01 stsp
776 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
777 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
778 0cf4efb1 2018-09-29 stsp view->lines = LINES;
779 0cf4efb1 2018-09-29 stsp view->cols = COLS;
780 6d0fee91 2018-08-01 stsp
781 6e3e5d9c 2018-10-18 stsp if (view->child) {
782 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
783 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
784 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
785 5c60c32a 2018-10-18 stsp if (view->child->focussed)
786 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
787 5c60c32a 2018-10-18 stsp else
788 5c60c32a 2018-10-18 stsp show_panel(view->panel);
789 5c60c32a 2018-10-18 stsp } else {
790 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
791 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
792 5c60c32a 2018-10-18 stsp }
793 5c60c32a 2018-10-18 stsp }
794 669b5ffa 2018-10-07 stsp
795 5c60c32a 2018-10-18 stsp return NULL;
796 669b5ffa 2018-10-07 stsp }
797 669b5ffa 2018-10-07 stsp
798 669b5ffa 2018-10-07 stsp static const struct got_error *
799 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
800 669b5ffa 2018-10-07 stsp {
801 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
802 669b5ffa 2018-10-07 stsp
803 669b5ffa 2018-10-07 stsp if (view->child == NULL)
804 669b5ffa 2018-10-07 stsp return NULL;
805 669b5ffa 2018-10-07 stsp
806 669b5ffa 2018-10-07 stsp err = view_close(view->child);
807 669b5ffa 2018-10-07 stsp view->child = NULL;
808 669b5ffa 2018-10-07 stsp return err;
809 669b5ffa 2018-10-07 stsp }
810 669b5ffa 2018-10-07 stsp
811 72a9cb46 2020-12-03 stsp static void
812 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
813 669b5ffa 2018-10-07 stsp {
814 669b5ffa 2018-10-07 stsp view->child = child;
815 669b5ffa 2018-10-07 stsp child->parent = view;
816 bfddd0d9 2018-09-29 stsp }
817 bfddd0d9 2018-09-29 stsp
818 bfddd0d9 2018-09-29 stsp static int
819 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
820 bfddd0d9 2018-09-29 stsp {
821 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
822 34bc9ec9 2019-02-22 stsp }
823 34bc9ec9 2019-02-22 stsp
824 34bc9ec9 2019-02-22 stsp static void
825 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
826 25791caa 2018-10-24 stsp {
827 25791caa 2018-10-24 stsp int cols, lines;
828 25791caa 2018-10-24 stsp struct winsize size;
829 25791caa 2018-10-24 stsp
830 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
831 25791caa 2018-10-24 stsp cols = 80; /* Default */
832 25791caa 2018-10-24 stsp lines = 24;
833 25791caa 2018-10-24 stsp } else {
834 25791caa 2018-10-24 stsp cols = size.ws_col;
835 25791caa 2018-10-24 stsp lines = size.ws_row;
836 25791caa 2018-10-24 stsp }
837 25791caa 2018-10-24 stsp resize_term(lines, cols);
838 2b49a8ae 2019-06-22 stsp }
839 2b49a8ae 2019-06-22 stsp
840 2b49a8ae 2019-06-22 stsp static const struct got_error *
841 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
842 2b49a8ae 2019-06-22 stsp {
843 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
844 2b49a8ae 2019-06-22 stsp char pattern[1024];
845 2b49a8ae 2019-06-22 stsp int ret;
846 c0c4acc8 2021-01-24 stsp
847 c0c4acc8 2021-01-24 stsp if (view->search_started) {
848 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
849 c0c4acc8 2021-01-24 stsp view->searching = 0;
850 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
851 c0c4acc8 2021-01-24 stsp }
852 c0c4acc8 2021-01-24 stsp view->search_started = 0;
853 2b49a8ae 2019-06-22 stsp
854 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
855 2b49a8ae 2019-06-22 stsp return NULL;
856 2b49a8ae 2019-06-22 stsp
857 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
858 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
859 2b49a8ae 2019-06-22 stsp
860 2b49a8ae 2019-06-22 stsp nocbreak();
861 2b49a8ae 2019-06-22 stsp echo();
862 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
863 2b49a8ae 2019-06-22 stsp cbreak();
864 2b49a8ae 2019-06-22 stsp noecho();
865 2b49a8ae 2019-06-22 stsp if (ret == ERR)
866 2b49a8ae 2019-06-22 stsp return NULL;
867 2b49a8ae 2019-06-22 stsp
868 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
869 7c32bd05 2019-06-22 stsp err = view->search_start(view);
870 7c32bd05 2019-06-22 stsp if (err) {
871 7c32bd05 2019-06-22 stsp regfree(&view->regex);
872 7c32bd05 2019-06-22 stsp return err;
873 7c32bd05 2019-06-22 stsp }
874 c0c4acc8 2021-01-24 stsp view->search_started = 1;
875 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
876 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
877 2b49a8ae 2019-06-22 stsp view->search_next(view);
878 2b49a8ae 2019-06-22 stsp }
879 2b49a8ae 2019-06-22 stsp
880 2b49a8ae 2019-06-22 stsp return NULL;
881 0cf4efb1 2018-09-29 stsp }
882 6d0fee91 2018-08-01 stsp
883 0cf4efb1 2018-09-29 stsp static const struct got_error *
884 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
885 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
886 e5a0f69f 2018-08-18 stsp {
887 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
888 669b5ffa 2018-10-07 stsp struct tog_view *v;
889 1a76625f 2018-10-22 stsp int ch, errcode;
890 e5a0f69f 2018-08-18 stsp
891 e5a0f69f 2018-08-18 stsp *new = NULL;
892 8f4ed634 2020-03-26 stsp
893 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
894 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
895 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
896 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
897 e5a0f69f 2018-08-18 stsp
898 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
899 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
900 82954512 2020-02-03 stsp if (errcode)
901 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
902 82954512 2020-02-03 stsp "pthread_mutex_unlock");
903 3da8ef85 2021-09-21 stsp sched_yield();
904 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
905 82954512 2020-02-03 stsp if (errcode)
906 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
907 82954512 2020-02-03 stsp "pthread_mutex_lock");
908 60493ae3 2019-06-20 stsp view->search_next(view);
909 60493ae3 2019-06-20 stsp return NULL;
910 60493ae3 2019-06-20 stsp }
911 60493ae3 2019-06-20 stsp
912 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
913 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
914 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
915 1a76625f 2018-10-22 stsp if (errcode)
916 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
917 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
918 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
919 1a76625f 2018-10-22 stsp if (errcode)
920 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
921 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
922 25791caa 2018-10-24 stsp
923 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
924 25791caa 2018-10-24 stsp tog_resizeterm();
925 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
926 61266923 2020-01-14 stsp tog_sigcont_received = 0;
927 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
928 25791caa 2018-10-24 stsp err = view_resize(v);
929 25791caa 2018-10-24 stsp if (err)
930 25791caa 2018-10-24 stsp return err;
931 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
932 25791caa 2018-10-24 stsp if (err)
933 25791caa 2018-10-24 stsp return err;
934 cdfcfb03 2020-12-06 stsp if (v->child) {
935 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
936 cdfcfb03 2020-12-06 stsp if (err)
937 cdfcfb03 2020-12-06 stsp return err;
938 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
939 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
940 cdfcfb03 2020-12-06 stsp if (err)
941 cdfcfb03 2020-12-06 stsp return err;
942 cdfcfb03 2020-12-06 stsp }
943 25791caa 2018-10-24 stsp }
944 25791caa 2018-10-24 stsp }
945 25791caa 2018-10-24 stsp
946 e5a0f69f 2018-08-18 stsp switch (ch) {
947 1e37a5c2 2019-05-12 jcs case '\t':
948 1e37a5c2 2019-05-12 jcs if (view->child) {
949 e78dc838 2020-12-04 stsp view->focussed = 0;
950 e78dc838 2020-12-04 stsp view->child->focussed = 1;
951 e78dc838 2020-12-04 stsp view->focus_child = 1;
952 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
953 e78dc838 2020-12-04 stsp view->focussed = 0;
954 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
955 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
956 1e37a5c2 2019-05-12 jcs }
957 1e37a5c2 2019-05-12 jcs break;
958 1e37a5c2 2019-05-12 jcs case 'q':
959 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
960 9970f7fc 2020-12-03 stsp view->dying = 1;
961 1e37a5c2 2019-05-12 jcs break;
962 1e37a5c2 2019-05-12 jcs case 'Q':
963 1e37a5c2 2019-05-12 jcs *done = 1;
964 1e37a5c2 2019-05-12 jcs break;
965 1e37a5c2 2019-05-12 jcs case 'f':
966 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
967 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
968 1e37a5c2 2019-05-12 jcs break;
969 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
970 e78dc838 2020-12-04 stsp view->focussed = 0;
971 e78dc838 2020-12-04 stsp view->child->focussed = 1;
972 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
973 1e37a5c2 2019-05-12 jcs } else
974 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
975 1e37a5c2 2019-05-12 jcs if (err)
976 1e37a5c2 2019-05-12 jcs break;
977 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
978 9970f7fc 2020-12-03 stsp KEY_RESIZE);
979 1e37a5c2 2019-05-12 jcs } else {
980 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
981 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
982 e78dc838 2020-12-04 stsp view->focussed = 1;
983 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
984 1e37a5c2 2019-05-12 jcs } else {
985 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
986 669b5ffa 2018-10-07 stsp }
987 1e37a5c2 2019-05-12 jcs if (err)
988 1e37a5c2 2019-05-12 jcs break;
989 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
990 1e37a5c2 2019-05-12 jcs }
991 1e37a5c2 2019-05-12 jcs break;
992 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
993 60493ae3 2019-06-20 stsp break;
994 60493ae3 2019-06-20 stsp case '/':
995 60493ae3 2019-06-20 stsp if (view->search_start)
996 2b49a8ae 2019-06-22 stsp view_search_start(view);
997 60493ae3 2019-06-20 stsp else
998 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
999 1e37a5c2 2019-05-12 jcs break;
1000 b1bf1435 2019-06-21 stsp case 'N':
1001 60493ae3 2019-06-20 stsp case 'n':
1002 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1003 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1004 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1005 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1006 60493ae3 2019-06-20 stsp view->search_next(view);
1007 60493ae3 2019-06-20 stsp } else
1008 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1009 60493ae3 2019-06-20 stsp break;
1010 1e37a5c2 2019-05-12 jcs default:
1011 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1012 1e37a5c2 2019-05-12 jcs break;
1013 e5a0f69f 2018-08-18 stsp }
1014 e5a0f69f 2018-08-18 stsp
1015 e5a0f69f 2018-08-18 stsp return err;
1016 e5a0f69f 2018-08-18 stsp }
1017 e5a0f69f 2018-08-18 stsp
1018 1a57306a 2018-09-02 stsp void
1019 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1020 1a57306a 2018-09-02 stsp {
1021 0cf4efb1 2018-09-29 stsp PANEL *panel;
1022 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1023 0cf4efb1 2018-09-29 stsp
1024 669b5ffa 2018-10-07 stsp if (view->parent)
1025 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1026 669b5ffa 2018-10-07 stsp
1027 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1028 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1029 1a57306a 2018-09-02 stsp return;
1030 1a57306a 2018-09-02 stsp
1031 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1032 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1033 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1034 bcbd79e2 2018-08-19 stsp }
1035 bcbd79e2 2018-08-19 stsp
1036 a3404814 2018-09-02 stsp int
1037 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1038 a3404814 2018-09-02 stsp {
1039 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1040 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1041 669b5ffa 2018-10-07 stsp return 0;
1042 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1043 669b5ffa 2018-10-07 stsp return 0;
1044 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1045 a3404814 2018-09-02 stsp return 0;
1046 a3404814 2018-09-02 stsp
1047 669b5ffa 2018-10-07 stsp return view->focussed;
1048 a3404814 2018-09-02 stsp }
1049 a3404814 2018-09-02 stsp
1050 bcbd79e2 2018-08-19 stsp static const struct got_error *
1051 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1052 e5a0f69f 2018-08-18 stsp {
1053 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1054 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1055 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1056 fd823528 2018-10-22 stsp int fast_refresh = 10;
1057 1a76625f 2018-10-22 stsp int done = 0, errcode;
1058 e5a0f69f 2018-08-18 stsp
1059 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1060 1a76625f 2018-10-22 stsp if (errcode)
1061 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1062 1a76625f 2018-10-22 stsp
1063 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1064 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1065 e5a0f69f 2018-08-18 stsp
1066 1004088d 2018-09-29 stsp view->focussed = 1;
1067 878940b7 2018-09-29 stsp err = view->show(view);
1068 0cf4efb1 2018-09-29 stsp if (err)
1069 0cf4efb1 2018-09-29 stsp return err;
1070 0cf4efb1 2018-09-29 stsp update_panels();
1071 0cf4efb1 2018-09-29 stsp doupdate();
1072 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1073 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1074 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1075 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1076 fd823528 2018-10-22 stsp
1077 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1078 e5a0f69f 2018-08-18 stsp if (err)
1079 e5a0f69f 2018-08-18 stsp break;
1080 9970f7fc 2020-12-03 stsp if (view->dying) {
1081 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1082 669b5ffa 2018-10-07 stsp
1083 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1084 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1085 9970f7fc 2020-12-03 stsp entry);
1086 e78dc838 2020-12-04 stsp else if (view->parent)
1087 669b5ffa 2018-10-07 stsp prev = view->parent;
1088 669b5ffa 2018-10-07 stsp
1089 e78dc838 2020-12-04 stsp if (view->parent) {
1090 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1091 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1092 e78dc838 2020-12-04 stsp } else
1093 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1094 669b5ffa 2018-10-07 stsp
1095 9970f7fc 2020-12-03 stsp err = view_close(view);
1096 fb59748f 2020-12-05 stsp if (err)
1097 e5a0f69f 2018-08-18 stsp goto done;
1098 669b5ffa 2018-10-07 stsp
1099 e78dc838 2020-12-04 stsp view = NULL;
1100 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1101 e78dc838 2020-12-04 stsp if (v->focussed)
1102 e78dc838 2020-12-04 stsp break;
1103 0cf4efb1 2018-09-29 stsp }
1104 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1105 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1106 e78dc838 2020-12-04 stsp if (prev)
1107 e78dc838 2020-12-04 stsp view = prev;
1108 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1109 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1110 e78dc838 2020-12-04 stsp tog_view_list_head);
1111 e78dc838 2020-12-04 stsp }
1112 e78dc838 2020-12-04 stsp if (view) {
1113 e78dc838 2020-12-04 stsp if (view->focus_child) {
1114 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1115 e78dc838 2020-12-04 stsp view = view->child;
1116 e78dc838 2020-12-04 stsp } else
1117 e78dc838 2020-12-04 stsp view->focussed = 1;
1118 e78dc838 2020-12-04 stsp }
1119 e78dc838 2020-12-04 stsp }
1120 e5a0f69f 2018-08-18 stsp }
1121 bcbd79e2 2018-08-19 stsp if (new_view) {
1122 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1123 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1124 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1125 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1126 86c66b02 2018-10-18 stsp continue;
1127 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1128 86c66b02 2018-10-18 stsp err = view_close(v);
1129 86c66b02 2018-10-18 stsp if (err)
1130 86c66b02 2018-10-18 stsp goto done;
1131 86c66b02 2018-10-18 stsp break;
1132 86c66b02 2018-10-18 stsp }
1133 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1134 fed7eaa8 2018-10-24 stsp view = new_view;
1135 e78dc838 2020-12-04 stsp }
1136 669b5ffa 2018-10-07 stsp if (view) {
1137 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1138 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1139 e78dc838 2020-12-04 stsp view = view->child;
1140 e78dc838 2020-12-04 stsp } else {
1141 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1142 e78dc838 2020-12-04 stsp view = view->parent;
1143 1a76625f 2018-10-22 stsp }
1144 e78dc838 2020-12-04 stsp show_panel(view->panel);
1145 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1146 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1147 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1148 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1149 669b5ffa 2018-10-07 stsp if (err)
1150 1a76625f 2018-10-22 stsp goto done;
1151 669b5ffa 2018-10-07 stsp }
1152 669b5ffa 2018-10-07 stsp err = view->show(view);
1153 0cf4efb1 2018-09-29 stsp if (err)
1154 1a76625f 2018-10-22 stsp goto done;
1155 669b5ffa 2018-10-07 stsp if (view->child) {
1156 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1157 669b5ffa 2018-10-07 stsp if (err)
1158 1a76625f 2018-10-22 stsp goto done;
1159 669b5ffa 2018-10-07 stsp }
1160 1a76625f 2018-10-22 stsp update_panels();
1161 1a76625f 2018-10-22 stsp doupdate();
1162 0cf4efb1 2018-09-29 stsp }
1163 e5a0f69f 2018-08-18 stsp }
1164 e5a0f69f 2018-08-18 stsp done:
1165 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1166 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1167 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1168 e5a0f69f 2018-08-18 stsp view_close(view);
1169 e5a0f69f 2018-08-18 stsp }
1170 1a76625f 2018-10-22 stsp
1171 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1172 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1173 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1174 1a76625f 2018-10-22 stsp
1175 e5a0f69f 2018-08-18 stsp return err;
1176 ea5e7bb5 2018-08-01 stsp }
1177 ea5e7bb5 2018-08-01 stsp
1178 4ed7e80c 2018-05-20 stsp __dead static void
1179 9f7d7167 2018-04-29 stsp usage_log(void)
1180 9f7d7167 2018-04-29 stsp {
1181 80ddbec8 2018-04-29 stsp endwin();
1182 c70c5802 2018-08-01 stsp fprintf(stderr,
1183 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1184 9f7d7167 2018-04-29 stsp getprogname());
1185 9f7d7167 2018-04-29 stsp exit(1);
1186 80ddbec8 2018-04-29 stsp }
1187 80ddbec8 2018-04-29 stsp
1188 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1189 80ddbec8 2018-04-29 stsp static const struct got_error *
1190 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1191 963b370f 2018-05-20 stsp {
1192 00dfcb92 2018-06-11 stsp char *vis = NULL;
1193 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1194 963b370f 2018-05-20 stsp
1195 963b370f 2018-05-20 stsp *ws = NULL;
1196 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1197 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1198 00dfcb92 2018-06-11 stsp int vislen;
1199 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1200 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1201 00dfcb92 2018-06-11 stsp
1202 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1203 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1204 00dfcb92 2018-06-11 stsp if (err)
1205 00dfcb92 2018-06-11 stsp return err;
1206 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1207 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1208 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1209 a7f50699 2018-06-11 stsp goto done;
1210 a7f50699 2018-06-11 stsp }
1211 00dfcb92 2018-06-11 stsp }
1212 963b370f 2018-05-20 stsp
1213 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1214 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1216 a7f50699 2018-06-11 stsp goto done;
1217 a7f50699 2018-06-11 stsp }
1218 963b370f 2018-05-20 stsp
1219 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1220 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1221 a7f50699 2018-06-11 stsp done:
1222 00dfcb92 2018-06-11 stsp free(vis);
1223 963b370f 2018-05-20 stsp if (err) {
1224 963b370f 2018-05-20 stsp free(*ws);
1225 963b370f 2018-05-20 stsp *ws = NULL;
1226 963b370f 2018-05-20 stsp *wlen = 0;
1227 963b370f 2018-05-20 stsp }
1228 963b370f 2018-05-20 stsp return err;
1229 963b370f 2018-05-20 stsp }
1230 963b370f 2018-05-20 stsp
1231 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1232 963b370f 2018-05-20 stsp static const struct got_error *
1233 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1234 27a741e5 2019-09-11 stsp int col_tab_align)
1235 963b370f 2018-05-20 stsp {
1236 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1237 963b370f 2018-05-20 stsp int cols = 0;
1238 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1239 963b370f 2018-05-20 stsp size_t wlen;
1240 963b370f 2018-05-20 stsp int i;
1241 963b370f 2018-05-20 stsp
1242 963b370f 2018-05-20 stsp *wlinep = NULL;
1243 b700b5d6 2018-07-10 stsp *widthp = 0;
1244 963b370f 2018-05-20 stsp
1245 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1246 963b370f 2018-05-20 stsp if (err)
1247 963b370f 2018-05-20 stsp return err;
1248 963b370f 2018-05-20 stsp
1249 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1250 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1251 3f670bfb 2020-12-10 stsp wlen--;
1252 3f670bfb 2020-12-10 stsp }
1253 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1254 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1255 3f670bfb 2020-12-10 stsp wlen--;
1256 3f670bfb 2020-12-10 stsp }
1257 3f670bfb 2020-12-10 stsp
1258 963b370f 2018-05-20 stsp i = 0;
1259 27a741e5 2019-09-11 stsp while (i < wlen) {
1260 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1261 27a741e5 2019-09-11 stsp
1262 27a741e5 2019-09-11 stsp if (width == 0) {
1263 b700b5d6 2018-07-10 stsp i++;
1264 27a741e5 2019-09-11 stsp continue;
1265 27a741e5 2019-09-11 stsp }
1266 27a741e5 2019-09-11 stsp
1267 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1268 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1269 27a741e5 2019-09-11 stsp break;
1270 27a741e5 2019-09-11 stsp cols += width;
1271 3c1f04f1 2018-09-13 stsp i++;
1272 27a741e5 2019-09-11 stsp } else if (width == -1) {
1273 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1274 27a741e5 2019-09-11 stsp width = TABSIZE -
1275 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1276 748d5cab 2020-12-14 naddy } else {
1277 748d5cab 2020-12-14 naddy width = 1;
1278 748d5cab 2020-12-14 naddy wline[i] = L'.';
1279 27a741e5 2019-09-11 stsp }
1280 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1281 748d5cab 2020-12-14 naddy break;
1282 748d5cab 2020-12-14 naddy cols += width;
1283 b700b5d6 2018-07-10 stsp i++;
1284 27a741e5 2019-09-11 stsp } else {
1285 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1286 963b370f 2018-05-20 stsp goto done;
1287 963b370f 2018-05-20 stsp }
1288 963b370f 2018-05-20 stsp }
1289 963b370f 2018-05-20 stsp wline[i] = L'\0';
1290 b700b5d6 2018-07-10 stsp if (widthp)
1291 b700b5d6 2018-07-10 stsp *widthp = cols;
1292 963b370f 2018-05-20 stsp done:
1293 963b370f 2018-05-20 stsp if (err)
1294 963b370f 2018-05-20 stsp free(wline);
1295 963b370f 2018-05-20 stsp else
1296 963b370f 2018-05-20 stsp *wlinep = wline;
1297 963b370f 2018-05-20 stsp return err;
1298 963b370f 2018-05-20 stsp }
1299 963b370f 2018-05-20 stsp
1300 8b473291 2019-02-21 stsp static const struct got_error*
1301 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1302 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1303 8b473291 2019-02-21 stsp {
1304 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1305 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1306 8b473291 2019-02-21 stsp char *s;
1307 8b473291 2019-02-21 stsp const char *name;
1308 8b473291 2019-02-21 stsp
1309 8b473291 2019-02-21 stsp *refs_str = NULL;
1310 8b473291 2019-02-21 stsp
1311 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1312 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1313 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1314 52b5abe1 2019-08-13 stsp int cmp;
1315 52b5abe1 2019-08-13 stsp
1316 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1317 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1318 8b473291 2019-02-21 stsp continue;
1319 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1320 8b473291 2019-02-21 stsp name += 5;
1321 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1322 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1323 7143d404 2019-03-12 stsp continue;
1324 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1325 8b473291 2019-02-21 stsp name += 6;
1326 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1327 8b473291 2019-02-21 stsp name += 8;
1328 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1329 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1330 79cc719f 2020-04-24 stsp continue;
1331 79cc719f 2020-04-24 stsp }
1332 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1333 48cae60d 2020-09-22 stsp if (err)
1334 48cae60d 2020-09-22 stsp break;
1335 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1336 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1337 5d844a1e 2019-08-13 stsp if (err) {
1338 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1339 48cae60d 2020-09-22 stsp free(ref_id);
1340 5d844a1e 2019-08-13 stsp break;
1341 48cae60d 2020-09-22 stsp }
1342 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1343 5d844a1e 2019-08-13 stsp err = NULL;
1344 5d844a1e 2019-08-13 stsp tag = NULL;
1345 5d844a1e 2019-08-13 stsp }
1346 52b5abe1 2019-08-13 stsp }
1347 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1348 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1349 48cae60d 2020-09-22 stsp free(ref_id);
1350 52b5abe1 2019-08-13 stsp if (tag)
1351 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1352 52b5abe1 2019-08-13 stsp if (cmp != 0)
1353 52b5abe1 2019-08-13 stsp continue;
1354 8b473291 2019-02-21 stsp s = *refs_str;
1355 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1356 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1357 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1358 8b473291 2019-02-21 stsp free(s);
1359 8b473291 2019-02-21 stsp *refs_str = NULL;
1360 8b473291 2019-02-21 stsp break;
1361 8b473291 2019-02-21 stsp }
1362 8b473291 2019-02-21 stsp free(s);
1363 8b473291 2019-02-21 stsp }
1364 8b473291 2019-02-21 stsp
1365 8b473291 2019-02-21 stsp return err;
1366 8b473291 2019-02-21 stsp }
1367 8b473291 2019-02-21 stsp
1368 963b370f 2018-05-20 stsp static const struct got_error *
1369 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1370 27a741e5 2019-09-11 stsp int col_tab_align)
1371 5813d178 2019-03-09 stsp {
1372 e6b8b890 2020-12-29 naddy char *smallerthan;
1373 5813d178 2019-03-09 stsp
1374 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1375 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1376 5813d178 2019-03-09 stsp author = smallerthan + 1;
1377 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1378 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1379 5813d178 2019-03-09 stsp }
1380 5813d178 2019-03-09 stsp
1381 5813d178 2019-03-09 stsp static const struct got_error *
1382 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1383 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1384 8fdc79fe 2020-12-01 naddy int author_display_cols)
1385 80ddbec8 2018-04-29 stsp {
1386 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1387 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1388 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1389 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1390 5813d178 2019-03-09 stsp char *author = NULL;
1391 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1392 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1393 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1394 bb737323 2018-05-20 stsp int col, limit;
1395 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1396 ccb26ccd 2018-11-05 stsp struct tm tm;
1397 45d799e2 2018-12-23 stsp time_t committer_time;
1398 11b20872 2019-11-08 stsp struct tog_color *tc;
1399 80ddbec8 2018-04-29 stsp
1400 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1401 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1402 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1403 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1404 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1405 b39d25c7 2018-07-10 stsp
1406 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1407 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1408 b39d25c7 2018-07-10 stsp else
1409 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1410 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1411 11b20872 2019-11-08 stsp if (tc)
1412 11b20872 2019-11-08 stsp wattr_on(view->window,
1413 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1414 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1415 11b20872 2019-11-08 stsp if (tc)
1416 11b20872 2019-11-08 stsp wattr_off(view->window,
1417 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1418 27a741e5 2019-09-11 stsp col = limit;
1419 b39d25c7 2018-07-10 stsp if (col > avail)
1420 b39d25c7 2018-07-10 stsp goto done;
1421 6570a66d 2019-11-08 stsp
1422 6570a66d 2019-11-08 stsp if (avail >= 120) {
1423 6570a66d 2019-11-08 stsp char *id_str;
1424 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1425 6570a66d 2019-11-08 stsp if (err)
1426 6570a66d 2019-11-08 stsp goto done;
1427 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1428 11b20872 2019-11-08 stsp if (tc)
1429 11b20872 2019-11-08 stsp wattr_on(view->window,
1430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1431 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1432 11b20872 2019-11-08 stsp if (tc)
1433 11b20872 2019-11-08 stsp wattr_off(view->window,
1434 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1435 6570a66d 2019-11-08 stsp free(id_str);
1436 6570a66d 2019-11-08 stsp col += 9;
1437 6570a66d 2019-11-08 stsp if (col > avail)
1438 6570a66d 2019-11-08 stsp goto done;
1439 6570a66d 2019-11-08 stsp }
1440 b39d25c7 2018-07-10 stsp
1441 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1442 5813d178 2019-03-09 stsp if (author == NULL) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1444 80ddbec8 2018-04-29 stsp goto done;
1445 80ddbec8 2018-04-29 stsp }
1446 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1447 bb737323 2018-05-20 stsp if (err)
1448 bb737323 2018-05-20 stsp goto done;
1449 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1450 11b20872 2019-11-08 stsp if (tc)
1451 11b20872 2019-11-08 stsp wattr_on(view->window,
1452 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1453 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1454 11b20872 2019-11-08 stsp if (tc)
1455 11b20872 2019-11-08 stsp wattr_off(view->window,
1456 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1457 bb737323 2018-05-20 stsp col += author_width;
1458 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1459 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1460 bb737323 2018-05-20 stsp col++;
1461 bb737323 2018-05-20 stsp author_width++;
1462 bb737323 2018-05-20 stsp }
1463 9c2eaf34 2018-05-20 stsp if (col > avail)
1464 9c2eaf34 2018-05-20 stsp goto done;
1465 80ddbec8 2018-04-29 stsp
1466 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1467 5943eee2 2019-08-13 stsp if (err)
1468 6d9fbc00 2018-04-29 stsp goto done;
1469 bb737323 2018-05-20 stsp logmsg = logmsg0;
1470 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1471 bb737323 2018-05-20 stsp logmsg++;
1472 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1473 bb737323 2018-05-20 stsp if (newline)
1474 bb737323 2018-05-20 stsp *newline = '\0';
1475 bb737323 2018-05-20 stsp limit = avail - col;
1476 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1477 bb737323 2018-05-20 stsp if (err)
1478 bb737323 2018-05-20 stsp goto done;
1479 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1480 bb737323 2018-05-20 stsp col += logmsg_width;
1481 27a741e5 2019-09-11 stsp while (col < avail) {
1482 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1483 bb737323 2018-05-20 stsp col++;
1484 881b2d3e 2018-04-30 stsp }
1485 80ddbec8 2018-04-29 stsp done:
1486 80ddbec8 2018-04-29 stsp free(logmsg0);
1487 bb737323 2018-05-20 stsp free(wlogmsg);
1488 5813d178 2019-03-09 stsp free(author);
1489 bb737323 2018-05-20 stsp free(wauthor);
1490 80ddbec8 2018-04-29 stsp free(line);
1491 80ddbec8 2018-04-29 stsp return err;
1492 80ddbec8 2018-04-29 stsp }
1493 26ed57b2 2018-05-19 stsp
1494 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1495 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1496 899d86c2 2018-05-10 stsp struct got_object_id *id)
1497 80ddbec8 2018-04-29 stsp {
1498 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1499 80ddbec8 2018-04-29 stsp
1500 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1501 80ddbec8 2018-04-29 stsp if (entry == NULL)
1502 899d86c2 2018-05-10 stsp return NULL;
1503 99db9666 2018-05-07 stsp
1504 899d86c2 2018-05-10 stsp entry->id = id;
1505 99db9666 2018-05-07 stsp entry->commit = commit;
1506 899d86c2 2018-05-10 stsp return entry;
1507 99db9666 2018-05-07 stsp }
1508 80ddbec8 2018-04-29 stsp
1509 99db9666 2018-05-07 stsp static void
1510 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1511 99db9666 2018-05-07 stsp {
1512 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1513 99db9666 2018-05-07 stsp
1514 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1515 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1516 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1517 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1518 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1519 99db9666 2018-05-07 stsp free(entry);
1520 99db9666 2018-05-07 stsp }
1521 99db9666 2018-05-07 stsp
1522 99db9666 2018-05-07 stsp static void
1523 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1524 99db9666 2018-05-07 stsp {
1525 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1526 99db9666 2018-05-07 stsp pop_commit(commits);
1527 c4972b91 2018-05-07 stsp }
1528 c4972b91 2018-05-07 stsp
1529 c4972b91 2018-05-07 stsp static const struct got_error *
1530 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1531 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1532 13add988 2019-10-15 stsp {
1533 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1534 13add988 2019-10-15 stsp regmatch_t regmatch;
1535 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1536 13add988 2019-10-15 stsp
1537 13add988 2019-10-15 stsp *have_match = 0;
1538 13add988 2019-10-15 stsp
1539 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1540 13add988 2019-10-15 stsp if (err)
1541 13add988 2019-10-15 stsp return err;
1542 13add988 2019-10-15 stsp
1543 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1544 13add988 2019-10-15 stsp if (err)
1545 13add988 2019-10-15 stsp goto done;
1546 13add988 2019-10-15 stsp
1547 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1548 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1549 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1550 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1551 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1552 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1553 13add988 2019-10-15 stsp *have_match = 1;
1554 13add988 2019-10-15 stsp done:
1555 13add988 2019-10-15 stsp free(id_str);
1556 13add988 2019-10-15 stsp free(logmsg);
1557 13add988 2019-10-15 stsp return err;
1558 13add988 2019-10-15 stsp }
1559 13add988 2019-10-15 stsp
1560 13add988 2019-10-15 stsp static const struct got_error *
1561 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1562 c4972b91 2018-05-07 stsp {
1563 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1564 9ba79e04 2018-06-11 stsp
1565 1a76625f 2018-10-22 stsp /*
1566 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1567 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1568 1a76625f 2018-10-22 stsp * while updating the display.
1569 1a76625f 2018-10-22 stsp */
1570 4e0d2870 2020-12-07 naddy do {
1571 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1572 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1573 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1574 1a76625f 2018-10-22 stsp int errcode;
1575 899d86c2 2018-05-10 stsp
1576 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1577 4e0d2870 2020-12-07 naddy NULL, NULL);
1578 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1579 ecb28ae0 2018-07-16 stsp break;
1580 899d86c2 2018-05-10 stsp
1581 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1582 9ba79e04 2018-06-11 stsp if (err)
1583 9ba79e04 2018-06-11 stsp break;
1584 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1585 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1587 9ba79e04 2018-06-11 stsp break;
1588 9ba79e04 2018-06-11 stsp }
1589 93e45b7c 2018-09-24 stsp
1590 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1591 1a76625f 2018-10-22 stsp if (errcode) {
1592 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1593 13add988 2019-10-15 stsp "pthread_mutex_lock");
1594 1a76625f 2018-10-22 stsp break;
1595 1a76625f 2018-10-22 stsp }
1596 1a76625f 2018-10-22 stsp
1597 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1598 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1599 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1600 1a76625f 2018-10-22 stsp
1601 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1602 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1603 7c1452c1 2020-03-26 stsp int have_match;
1604 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1605 7c1452c1 2020-03-26 stsp if (err)
1606 7c1452c1 2020-03-26 stsp break;
1607 7c1452c1 2020-03-26 stsp if (have_match)
1608 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1609 13add988 2019-10-15 stsp }
1610 13add988 2019-10-15 stsp
1611 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1612 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1613 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1614 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1615 7c1452c1 2020-03-26 stsp if (err)
1616 13add988 2019-10-15 stsp break;
1617 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1618 899d86c2 2018-05-10 stsp
1619 9ba79e04 2018-06-11 stsp return err;
1620 0553a4e3 2018-04-30 stsp }
1621 0553a4e3 2018-04-30 stsp
1622 2b779855 2020-12-05 naddy static void
1623 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1624 2b779855 2020-12-05 naddy {
1625 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1626 2b779855 2020-12-05 naddy int ncommits = 0;
1627 2b779855 2020-12-05 naddy
1628 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1629 2b779855 2020-12-05 naddy while (entry) {
1630 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1631 2b779855 2020-12-05 naddy s->selected_entry = entry;
1632 2b779855 2020-12-05 naddy break;
1633 2b779855 2020-12-05 naddy }
1634 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1635 2b779855 2020-12-05 naddy ncommits++;
1636 2b779855 2020-12-05 naddy }
1637 2b779855 2020-12-05 naddy }
1638 2b779855 2020-12-05 naddy
1639 0553a4e3 2018-04-30 stsp static const struct got_error *
1640 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1641 0553a4e3 2018-04-30 stsp {
1642 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1643 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1644 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1645 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1646 60493ae3 2019-06-20 stsp int width;
1647 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1648 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1649 8b473291 2019-02-21 stsp char *refs_str = NULL;
1650 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1651 11b20872 2019-11-08 stsp struct tog_color *tc;
1652 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1653 0553a4e3 2018-04-30 stsp
1654 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1655 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1656 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1657 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1658 1a76625f 2018-10-22 stsp if (err)
1659 ecb28ae0 2018-07-16 stsp return err;
1660 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1661 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1662 d2075bf3 2020-12-25 stsp if (refs) {
1663 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1664 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1665 d2075bf3 2020-12-25 stsp if (err)
1666 d2075bf3 2020-12-25 stsp goto done;
1667 d2075bf3 2020-12-25 stsp }
1668 867c6645 2018-07-10 stsp }
1669 359bfafd 2019-02-22 stsp
1670 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1671 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1672 1a76625f 2018-10-22 stsp
1673 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1674 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1675 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1676 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1677 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1678 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1679 8f4ed634 2020-03-26 stsp goto done;
1680 8f4ed634 2020-03-26 stsp }
1681 8f4ed634 2020-03-26 stsp } else {
1682 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1683 f9686aa5 2020-03-27 stsp
1684 f9686aa5 2020-03-27 stsp if (view->searching) {
1685 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1686 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1687 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1688 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1689 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1690 f9686aa5 2020-03-27 stsp search_str = "searching...";
1691 f9686aa5 2020-03-27 stsp }
1692 f9686aa5 2020-03-27 stsp
1693 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1694 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1695 f9686aa5 2020-03-27 stsp search_str ? search_str :
1696 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1697 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1698 8f4ed634 2020-03-26 stsp goto done;
1699 8f4ed634 2020-03-26 stsp }
1700 8b473291 2019-02-21 stsp }
1701 1a76625f 2018-10-22 stsp
1702 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1703 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1704 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1705 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1706 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1707 1a76625f 2018-10-22 stsp header = NULL;
1708 1a76625f 2018-10-22 stsp goto done;
1709 1a76625f 2018-10-22 stsp }
1710 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1711 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1712 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1714 1a76625f 2018-10-22 stsp header = NULL;
1715 1a76625f 2018-10-22 stsp goto done;
1716 ecb28ae0 2018-07-16 stsp }
1717 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1718 1a76625f 2018-10-22 stsp if (err)
1719 1a76625f 2018-10-22 stsp goto done;
1720 867c6645 2018-07-10 stsp
1721 2814baeb 2018-08-01 stsp werase(view->window);
1722 867c6645 2018-07-10 stsp
1723 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1724 a3404814 2018-09-02 stsp wstandout(view->window);
1725 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1726 11b20872 2019-11-08 stsp if (tc)
1727 11b20872 2019-11-08 stsp wattr_on(view->window,
1728 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1729 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1730 11b20872 2019-11-08 stsp if (tc)
1731 11b20872 2019-11-08 stsp wattr_off(view->window,
1732 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1733 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1734 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1735 1a76625f 2018-10-22 stsp width++;
1736 1a76625f 2018-10-22 stsp }
1737 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1738 a3404814 2018-09-02 stsp wstandend(view->window);
1739 ecb28ae0 2018-07-16 stsp free(wline);
1740 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1741 1a76625f 2018-10-22 stsp goto done;
1742 0553a4e3 2018-04-30 stsp
1743 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1744 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1745 5813d178 2019-03-09 stsp ncommits = 0;
1746 5813d178 2019-03-09 stsp while (entry) {
1747 5813d178 2019-03-09 stsp char *author;
1748 5813d178 2019-03-09 stsp wchar_t *wauthor;
1749 5813d178 2019-03-09 stsp int width;
1750 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1751 5813d178 2019-03-09 stsp break;
1752 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1753 5813d178 2019-03-09 stsp if (author == NULL) {
1754 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1755 5813d178 2019-03-09 stsp goto done;
1756 5813d178 2019-03-09 stsp }
1757 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1758 27a741e5 2019-09-11 stsp date_display_cols);
1759 5813d178 2019-03-09 stsp if (author_cols < width)
1760 5813d178 2019-03-09 stsp author_cols = width;
1761 5813d178 2019-03-09 stsp free(wauthor);
1762 5813d178 2019-03-09 stsp free(author);
1763 7ca04879 2019-10-19 stsp ncommits++;
1764 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1765 5813d178 2019-03-09 stsp }
1766 5813d178 2019-03-09 stsp
1767 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1768 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1769 867c6645 2018-07-10 stsp ncommits = 0;
1770 899d86c2 2018-05-10 stsp while (entry) {
1771 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1772 899d86c2 2018-05-10 stsp break;
1773 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1774 2814baeb 2018-08-01 stsp wstandout(view->window);
1775 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1776 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1777 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1778 2814baeb 2018-08-01 stsp wstandend(view->window);
1779 0553a4e3 2018-04-30 stsp if (err)
1780 60493ae3 2019-06-20 stsp goto done;
1781 0553a4e3 2018-04-30 stsp ncommits++;
1782 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1783 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1784 80ddbec8 2018-04-29 stsp }
1785 80ddbec8 2018-04-29 stsp
1786 1a57306a 2018-09-02 stsp view_vborder(view);
1787 dd038bc6 2021-09-21 thomas.ad update_panels();
1788 dd038bc6 2021-09-21 thomas.ad doupdate();
1789 1a76625f 2018-10-22 stsp done:
1790 1a76625f 2018-10-22 stsp free(id_str);
1791 8b473291 2019-02-21 stsp free(refs_str);
1792 1a76625f 2018-10-22 stsp free(ncommits_str);
1793 1a76625f 2018-10-22 stsp free(header);
1794 80ddbec8 2018-04-29 stsp return err;
1795 9f7d7167 2018-04-29 stsp }
1796 07b55e75 2018-05-10 stsp
1797 07b55e75 2018-05-10 stsp static void
1798 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1799 07b55e75 2018-05-10 stsp {
1800 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1801 07b55e75 2018-05-10 stsp int nscrolled = 0;
1802 07b55e75 2018-05-10 stsp
1803 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1804 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1805 07b55e75 2018-05-10 stsp return;
1806 9f7d7167 2018-04-29 stsp
1807 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1808 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1809 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1810 07b55e75 2018-05-10 stsp if (entry) {
1811 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1812 07b55e75 2018-05-10 stsp nscrolled++;
1813 07b55e75 2018-05-10 stsp }
1814 07b55e75 2018-05-10 stsp }
1815 aa075928 2018-05-10 stsp }
1816 aa075928 2018-05-10 stsp
1817 aa075928 2018-05-10 stsp static const struct got_error *
1818 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1819 aa075928 2018-05-10 stsp {
1820 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1821 5e224a3e 2019-02-22 stsp int errcode;
1822 8a42fca8 2019-02-22 stsp
1823 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1824 aa075928 2018-05-10 stsp
1825 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1826 ffe38506 2020-12-01 naddy if (ta->log_complete)
1827 5e224a3e 2019-02-22 stsp break;
1828 b295e71b 2019-02-22 stsp
1829 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1830 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1831 7aafa0d1 2019-02-22 stsp if (errcode)
1832 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1833 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1834 7c1452c1 2020-03-26 stsp
1835 7c1452c1 2020-03-26 stsp /*
1836 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1837 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1838 7c1452c1 2020-03-26 stsp */
1839 7c1452c1 2020-03-26 stsp if (!wait)
1840 7c1452c1 2020-03-26 stsp break;
1841 7c1452c1 2020-03-26 stsp
1842 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1843 ffe38506 2020-12-01 naddy show_log_view(view);
1844 7c1452c1 2020-03-26 stsp update_panels();
1845 7c1452c1 2020-03-26 stsp doupdate();
1846 7c1452c1 2020-03-26 stsp
1847 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1848 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1849 82954512 2020-02-03 stsp if (errcode)
1850 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1851 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1852 82954512 2020-02-03 stsp
1853 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1854 ffe38506 2020-12-01 naddy show_log_view(view);
1855 7c1452c1 2020-03-26 stsp update_panels();
1856 7c1452c1 2020-03-26 stsp doupdate();
1857 5e224a3e 2019-02-22 stsp }
1858 5e224a3e 2019-02-22 stsp
1859 5e224a3e 2019-02-22 stsp return NULL;
1860 5e224a3e 2019-02-22 stsp }
1861 5e224a3e 2019-02-22 stsp
1862 5e224a3e 2019-02-22 stsp static const struct got_error *
1863 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1864 5e224a3e 2019-02-22 stsp {
1865 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1866 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1867 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1868 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1869 5e224a3e 2019-02-22 stsp
1870 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1871 5e224a3e 2019-02-22 stsp return NULL;
1872 5e224a3e 2019-02-22 stsp
1873 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1874 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1875 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1876 08ebd0a9 2019-02-22 stsp /*
1877 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1878 08ebd0a9 2019-02-22 stsp */
1879 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1880 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1881 5e224a3e 2019-02-22 stsp if (err)
1882 5e224a3e 2019-02-22 stsp return err;
1883 7aafa0d1 2019-02-22 stsp }
1884 b295e71b 2019-02-22 stsp
1885 7aafa0d1 2019-02-22 stsp do {
1886 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1887 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1888 88048b54 2019-02-21 stsp break;
1889 88048b54 2019-02-21 stsp
1890 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1891 aa075928 2018-05-10 stsp
1892 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1893 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1894 dd0a52c1 2018-05-20 stsp break;
1895 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1896 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1897 aa075928 2018-05-10 stsp
1898 dd0a52c1 2018-05-20 stsp return err;
1899 07b55e75 2018-05-10 stsp }
1900 4a7f7875 2018-05-10 stsp
1901 cd0acaa7 2018-05-20 stsp static const struct got_error *
1902 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1903 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1904 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1905 cd0acaa7 2018-05-20 stsp {
1906 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1907 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1908 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1909 cd0acaa7 2018-05-20 stsp
1910 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1911 15a94983 2018-12-23 stsp if (diff_view == NULL)
1912 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1913 ea5e7bb5 2018-08-01 stsp
1914 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1915 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1916 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1917 e5a0f69f 2018-08-18 stsp if (err == NULL)
1918 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1919 cd0acaa7 2018-05-20 stsp return err;
1920 4a7f7875 2018-05-10 stsp }
1921 4a7f7875 2018-05-10 stsp
1922 80ddbec8 2018-04-29 stsp static const struct got_error *
1923 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1924 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1925 9343a5fb 2018-06-23 stsp {
1926 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1927 941e9f74 2019-05-21 stsp
1928 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1929 941e9f74 2019-05-21 stsp if (parent == NULL)
1930 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1931 941e9f74 2019-05-21 stsp
1932 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1933 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1934 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1935 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1936 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1937 941e9f74 2019-05-21 stsp s->tree = subtree;
1938 941e9f74 2019-05-21 stsp s->selected = 0;
1939 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1940 941e9f74 2019-05-21 stsp return NULL;
1941 941e9f74 2019-05-21 stsp }
1942 941e9f74 2019-05-21 stsp
1943 941e9f74 2019-05-21 stsp static const struct got_error *
1944 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1945 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1946 941e9f74 2019-05-21 stsp {
1947 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1948 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1949 941e9f74 2019-05-21 stsp const char *p;
1950 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1951 9343a5fb 2018-06-23 stsp
1952 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1953 941e9f74 2019-05-21 stsp p = path;
1954 941e9f74 2019-05-21 stsp while (*p) {
1955 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1956 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1957 56e0773d 2019-11-28 stsp char *te_name;
1958 33cbf02b 2020-01-12 stsp
1959 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1960 33cbf02b 2020-01-12 stsp p++;
1961 941e9f74 2019-05-21 stsp
1962 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1963 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1964 941e9f74 2019-05-21 stsp if (slash == NULL)
1965 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1966 33cbf02b 2020-01-12 stsp else
1967 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1968 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1969 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1970 56e0773d 2019-11-28 stsp break;
1971 941e9f74 2019-05-21 stsp }
1972 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1973 56e0773d 2019-11-28 stsp if (te == NULL) {
1974 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1975 56e0773d 2019-11-28 stsp free(te_name);
1976 941e9f74 2019-05-21 stsp break;
1977 941e9f74 2019-05-21 stsp }
1978 56e0773d 2019-11-28 stsp free(te_name);
1979 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1980 941e9f74 2019-05-21 stsp
1981 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1982 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1983 b03c880f 2019-05-21 stsp
1984 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1985 941e9f74 2019-05-21 stsp if (slash)
1986 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1987 941e9f74 2019-05-21 stsp else
1988 941e9f74 2019-05-21 stsp subpath = strdup(path);
1989 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1990 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1991 941e9f74 2019-05-21 stsp break;
1992 941e9f74 2019-05-21 stsp }
1993 941e9f74 2019-05-21 stsp
1994 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
1995 941e9f74 2019-05-21 stsp subpath);
1996 941e9f74 2019-05-21 stsp if (err)
1997 941e9f74 2019-05-21 stsp break;
1998 941e9f74 2019-05-21 stsp
1999 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2000 941e9f74 2019-05-21 stsp free(tree_id);
2001 941e9f74 2019-05-21 stsp if (err)
2002 941e9f74 2019-05-21 stsp break;
2003 941e9f74 2019-05-21 stsp
2004 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2005 941e9f74 2019-05-21 stsp if (err) {
2006 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2007 941e9f74 2019-05-21 stsp break;
2008 941e9f74 2019-05-21 stsp }
2009 941e9f74 2019-05-21 stsp if (slash == NULL)
2010 941e9f74 2019-05-21 stsp break;
2011 941e9f74 2019-05-21 stsp free(subpath);
2012 941e9f74 2019-05-21 stsp subpath = NULL;
2013 941e9f74 2019-05-21 stsp p = slash;
2014 941e9f74 2019-05-21 stsp }
2015 941e9f74 2019-05-21 stsp
2016 941e9f74 2019-05-21 stsp free(subpath);
2017 1a76625f 2018-10-22 stsp return err;
2018 61266923 2020-01-14 stsp }
2019 61266923 2020-01-14 stsp
2020 61266923 2020-01-14 stsp static const struct got_error *
2021 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2022 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2023 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2024 55cccc34 2020-02-20 stsp {
2025 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2026 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2027 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2028 55cccc34 2020-02-20 stsp
2029 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2030 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2031 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2032 55cccc34 2020-02-20 stsp
2033 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2034 bc573f3b 2021-07-10 stsp if (err)
2035 55cccc34 2020-02-20 stsp return err;
2036 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2037 55cccc34 2020-02-20 stsp
2038 55cccc34 2020-02-20 stsp *new_view = tree_view;
2039 55cccc34 2020-02-20 stsp
2040 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2041 55cccc34 2020-02-20 stsp return NULL;
2042 55cccc34 2020-02-20 stsp
2043 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2044 55cccc34 2020-02-20 stsp }
2045 55cccc34 2020-02-20 stsp
2046 55cccc34 2020-02-20 stsp static const struct got_error *
2047 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2048 61266923 2020-01-14 stsp {
2049 61266923 2020-01-14 stsp sigset_t sigset;
2050 61266923 2020-01-14 stsp int errcode;
2051 61266923 2020-01-14 stsp
2052 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2053 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2054 61266923 2020-01-14 stsp
2055 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2056 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2057 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2058 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2059 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2060 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2061 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2062 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2063 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2064 61266923 2020-01-14 stsp
2065 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2066 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2067 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2068 61266923 2020-01-14 stsp
2069 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2070 61266923 2020-01-14 stsp if (errcode)
2071 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2072 61266923 2020-01-14 stsp
2073 61266923 2020-01-14 stsp return NULL;
2074 1a76625f 2018-10-22 stsp }
2075 1a76625f 2018-10-22 stsp
2076 1a76625f 2018-10-22 stsp static void *
2077 1a76625f 2018-10-22 stsp log_thread(void *arg)
2078 1a76625f 2018-10-22 stsp {
2079 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2080 1a76625f 2018-10-22 stsp int errcode = 0;
2081 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2082 1a76625f 2018-10-22 stsp int done = 0;
2083 61266923 2020-01-14 stsp
2084 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2085 61266923 2020-01-14 stsp if (err)
2086 61266923 2020-01-14 stsp return (void *)err;
2087 1a76625f 2018-10-22 stsp
2088 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2089 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2090 1a76625f 2018-10-22 stsp if (err) {
2091 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2092 1a76625f 2018-10-22 stsp return (void *)err;
2093 1a76625f 2018-10-22 stsp err = NULL;
2094 1a76625f 2018-10-22 stsp done = 1;
2095 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2096 1a76625f 2018-10-22 stsp a->commits_needed--;
2097 1a76625f 2018-10-22 stsp
2098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2099 3abe8080 2019-04-10 stsp if (errcode) {
2100 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2101 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2102 3abe8080 2019-04-10 stsp break;
2103 3abe8080 2019-04-10 stsp } else if (*a->quit)
2104 1a76625f 2018-10-22 stsp done = 1;
2105 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2106 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2107 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2108 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2109 1a76625f 2018-10-22 stsp }
2110 1a76625f 2018-10-22 stsp
2111 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2112 7c1452c1 2020-03-26 stsp if (errcode) {
2113 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2114 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2115 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2116 7c1452c1 2020-03-26 stsp break;
2117 7c1452c1 2020-03-26 stsp }
2118 7c1452c1 2020-03-26 stsp
2119 1a76625f 2018-10-22 stsp if (done)
2120 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2121 7c1452c1 2020-03-26 stsp else {
2122 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2123 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2124 7c1452c1 2020-03-26 stsp &tog_mutex);
2125 7c1452c1 2020-03-26 stsp if (errcode)
2126 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2127 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2128 21355643 2020-12-06 stsp if (*a->quit)
2129 21355643 2020-12-06 stsp done = 1;
2130 7c1452c1 2020-03-26 stsp }
2131 1a76625f 2018-10-22 stsp }
2132 1a76625f 2018-10-22 stsp
2133 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2134 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2135 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2136 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2137 1a76625f 2018-10-22 stsp }
2138 3abe8080 2019-04-10 stsp a->log_complete = 1;
2139 1a76625f 2018-10-22 stsp return (void *)err;
2140 1a76625f 2018-10-22 stsp }
2141 1a76625f 2018-10-22 stsp
2142 1a76625f 2018-10-22 stsp static const struct got_error *
2143 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2144 1a76625f 2018-10-22 stsp {
2145 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2146 1a76625f 2018-10-22 stsp int errcode;
2147 1a76625f 2018-10-22 stsp
2148 1a76625f 2018-10-22 stsp if (s->thread) {
2149 1a76625f 2018-10-22 stsp s->quit = 1;
2150 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2151 1a76625f 2018-10-22 stsp if (errcode)
2152 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2153 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2154 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2155 1a76625f 2018-10-22 stsp if (errcode)
2156 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2157 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2158 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2159 1a76625f 2018-10-22 stsp if (errcode)
2160 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2161 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2162 1a76625f 2018-10-22 stsp if (errcode)
2163 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2164 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2165 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2166 1a76625f 2018-10-22 stsp }
2167 1a76625f 2018-10-22 stsp
2168 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2169 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2170 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2171 1a76625f 2018-10-22 stsp }
2172 1a76625f 2018-10-22 stsp
2173 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2174 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2175 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2176 1a76625f 2018-10-22 stsp }
2177 1a76625f 2018-10-22 stsp
2178 9343a5fb 2018-06-23 stsp return err;
2179 9343a5fb 2018-06-23 stsp }
2180 9343a5fb 2018-06-23 stsp
2181 9343a5fb 2018-06-23 stsp static const struct got_error *
2182 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2183 1a76625f 2018-10-22 stsp {
2184 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2185 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2186 276b94a1 2020-11-13 naddy int errcode;
2187 1a76625f 2018-10-22 stsp
2188 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2189 276b94a1 2020-11-13 naddy
2190 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2191 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2192 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2193 276b94a1 2020-11-13 naddy
2194 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2195 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2196 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2197 276b94a1 2020-11-13 naddy
2198 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2199 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2200 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2201 1a76625f 2018-10-22 stsp free(s->start_id);
2202 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2203 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2204 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2205 1a76625f 2018-10-22 stsp return err;
2206 1a76625f 2018-10-22 stsp }
2207 1a76625f 2018-10-22 stsp
2208 1a76625f 2018-10-22 stsp static const struct got_error *
2209 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2210 60493ae3 2019-06-20 stsp {
2211 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2212 60493ae3 2019-06-20 stsp
2213 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2214 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2215 60493ae3 2019-06-20 stsp return NULL;
2216 60493ae3 2019-06-20 stsp }
2217 60493ae3 2019-06-20 stsp
2218 60493ae3 2019-06-20 stsp static const struct got_error *
2219 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2220 60493ae3 2019-06-20 stsp {
2221 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2222 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2223 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2224 60493ae3 2019-06-20 stsp
2225 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2226 f9686aa5 2020-03-27 stsp show_log_view(view);
2227 f9686aa5 2020-03-27 stsp update_panels();
2228 f9686aa5 2020-03-27 stsp doupdate();
2229 f9686aa5 2020-03-27 stsp
2230 96e2b566 2019-07-08 stsp if (s->search_entry) {
2231 13add988 2019-10-15 stsp int errcode, ch;
2232 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2233 13add988 2019-10-15 stsp if (errcode)
2234 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2235 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2236 13add988 2019-10-15 stsp ch = wgetch(view->window);
2237 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2238 13add988 2019-10-15 stsp if (errcode)
2239 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2240 13add988 2019-10-15 stsp "pthread_mutex_lock");
2241 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2242 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2243 678cbce5 2019-07-28 stsp return NULL;
2244 678cbce5 2019-07-28 stsp }
2245 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2246 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2247 96e2b566 2019-07-08 stsp else
2248 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2249 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2250 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2251 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2252 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2253 b1bf1435 2019-06-21 stsp else
2254 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2255 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2256 20be8d96 2019-06-21 stsp } else {
2257 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2258 20be8d96 2019-06-21 stsp }
2259 60493ae3 2019-06-20 stsp
2260 60493ae3 2019-06-20 stsp while (1) {
2261 13add988 2019-10-15 stsp int have_match = 0;
2262 13add988 2019-10-15 stsp
2263 60493ae3 2019-06-20 stsp if (entry == NULL) {
2264 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2265 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2266 f9967bca 2020-03-27 stsp view->search_next_done =
2267 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2268 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2269 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2270 f801134a 2019-06-25 stsp return NULL;
2271 60493ae3 2019-06-20 stsp }
2272 96e2b566 2019-07-08 stsp /*
2273 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2274 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2275 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2276 96e2b566 2019-07-08 stsp */
2277 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2278 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2279 60493ae3 2019-06-20 stsp }
2280 60493ae3 2019-06-20 stsp
2281 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2282 13add988 2019-10-15 stsp &view->regex);
2283 5943eee2 2019-08-13 stsp if (err)
2284 13add988 2019-10-15 stsp break;
2285 13add988 2019-10-15 stsp if (have_match) {
2286 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2287 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2288 60493ae3 2019-06-20 stsp break;
2289 60493ae3 2019-06-20 stsp }
2290 13add988 2019-10-15 stsp
2291 96e2b566 2019-07-08 stsp s->search_entry = entry;
2292 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2293 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2294 b1bf1435 2019-06-21 stsp else
2295 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2296 60493ae3 2019-06-20 stsp }
2297 60493ae3 2019-06-20 stsp
2298 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2299 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2300 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2301 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2302 60493ae3 2019-06-20 stsp if (err)
2303 60493ae3 2019-06-20 stsp return err;
2304 ead14cbe 2019-06-21 stsp cur++;
2305 ead14cbe 2019-06-21 stsp }
2306 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2307 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2308 60493ae3 2019-06-20 stsp if (err)
2309 60493ae3 2019-06-20 stsp return err;
2310 ead14cbe 2019-06-21 stsp cur--;
2311 60493ae3 2019-06-20 stsp }
2312 60493ae3 2019-06-20 stsp }
2313 60493ae3 2019-06-20 stsp
2314 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2315 96e2b566 2019-07-08 stsp
2316 60493ae3 2019-06-20 stsp return NULL;
2317 60493ae3 2019-06-20 stsp }
2318 60493ae3 2019-06-20 stsp
2319 60493ae3 2019-06-20 stsp static const struct got_error *
2320 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2321 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2322 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2323 80ddbec8 2018-04-29 stsp {
2324 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2325 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2326 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2327 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2328 1a76625f 2018-10-22 stsp int errcode;
2329 80ddbec8 2018-04-29 stsp
2330 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2331 f135c941 2020-02-20 stsp free(s->in_repo_path);
2332 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2333 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2334 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2335 f135c941 2020-02-20 stsp }
2336 ecb28ae0 2018-07-16 stsp
2337 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2338 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2339 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2340 78756c87 2020-11-24 stsp
2341 fb2756b9 2018-08-04 stsp s->repo = repo;
2342 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2343 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2344 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2345 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2346 9cd7cbd1 2020-12-07 stsp goto done;
2347 9cd7cbd1 2020-12-07 stsp }
2348 9cd7cbd1 2020-12-07 stsp }
2349 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2350 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2352 5036bf37 2018-09-24 stsp goto done;
2353 5036bf37 2018-09-24 stsp }
2354 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2355 e5a0f69f 2018-08-18 stsp
2356 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2357 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2358 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2359 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2360 11b20872 2019-11-08 stsp if (err)
2361 11b20872 2019-11-08 stsp goto done;
2362 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2363 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2364 11b20872 2019-11-08 stsp if (err) {
2365 11b20872 2019-11-08 stsp free_colors(&s->colors);
2366 11b20872 2019-11-08 stsp goto done;
2367 11b20872 2019-11-08 stsp }
2368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2370 11b20872 2019-11-08 stsp if (err) {
2371 11b20872 2019-11-08 stsp free_colors(&s->colors);
2372 11b20872 2019-11-08 stsp goto done;
2373 11b20872 2019-11-08 stsp }
2374 11b20872 2019-11-08 stsp }
2375 11b20872 2019-11-08 stsp
2376 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2377 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2378 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2379 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2380 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2381 1a76625f 2018-10-22 stsp
2382 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2383 1a76625f 2018-10-22 stsp if (err)
2384 1a76625f 2018-10-22 stsp goto done;
2385 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2386 b672a97a 2020-01-27 stsp !s->log_branches);
2387 1a76625f 2018-10-22 stsp if (err)
2388 1a76625f 2018-10-22 stsp goto done;
2389 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2390 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2391 c5b78334 2020-01-12 stsp if (err)
2392 c5b78334 2020-01-12 stsp goto done;
2393 1a76625f 2018-10-22 stsp
2394 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2395 1a76625f 2018-10-22 stsp if (errcode) {
2396 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2397 1a76625f 2018-10-22 stsp goto done;
2398 1a76625f 2018-10-22 stsp }
2399 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2400 7c1452c1 2020-03-26 stsp if (errcode) {
2401 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2402 7c1452c1 2020-03-26 stsp goto done;
2403 7c1452c1 2020-03-26 stsp }
2404 1a76625f 2018-10-22 stsp
2405 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2406 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2407 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2408 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2409 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2410 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2411 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2412 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2413 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2414 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2415 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2416 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2417 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2418 ba4f502b 2018-08-04 stsp done:
2419 1a76625f 2018-10-22 stsp if (err)
2420 1a76625f 2018-10-22 stsp close_log_view(view);
2421 ba4f502b 2018-08-04 stsp return err;
2422 ba4f502b 2018-08-04 stsp }
2423 ba4f502b 2018-08-04 stsp
2424 e5a0f69f 2018-08-18 stsp static const struct got_error *
2425 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2426 ba4f502b 2018-08-04 stsp {
2427 f2f6d207 2020-11-24 stsp const struct got_error *err;
2428 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2429 ba4f502b 2018-08-04 stsp
2430 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2431 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2432 2b380cc8 2018-10-24 stsp &s->thread_args);
2433 2b380cc8 2018-10-24 stsp if (errcode)
2434 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2435 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2436 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2437 f2f6d207 2020-11-24 stsp if (err)
2438 f2f6d207 2020-11-24 stsp return err;
2439 f2f6d207 2020-11-24 stsp }
2440 2b380cc8 2018-10-24 stsp }
2441 2b380cc8 2018-10-24 stsp
2442 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2443 e5a0f69f 2018-08-18 stsp }
2444 04cc582a 2018-08-01 stsp
2445 e5a0f69f 2018-08-18 stsp static const struct got_error *
2446 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2447 e5a0f69f 2018-08-18 stsp {
2448 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2449 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2450 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2451 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2452 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2453 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
2454 80ddbec8 2018-04-29 stsp
2455 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2456 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2457 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2458 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2459 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2460 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2461 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2462 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2463 fb280deb 2021-08-30 stsp select_commit(s);
2464 fb280deb 2021-08-30 stsp }
2465 528dedf3 2021-08-30 stsp return NULL;
2466 528dedf3 2021-08-30 stsp }
2467 528dedf3 2021-08-30 stsp
2468 528dedf3 2021-08-30 stsp switch (ch) {
2469 1e37a5c2 2019-05-12 jcs case 'q':
2470 1e37a5c2 2019-05-12 jcs s->quit = 1;
2471 1e37a5c2 2019-05-12 jcs break;
2472 1e37a5c2 2019-05-12 jcs case 'k':
2473 1e37a5c2 2019-05-12 jcs case KEY_UP:
2474 1e37a5c2 2019-05-12 jcs case '<':
2475 1e37a5c2 2019-05-12 jcs case ',':
2476 f7140bf5 2021-10-17 thomas case CTRL('p'):
2477 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2478 1a76625f 2018-10-22 stsp break;
2479 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2480 1e37a5c2 2019-05-12 jcs s->selected--;
2481 1144d21a 2019-06-21 stsp else
2482 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2483 912a3f79 2021-08-30 j select_commit(s);
2484 912a3f79 2021-08-30 j break;
2485 912a3f79 2021-08-30 j case 'g':
2486 912a3f79 2021-08-30 j case KEY_HOME:
2487 912a3f79 2021-08-30 j s->selected = 0;
2488 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2489 2b779855 2020-12-05 naddy select_commit(s);
2490 1e37a5c2 2019-05-12 jcs break;
2491 70f17a53 2022-06-13 thomas case CTRL('u'):
2492 70f17a53 2022-06-13 thomas nscroll /= 2;
2493 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2494 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2495 a4292ac5 2019-05-12 jcs case CTRL('b'):
2496 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2497 e5a0f69f 2018-08-18 stsp break;
2498 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2499 70f17a53 2022-06-13 thomas s->selected = MAX(0, s->selected - nscroll - 1);
2500 2b779855 2020-12-05 naddy else
2501 70f17a53 2022-06-13 thomas log_scroll_up(s, nscroll);
2502 2b779855 2020-12-05 naddy select_commit(s);
2503 1e37a5c2 2019-05-12 jcs break;
2504 1e37a5c2 2019-05-12 jcs case 'j':
2505 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2506 1e37a5c2 2019-05-12 jcs case '>':
2507 1e37a5c2 2019-05-12 jcs case '.':
2508 f7140bf5 2021-10-17 thomas case CTRL('n'):
2509 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2510 e5a0f69f 2018-08-18 stsp break;
2511 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2512 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2513 1e37a5c2 2019-05-12 jcs s->selected++;
2514 2b779855 2020-12-05 naddy else {
2515 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2516 2b779855 2020-12-05 naddy if (err)
2517 2b779855 2020-12-05 naddy break;
2518 912a3f79 2021-08-30 j }
2519 912a3f79 2021-08-30 j select_commit(s);
2520 912a3f79 2021-08-30 j break;
2521 912a3f79 2021-08-30 j case 'G':
2522 912a3f79 2021-08-30 j case KEY_END: {
2523 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2524 912a3f79 2021-08-30 j * traverse them all. */
2525 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2526 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2527 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2528 80ddbec8 2018-04-29 stsp }
2529 912a3f79 2021-08-30 j
2530 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2531 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2532 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2533 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2534 f3bc9f1d 2021-09-05 naddy break;
2535 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2536 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2537 f3bc9f1d 2021-09-05 naddy }
2538 f3bc9f1d 2021-09-05 naddy if (n > 0)
2539 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2540 2b779855 2020-12-05 naddy select_commit(s);
2541 1e37a5c2 2019-05-12 jcs break;
2542 912a3f79 2021-08-30 j }
2543 bccd1d5d 2022-06-13 thomas case CTRL('d'):
2544 70f17a53 2022-06-13 thomas nscroll /= 2;
2545 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2546 70f17a53 2022-06-13 thomas case KEY_NPAGE:
2547 70f17a53 2022-06-13 thomas case CTRL('f'): {
2548 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2549 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2550 1e37a5c2 2019-05-12 jcs if (first == NULL)
2551 e5a0f69f 2018-08-18 stsp break;
2552 70f17a53 2022-06-13 thomas err = log_scroll_down(view, nscroll);
2553 1cae65b4 2019-09-22 stsp if (err)
2554 1cae65b4 2019-09-22 stsp break;
2555 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2556 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2557 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2558 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2559 70f17a53 2022-06-13 thomas s->selected += MIN(s->last_displayed_entry->idx -
2560 70f17a53 2022-06-13 thomas s->selected_entry->idx, nscroll + 1);
2561 1e37a5c2 2019-05-12 jcs }
2562 2b779855 2020-12-05 naddy select_commit(s);
2563 1e37a5c2 2019-05-12 jcs break;
2564 1e37a5c2 2019-05-12 jcs }
2565 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2566 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2567 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2568 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2569 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2570 2b779855 2020-12-05 naddy select_commit(s);
2571 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2572 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2573 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2574 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2575 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2576 0bf7f153 2020-12-02 naddy }
2577 1e37a5c2 2019-05-12 jcs break;
2578 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2579 87c7274c 2019-05-12 jcs case ' ':
2580 1e37a5c2 2019-05-12 jcs case '\r':
2581 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2582 e5a0f69f 2018-08-18 stsp break;
2583 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2584 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2585 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2586 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2587 78756c87 2020-11-24 stsp view, s->repo);
2588 1e37a5c2 2019-05-12 jcs if (err)
2589 1e37a5c2 2019-05-12 jcs break;
2590 e78dc838 2020-12-04 stsp view->focussed = 0;
2591 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2592 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2593 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2594 f7013a22 2018-10-24 stsp if (err)
2595 1e37a5c2 2019-05-12 jcs return err;
2596 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2597 e78dc838 2020-12-04 stsp view->focus_child = 1;
2598 1e37a5c2 2019-05-12 jcs } else
2599 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2600 1e37a5c2 2019-05-12 jcs break;
2601 1e37a5c2 2019-05-12 jcs case 't':
2602 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2603 5036bf37 2018-09-24 stsp break;
2604 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2605 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2606 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2607 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2608 4e97c21c 2020-12-06 stsp s->repo);
2609 1e37a5c2 2019-05-12 jcs if (err)
2610 e5a0f69f 2018-08-18 stsp break;
2611 e78dc838 2020-12-04 stsp view->focussed = 0;
2612 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2613 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2614 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2615 1e37a5c2 2019-05-12 jcs if (err)
2616 1e37a5c2 2019-05-12 jcs return err;
2617 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2618 e78dc838 2020-12-04 stsp view->focus_child = 1;
2619 1e37a5c2 2019-05-12 jcs } else
2620 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2621 1e37a5c2 2019-05-12 jcs break;
2622 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2623 21355643 2020-12-06 stsp case CTRL('l'):
2624 21355643 2020-12-06 stsp case 'B':
2625 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2626 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2627 1e37a5c2 2019-05-12 jcs break;
2628 21355643 2020-12-06 stsp err = stop_log_thread(s);
2629 74cfe85e 2020-10-20 stsp if (err)
2630 74cfe85e 2020-10-20 stsp return err;
2631 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2632 21355643 2020-12-06 stsp char *parent_path;
2633 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2634 21355643 2020-12-06 stsp if (err)
2635 21355643 2020-12-06 stsp return err;
2636 21355643 2020-12-06 stsp free(s->in_repo_path);
2637 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2638 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2639 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2640 21355643 2020-12-06 stsp struct got_object_id *start_id;
2641 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2642 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2643 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2644 21355643 2020-12-06 stsp if (err)
2645 21355643 2020-12-06 stsp return err;
2646 21355643 2020-12-06 stsp free(s->start_id);
2647 21355643 2020-12-06 stsp s->start_id = start_id;
2648 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2649 21355643 2020-12-06 stsp } else /* 'B' */
2650 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2651 21355643 2020-12-06 stsp
2652 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2653 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2654 74cfe85e 2020-10-20 stsp if (err)
2655 21355643 2020-12-06 stsp return err;
2656 51a10b52 2020-12-26 stsp tog_free_refs();
2657 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2658 ca51c541 2020-12-07 stsp if (err)
2659 ca51c541 2020-12-07 stsp return err;
2660 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2661 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2662 d01904d4 2019-06-25 stsp if (err)
2663 d01904d4 2019-06-25 stsp return err;
2664 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2665 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2666 b672a97a 2020-01-27 stsp if (err)
2667 b672a97a 2020-01-27 stsp return err;
2668 21355643 2020-12-06 stsp free_commits(&s->commits);
2669 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2670 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2671 21355643 2020-12-06 stsp s->selected_entry = NULL;
2672 21355643 2020-12-06 stsp s->selected = 0;
2673 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2674 21355643 2020-12-06 stsp s->quit = 0;
2675 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2676 d01904d4 2019-06-25 stsp break;
2677 6458efa5 2020-11-24 stsp case 'r':
2678 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2679 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2680 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2681 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2682 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2683 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2684 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2685 6458efa5 2020-11-24 stsp if (err) {
2686 6458efa5 2020-11-24 stsp view_close(ref_view);
2687 6458efa5 2020-11-24 stsp return err;
2688 6458efa5 2020-11-24 stsp }
2689 e78dc838 2020-12-04 stsp view->focussed = 0;
2690 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2691 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2692 6458efa5 2020-11-24 stsp err = view_close_child(view);
2693 6458efa5 2020-11-24 stsp if (err)
2694 6458efa5 2020-11-24 stsp return err;
2695 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2696 e78dc838 2020-12-04 stsp view->focus_child = 1;
2697 6458efa5 2020-11-24 stsp } else
2698 6458efa5 2020-11-24 stsp *new_view = ref_view;
2699 6458efa5 2020-11-24 stsp break;
2700 1e37a5c2 2019-05-12 jcs default:
2701 1e37a5c2 2019-05-12 jcs break;
2702 899d86c2 2018-05-10 stsp }
2703 e5a0f69f 2018-08-18 stsp
2704 80ddbec8 2018-04-29 stsp return err;
2705 80ddbec8 2018-04-29 stsp }
2706 80ddbec8 2018-04-29 stsp
2707 4ed7e80c 2018-05-20 stsp static const struct got_error *
2708 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2709 c2db6724 2019-01-04 stsp {
2710 c2db6724 2019-01-04 stsp const struct got_error *error;
2711 c2db6724 2019-01-04 stsp
2712 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2713 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2714 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2715 37c06ea4 2019-07-15 stsp #endif
2716 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2717 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2718 c2db6724 2019-01-04 stsp
2719 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2720 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2721 c2db6724 2019-01-04 stsp
2722 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2723 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2724 c2db6724 2019-01-04 stsp
2725 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2726 c2db6724 2019-01-04 stsp if (error != NULL)
2727 c2db6724 2019-01-04 stsp return error;
2728 c2db6724 2019-01-04 stsp
2729 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2730 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2731 c2db6724 2019-01-04 stsp
2732 c2db6724 2019-01-04 stsp return NULL;
2733 c2db6724 2019-01-04 stsp }
2734 c2db6724 2019-01-04 stsp
2735 a915003a 2019-02-05 stsp static void
2736 a915003a 2019-02-05 stsp init_curses(void)
2737 a915003a 2019-02-05 stsp {
2738 296152d1 2022-05-31 thomas /*
2739 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
2740 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
2741 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
2742 296152d1 2022-05-31 thomas */
2743 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
2744 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
2745 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
2746 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
2747 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
2748 296152d1 2022-05-31 thomas
2749 a915003a 2019-02-05 stsp initscr();
2750 a915003a 2019-02-05 stsp cbreak();
2751 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2752 a915003a 2019-02-05 stsp noecho();
2753 a915003a 2019-02-05 stsp nonl();
2754 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2755 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2756 a915003a 2019-02-05 stsp curs_set(0);
2757 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2758 6d17833f 2019-11-08 stsp start_color();
2759 6d17833f 2019-11-08 stsp use_default_colors();
2760 6d17833f 2019-11-08 stsp }
2761 a915003a 2019-02-05 stsp }
2762 a915003a 2019-02-05 stsp
2763 c2db6724 2019-01-04 stsp static const struct got_error *
2764 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2765 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2766 f135c941 2020-02-20 stsp {
2767 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2768 f135c941 2020-02-20 stsp
2769 f135c941 2020-02-20 stsp if (argc == 0) {
2770 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2771 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2772 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2773 f135c941 2020-02-20 stsp return NULL;
2774 f135c941 2020-02-20 stsp }
2775 f135c941 2020-02-20 stsp
2776 f135c941 2020-02-20 stsp if (worktree) {
2777 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2778 bfd61697 2020-11-14 stsp char *p;
2779 f135c941 2020-02-20 stsp
2780 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2781 f135c941 2020-02-20 stsp if (err)
2782 f135c941 2020-02-20 stsp return err;
2783 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2784 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2785 bfd61697 2020-11-14 stsp p) == -1) {
2786 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2787 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2788 f135c941 2020-02-20 stsp }
2789 f135c941 2020-02-20 stsp free(p);
2790 f135c941 2020-02-20 stsp } else
2791 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2792 f135c941 2020-02-20 stsp
2793 f135c941 2020-02-20 stsp return err;
2794 f135c941 2020-02-20 stsp }
2795 f135c941 2020-02-20 stsp
2796 f135c941 2020-02-20 stsp static const struct got_error *
2797 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2798 9f7d7167 2018-04-29 stsp {
2799 80ddbec8 2018-04-29 stsp const struct got_error *error;
2800 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2801 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2802 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2803 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2804 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2805 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2806 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2807 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2808 04cc582a 2018-08-01 stsp struct tog_view *view;
2809 80ddbec8 2018-04-29 stsp
2810 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2811 80ddbec8 2018-04-29 stsp switch (ch) {
2812 b672a97a 2020-01-27 stsp case 'b':
2813 b672a97a 2020-01-27 stsp log_branches = 1;
2814 b672a97a 2020-01-27 stsp break;
2815 80ddbec8 2018-04-29 stsp case 'c':
2816 80ddbec8 2018-04-29 stsp start_commit = optarg;
2817 80ddbec8 2018-04-29 stsp break;
2818 ecb28ae0 2018-07-16 stsp case 'r':
2819 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2820 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2821 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2822 9ba1d308 2019-10-21 stsp optarg);
2823 ecb28ae0 2018-07-16 stsp break;
2824 80ddbec8 2018-04-29 stsp default:
2825 17020d27 2019-03-07 stsp usage_log();
2826 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2827 80ddbec8 2018-04-29 stsp }
2828 80ddbec8 2018-04-29 stsp }
2829 80ddbec8 2018-04-29 stsp
2830 80ddbec8 2018-04-29 stsp argc -= optind;
2831 80ddbec8 2018-04-29 stsp argv += optind;
2832 80ddbec8 2018-04-29 stsp
2833 f135c941 2020-02-20 stsp if (argc > 1)
2834 f135c941 2020-02-20 stsp usage_log();
2835 963f97a1 2019-03-18 stsp
2836 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2837 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2838 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2839 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2840 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2841 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2842 c156c7a4 2020-12-18 stsp goto done;
2843 a1fbf39a 2019-08-11 stsp if (worktree)
2844 6962eb72 2020-02-20 stsp repo_path =
2845 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2846 a1fbf39a 2019-08-11 stsp else
2847 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2848 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2849 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2850 c156c7a4 2020-12-18 stsp goto done;
2851 c156c7a4 2020-12-18 stsp }
2852 ecb28ae0 2018-07-16 stsp }
2853 ecb28ae0 2018-07-16 stsp
2854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2855 80ddbec8 2018-04-29 stsp if (error != NULL)
2856 ecb28ae0 2018-07-16 stsp goto done;
2857 80ddbec8 2018-04-29 stsp
2858 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2859 f135c941 2020-02-20 stsp repo, worktree);
2860 f135c941 2020-02-20 stsp if (error)
2861 f135c941 2020-02-20 stsp goto done;
2862 f135c941 2020-02-20 stsp
2863 f135c941 2020-02-20 stsp init_curses();
2864 f135c941 2020-02-20 stsp
2865 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2866 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2867 c02c541e 2019-03-29 stsp if (error)
2868 c02c541e 2019-03-29 stsp goto done;
2869 c02c541e 2019-03-29 stsp
2870 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2871 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2872 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2873 87670572 2020-12-26 naddy if (error)
2874 87670572 2020-12-26 naddy goto done;
2875 87670572 2020-12-26 naddy }
2876 51a10b52 2020-12-26 stsp
2877 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2878 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2879 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2880 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2881 d8f38dc4 2020-12-05 stsp if (error)
2882 d8f38dc4 2020-12-05 stsp goto done;
2883 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2884 d8f38dc4 2020-12-05 stsp } else {
2885 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2886 d8f38dc4 2020-12-05 stsp if (error == NULL)
2887 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2888 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2889 d8f38dc4 2020-12-05 stsp goto done;
2890 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2891 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2892 d8f38dc4 2020-12-05 stsp if (error)
2893 d8f38dc4 2020-12-05 stsp goto done;
2894 d8f38dc4 2020-12-05 stsp }
2895 8b473291 2019-02-21 stsp
2896 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2897 04cc582a 2018-08-01 stsp if (view == NULL) {
2898 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2899 04cc582a 2018-08-01 stsp goto done;
2900 04cc582a 2018-08-01 stsp }
2901 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2902 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2903 ba4f502b 2018-08-04 stsp if (error)
2904 ba4f502b 2018-08-04 stsp goto done;
2905 2fc00ff4 2019-08-31 stsp if (worktree) {
2906 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2907 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2908 2fc00ff4 2019-08-31 stsp worktree = NULL;
2909 2fc00ff4 2019-08-31 stsp }
2910 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2911 ecb28ae0 2018-07-16 stsp done:
2912 f135c941 2020-02-20 stsp free(in_repo_path);
2913 ecb28ae0 2018-07-16 stsp free(repo_path);
2914 ecb28ae0 2018-07-16 stsp free(cwd);
2915 899d86c2 2018-05-10 stsp free(start_id);
2916 d8f38dc4 2020-12-05 stsp free(label);
2917 d8f38dc4 2020-12-05 stsp if (ref)
2918 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2919 1d0f4054 2021-06-17 stsp if (repo) {
2920 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2921 1d0f4054 2021-06-17 stsp if (error == NULL)
2922 1d0f4054 2021-06-17 stsp error = close_err;
2923 1d0f4054 2021-06-17 stsp }
2924 ec142235 2019-03-07 stsp if (worktree)
2925 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2926 51a10b52 2020-12-26 stsp tog_free_refs();
2927 80ddbec8 2018-04-29 stsp return error;
2928 9f7d7167 2018-04-29 stsp }
2929 9f7d7167 2018-04-29 stsp
2930 4ed7e80c 2018-05-20 stsp __dead static void
2931 9f7d7167 2018-04-29 stsp usage_diff(void)
2932 9f7d7167 2018-04-29 stsp {
2933 80ddbec8 2018-04-29 stsp endwin();
2934 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2935 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2936 9f7d7167 2018-04-29 stsp exit(1);
2937 b304db33 2018-05-20 stsp }
2938 b304db33 2018-05-20 stsp
2939 6d17833f 2019-11-08 stsp static int
2940 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2941 41605754 2020-11-12 stsp regmatch_t *regmatch)
2942 6d17833f 2019-11-08 stsp {
2943 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2944 6d17833f 2019-11-08 stsp }
2945 6d17833f 2019-11-08 stsp
2946 f26dddb7 2019-11-08 stsp struct tog_color *
2947 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2948 6d17833f 2019-11-08 stsp {
2949 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2950 6d17833f 2019-11-08 stsp
2951 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2952 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2953 6d17833f 2019-11-08 stsp return tc;
2954 6d17833f 2019-11-08 stsp }
2955 6d17833f 2019-11-08 stsp
2956 6d17833f 2019-11-08 stsp return NULL;
2957 6d17833f 2019-11-08 stsp }
2958 6d17833f 2019-11-08 stsp
2959 4ed7e80c 2018-05-20 stsp static const struct got_error *
2960 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2961 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2962 41605754 2020-11-12 stsp {
2963 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2964 41605754 2020-11-12 stsp wchar_t *wline;
2965 41605754 2020-11-12 stsp int width;
2966 41605754 2020-11-12 stsp char *s;
2967 41605754 2020-11-12 stsp
2968 41605754 2020-11-12 stsp *wtotal = 0;
2969 41605754 2020-11-12 stsp
2970 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2971 41605754 2020-11-12 stsp if (s == NULL)
2972 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2973 41605754 2020-11-12 stsp
2974 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2975 41605754 2020-11-12 stsp if (err) {
2976 41605754 2020-11-12 stsp free(s);
2977 41605754 2020-11-12 stsp return err;
2978 41605754 2020-11-12 stsp }
2979 41605754 2020-11-12 stsp waddwstr(window, wline);
2980 41605754 2020-11-12 stsp free(wline);
2981 41605754 2020-11-12 stsp free(s);
2982 41605754 2020-11-12 stsp wlimit -= width;
2983 41605754 2020-11-12 stsp *wtotal += width;
2984 41605754 2020-11-12 stsp
2985 41605754 2020-11-12 stsp if (wlimit > 0) {
2986 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2987 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2988 41605754 2020-11-12 stsp if (s == NULL) {
2989 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2990 41605754 2020-11-12 stsp free(s);
2991 41605754 2020-11-12 stsp return err;
2992 41605754 2020-11-12 stsp }
2993 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2994 41605754 2020-11-12 stsp if (err) {
2995 41605754 2020-11-12 stsp free(s);
2996 41605754 2020-11-12 stsp return err;
2997 41605754 2020-11-12 stsp }
2998 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2999 41605754 2020-11-12 stsp waddwstr(window, wline);
3000 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
3001 41605754 2020-11-12 stsp free(wline);
3002 41605754 2020-11-12 stsp free(s);
3003 41605754 2020-11-12 stsp wlimit -= width;
3004 41605754 2020-11-12 stsp *wtotal += width;
3005 41605754 2020-11-12 stsp }
3006 41605754 2020-11-12 stsp
3007 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3008 41605754 2020-11-12 stsp err = format_line(&wline, &width,
3009 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
3010 41605754 2020-11-12 stsp if (err)
3011 41605754 2020-11-12 stsp return err;
3012 41605754 2020-11-12 stsp waddwstr(window, wline);
3013 41605754 2020-11-12 stsp free(wline);
3014 41605754 2020-11-12 stsp *wtotal += width;
3015 41605754 2020-11-12 stsp }
3016 41605754 2020-11-12 stsp
3017 41605754 2020-11-12 stsp return NULL;
3018 41605754 2020-11-12 stsp }
3019 41605754 2020-11-12 stsp
3020 41605754 2020-11-12 stsp static const struct got_error *
3021 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3022 26ed57b2 2018-05-19 stsp {
3023 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3024 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3025 61e69b96 2018-05-20 stsp const struct got_error *err;
3026 fe621944 2020-11-10 stsp int nprinted = 0;
3027 b304db33 2018-05-20 stsp char *line;
3028 826082fe 2020-12-10 stsp size_t linesize = 0;
3029 826082fe 2020-12-10 stsp ssize_t linelen;
3030 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3031 61e69b96 2018-05-20 stsp wchar_t *wline;
3032 e0b650dd 2018-05-20 stsp int width;
3033 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3034 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3035 fe621944 2020-11-10 stsp off_t line_offset;
3036 26ed57b2 2018-05-19 stsp
3037 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3038 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3039 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3040 fe621944 2020-11-10 stsp
3041 f7d12f7e 2018-08-01 stsp werase(view->window);
3042 a3404814 2018-09-02 stsp
3043 a3404814 2018-09-02 stsp if (header) {
3044 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3045 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3046 135a2da0 2020-11-11 stsp header) == -1)
3047 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3048 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3049 135a2da0 2020-11-11 stsp free(line);
3050 135a2da0 2020-11-11 stsp if (err)
3051 a3404814 2018-09-02 stsp return err;
3052 a3404814 2018-09-02 stsp
3053 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3054 a3404814 2018-09-02 stsp wstandout(view->window);
3055 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3056 e54cc94a 2020-11-11 stsp free(wline);
3057 e54cc94a 2020-11-11 stsp wline = NULL;
3058 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3059 a3404814 2018-09-02 stsp wstandend(view->window);
3060 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3061 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3062 26ed57b2 2018-05-19 stsp
3063 a3404814 2018-09-02 stsp if (max_lines <= 1)
3064 a3404814 2018-09-02 stsp return NULL;
3065 a3404814 2018-09-02 stsp max_lines--;
3066 a3404814 2018-09-02 stsp }
3067 a3404814 2018-09-02 stsp
3068 89f1a395 2020-12-01 naddy s->eof = 0;
3069 826082fe 2020-12-10 stsp line = NULL;
3070 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3071 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3072 826082fe 2020-12-10 stsp if (linelen == -1) {
3073 826082fe 2020-12-10 stsp if (feof(s->f)) {
3074 826082fe 2020-12-10 stsp s->eof = 1;
3075 826082fe 2020-12-10 stsp break;
3076 826082fe 2020-12-10 stsp }
3077 826082fe 2020-12-10 stsp free(line);
3078 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3079 61e69b96 2018-05-20 stsp }
3080 6d17833f 2019-11-08 stsp
3081 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3082 f26dddb7 2019-11-08 stsp if (tc)
3083 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3084 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3085 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3086 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3087 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3088 41605754 2020-11-12 stsp view->window, regmatch);
3089 41605754 2020-11-12 stsp if (err) {
3090 41605754 2020-11-12 stsp free(line);
3091 41605754 2020-11-12 stsp return err;
3092 41605754 2020-11-12 stsp }
3093 41605754 2020-11-12 stsp } else {
3094 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3095 41605754 2020-11-12 stsp if (err) {
3096 41605754 2020-11-12 stsp free(line);
3097 41605754 2020-11-12 stsp return err;
3098 41605754 2020-11-12 stsp }
3099 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3100 41605754 2020-11-12 stsp free(wline);
3101 41605754 2020-11-12 stsp wline = NULL;
3102 41605754 2020-11-12 stsp }
3103 f26dddb7 2019-11-08 stsp if (tc)
3104 6d17833f 2019-11-08 stsp wattr_off(view->window,
3105 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3106 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3107 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3108 fe621944 2020-11-10 stsp nprinted++;
3109 826082fe 2020-12-10 stsp }
3110 826082fe 2020-12-10 stsp free(line);
3111 fe621944 2020-11-10 stsp if (nprinted >= 1)
3112 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3113 89f1a395 2020-12-01 naddy (nprinted - 1);
3114 fe621944 2020-11-10 stsp else
3115 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3116 26ed57b2 2018-05-19 stsp
3117 1a57306a 2018-09-02 stsp view_vborder(view);
3118 c3e9aa98 2019-05-13 jcs
3119 89f1a395 2020-12-01 naddy if (s->eof) {
3120 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3121 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3122 c3e9aa98 2019-05-13 jcs nprinted++;
3123 c3e9aa98 2019-05-13 jcs }
3124 c3e9aa98 2019-05-13 jcs
3125 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3126 c3e9aa98 2019-05-13 jcs if (err) {
3127 c3e9aa98 2019-05-13 jcs return err;
3128 c3e9aa98 2019-05-13 jcs }
3129 26ed57b2 2018-05-19 stsp
3130 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3131 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3132 e54cc94a 2020-11-11 stsp free(wline);
3133 e54cc94a 2020-11-11 stsp wline = NULL;
3134 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3135 c3e9aa98 2019-05-13 jcs }
3136 c3e9aa98 2019-05-13 jcs
3137 26ed57b2 2018-05-19 stsp return NULL;
3138 abd2672a 2018-12-23 stsp }
3139 abd2672a 2018-12-23 stsp
3140 abd2672a 2018-12-23 stsp static char *
3141 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3142 abd2672a 2018-12-23 stsp {
3143 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3144 09867e48 2019-08-13 stsp char *p, *s;
3145 09867e48 2019-08-13 stsp
3146 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3147 09867e48 2019-08-13 stsp if (tm == NULL)
3148 09867e48 2019-08-13 stsp return NULL;
3149 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3150 09867e48 2019-08-13 stsp if (s == NULL)
3151 09867e48 2019-08-13 stsp return NULL;
3152 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3153 abd2672a 2018-12-23 stsp if (p)
3154 abd2672a 2018-12-23 stsp *p = '\0';
3155 abd2672a 2018-12-23 stsp return s;
3156 9f7d7167 2018-04-29 stsp }
3157 9f7d7167 2018-04-29 stsp
3158 4ed7e80c 2018-05-20 stsp static const struct got_error *
3159 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3160 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3161 0208f208 2020-05-05 stsp {
3162 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3163 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3164 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3165 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3166 0208f208 2020-05-05 stsp
3167 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3168 0208f208 2020-05-05 stsp if (qid != NULL) {
3169 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3170 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3171 ec242592 2022-04-22 thomas &qid->id);
3172 0208f208 2020-05-05 stsp if (err)
3173 0208f208 2020-05-05 stsp return err;
3174 0208f208 2020-05-05 stsp
3175 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3176 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3177 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3178 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3179 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3180 aa8b5dd0 2021-08-01 stsp }
3181 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3182 0208f208 2020-05-05 stsp
3183 0208f208 2020-05-05 stsp }
3184 0208f208 2020-05-05 stsp
3185 0208f208 2020-05-05 stsp if (tree_id1) {
3186 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3187 0208f208 2020-05-05 stsp if (err)
3188 0208f208 2020-05-05 stsp goto done;
3189 0208f208 2020-05-05 stsp }
3190 0208f208 2020-05-05 stsp
3191 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3192 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3193 0208f208 2020-05-05 stsp if (err)
3194 0208f208 2020-05-05 stsp goto done;
3195 0208f208 2020-05-05 stsp
3196 a0f32f33 2022-06-13 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3197 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3198 0208f208 2020-05-05 stsp done:
3199 0208f208 2020-05-05 stsp if (tree1)
3200 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3201 0208f208 2020-05-05 stsp if (tree2)
3202 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3203 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3204 0208f208 2020-05-05 stsp return err;
3205 0208f208 2020-05-05 stsp }
3206 0208f208 2020-05-05 stsp
3207 0208f208 2020-05-05 stsp static const struct got_error *
3208 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3209 abd2672a 2018-12-23 stsp {
3210 fe621944 2020-11-10 stsp off_t *p;
3211 fe621944 2020-11-10 stsp
3212 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3213 fe621944 2020-11-10 stsp if (p == NULL)
3214 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3215 fe621944 2020-11-10 stsp *line_offsets = p;
3216 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3217 fe621944 2020-11-10 stsp (*nlines)++;
3218 fe621944 2020-11-10 stsp return NULL;
3219 fe621944 2020-11-10 stsp }
3220 fe621944 2020-11-10 stsp
3221 fe621944 2020-11-10 stsp static const struct got_error *
3222 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3223 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3224 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3225 fe621944 2020-11-10 stsp {
3226 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3227 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3228 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3229 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3230 45d799e2 2018-12-23 stsp time_t committer_time;
3231 45d799e2 2018-12-23 stsp const char *author, *committer;
3232 8b473291 2019-02-21 stsp char *refs_str = NULL;
3233 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3234 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3235 fe621944 2020-11-10 stsp off_t outoff = 0;
3236 fe621944 2020-11-10 stsp int n;
3237 abd2672a 2018-12-23 stsp
3238 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3239 0208f208 2020-05-05 stsp
3240 8b473291 2019-02-21 stsp if (refs) {
3241 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3242 8b473291 2019-02-21 stsp if (err)
3243 8b473291 2019-02-21 stsp return err;
3244 8b473291 2019-02-21 stsp }
3245 8b473291 2019-02-21 stsp
3246 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3247 abd2672a 2018-12-23 stsp if (err)
3248 abd2672a 2018-12-23 stsp return err;
3249 abd2672a 2018-12-23 stsp
3250 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3251 15a94983 2018-12-23 stsp if (err) {
3252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3253 15a94983 2018-12-23 stsp goto done;
3254 15a94983 2018-12-23 stsp }
3255 abd2672a 2018-12-23 stsp
3256 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3257 fe621944 2020-11-10 stsp if (err)
3258 fe621944 2020-11-10 stsp goto done;
3259 fe621944 2020-11-10 stsp
3260 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3261 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3262 fe621944 2020-11-10 stsp if (n < 0) {
3263 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3264 abd2672a 2018-12-23 stsp goto done;
3265 abd2672a 2018-12-23 stsp }
3266 fe621944 2020-11-10 stsp outoff += n;
3267 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3268 fe621944 2020-11-10 stsp if (err)
3269 fe621944 2020-11-10 stsp goto done;
3270 fe621944 2020-11-10 stsp
3271 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3272 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3273 fe621944 2020-11-10 stsp if (n < 0) {
3274 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3275 abd2672a 2018-12-23 stsp goto done;
3276 abd2672a 2018-12-23 stsp }
3277 fe621944 2020-11-10 stsp outoff += n;
3278 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3279 fe621944 2020-11-10 stsp if (err)
3280 fe621944 2020-11-10 stsp goto done;
3281 fe621944 2020-11-10 stsp
3282 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3283 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3284 fe621944 2020-11-10 stsp if (datestr) {
3285 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3286 fe621944 2020-11-10 stsp if (n < 0) {
3287 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3288 fe621944 2020-11-10 stsp goto done;
3289 fe621944 2020-11-10 stsp }
3290 fe621944 2020-11-10 stsp outoff += n;
3291 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3292 fe621944 2020-11-10 stsp if (err)
3293 fe621944 2020-11-10 stsp goto done;
3294 abd2672a 2018-12-23 stsp }
3295 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3296 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3297 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3298 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3299 fe621944 2020-11-10 stsp if (n < 0) {
3300 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3301 fe621944 2020-11-10 stsp goto done;
3302 fe621944 2020-11-10 stsp }
3303 fe621944 2020-11-10 stsp outoff += n;
3304 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3305 fe621944 2020-11-10 stsp if (err)
3306 fe621944 2020-11-10 stsp goto done;
3307 abd2672a 2018-12-23 stsp }
3308 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3309 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3310 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3311 ac4dc263 2021-09-24 thomas int pn = 1;
3312 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3313 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3314 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3315 ac4dc263 2021-09-24 thomas if (err)
3316 ac4dc263 2021-09-24 thomas goto done;
3317 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3318 ac4dc263 2021-09-24 thomas if (n < 0) {
3319 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3320 ac4dc263 2021-09-24 thomas goto done;
3321 ac4dc263 2021-09-24 thomas }
3322 ac4dc263 2021-09-24 thomas outoff += n;
3323 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3324 ac4dc263 2021-09-24 thomas if (err)
3325 ac4dc263 2021-09-24 thomas goto done;
3326 ac4dc263 2021-09-24 thomas free(id_str);
3327 ac4dc263 2021-09-24 thomas id_str = NULL;
3328 ac4dc263 2021-09-24 thomas }
3329 ac4dc263 2021-09-24 thomas }
3330 ac4dc263 2021-09-24 thomas
3331 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3332 5943eee2 2019-08-13 stsp if (err)
3333 5943eee2 2019-08-13 stsp goto done;
3334 fe621944 2020-11-10 stsp s = logmsg;
3335 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3336 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3337 fe621944 2020-11-10 stsp if (n < 0) {
3338 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3339 fe621944 2020-11-10 stsp goto done;
3340 fe621944 2020-11-10 stsp }
3341 fe621944 2020-11-10 stsp outoff += n;
3342 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3343 fe621944 2020-11-10 stsp if (err)
3344 fe621944 2020-11-10 stsp goto done;
3345 abd2672a 2018-12-23 stsp }
3346 fe621944 2020-11-10 stsp
3347 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3348 0208f208 2020-05-05 stsp if (err)
3349 0208f208 2020-05-05 stsp goto done;
3350 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3351 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3352 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3353 fe621944 2020-11-10 stsp if (n < 0) {
3354 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3355 fe621944 2020-11-10 stsp goto done;
3356 fe621944 2020-11-10 stsp }
3357 fe621944 2020-11-10 stsp outoff += n;
3358 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3359 fe621944 2020-11-10 stsp if (err)
3360 fe621944 2020-11-10 stsp goto done;
3361 0208f208 2020-05-05 stsp free((char *)pe->path);
3362 0208f208 2020-05-05 stsp free(pe->data);
3363 0208f208 2020-05-05 stsp }
3364 fe621944 2020-11-10 stsp
3365 0208f208 2020-05-05 stsp fputc('\n', outfile);
3366 fe621944 2020-11-10 stsp outoff++;
3367 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3368 abd2672a 2018-12-23 stsp done:
3369 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3370 abd2672a 2018-12-23 stsp free(id_str);
3371 5943eee2 2019-08-13 stsp free(logmsg);
3372 8b473291 2019-02-21 stsp free(refs_str);
3373 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3374 7510f233 2020-08-09 stsp if (err) {
3375 ae6a6978 2020-08-09 stsp free(*line_offsets);
3376 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3377 ae6a6978 2020-08-09 stsp *nlines = 0;
3378 7510f233 2020-08-09 stsp }
3379 fe621944 2020-11-10 stsp return err;
3380 abd2672a 2018-12-23 stsp }
3381 abd2672a 2018-12-23 stsp
3382 abd2672a 2018-12-23 stsp static const struct got_error *
3383 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3384 26ed57b2 2018-05-19 stsp {
3385 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3386 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3387 15a94983 2018-12-23 stsp int obj_type;
3388 fe621944 2020-11-10 stsp
3389 fe621944 2020-11-10 stsp free(s->line_offsets);
3390 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3391 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3392 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3393 fe621944 2020-11-10 stsp s->nlines = 0;
3394 26ed57b2 2018-05-19 stsp
3395 511a516b 2018-05-19 stsp f = got_opentemp();
3396 48ae06ee 2018-10-18 stsp if (f == NULL) {
3397 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3398 48ae06ee 2018-10-18 stsp goto done;
3399 48ae06ee 2018-10-18 stsp }
3400 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3401 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3402 fb43ecf1 2019-02-11 stsp goto done;
3403 fb43ecf1 2019-02-11 stsp }
3404 48ae06ee 2018-10-18 stsp s->f = f;
3405 26ed57b2 2018-05-19 stsp
3406 15a94983 2018-12-23 stsp if (s->id1)
3407 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3408 15a94983 2018-12-23 stsp else
3409 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3410 15a94983 2018-12-23 stsp if (err)
3411 15a94983 2018-12-23 stsp goto done;
3412 15a94983 2018-12-23 stsp
3413 15a94983 2018-12-23 stsp switch (obj_type) {
3414 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3415 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3416 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3417 a0f32f33 2022-06-13 thomas s->diff_context, s->ignore_whitespace, s->force_text_diff,
3418 a0f32f33 2022-06-13 thomas s->repo, s->f);
3419 26ed57b2 2018-05-19 stsp break;
3420 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3421 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3422 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3423 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3424 26ed57b2 2018-05-19 stsp break;
3425 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3426 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3427 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3428 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3429 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3430 abd2672a 2018-12-23 stsp
3431 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3432 abd2672a 2018-12-23 stsp if (err)
3433 3ffacbe1 2020-02-02 tracey goto done;
3434 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3435 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3436 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3437 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3438 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3439 f44b1f58 2020-02-02 tracey if (err)
3440 f44b1f58 2020-02-02 tracey goto done;
3441 f44b1f58 2020-02-02 tracey } else {
3442 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3443 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3444 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3445 fe621944 2020-11-10 stsp err = write_commit_info(
3446 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3447 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3448 f44b1f58 2020-02-02 tracey if (err)
3449 f44b1f58 2020-02-02 tracey goto done;
3450 f5404e4e 2020-02-02 tracey break;
3451 15a087fe 2019-02-21 stsp }
3452 abd2672a 2018-12-23 stsp }
3453 abd2672a 2018-12-23 stsp }
3454 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3455 abd2672a 2018-12-23 stsp
3456 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3457 a0f32f33 2022-06-13 thomas s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3458 a0f32f33 2022-06-13 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3459 26ed57b2 2018-05-19 stsp break;
3460 abd2672a 2018-12-23 stsp }
3461 26ed57b2 2018-05-19 stsp default:
3462 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3463 48ae06ee 2018-10-18 stsp break;
3464 26ed57b2 2018-05-19 stsp }
3465 f44b1f58 2020-02-02 tracey if (err)
3466 f44b1f58 2020-02-02 tracey goto done;
3467 48ae06ee 2018-10-18 stsp done:
3468 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3469 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3470 48ae06ee 2018-10-18 stsp return err;
3471 48ae06ee 2018-10-18 stsp }
3472 26ed57b2 2018-05-19 stsp
3473 f5215bb9 2019-02-22 stsp static void
3474 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3475 f5215bb9 2019-02-22 stsp {
3476 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3477 f5215bb9 2019-02-22 stsp update_panels();
3478 f5215bb9 2019-02-22 stsp doupdate();
3479 f44b1f58 2020-02-02 tracey }
3480 f44b1f58 2020-02-02 tracey
3481 f44b1f58 2020-02-02 tracey static const struct got_error *
3482 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3483 f44b1f58 2020-02-02 tracey {
3484 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3485 f44b1f58 2020-02-02 tracey
3486 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3487 f44b1f58 2020-02-02 tracey return NULL;
3488 f44b1f58 2020-02-02 tracey }
3489 f44b1f58 2020-02-02 tracey
3490 f44b1f58 2020-02-02 tracey static const struct got_error *
3491 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3492 f44b1f58 2020-02-02 tracey {
3493 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3494 f44b1f58 2020-02-02 tracey int lineno;
3495 826082fe 2020-12-10 stsp char *line = NULL;
3496 826082fe 2020-12-10 stsp size_t linesize = 0;
3497 826082fe 2020-12-10 stsp ssize_t linelen;
3498 f44b1f58 2020-02-02 tracey
3499 f44b1f58 2020-02-02 tracey if (!view->searching) {
3500 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3501 f44b1f58 2020-02-02 tracey return NULL;
3502 f44b1f58 2020-02-02 tracey }
3503 f44b1f58 2020-02-02 tracey
3504 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3505 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3506 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3507 f44b1f58 2020-02-02 tracey else
3508 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3509 4b3f9dac 2021-12-17 thomas } else
3510 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3511 f44b1f58 2020-02-02 tracey
3512 f44b1f58 2020-02-02 tracey while (1) {
3513 f44b1f58 2020-02-02 tracey off_t offset;
3514 f44b1f58 2020-02-02 tracey
3515 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3516 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3517 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3518 f44b1f58 2020-02-02 tracey break;
3519 f44b1f58 2020-02-02 tracey }
3520 f44b1f58 2020-02-02 tracey
3521 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3522 f44b1f58 2020-02-02 tracey lineno = 1;
3523 f44b1f58 2020-02-02 tracey else
3524 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3525 f44b1f58 2020-02-02 tracey }
3526 f44b1f58 2020-02-02 tracey
3527 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3528 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3529 f44b1f58 2020-02-02 tracey free(line);
3530 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3531 f44b1f58 2020-02-02 tracey }
3532 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3533 826082fe 2020-12-10 stsp if (linelen != -1 &&
3534 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3535 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3536 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3537 f44b1f58 2020-02-02 tracey break;
3538 f44b1f58 2020-02-02 tracey }
3539 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3540 f44b1f58 2020-02-02 tracey lineno++;
3541 f44b1f58 2020-02-02 tracey else
3542 f44b1f58 2020-02-02 tracey lineno--;
3543 f44b1f58 2020-02-02 tracey }
3544 826082fe 2020-12-10 stsp free(line);
3545 f44b1f58 2020-02-02 tracey
3546 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3547 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3548 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3549 f44b1f58 2020-02-02 tracey }
3550 f44b1f58 2020-02-02 tracey
3551 f44b1f58 2020-02-02 tracey return NULL;
3552 f5215bb9 2019-02-22 stsp }
3553 f5215bb9 2019-02-22 stsp
3554 48ae06ee 2018-10-18 stsp static const struct got_error *
3555 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
3556 a0f32f33 2022-06-13 thomas {
3557 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
3558 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
3559 a0f32f33 2022-06-13 thomas
3560 a0f32f33 2022-06-13 thomas free(s->id1);
3561 a0f32f33 2022-06-13 thomas s->id1 = NULL;
3562 a0f32f33 2022-06-13 thomas free(s->id2);
3563 a0f32f33 2022-06-13 thomas s->id2 = NULL;
3564 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
3565 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3566 a0f32f33 2022-06-13 thomas s->f = NULL;
3567 a0f32f33 2022-06-13 thomas if (s->f1 && fclose(s->f1) == EOF)
3568 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3569 a0f32f33 2022-06-13 thomas s->f1 = NULL;
3570 a0f32f33 2022-06-13 thomas if (s->f2 && fclose(s->f2) == EOF)
3571 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
3572 a0f32f33 2022-06-13 thomas s->f2 = NULL;
3573 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
3574 a0f32f33 2022-06-13 thomas free(s->line_offsets);
3575 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
3576 a0f32f33 2022-06-13 thomas s->nlines = 0;
3577 a0f32f33 2022-06-13 thomas return err;
3578 a0f32f33 2022-06-13 thomas }
3579 a0f32f33 2022-06-13 thomas
3580 a0f32f33 2022-06-13 thomas static const struct got_error *
3581 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3582 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3583 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3584 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3585 48ae06ee 2018-10-18 stsp {
3586 48ae06ee 2018-10-18 stsp const struct got_error *err;
3587 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3588 5dc9f4bc 2018-08-04 stsp
3589 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
3590 a0f32f33 2022-06-13 thomas
3591 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3592 15a94983 2018-12-23 stsp int type1, type2;
3593 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3594 15a94983 2018-12-23 stsp if (err)
3595 15a94983 2018-12-23 stsp return err;
3596 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3597 15a94983 2018-12-23 stsp if (err)
3598 15a94983 2018-12-23 stsp return err;
3599 15a94983 2018-12-23 stsp
3600 15a94983 2018-12-23 stsp if (type1 != type2)
3601 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3602 15a94983 2018-12-23 stsp }
3603 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3604 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3605 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3606 f44b1f58 2020-02-02 tracey s->repo = repo;
3607 f44b1f58 2020-02-02 tracey s->id1 = id1;
3608 f44b1f58 2020-02-02 tracey s->id2 = id2;
3609 3dbaef42 2020-11-24 stsp s->label1 = label1;
3610 3dbaef42 2020-11-24 stsp s->label2 = label2;
3611 48ae06ee 2018-10-18 stsp
3612 15a94983 2018-12-23 stsp if (id1) {
3613 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3614 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3615 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3616 a0f32f33 2022-06-13 thomas s->f1 = got_opentemp();
3617 a0f32f33 2022-06-13 thomas if (s->f1 == NULL) {
3618 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3619 a0f32f33 2022-06-13 thomas goto done;
3620 a0f32f33 2022-06-13 thomas }
3621 48ae06ee 2018-10-18 stsp } else
3622 5465d566 2020-02-01 tracey s->id1 = NULL;
3623 48ae06ee 2018-10-18 stsp
3624 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3625 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3626 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
3627 a0f32f33 2022-06-13 thomas goto done;
3628 48ae06ee 2018-10-18 stsp }
3629 a0f32f33 2022-06-13 thomas
3630 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
3631 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
3632 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
3633 a0f32f33 2022-06-13 thomas goto done;
3634 a0f32f33 2022-06-13 thomas }
3635 a0f32f33 2022-06-13 thomas
3636 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3637 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3638 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3639 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3640 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3641 5465d566 2020-02-01 tracey s->log_view = log_view;
3642 5465d566 2020-02-01 tracey s->repo = repo;
3643 6d17833f 2019-11-08 stsp
3644 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3645 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3646 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3647 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3648 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3649 6d17833f 2019-11-08 stsp if (err)
3650 a0f32f33 2022-06-13 thomas goto done;
3651 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3652 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3653 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3654 a0f32f33 2022-06-13 thomas if (err)
3655 a0f32f33 2022-06-13 thomas goto done;
3656 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3657 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3658 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3659 a0f32f33 2022-06-13 thomas if (err)
3660 a0f32f33 2022-06-13 thomas goto done;
3661 6d17833f 2019-11-08 stsp
3662 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3663 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3664 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3665 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3666 a0f32f33 2022-06-13 thomas if (err)
3667 a0f32f33 2022-06-13 thomas goto done;
3668 11b20872 2019-11-08 stsp
3669 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3670 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3671 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3672 a0f32f33 2022-06-13 thomas if (err)
3673 a0f32f33 2022-06-13 thomas goto done;
3674 11b20872 2019-11-08 stsp
3675 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3676 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3677 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3678 a0f32f33 2022-06-13 thomas if (err)
3679 a0f32f33 2022-06-13 thomas goto done;
3680 6d17833f 2019-11-08 stsp }
3681 5dc9f4bc 2018-08-04 stsp
3682 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3683 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3684 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3685 f5215bb9 2019-02-22 stsp
3686 5465d566 2020-02-01 tracey err = create_diff(s);
3687 48ae06ee 2018-10-18 stsp
3688 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3689 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3690 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3691 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3692 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3693 a0f32f33 2022-06-13 thomas done:
3694 a0f32f33 2022-06-13 thomas if (err)
3695 a0f32f33 2022-06-13 thomas close_diff_view(view);
3696 e5a0f69f 2018-08-18 stsp return err;
3697 5dc9f4bc 2018-08-04 stsp }
3698 5dc9f4bc 2018-08-04 stsp
3699 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3700 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3701 5dc9f4bc 2018-08-04 stsp {
3702 a3404814 2018-09-02 stsp const struct got_error *err;
3703 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3704 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3705 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3706 a3404814 2018-09-02 stsp
3707 a3404814 2018-09-02 stsp if (s->id1) {
3708 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3709 a3404814 2018-09-02 stsp if (err)
3710 a3404814 2018-09-02 stsp return err;
3711 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3712 3dbaef42 2020-11-24 stsp } else
3713 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3714 3dbaef42 2020-11-24 stsp
3715 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3716 a3404814 2018-09-02 stsp if (err)
3717 a3404814 2018-09-02 stsp return err;
3718 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3719 26ed57b2 2018-05-19 stsp
3720 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3721 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3722 a3404814 2018-09-02 stsp free(id_str1);
3723 a3404814 2018-09-02 stsp free(id_str2);
3724 a3404814 2018-09-02 stsp return err;
3725 a3404814 2018-09-02 stsp }
3726 a3404814 2018-09-02 stsp free(id_str1);
3727 a3404814 2018-09-02 stsp free(id_str2);
3728 a3404814 2018-09-02 stsp
3729 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3730 267bb3b8 2021-08-01 stsp free(header);
3731 267bb3b8 2021-08-01 stsp return err;
3732 15a087fe 2019-02-21 stsp }
3733 15a087fe 2019-02-21 stsp
3734 15a087fe 2019-02-21 stsp static const struct got_error *
3735 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3736 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3737 15a087fe 2019-02-21 stsp {
3738 d7a04538 2019-02-21 stsp const struct got_error *err;
3739 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3740 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3741 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3742 15a087fe 2019-02-21 stsp
3743 15a087fe 2019-02-21 stsp free(s->id2);
3744 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3745 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3746 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3747 15a087fe 2019-02-21 stsp
3748 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3749 d7a04538 2019-02-21 stsp if (err)
3750 d7a04538 2019-02-21 stsp return err;
3751 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3752 15a087fe 2019-02-21 stsp free(s->id1);
3753 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3754 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3755 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3756 15a087fe 2019-02-21 stsp return NULL;
3757 0cf4efb1 2018-09-29 stsp }
3758 0cf4efb1 2018-09-29 stsp
3759 0cf4efb1 2018-09-29 stsp static const struct got_error *
3760 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3761 e5a0f69f 2018-08-18 stsp {
3762 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3763 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3764 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3765 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3766 826082fe 2020-12-10 stsp char *line = NULL;
3767 826082fe 2020-12-10 stsp size_t linesize = 0;
3768 826082fe 2020-12-10 stsp ssize_t linelen;
3769 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
3770 e5a0f69f 2018-08-18 stsp
3771 e5a0f69f 2018-08-18 stsp switch (ch) {
3772 64453f7e 2020-11-21 stsp case 'a':
3773 3dbaef42 2020-11-24 stsp case 'w':
3774 3dbaef42 2020-11-24 stsp if (ch == 'a')
3775 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3776 3dbaef42 2020-11-24 stsp if (ch == 'w')
3777 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3778 64453f7e 2020-11-21 stsp wclear(view->window);
3779 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3780 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3781 aa61903a 2021-12-31 thomas s->matched_line = 0;
3782 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3783 64453f7e 2020-11-21 stsp err = create_diff(s);
3784 912a3f79 2021-08-30 j break;
3785 912a3f79 2021-08-30 j case 'g':
3786 912a3f79 2021-08-30 j case KEY_HOME:
3787 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3788 64453f7e 2020-11-21 stsp break;
3789 912a3f79 2021-08-30 j case 'G':
3790 912a3f79 2021-08-30 j case KEY_END:
3791 912a3f79 2021-08-30 j if (s->eof)
3792 912a3f79 2021-08-30 j break;
3793 912a3f79 2021-08-30 j
3794 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3795 912a3f79 2021-08-30 j s->eof = 1;
3796 912a3f79 2021-08-30 j break;
3797 1e37a5c2 2019-05-12 jcs case 'k':
3798 1e37a5c2 2019-05-12 jcs case KEY_UP:
3799 f7140bf5 2021-10-17 thomas case CTRL('p'):
3800 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3801 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3802 1e37a5c2 2019-05-12 jcs break;
3803 70f17a53 2022-06-13 thomas case CTRL('u'):
3804 70f17a53 2022-06-13 thomas nscroll /= 2;
3805 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3806 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3807 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3808 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3809 26ed57b2 2018-05-19 stsp break;
3810 1e37a5c2 2019-05-12 jcs i = 0;
3811 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
3812 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3813 1e37a5c2 2019-05-12 jcs break;
3814 1e37a5c2 2019-05-12 jcs case 'j':
3815 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3816 f7140bf5 2021-10-17 thomas case CTRL('n'):
3817 1e37a5c2 2019-05-12 jcs if (!s->eof)
3818 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3819 1e37a5c2 2019-05-12 jcs break;
3820 70f17a53 2022-06-13 thomas case CTRL('d'):
3821 70f17a53 2022-06-13 thomas nscroll /= 2;
3822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3823 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3824 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3825 1e37a5c2 2019-05-12 jcs case ' ':
3826 00ba99a7 2019-05-12 jcs if (s->eof)
3827 1e37a5c2 2019-05-12 jcs break;
3828 1e37a5c2 2019-05-12 jcs i = 0;
3829 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
3830 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3831 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3832 826082fe 2020-12-10 stsp if (linelen == -1) {
3833 826082fe 2020-12-10 stsp if (feof(s->f)) {
3834 826082fe 2020-12-10 stsp s->eof = 1;
3835 826082fe 2020-12-10 stsp } else
3836 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3837 34bc9ec9 2019-02-22 stsp break;
3838 826082fe 2020-12-10 stsp }
3839 1e37a5c2 2019-05-12 jcs }
3840 826082fe 2020-12-10 stsp free(line);
3841 1e37a5c2 2019-05-12 jcs break;
3842 1e37a5c2 2019-05-12 jcs case '[':
3843 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3844 1e37a5c2 2019-05-12 jcs s->diff_context--;
3845 aa61903a 2021-12-31 thomas s->matched_line = 0;
3846 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3847 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3848 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3849 27829c9e 2020-11-21 stsp s->nlines) {
3850 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3851 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3852 27829c9e 2020-11-21 stsp }
3853 1e37a5c2 2019-05-12 jcs }
3854 1e37a5c2 2019-05-12 jcs break;
3855 1e37a5c2 2019-05-12 jcs case ']':
3856 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3857 1e37a5c2 2019-05-12 jcs s->diff_context++;
3858 aa61903a 2021-12-31 thomas s->matched_line = 0;
3859 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3860 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3861 1e37a5c2 2019-05-12 jcs }
3862 1e37a5c2 2019-05-12 jcs break;
3863 1e37a5c2 2019-05-12 jcs case '<':
3864 1e37a5c2 2019-05-12 jcs case ',':
3865 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3866 48ae06ee 2018-10-18 stsp break;
3867 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3868 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3869 6524637e 2019-02-21 stsp
3870 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3871 1e37a5c2 2019-05-12 jcs if (err)
3872 1e37a5c2 2019-05-12 jcs break;
3873 15a087fe 2019-02-21 stsp
3874 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3875 5a8b5076 2020-12-05 stsp break;
3876 5a8b5076 2020-12-05 stsp
3877 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3878 1e37a5c2 2019-05-12 jcs if (err)
3879 1e37a5c2 2019-05-12 jcs break;
3880 15a087fe 2019-02-21 stsp
3881 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3882 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3883 aa61903a 2021-12-31 thomas s->matched_line = 0;
3884 15a087fe 2019-02-21 stsp
3885 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3886 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3887 1e37a5c2 2019-05-12 jcs break;
3888 1e37a5c2 2019-05-12 jcs case '>':
3889 1e37a5c2 2019-05-12 jcs case '.':
3890 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3891 15a087fe 2019-02-21 stsp break;
3892 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3893 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3894 5e224a3e 2019-02-22 stsp
3895 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3896 1e37a5c2 2019-05-12 jcs if (err)
3897 5a8b5076 2020-12-05 stsp break;
3898 5a8b5076 2020-12-05 stsp
3899 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3900 1e37a5c2 2019-05-12 jcs break;
3901 15a087fe 2019-02-21 stsp
3902 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3903 1e37a5c2 2019-05-12 jcs if (err)
3904 1e37a5c2 2019-05-12 jcs break;
3905 15a087fe 2019-02-21 stsp
3906 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3907 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3908 aa61903a 2021-12-31 thomas s->matched_line = 0;
3909 1e37a5c2 2019-05-12 jcs
3910 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3911 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3912 1e37a5c2 2019-05-12 jcs break;
3913 1e37a5c2 2019-05-12 jcs default:
3914 1e37a5c2 2019-05-12 jcs break;
3915 26ed57b2 2018-05-19 stsp }
3916 e5a0f69f 2018-08-18 stsp
3917 bcbd79e2 2018-08-19 stsp return err;
3918 26ed57b2 2018-05-19 stsp }
3919 26ed57b2 2018-05-19 stsp
3920 4ed7e80c 2018-05-20 stsp static const struct got_error *
3921 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3922 9f7d7167 2018-04-29 stsp {
3923 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3924 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3925 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3926 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3927 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3928 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3929 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3930 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3931 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3932 3dbaef42 2020-11-24 stsp const char *errstr;
3933 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3934 70ac5f84 2019-03-28 stsp
3935 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3936 26ed57b2 2018-05-19 stsp switch (ch) {
3937 64453f7e 2020-11-21 stsp case 'a':
3938 64453f7e 2020-11-21 stsp force_text_diff = 1;
3939 3dbaef42 2020-11-24 stsp break;
3940 3dbaef42 2020-11-24 stsp case 'C':
3941 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3942 3dbaef42 2020-11-24 stsp &errstr);
3943 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3944 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3945 8f666e67 2022-02-12 thomas errstr, errstr);
3946 64453f7e 2020-11-21 stsp break;
3947 09b5bff8 2020-02-23 naddy case 'r':
3948 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3949 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3950 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3951 09b5bff8 2020-02-23 naddy optarg);
3952 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3953 09b5bff8 2020-02-23 naddy break;
3954 3dbaef42 2020-11-24 stsp case 'w':
3955 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3956 3dbaef42 2020-11-24 stsp break;
3957 26ed57b2 2018-05-19 stsp default:
3958 17020d27 2019-03-07 stsp usage_diff();
3959 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3960 26ed57b2 2018-05-19 stsp }
3961 26ed57b2 2018-05-19 stsp }
3962 26ed57b2 2018-05-19 stsp
3963 26ed57b2 2018-05-19 stsp argc -= optind;
3964 26ed57b2 2018-05-19 stsp argv += optind;
3965 26ed57b2 2018-05-19 stsp
3966 26ed57b2 2018-05-19 stsp if (argc == 0) {
3967 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3968 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3969 15a94983 2018-12-23 stsp id_str1 = argv[0];
3970 15a94983 2018-12-23 stsp id_str2 = argv[1];
3971 26ed57b2 2018-05-19 stsp } else
3972 26ed57b2 2018-05-19 stsp usage_diff();
3973 eb6600df 2019-01-04 stsp
3974 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3975 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3976 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3977 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3978 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3979 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3980 c156c7a4 2020-12-18 stsp goto done;
3981 a273ac94 2020-02-23 naddy if (worktree)
3982 a273ac94 2020-02-23 naddy repo_path =
3983 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3984 a273ac94 2020-02-23 naddy else
3985 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3986 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3987 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3988 c156c7a4 2020-12-18 stsp goto done;
3989 c156c7a4 2020-12-18 stsp }
3990 a273ac94 2020-02-23 naddy }
3991 a273ac94 2020-02-23 naddy
3992 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3993 eb6600df 2019-01-04 stsp if (error)
3994 eb6600df 2019-01-04 stsp goto done;
3995 26ed57b2 2018-05-19 stsp
3996 a273ac94 2020-02-23 naddy init_curses();
3997 a273ac94 2020-02-23 naddy
3998 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3999 51a10b52 2020-12-26 stsp if (error)
4000 51a10b52 2020-12-26 stsp goto done;
4001 51a10b52 2020-12-26 stsp
4002 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4003 26ed57b2 2018-05-19 stsp if (error)
4004 26ed57b2 2018-05-19 stsp goto done;
4005 26ed57b2 2018-05-19 stsp
4006 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4007 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4008 26ed57b2 2018-05-19 stsp if (error)
4009 26ed57b2 2018-05-19 stsp goto done;
4010 26ed57b2 2018-05-19 stsp
4011 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4012 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4013 26ed57b2 2018-05-19 stsp if (error)
4014 26ed57b2 2018-05-19 stsp goto done;
4015 26ed57b2 2018-05-19 stsp
4016 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4017 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4018 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4019 ea5e7bb5 2018-08-01 stsp goto done;
4020 ea5e7bb5 2018-08-01 stsp }
4021 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4022 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4023 5dc9f4bc 2018-08-04 stsp if (error)
4024 5dc9f4bc 2018-08-04 stsp goto done;
4025 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4026 26ed57b2 2018-05-19 stsp done:
4027 3dbaef42 2020-11-24 stsp free(label1);
4028 3dbaef42 2020-11-24 stsp free(label2);
4029 c02c541e 2019-03-29 stsp free(repo_path);
4030 a273ac94 2020-02-23 naddy free(cwd);
4031 1d0f4054 2021-06-17 stsp if (repo) {
4032 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4033 1d0f4054 2021-06-17 stsp if (error == NULL)
4034 1d0f4054 2021-06-17 stsp error = close_err;
4035 1d0f4054 2021-06-17 stsp }
4036 a273ac94 2020-02-23 naddy if (worktree)
4037 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4038 51a10b52 2020-12-26 stsp tog_free_refs();
4039 26ed57b2 2018-05-19 stsp return error;
4040 9f7d7167 2018-04-29 stsp }
4041 9f7d7167 2018-04-29 stsp
4042 4ed7e80c 2018-05-20 stsp __dead static void
4043 9f7d7167 2018-04-29 stsp usage_blame(void)
4044 9f7d7167 2018-04-29 stsp {
4045 80ddbec8 2018-04-29 stsp endwin();
4046 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4047 9f7d7167 2018-04-29 stsp getprogname());
4048 9f7d7167 2018-04-29 stsp exit(1);
4049 9f7d7167 2018-04-29 stsp }
4050 84451b3e 2018-07-10 stsp
4051 84451b3e 2018-07-10 stsp struct tog_blame_line {
4052 84451b3e 2018-07-10 stsp int annotated;
4053 84451b3e 2018-07-10 stsp struct got_object_id *id;
4054 84451b3e 2018-07-10 stsp };
4055 9f7d7167 2018-04-29 stsp
4056 4ed7e80c 2018-05-20 stsp static const struct got_error *
4057 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4058 84451b3e 2018-07-10 stsp {
4059 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4060 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4061 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4062 84451b3e 2018-07-10 stsp const struct got_error *err;
4063 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4064 826082fe 2020-12-10 stsp char *line = NULL;
4065 826082fe 2020-12-10 stsp size_t linesize = 0;
4066 826082fe 2020-12-10 stsp ssize_t linelen;
4067 84451b3e 2018-07-10 stsp wchar_t *wline;
4068 27a741e5 2019-09-11 stsp int width;
4069 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4070 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4071 ab089a2a 2018-07-12 stsp char *id_str;
4072 11b20872 2019-11-08 stsp struct tog_color *tc;
4073 ab089a2a 2018-07-12 stsp
4074 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4075 ab089a2a 2018-07-12 stsp if (err)
4076 ab089a2a 2018-07-12 stsp return err;
4077 84451b3e 2018-07-10 stsp
4078 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4079 f7d12f7e 2018-08-01 stsp werase(view->window);
4080 84451b3e 2018-07-10 stsp
4081 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4083 ab089a2a 2018-07-12 stsp free(id_str);
4084 ab089a2a 2018-07-12 stsp return err;
4085 ab089a2a 2018-07-12 stsp }
4086 ab089a2a 2018-07-12 stsp
4087 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4088 ab089a2a 2018-07-12 stsp free(line);
4089 2550e4c3 2018-07-13 stsp line = NULL;
4090 1cae65b4 2019-09-22 stsp if (err)
4091 1cae65b4 2019-09-22 stsp return err;
4092 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4093 a3404814 2018-09-02 stsp wstandout(view->window);
4094 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4095 11b20872 2019-11-08 stsp if (tc)
4096 11b20872 2019-11-08 stsp wattr_on(view->window,
4097 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4098 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4099 11b20872 2019-11-08 stsp if (tc)
4100 11b20872 2019-11-08 stsp wattr_off(view->window,
4101 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4102 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4103 a3404814 2018-09-02 stsp wstandend(view->window);
4104 2550e4c3 2018-07-13 stsp free(wline);
4105 2550e4c3 2018-07-13 stsp wline = NULL;
4106 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4107 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4108 ab089a2a 2018-07-12 stsp
4109 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4110 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4111 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4112 ab089a2a 2018-07-12 stsp free(id_str);
4113 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4114 ab089a2a 2018-07-12 stsp }
4115 ab089a2a 2018-07-12 stsp free(id_str);
4116 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4117 3f60a8ef 2018-07-10 stsp free(line);
4118 2550e4c3 2018-07-13 stsp line = NULL;
4119 3f60a8ef 2018-07-10 stsp if (err)
4120 3f60a8ef 2018-07-10 stsp return err;
4121 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4122 2550e4c3 2018-07-13 stsp free(wline);
4123 2550e4c3 2018-07-13 stsp wline = NULL;
4124 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4125 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4126 3f60a8ef 2018-07-10 stsp
4127 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4128 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4129 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4130 826082fe 2020-12-10 stsp if (linelen == -1) {
4131 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4132 826082fe 2020-12-10 stsp s->eof = 1;
4133 826082fe 2020-12-10 stsp break;
4134 826082fe 2020-12-10 stsp }
4135 84451b3e 2018-07-10 stsp free(line);
4136 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4137 84451b3e 2018-07-10 stsp }
4138 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4139 826082fe 2020-12-10 stsp continue;
4140 84451b3e 2018-07-10 stsp
4141 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4142 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4143 b700b5d6 2018-07-10 stsp
4144 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4145 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4146 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4147 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4148 27a741e5 2019-09-11 stsp !(view->focussed &&
4149 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4150 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4151 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4152 8d0fe45a 2019-08-12 stsp char *id_str;
4153 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4154 8d0fe45a 2019-08-12 stsp if (err) {
4155 8d0fe45a 2019-08-12 stsp free(line);
4156 8d0fe45a 2019-08-12 stsp return err;
4157 8d0fe45a 2019-08-12 stsp }
4158 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4159 11b20872 2019-11-08 stsp if (tc)
4160 11b20872 2019-11-08 stsp wattr_on(view->window,
4161 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4162 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4163 11b20872 2019-11-08 stsp if (tc)
4164 11b20872 2019-11-08 stsp wattr_off(view->window,
4165 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4166 8d0fe45a 2019-08-12 stsp free(id_str);
4167 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4168 8d0fe45a 2019-08-12 stsp } else {
4169 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4170 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4171 84451b3e 2018-07-10 stsp }
4172 ee41ec32 2018-07-10 stsp } else {
4173 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4174 ee41ec32 2018-07-10 stsp prev_id = NULL;
4175 ee41ec32 2018-07-10 stsp }
4176 84451b3e 2018-07-10 stsp
4177 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4178 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4179 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4180 27a741e5 2019-09-11 stsp
4181 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4182 41605754 2020-11-12 stsp width = 9;
4183 41605754 2020-11-12 stsp wline = wcsdup(L"");
4184 41605754 2020-11-12 stsp if (wline == NULL) {
4185 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4186 41605754 2020-11-12 stsp free(line);
4187 41605754 2020-11-12 stsp return err;
4188 41605754 2020-11-12 stsp }
4189 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4190 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4191 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4192 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4193 41605754 2020-11-12 stsp view->window, regmatch);
4194 41605754 2020-11-12 stsp if (err) {
4195 41605754 2020-11-12 stsp free(line);
4196 41605754 2020-11-12 stsp return err;
4197 41605754 2020-11-12 stsp }
4198 41605754 2020-11-12 stsp width += 9;
4199 41605754 2020-11-12 stsp } else {
4200 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4201 41605754 2020-11-12 stsp view->ncols - 9, 9);
4202 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4203 41605754 2020-11-12 stsp free(wline);
4204 41605754 2020-11-12 stsp wline = NULL;
4205 41605754 2020-11-12 stsp width += 9;
4206 41605754 2020-11-12 stsp }
4207 41605754 2020-11-12 stsp
4208 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4209 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4210 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4211 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4212 84451b3e 2018-07-10 stsp }
4213 826082fe 2020-12-10 stsp free(line);
4214 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4215 84451b3e 2018-07-10 stsp
4216 1a57306a 2018-09-02 stsp view_vborder(view);
4217 84451b3e 2018-07-10 stsp
4218 84451b3e 2018-07-10 stsp return NULL;
4219 84451b3e 2018-07-10 stsp }
4220 84451b3e 2018-07-10 stsp
4221 84451b3e 2018-07-10 stsp static const struct got_error *
4222 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4223 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4224 84451b3e 2018-07-10 stsp {
4225 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4226 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4227 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4228 1a76625f 2018-10-22 stsp int errcode;
4229 84451b3e 2018-07-10 stsp
4230 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4231 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4232 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4233 84451b3e 2018-07-10 stsp
4234 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4235 1a76625f 2018-10-22 stsp if (errcode)
4236 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4237 84451b3e 2018-07-10 stsp
4238 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4239 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4240 d68a0a7d 2018-07-10 stsp goto done;
4241 d68a0a7d 2018-07-10 stsp }
4242 d68a0a7d 2018-07-10 stsp
4243 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4244 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4245 d68a0a7d 2018-07-10 stsp
4246 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4247 d68a0a7d 2018-07-10 stsp if (line->annotated)
4248 d68a0a7d 2018-07-10 stsp goto done;
4249 d68a0a7d 2018-07-10 stsp
4250 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4251 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4252 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4253 84451b3e 2018-07-10 stsp goto done;
4254 84451b3e 2018-07-10 stsp }
4255 84451b3e 2018-07-10 stsp line->annotated = 1;
4256 84451b3e 2018-07-10 stsp done:
4257 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4258 1a76625f 2018-10-22 stsp if (errcode)
4259 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4260 84451b3e 2018-07-10 stsp return err;
4261 84451b3e 2018-07-10 stsp }
4262 84451b3e 2018-07-10 stsp
4263 84451b3e 2018-07-10 stsp static void *
4264 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4265 84451b3e 2018-07-10 stsp {
4266 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4267 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4268 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4269 1a76625f 2018-10-22 stsp int errcode;
4270 18430de3 2018-07-10 stsp
4271 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4272 61266923 2020-01-14 stsp if (err)
4273 61266923 2020-01-14 stsp return (void *)err;
4274 61266923 2020-01-14 stsp
4275 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4276 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4277 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4278 fc06ba56 2019-08-22 stsp err = NULL;
4279 18430de3 2018-07-10 stsp
4280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4281 1a76625f 2018-10-22 stsp if (errcode)
4282 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4283 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4284 18430de3 2018-07-10 stsp
4285 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4286 1d0f4054 2021-06-17 stsp if (err == NULL)
4287 1d0f4054 2021-06-17 stsp err = close_err;
4288 c9beca56 2018-07-22 stsp ta->repo = NULL;
4289 c9beca56 2018-07-22 stsp *ta->complete = 1;
4290 18430de3 2018-07-10 stsp
4291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4292 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4293 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4294 18430de3 2018-07-10 stsp
4295 18430de3 2018-07-10 stsp return (void *)err;
4296 84451b3e 2018-07-10 stsp }
4297 84451b3e 2018-07-10 stsp
4298 245d91c1 2018-07-12 stsp static struct got_object_id *
4299 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4300 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4301 245d91c1 2018-07-12 stsp {
4302 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4303 8d0fe45a 2019-08-12 stsp
4304 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4305 8d0fe45a 2019-08-12 stsp return NULL;
4306 b880a918 2018-07-10 stsp
4307 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4308 245d91c1 2018-07-12 stsp if (!line->annotated)
4309 245d91c1 2018-07-12 stsp return NULL;
4310 245d91c1 2018-07-12 stsp
4311 245d91c1 2018-07-12 stsp return line->id;
4312 b880a918 2018-07-10 stsp }
4313 245d91c1 2018-07-12 stsp
4314 b880a918 2018-07-10 stsp static const struct got_error *
4315 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4316 a70480e0 2018-06-23 stsp {
4317 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4318 245d91c1 2018-07-12 stsp int i;
4319 245d91c1 2018-07-12 stsp
4320 245d91c1 2018-07-12 stsp if (blame->thread) {
4321 1a76625f 2018-10-22 stsp int errcode;
4322 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4323 1a76625f 2018-10-22 stsp if (errcode)
4324 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4325 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4326 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4327 1a76625f 2018-10-22 stsp if (errcode)
4328 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4329 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4330 1a76625f 2018-10-22 stsp if (errcode)
4331 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4332 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4333 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4334 245d91c1 2018-07-12 stsp err = NULL;
4335 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4336 245d91c1 2018-07-12 stsp }
4337 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4338 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4339 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4340 1d0f4054 2021-06-17 stsp if (err == NULL)
4341 1d0f4054 2021-06-17 stsp err = close_err;
4342 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4343 245d91c1 2018-07-12 stsp }
4344 245d91c1 2018-07-12 stsp if (blame->f) {
4345 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4346 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4347 245d91c1 2018-07-12 stsp blame->f = NULL;
4348 245d91c1 2018-07-12 stsp }
4349 57670559 2018-12-23 stsp if (blame->lines) {
4350 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4351 57670559 2018-12-23 stsp free(blame->lines[i].id);
4352 57670559 2018-12-23 stsp free(blame->lines);
4353 57670559 2018-12-23 stsp blame->lines = NULL;
4354 57670559 2018-12-23 stsp }
4355 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4356 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4357 245d91c1 2018-07-12 stsp
4358 245d91c1 2018-07-12 stsp return err;
4359 245d91c1 2018-07-12 stsp }
4360 245d91c1 2018-07-12 stsp
4361 245d91c1 2018-07-12 stsp static const struct got_error *
4362 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4363 fc06ba56 2019-08-22 stsp {
4364 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4365 fc06ba56 2019-08-22 stsp int *done = arg;
4366 fc06ba56 2019-08-22 stsp int errcode;
4367 fc06ba56 2019-08-22 stsp
4368 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4369 fc06ba56 2019-08-22 stsp if (errcode)
4370 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4371 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4372 fc06ba56 2019-08-22 stsp
4373 fc06ba56 2019-08-22 stsp if (*done)
4374 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4375 fc06ba56 2019-08-22 stsp
4376 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4377 fc06ba56 2019-08-22 stsp if (errcode)
4378 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4379 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4380 fc06ba56 2019-08-22 stsp
4381 fc06ba56 2019-08-22 stsp return err;
4382 fc06ba56 2019-08-22 stsp }
4383 fc06ba56 2019-08-22 stsp
4384 fc06ba56 2019-08-22 stsp static const struct got_error *
4385 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4386 245d91c1 2018-07-12 stsp {
4387 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4388 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4389 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4390 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4391 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4392 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4393 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4394 15a94983 2018-12-23 stsp int obj_type;
4395 a70480e0 2018-06-23 stsp
4396 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4397 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4398 27d434c2 2018-09-15 stsp if (err)
4399 15a94983 2018-12-23 stsp return err;
4400 945f9229 2022-04-16 thomas
4401 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4402 945f9229 2022-04-16 thomas if (err)
4403 945f9229 2022-04-16 thomas goto done;
4404 27d434c2 2018-09-15 stsp
4405 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4406 84451b3e 2018-07-10 stsp if (err)
4407 84451b3e 2018-07-10 stsp goto done;
4408 27d434c2 2018-09-15 stsp
4409 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4410 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4411 84451b3e 2018-07-10 stsp goto done;
4412 84451b3e 2018-07-10 stsp }
4413 a70480e0 2018-06-23 stsp
4414 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4415 a70480e0 2018-06-23 stsp if (err)
4416 a70480e0 2018-06-23 stsp goto done;
4417 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4418 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4419 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4420 84451b3e 2018-07-10 stsp goto done;
4421 84451b3e 2018-07-10 stsp }
4422 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4423 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4424 1fddf795 2021-01-20 stsp if (err)
4425 1fddf795 2021-01-20 stsp goto done;
4426 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4427 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4428 84451b3e 2018-07-10 stsp goto done;
4429 1fddf795 2021-01-20 stsp }
4430 b02560ec 2019-08-19 stsp
4431 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4432 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4433 b02560ec 2019-08-19 stsp blame->nlines--;
4434 a70480e0 2018-06-23 stsp
4435 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4436 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4437 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4438 84451b3e 2018-07-10 stsp goto done;
4439 84451b3e 2018-07-10 stsp }
4440 a70480e0 2018-06-23 stsp
4441 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4442 bd24772e 2018-07-11 stsp if (err)
4443 bd24772e 2018-07-11 stsp goto done;
4444 bd24772e 2018-07-11 stsp
4445 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4446 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4447 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4448 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4449 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4450 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4451 245d91c1 2018-07-12 stsp goto done;
4452 245d91c1 2018-07-12 stsp }
4453 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4454 245d91c1 2018-07-12 stsp
4455 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4456 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4457 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4458 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4459 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4460 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4461 a5388363 2020-12-01 naddy s->blame_complete = 0;
4462 f5a09613 2020-12-13 naddy
4463 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4464 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4465 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4466 f5a09613 2020-12-13 naddy s->selected_line = 1;
4467 f5a09613 2020-12-13 naddy }
4468 aa61903a 2021-12-31 thomas s->matched_line = 0;
4469 245d91c1 2018-07-12 stsp
4470 245d91c1 2018-07-12 stsp done:
4471 945f9229 2022-04-16 thomas if (commit)
4472 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4473 245d91c1 2018-07-12 stsp if (blob)
4474 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4475 27d434c2 2018-09-15 stsp free(obj_id);
4476 245d91c1 2018-07-12 stsp if (err)
4477 245d91c1 2018-07-12 stsp stop_blame(blame);
4478 245d91c1 2018-07-12 stsp return err;
4479 245d91c1 2018-07-12 stsp }
4480 245d91c1 2018-07-12 stsp
4481 245d91c1 2018-07-12 stsp static const struct got_error *
4482 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4483 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4484 245d91c1 2018-07-12 stsp {
4485 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4486 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4487 dbc6a6b6 2018-07-12 stsp
4488 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4489 245d91c1 2018-07-12 stsp
4490 c4843652 2019-08-12 stsp s->path = strdup(path);
4491 c4843652 2019-08-12 stsp if (s->path == NULL)
4492 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4493 c4843652 2019-08-12 stsp
4494 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4495 c4843652 2019-08-12 stsp if (err) {
4496 c4843652 2019-08-12 stsp free(s->path);
4497 7cbe629d 2018-08-04 stsp return err;
4498 c4843652 2019-08-12 stsp }
4499 245d91c1 2018-07-12 stsp
4500 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4501 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4502 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4503 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4504 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4505 fb2756b9 2018-08-04 stsp s->repo = repo;
4506 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4507 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4508 7cbe629d 2018-08-04 stsp
4509 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4510 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4511 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4512 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4513 11b20872 2019-11-08 stsp if (err)
4514 11b20872 2019-11-08 stsp return err;
4515 11b20872 2019-11-08 stsp }
4516 11b20872 2019-11-08 stsp
4517 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4518 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4519 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4520 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4521 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4522 e5a0f69f 2018-08-18 stsp
4523 a5388363 2020-12-01 naddy return run_blame(view);
4524 7cbe629d 2018-08-04 stsp }
4525 7cbe629d 2018-08-04 stsp
4526 e5a0f69f 2018-08-18 stsp static const struct got_error *
4527 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4528 7cbe629d 2018-08-04 stsp {
4529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4530 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4531 7cbe629d 2018-08-04 stsp
4532 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4533 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4534 e5a0f69f 2018-08-18 stsp
4535 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4536 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4537 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4538 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4539 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4540 7cbe629d 2018-08-04 stsp }
4541 e5a0f69f 2018-08-18 stsp
4542 e5a0f69f 2018-08-18 stsp free(s->path);
4543 11b20872 2019-11-08 stsp free_colors(&s->colors);
4544 e5a0f69f 2018-08-18 stsp
4545 e5a0f69f 2018-08-18 stsp return err;
4546 7cbe629d 2018-08-04 stsp }
4547 7cbe629d 2018-08-04 stsp
4548 7cbe629d 2018-08-04 stsp static const struct got_error *
4549 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4550 6c4c42e0 2019-06-24 stsp {
4551 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4552 6c4c42e0 2019-06-24 stsp
4553 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4554 6c4c42e0 2019-06-24 stsp return NULL;
4555 6c4c42e0 2019-06-24 stsp }
4556 6c4c42e0 2019-06-24 stsp
4557 6c4c42e0 2019-06-24 stsp static const struct got_error *
4558 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4559 6c4c42e0 2019-06-24 stsp {
4560 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4561 6c4c42e0 2019-06-24 stsp int lineno;
4562 826082fe 2020-12-10 stsp char *line = NULL;
4563 826082fe 2020-12-10 stsp size_t linesize = 0;
4564 826082fe 2020-12-10 stsp ssize_t linelen;
4565 6c4c42e0 2019-06-24 stsp
4566 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4567 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4568 6c4c42e0 2019-06-24 stsp return NULL;
4569 6c4c42e0 2019-06-24 stsp }
4570 6c4c42e0 2019-06-24 stsp
4571 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4572 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4573 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4574 6c4c42e0 2019-06-24 stsp else
4575 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4576 4b3f9dac 2021-12-17 thomas } else
4577 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4578 6c4c42e0 2019-06-24 stsp
4579 6c4c42e0 2019-06-24 stsp while (1) {
4580 6c4c42e0 2019-06-24 stsp off_t offset;
4581 6c4c42e0 2019-06-24 stsp
4582 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4583 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4584 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4585 6c4c42e0 2019-06-24 stsp break;
4586 6c4c42e0 2019-06-24 stsp }
4587 2246482e 2019-06-25 stsp
4588 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4589 6c4c42e0 2019-06-24 stsp lineno = 1;
4590 6c4c42e0 2019-06-24 stsp else
4591 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4592 6c4c42e0 2019-06-24 stsp }
4593 6c4c42e0 2019-06-24 stsp
4594 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4595 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4596 6c4c42e0 2019-06-24 stsp free(line);
4597 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4598 6c4c42e0 2019-06-24 stsp }
4599 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4600 826082fe 2020-12-10 stsp if (linelen != -1 &&
4601 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4602 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4603 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4604 6c4c42e0 2019-06-24 stsp break;
4605 6c4c42e0 2019-06-24 stsp }
4606 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4607 6c4c42e0 2019-06-24 stsp lineno++;
4608 6c4c42e0 2019-06-24 stsp else
4609 6c4c42e0 2019-06-24 stsp lineno--;
4610 6c4c42e0 2019-06-24 stsp }
4611 826082fe 2020-12-10 stsp free(line);
4612 6c4c42e0 2019-06-24 stsp
4613 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4614 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4615 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4616 6c4c42e0 2019-06-24 stsp }
4617 6c4c42e0 2019-06-24 stsp
4618 6c4c42e0 2019-06-24 stsp return NULL;
4619 6c4c42e0 2019-06-24 stsp }
4620 6c4c42e0 2019-06-24 stsp
4621 6c4c42e0 2019-06-24 stsp static const struct got_error *
4622 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4623 7cbe629d 2018-08-04 stsp {
4624 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4625 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4626 2b380cc8 2018-10-24 stsp int errcode;
4627 2b380cc8 2018-10-24 stsp
4628 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4629 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4630 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4631 2b380cc8 2018-10-24 stsp if (errcode)
4632 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4633 51fe7530 2019-08-19 stsp
4634 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4635 2b380cc8 2018-10-24 stsp }
4636 e5a0f69f 2018-08-18 stsp
4637 51fe7530 2019-08-19 stsp if (s->blame_complete)
4638 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4639 51fe7530 2019-08-19 stsp
4640 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4641 e5a0f69f 2018-08-18 stsp
4642 669b5ffa 2018-10-07 stsp view_vborder(view);
4643 e5a0f69f 2018-08-18 stsp return err;
4644 e5a0f69f 2018-08-18 stsp }
4645 e5a0f69f 2018-08-18 stsp
4646 e5a0f69f 2018-08-18 stsp static const struct got_error *
4647 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4648 e5a0f69f 2018-08-18 stsp {
4649 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4650 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4651 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4652 70f17a53 2022-06-13 thomas int begin_x = 0, nscroll = view->nlines - 2;
4653 7cbe629d 2018-08-04 stsp
4654 e5a0f69f 2018-08-18 stsp switch (ch) {
4655 1e37a5c2 2019-05-12 jcs case 'q':
4656 1e37a5c2 2019-05-12 jcs s->done = 1;
4657 4deef56f 2021-09-02 naddy break;
4658 4deef56f 2021-09-02 naddy case 'g':
4659 4deef56f 2021-09-02 naddy case KEY_HOME:
4660 4deef56f 2021-09-02 naddy s->selected_line = 1;
4661 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4662 4deef56f 2021-09-02 naddy break;
4663 4deef56f 2021-09-02 naddy case 'G':
4664 4deef56f 2021-09-02 naddy case KEY_END:
4665 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4666 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4667 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4668 4deef56f 2021-09-02 naddy } else {
4669 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4670 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4671 4deef56f 2021-09-02 naddy (view->nlines - 3);
4672 4deef56f 2021-09-02 naddy }
4673 1e37a5c2 2019-05-12 jcs break;
4674 1e37a5c2 2019-05-12 jcs case 'k':
4675 1e37a5c2 2019-05-12 jcs case KEY_UP:
4676 f7140bf5 2021-10-17 thomas case CTRL('p'):
4677 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4678 1e37a5c2 2019-05-12 jcs s->selected_line--;
4679 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4680 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4681 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4682 1e37a5c2 2019-05-12 jcs break;
4683 70f17a53 2022-06-13 thomas case CTRL('u'):
4684 70f17a53 2022-06-13 thomas nscroll /= 2;
4685 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4686 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4687 ea025d1d 2020-02-22 naddy case CTRL('b'):
4688 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4689 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
4690 e5a0f69f 2018-08-18 stsp break;
4691 1e37a5c2 2019-05-12 jcs }
4692 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
4693 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
4694 1e37a5c2 2019-05-12 jcs else
4695 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4696 1e37a5c2 2019-05-12 jcs break;
4697 1e37a5c2 2019-05-12 jcs case 'j':
4698 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4699 f7140bf5 2021-10-17 thomas case CTRL('n'):
4700 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4701 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4702 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4703 1e37a5c2 2019-05-12 jcs s->selected_line++;
4704 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4705 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4706 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4707 1e37a5c2 2019-05-12 jcs break;
4708 1e37a5c2 2019-05-12 jcs case 'b':
4709 1e37a5c2 2019-05-12 jcs case 'p': {
4710 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4711 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4712 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4713 1e37a5c2 2019-05-12 jcs if (id == NULL)
4714 e5a0f69f 2018-08-18 stsp break;
4715 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4716 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4717 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4718 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4719 1e37a5c2 2019-05-12 jcs int obj_type;
4720 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4721 1e37a5c2 2019-05-12 jcs s->repo, id);
4722 e5a0f69f 2018-08-18 stsp if (err)
4723 e5a0f69f 2018-08-18 stsp break;
4724 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4725 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4726 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4727 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4728 e5a0f69f 2018-08-18 stsp break;
4729 e5a0f69f 2018-08-18 stsp }
4730 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4731 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4732 ec242592 2022-04-22 thomas s->repo, &pid->id);
4733 945f9229 2022-04-16 thomas if (err)
4734 945f9229 2022-04-16 thomas break;
4735 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4736 945f9229 2022-04-16 thomas pcommit, s->path);
4737 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4738 e5a0f69f 2018-08-18 stsp if (err) {
4739 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4740 1e37a5c2 2019-05-12 jcs err = NULL;
4741 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4742 e5a0f69f 2018-08-18 stsp break;
4743 e5a0f69f 2018-08-18 stsp }
4744 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4745 1e37a5c2 2019-05-12 jcs blob_id);
4746 1e37a5c2 2019-05-12 jcs free(blob_id);
4747 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4748 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4749 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4750 e5a0f69f 2018-08-18 stsp break;
4751 1e37a5c2 2019-05-12 jcs }
4752 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4753 ec242592 2022-04-22 thomas &pid->id);
4754 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4755 1e37a5c2 2019-05-12 jcs } else {
4756 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4757 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4758 1e37a5c2 2019-05-12 jcs break;
4759 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4760 1e37a5c2 2019-05-12 jcs id);
4761 1e37a5c2 2019-05-12 jcs }
4762 1e37a5c2 2019-05-12 jcs if (err)
4763 e5a0f69f 2018-08-18 stsp break;
4764 1e37a5c2 2019-05-12 jcs s->done = 1;
4765 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4766 1e37a5c2 2019-05-12 jcs s->done = 0;
4767 1e37a5c2 2019-05-12 jcs if (thread_err)
4768 1e37a5c2 2019-05-12 jcs break;
4769 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4770 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4771 a5388363 2020-12-01 naddy err = run_blame(view);
4772 1e37a5c2 2019-05-12 jcs if (err)
4773 1e37a5c2 2019-05-12 jcs break;
4774 1e37a5c2 2019-05-12 jcs break;
4775 1e37a5c2 2019-05-12 jcs }
4776 1e37a5c2 2019-05-12 jcs case 'B': {
4777 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4778 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4779 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4780 1e37a5c2 2019-05-12 jcs break;
4781 1e37a5c2 2019-05-12 jcs s->done = 1;
4782 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4783 1e37a5c2 2019-05-12 jcs s->done = 0;
4784 1e37a5c2 2019-05-12 jcs if (thread_err)
4785 1e37a5c2 2019-05-12 jcs break;
4786 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4787 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4788 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4789 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4790 a5388363 2020-12-01 naddy err = run_blame(view);
4791 1e37a5c2 2019-05-12 jcs if (err)
4792 1e37a5c2 2019-05-12 jcs break;
4793 1e37a5c2 2019-05-12 jcs break;
4794 1e37a5c2 2019-05-12 jcs }
4795 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4796 1e37a5c2 2019-05-12 jcs case '\r': {
4797 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4798 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4799 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4800 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4801 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4802 1e37a5c2 2019-05-12 jcs if (id == NULL)
4803 1e37a5c2 2019-05-12 jcs break;
4804 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4805 1e37a5c2 2019-05-12 jcs if (err)
4806 1e37a5c2 2019-05-12 jcs break;
4807 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4808 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4809 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4810 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4811 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4812 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4813 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4814 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4815 1e37a5c2 2019-05-12 jcs break;
4816 15a94983 2018-12-23 stsp }
4817 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4818 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4819 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4820 1e37a5c2 2019-05-12 jcs if (err) {
4821 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4822 1e37a5c2 2019-05-12 jcs break;
4823 1e37a5c2 2019-05-12 jcs }
4824 e78dc838 2020-12-04 stsp view->focussed = 0;
4825 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4826 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4827 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4828 1e37a5c2 2019-05-12 jcs if (err)
4829 34bc9ec9 2019-02-22 stsp break;
4830 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4831 e78dc838 2020-12-04 stsp view->focus_child = 1;
4832 1e37a5c2 2019-05-12 jcs } else
4833 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4834 1e37a5c2 2019-05-12 jcs if (err)
4835 e5a0f69f 2018-08-18 stsp break;
4836 1e37a5c2 2019-05-12 jcs break;
4837 1e37a5c2 2019-05-12 jcs }
4838 70f17a53 2022-06-13 thomas case CTRL('d'):
4839 70f17a53 2022-06-13 thomas nscroll /= 2;
4840 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4841 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4842 ea025d1d 2020-02-22 naddy case CTRL('f'):
4843 1e37a5c2 2019-05-12 jcs case ' ':
4844 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4845 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4846 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4847 e5a0f69f 2018-08-18 stsp break;
4848 1e37a5c2 2019-05-12 jcs }
4849 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4850 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4851 70f17a53 2022-06-13 thomas s->selected_line +=
4852 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
4853 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
4854 1e37a5c2 2019-05-12 jcs }
4855 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
4856 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
4857 1e37a5c2 2019-05-12 jcs else
4858 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4859 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
4860 1e37a5c2 2019-05-12 jcs break;
4861 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4862 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4863 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4864 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4865 1e37a5c2 2019-05-12 jcs }
4866 1e37a5c2 2019-05-12 jcs break;
4867 1e37a5c2 2019-05-12 jcs default:
4868 1e37a5c2 2019-05-12 jcs break;
4869 a70480e0 2018-06-23 stsp }
4870 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4871 a70480e0 2018-06-23 stsp }
4872 a70480e0 2018-06-23 stsp
4873 a70480e0 2018-06-23 stsp static const struct got_error *
4874 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4875 9f7d7167 2018-04-29 stsp {
4876 a70480e0 2018-06-23 stsp const struct got_error *error;
4877 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4878 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4879 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4880 0587e10c 2020-07-23 stsp char *link_target = NULL;
4881 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4882 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4883 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4884 a70480e0 2018-06-23 stsp int ch;
4885 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4886 a70480e0 2018-06-23 stsp
4887 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4888 a70480e0 2018-06-23 stsp switch (ch) {
4889 a70480e0 2018-06-23 stsp case 'c':
4890 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4891 a70480e0 2018-06-23 stsp break;
4892 69069811 2018-08-02 stsp case 'r':
4893 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4894 69069811 2018-08-02 stsp if (repo_path == NULL)
4895 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4896 9ba1d308 2019-10-21 stsp optarg);
4897 69069811 2018-08-02 stsp break;
4898 a70480e0 2018-06-23 stsp default:
4899 17020d27 2019-03-07 stsp usage_blame();
4900 a70480e0 2018-06-23 stsp /* NOTREACHED */
4901 a70480e0 2018-06-23 stsp }
4902 a70480e0 2018-06-23 stsp }
4903 a70480e0 2018-06-23 stsp
4904 a70480e0 2018-06-23 stsp argc -= optind;
4905 a70480e0 2018-06-23 stsp argv += optind;
4906 a70480e0 2018-06-23 stsp
4907 f135c941 2020-02-20 stsp if (argc != 1)
4908 a70480e0 2018-06-23 stsp usage_blame();
4909 6962eb72 2020-02-20 stsp
4910 69069811 2018-08-02 stsp if (repo_path == NULL) {
4911 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4912 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4913 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4914 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4915 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4916 c156c7a4 2020-12-18 stsp goto done;
4917 f135c941 2020-02-20 stsp if (worktree)
4918 eb41ed75 2019-02-05 stsp repo_path =
4919 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4920 f135c941 2020-02-20 stsp else
4921 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4922 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4923 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4924 c156c7a4 2020-12-18 stsp goto done;
4925 c156c7a4 2020-12-18 stsp }
4926 f135c941 2020-02-20 stsp }
4927 a915003a 2019-02-05 stsp
4928 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4929 c02c541e 2019-03-29 stsp if (error != NULL)
4930 8e94dd5b 2019-01-04 stsp goto done;
4931 69069811 2018-08-02 stsp
4932 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4933 f135c941 2020-02-20 stsp worktree);
4934 c02c541e 2019-03-29 stsp if (error)
4935 92205607 2019-01-04 stsp goto done;
4936 69069811 2018-08-02 stsp
4937 f135c941 2020-02-20 stsp init_curses();
4938 f135c941 2020-02-20 stsp
4939 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4940 51a10b52 2020-12-26 stsp if (error)
4941 51a10b52 2020-12-26 stsp goto done;
4942 51a10b52 2020-12-26 stsp
4943 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4944 eb41ed75 2019-02-05 stsp if (error)
4945 69069811 2018-08-02 stsp goto done;
4946 a70480e0 2018-06-23 stsp
4947 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4948 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4949 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4950 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4951 a70480e0 2018-06-23 stsp if (error != NULL)
4952 66b4983c 2018-06-23 stsp goto done;
4953 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4954 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4955 a70480e0 2018-06-23 stsp } else {
4956 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4957 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4958 a70480e0 2018-06-23 stsp }
4959 a19e88aa 2018-06-23 stsp if (error != NULL)
4960 8b473291 2019-02-21 stsp goto done;
4961 8b473291 2019-02-21 stsp
4962 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4963 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4964 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4965 e1cd8fed 2018-08-01 stsp goto done;
4966 e1cd8fed 2018-08-01 stsp }
4967 0587e10c 2020-07-23 stsp
4968 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
4969 945f9229 2022-04-16 thomas if (error)
4970 945f9229 2022-04-16 thomas goto done;
4971 945f9229 2022-04-16 thomas
4972 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4973 945f9229 2022-04-16 thomas commit, repo);
4974 7cbe629d 2018-08-04 stsp if (error)
4975 7cbe629d 2018-08-04 stsp goto done;
4976 0587e10c 2020-07-23 stsp
4977 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4978 78756c87 2020-11-24 stsp commit_id, repo);
4979 0587e10c 2020-07-23 stsp if (error)
4980 0587e10c 2020-07-23 stsp goto done;
4981 12314ad4 2019-08-31 stsp if (worktree) {
4982 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4983 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4984 12314ad4 2019-08-31 stsp worktree = NULL;
4985 12314ad4 2019-08-31 stsp }
4986 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4987 a70480e0 2018-06-23 stsp done:
4988 69069811 2018-08-02 stsp free(repo_path);
4989 f135c941 2020-02-20 stsp free(in_repo_path);
4990 0587e10c 2020-07-23 stsp free(link_target);
4991 69069811 2018-08-02 stsp free(cwd);
4992 a70480e0 2018-06-23 stsp free(commit_id);
4993 945f9229 2022-04-16 thomas if (commit)
4994 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4995 eb41ed75 2019-02-05 stsp if (worktree)
4996 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4997 1d0f4054 2021-06-17 stsp if (repo) {
4998 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4999 1d0f4054 2021-06-17 stsp if (error == NULL)
5000 1d0f4054 2021-06-17 stsp error = close_err;
5001 1d0f4054 2021-06-17 stsp }
5002 51a10b52 2020-12-26 stsp tog_free_refs();
5003 a70480e0 2018-06-23 stsp return error;
5004 ffd1d5e5 2018-06-23 stsp }
5005 ffd1d5e5 2018-06-23 stsp
5006 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5007 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5008 ffd1d5e5 2018-06-23 stsp {
5009 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5010 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5011 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5012 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5013 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5014 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5015 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5016 ffd1d5e5 2018-06-23 stsp
5017 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5018 ffd1d5e5 2018-06-23 stsp
5019 f7d12f7e 2018-08-01 stsp werase(view->window);
5020 ffd1d5e5 2018-06-23 stsp
5021 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5022 ffd1d5e5 2018-06-23 stsp return NULL;
5023 ffd1d5e5 2018-06-23 stsp
5024 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5025 ffd1d5e5 2018-06-23 stsp if (err)
5026 ffd1d5e5 2018-06-23 stsp return err;
5027 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5028 a3404814 2018-09-02 stsp wstandout(view->window);
5029 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5030 11b20872 2019-11-08 stsp if (tc)
5031 11b20872 2019-11-08 stsp wattr_on(view->window,
5032 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5033 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5034 11b20872 2019-11-08 stsp if (tc)
5035 11b20872 2019-11-08 stsp wattr_off(view->window,
5036 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5037 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5038 a3404814 2018-09-02 stsp wstandend(view->window);
5039 2550e4c3 2018-07-13 stsp free(wline);
5040 2550e4c3 2018-07-13 stsp wline = NULL;
5041 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5042 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5043 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5044 ffd1d5e5 2018-06-23 stsp return NULL;
5045 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
5046 ce52c690 2018-06-23 stsp if (err)
5047 ce52c690 2018-06-23 stsp return err;
5048 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5049 2550e4c3 2018-07-13 stsp free(wline);
5050 2550e4c3 2018-07-13 stsp wline = NULL;
5051 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5052 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5053 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5054 ffd1d5e5 2018-06-23 stsp return NULL;
5055 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5056 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5057 a1eca9bb 2018-06-23 stsp return NULL;
5058 ffd1d5e5 2018-06-23 stsp
5059 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5060 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5061 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5062 0cf4efb1 2018-09-29 stsp if (view->focussed)
5063 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5064 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5065 ffd1d5e5 2018-06-23 stsp }
5066 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5067 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5068 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5069 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5070 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5071 ffd1d5e5 2018-06-23 stsp return NULL;
5072 ffd1d5e5 2018-06-23 stsp n = 1;
5073 ffd1d5e5 2018-06-23 stsp } else {
5074 ffd1d5e5 2018-06-23 stsp n = 0;
5075 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5076 ffd1d5e5 2018-06-23 stsp }
5077 ffd1d5e5 2018-06-23 stsp
5078 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5079 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5080 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5081 848d6979 2019-08-12 stsp const char *modestr = "";
5082 56e0773d 2019-11-28 stsp mode_t mode;
5083 1d13200f 2018-07-12 stsp
5084 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5085 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5086 56e0773d 2019-11-28 stsp
5087 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5088 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5089 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5090 1d13200f 2018-07-12 stsp if (err)
5091 638f9024 2019-05-13 stsp return got_error_from_errno(
5092 230a42bd 2019-05-11 jcs "got_object_id_str");
5093 1d13200f 2018-07-12 stsp }
5094 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5095 63c5ca5d 2019-08-24 stsp modestr = "$";
5096 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5097 0d6c6ee3 2020-05-20 stsp int i;
5098 0d6c6ee3 2020-05-20 stsp
5099 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5100 d86d3b18 2020-12-01 naddy te, s->repo);
5101 0d6c6ee3 2020-05-20 stsp if (err) {
5102 0d6c6ee3 2020-05-20 stsp free(id_str);
5103 0d6c6ee3 2020-05-20 stsp return err;
5104 0d6c6ee3 2020-05-20 stsp }
5105 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5106 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5107 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5108 0d6c6ee3 2020-05-20 stsp }
5109 848d6979 2019-08-12 stsp modestr = "@";
5110 0d6c6ee3 2020-05-20 stsp }
5111 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5112 848d6979 2019-08-12 stsp modestr = "/";
5113 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5114 848d6979 2019-08-12 stsp modestr = "*";
5115 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5116 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5117 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5118 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5119 1d13200f 2018-07-12 stsp free(id_str);
5120 0d6c6ee3 2020-05-20 stsp free(link_target);
5121 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5122 1d13200f 2018-07-12 stsp }
5123 1d13200f 2018-07-12 stsp free(id_str);
5124 0d6c6ee3 2020-05-20 stsp free(link_target);
5125 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5126 ffd1d5e5 2018-06-23 stsp if (err) {
5127 ffd1d5e5 2018-06-23 stsp free(line);
5128 ffd1d5e5 2018-06-23 stsp break;
5129 ffd1d5e5 2018-06-23 stsp }
5130 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5131 0cf4efb1 2018-09-29 stsp if (view->focussed)
5132 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5133 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5134 ffd1d5e5 2018-06-23 stsp }
5135 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5136 f26dddb7 2019-11-08 stsp if (tc)
5137 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5138 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5139 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5140 f26dddb7 2019-11-08 stsp if (tc)
5141 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5142 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5143 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5144 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5145 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5146 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5147 ffd1d5e5 2018-06-23 stsp free(line);
5148 2550e4c3 2018-07-13 stsp free(wline);
5149 2550e4c3 2018-07-13 stsp wline = NULL;
5150 ffd1d5e5 2018-06-23 stsp n++;
5151 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5152 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5153 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5154 ffd1d5e5 2018-06-23 stsp break;
5155 ffd1d5e5 2018-06-23 stsp }
5156 ffd1d5e5 2018-06-23 stsp
5157 ffd1d5e5 2018-06-23 stsp return err;
5158 ffd1d5e5 2018-06-23 stsp }
5159 ffd1d5e5 2018-06-23 stsp
5160 ffd1d5e5 2018-06-23 stsp static void
5161 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5162 ffd1d5e5 2018-06-23 stsp {
5163 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5164 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5165 fa86c4bf 2020-11-29 stsp int i = 0;
5166 ffd1d5e5 2018-06-23 stsp
5167 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5168 ffd1d5e5 2018-06-23 stsp return;
5169 ffd1d5e5 2018-06-23 stsp
5170 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5171 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5172 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5173 fa86c4bf 2020-11-29 stsp if (!isroot)
5174 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5175 fa86c4bf 2020-11-29 stsp break;
5176 fa86c4bf 2020-11-29 stsp }
5177 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5178 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5179 ffd1d5e5 2018-06-23 stsp }
5180 ffd1d5e5 2018-06-23 stsp }
5181 ffd1d5e5 2018-06-23 stsp
5182 fa86c4bf 2020-11-29 stsp static void
5183 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5184 ffd1d5e5 2018-06-23 stsp {
5185 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5186 ffd1d5e5 2018-06-23 stsp int n = 0;
5187 ffd1d5e5 2018-06-23 stsp
5188 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5189 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5190 694d3271 2020-12-01 naddy s->first_displayed_entry);
5191 694d3271 2020-12-01 naddy else
5192 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5193 56e0773d 2019-11-28 stsp
5194 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5195 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5196 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5197 768394f3 2019-01-24 stsp if (last) {
5198 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5199 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5200 768394f3 2019-01-24 stsp }
5201 ffd1d5e5 2018-06-23 stsp }
5202 ffd1d5e5 2018-06-23 stsp }
5203 ffd1d5e5 2018-06-23 stsp
5204 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5205 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5206 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5207 ffd1d5e5 2018-06-23 stsp {
5208 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5209 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5210 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5211 ffd1d5e5 2018-06-23 stsp
5212 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5213 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5214 56e0773d 2019-11-28 stsp + 1 /* slash */;
5215 ce52c690 2018-06-23 stsp if (te)
5216 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5217 ce52c690 2018-06-23 stsp
5218 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5219 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5220 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5221 ffd1d5e5 2018-06-23 stsp
5222 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5223 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5224 d9765a41 2018-06-23 stsp while (pt) {
5225 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5226 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5227 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5228 cb2ebc8a 2018-06-23 stsp goto done;
5229 cb2ebc8a 2018-06-23 stsp }
5230 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5231 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5232 cb2ebc8a 2018-06-23 stsp goto done;
5233 cb2ebc8a 2018-06-23 stsp }
5234 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5235 ffd1d5e5 2018-06-23 stsp }
5236 ce52c690 2018-06-23 stsp if (te) {
5237 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5238 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5239 ce52c690 2018-06-23 stsp goto done;
5240 ce52c690 2018-06-23 stsp }
5241 cb2ebc8a 2018-06-23 stsp }
5242 ce52c690 2018-06-23 stsp done:
5243 ce52c690 2018-06-23 stsp if (err) {
5244 ce52c690 2018-06-23 stsp free(*path);
5245 ce52c690 2018-06-23 stsp *path = NULL;
5246 ce52c690 2018-06-23 stsp }
5247 ce52c690 2018-06-23 stsp return err;
5248 ce52c690 2018-06-23 stsp }
5249 ce52c690 2018-06-23 stsp
5250 ce52c690 2018-06-23 stsp static const struct got_error *
5251 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5252 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5253 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5254 ce52c690 2018-06-23 stsp {
5255 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5256 ce52c690 2018-06-23 stsp char *path;
5257 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5258 a0de39f3 2019-08-09 stsp
5259 a0de39f3 2019-08-09 stsp *new_view = NULL;
5260 69efd4c4 2018-07-18 stsp
5261 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5262 ce52c690 2018-06-23 stsp if (err)
5263 ce52c690 2018-06-23 stsp return err;
5264 ffd1d5e5 2018-06-23 stsp
5265 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5266 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5267 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5268 83ce39e3 2019-08-12 stsp goto done;
5269 83ce39e3 2019-08-12 stsp }
5270 cdf1ee82 2018-08-01 stsp
5271 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5272 e5a0f69f 2018-08-18 stsp if (err) {
5273 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5274 fc06ba56 2019-08-22 stsp err = NULL;
5275 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5276 e5a0f69f 2018-08-18 stsp } else
5277 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5278 83ce39e3 2019-08-12 stsp done:
5279 83ce39e3 2019-08-12 stsp free(path);
5280 69efd4c4 2018-07-18 stsp return err;
5281 69efd4c4 2018-07-18 stsp }
5282 69efd4c4 2018-07-18 stsp
5283 69efd4c4 2018-07-18 stsp static const struct got_error *
5284 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5285 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5286 69efd4c4 2018-07-18 stsp {
5287 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5288 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5289 69efd4c4 2018-07-18 stsp char *path;
5290 a0de39f3 2019-08-09 stsp
5291 a0de39f3 2019-08-09 stsp *new_view = NULL;
5292 69efd4c4 2018-07-18 stsp
5293 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5294 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5295 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5296 e5a0f69f 2018-08-18 stsp
5297 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5298 69efd4c4 2018-07-18 stsp if (err)
5299 69efd4c4 2018-07-18 stsp return err;
5300 69efd4c4 2018-07-18 stsp
5301 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5302 4e97c21c 2020-12-06 stsp path, 0);
5303 ba4f502b 2018-08-04 stsp if (err)
5304 e5a0f69f 2018-08-18 stsp view_close(log_view);
5305 e5a0f69f 2018-08-18 stsp else
5306 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5307 cb2ebc8a 2018-06-23 stsp free(path);
5308 cb2ebc8a 2018-06-23 stsp return err;
5309 ffd1d5e5 2018-06-23 stsp }
5310 ffd1d5e5 2018-06-23 stsp
5311 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5312 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5313 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5314 ffd1d5e5 2018-06-23 stsp {
5315 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5316 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5317 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5318 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5319 ffd1d5e5 2018-06-23 stsp
5320 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5321 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5322 bc573f3b 2021-07-10 stsp
5323 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5324 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5325 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5326 ffd1d5e5 2018-06-23 stsp
5327 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5328 bc573f3b 2021-07-10 stsp if (err)
5329 bc573f3b 2021-07-10 stsp goto done;
5330 bc573f3b 2021-07-10 stsp
5331 bc573f3b 2021-07-10 stsp /*
5332 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5333 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5334 bc573f3b 2021-07-10 stsp * closed on demand.
5335 bc573f3b 2021-07-10 stsp */
5336 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5337 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5338 bc573f3b 2021-07-10 stsp if (err)
5339 bc573f3b 2021-07-10 stsp goto done;
5340 bc573f3b 2021-07-10 stsp s->tree = s->root;
5341 bc573f3b 2021-07-10 stsp
5342 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5343 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5344 ffd1d5e5 2018-06-23 stsp goto done;
5345 ffd1d5e5 2018-06-23 stsp
5346 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5347 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5348 ffd1d5e5 2018-06-23 stsp goto done;
5349 ffd1d5e5 2018-06-23 stsp }
5350 ffd1d5e5 2018-06-23 stsp
5351 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5352 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5353 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5354 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5355 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5356 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5357 9cd7cbd1 2020-12-07 stsp goto done;
5358 9cd7cbd1 2020-12-07 stsp }
5359 9cd7cbd1 2020-12-07 stsp }
5360 fb2756b9 2018-08-04 stsp s->repo = repo;
5361 c0b01bdb 2019-11-08 stsp
5362 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5363 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5364 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5365 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5366 c0b01bdb 2019-11-08 stsp if (err)
5367 c0b01bdb 2019-11-08 stsp goto done;
5368 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5369 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5370 bc573f3b 2021-07-10 stsp if (err)
5371 c0b01bdb 2019-11-08 stsp goto done;
5372 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5373 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5374 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5375 bc573f3b 2021-07-10 stsp if (err)
5376 c0b01bdb 2019-11-08 stsp goto done;
5377 e5a0f69f 2018-08-18 stsp
5378 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5379 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5380 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5381 bc573f3b 2021-07-10 stsp if (err)
5382 11b20872 2019-11-08 stsp goto done;
5383 11b20872 2019-11-08 stsp
5384 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5385 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5386 bc573f3b 2021-07-10 stsp if (err)
5387 c0b01bdb 2019-11-08 stsp goto done;
5388 c0b01bdb 2019-11-08 stsp }
5389 c0b01bdb 2019-11-08 stsp
5390 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5391 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5392 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5393 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5394 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5395 ad80ab7b 2018-08-04 stsp done:
5396 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5397 bc573f3b 2021-07-10 stsp if (commit)
5398 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5399 bc573f3b 2021-07-10 stsp if (err)
5400 bc573f3b 2021-07-10 stsp close_tree_view(view);
5401 ad80ab7b 2018-08-04 stsp return err;
5402 ad80ab7b 2018-08-04 stsp }
5403 ad80ab7b 2018-08-04 stsp
5404 e5a0f69f 2018-08-18 stsp static const struct got_error *
5405 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5406 ad80ab7b 2018-08-04 stsp {
5407 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5408 ad80ab7b 2018-08-04 stsp
5409 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5410 fb2756b9 2018-08-04 stsp free(s->tree_label);
5411 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5412 6484ec90 2018-09-29 stsp free(s->commit_id);
5413 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5414 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5415 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5416 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5417 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5418 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5419 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5420 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5421 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5422 ad80ab7b 2018-08-04 stsp free(parent);
5423 ad80ab7b 2018-08-04 stsp
5424 ad80ab7b 2018-08-04 stsp }
5425 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5426 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5427 bc573f3b 2021-07-10 stsp if (s->root)
5428 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5429 7c32bd05 2019-06-22 stsp return NULL;
5430 7c32bd05 2019-06-22 stsp }
5431 7c32bd05 2019-06-22 stsp
5432 7c32bd05 2019-06-22 stsp static const struct got_error *
5433 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5434 7c32bd05 2019-06-22 stsp {
5435 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5436 7c32bd05 2019-06-22 stsp
5437 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5438 7c32bd05 2019-06-22 stsp return NULL;
5439 7c32bd05 2019-06-22 stsp }
5440 7c32bd05 2019-06-22 stsp
5441 7c32bd05 2019-06-22 stsp static int
5442 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5443 7c32bd05 2019-06-22 stsp {
5444 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5445 7c32bd05 2019-06-22 stsp
5446 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5447 56e0773d 2019-11-28 stsp 0) == 0;
5448 7c32bd05 2019-06-22 stsp }
5449 7c32bd05 2019-06-22 stsp
5450 7c32bd05 2019-06-22 stsp static const struct got_error *
5451 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5452 7c32bd05 2019-06-22 stsp {
5453 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5454 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5455 7c32bd05 2019-06-22 stsp
5456 7c32bd05 2019-06-22 stsp if (!view->searching) {
5457 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5458 7c32bd05 2019-06-22 stsp return NULL;
5459 7c32bd05 2019-06-22 stsp }
5460 7c32bd05 2019-06-22 stsp
5461 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5462 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5463 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5464 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5465 56e0773d 2019-11-28 stsp s->selected_entry);
5466 7c32bd05 2019-06-22 stsp else
5467 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5468 56e0773d 2019-11-28 stsp } else {
5469 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5470 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5471 56e0773d 2019-11-28 stsp else
5472 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5473 56e0773d 2019-11-28 stsp s->selected_entry);
5474 7c32bd05 2019-06-22 stsp }
5475 7c32bd05 2019-06-22 stsp } else {
5476 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5477 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5478 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5479 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5480 56e0773d 2019-11-28 stsp else
5481 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5482 7c32bd05 2019-06-22 stsp }
5483 7c32bd05 2019-06-22 stsp
5484 7c32bd05 2019-06-22 stsp while (1) {
5485 56e0773d 2019-11-28 stsp if (te == NULL) {
5486 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5487 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5488 ac66afb8 2019-06-24 stsp return NULL;
5489 ac66afb8 2019-06-24 stsp }
5490 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5491 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5492 56e0773d 2019-11-28 stsp else
5493 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5494 7c32bd05 2019-06-22 stsp }
5495 7c32bd05 2019-06-22 stsp
5496 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5497 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5498 56e0773d 2019-11-28 stsp s->matched_entry = te;
5499 7c32bd05 2019-06-22 stsp break;
5500 7c32bd05 2019-06-22 stsp }
5501 7c32bd05 2019-06-22 stsp
5502 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5503 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5504 56e0773d 2019-11-28 stsp else
5505 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5506 7c32bd05 2019-06-22 stsp }
5507 e5a0f69f 2018-08-18 stsp
5508 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5509 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5510 7c32bd05 2019-06-22 stsp s->selected = 0;
5511 7c32bd05 2019-06-22 stsp }
5512 7c32bd05 2019-06-22 stsp
5513 e5a0f69f 2018-08-18 stsp return NULL;
5514 ad80ab7b 2018-08-04 stsp }
5515 ad80ab7b 2018-08-04 stsp
5516 ad80ab7b 2018-08-04 stsp static const struct got_error *
5517 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5518 ad80ab7b 2018-08-04 stsp {
5519 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5520 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5521 e5a0f69f 2018-08-18 stsp char *parent_path;
5522 ad80ab7b 2018-08-04 stsp
5523 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5524 e5a0f69f 2018-08-18 stsp if (err)
5525 e5a0f69f 2018-08-18 stsp return err;
5526 ffd1d5e5 2018-06-23 stsp
5527 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5528 e5a0f69f 2018-08-18 stsp free(parent_path);
5529 669b5ffa 2018-10-07 stsp
5530 669b5ffa 2018-10-07 stsp view_vborder(view);
5531 e5a0f69f 2018-08-18 stsp return err;
5532 e5a0f69f 2018-08-18 stsp }
5533 ce52c690 2018-06-23 stsp
5534 e5a0f69f 2018-08-18 stsp static const struct got_error *
5535 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5536 e5a0f69f 2018-08-18 stsp {
5537 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5538 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5539 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5540 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5541 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 3;
5542 ffd1d5e5 2018-06-23 stsp
5543 e5a0f69f 2018-08-18 stsp switch (ch) {
5544 1e37a5c2 2019-05-12 jcs case 'i':
5545 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5546 1e37a5c2 2019-05-12 jcs break;
5547 1e37a5c2 2019-05-12 jcs case 'l':
5548 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5549 ffd1d5e5 2018-06-23 stsp break;
5550 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5551 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5552 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5553 e78dc838 2020-12-04 stsp view->focussed = 0;
5554 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5555 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5556 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5557 1e37a5c2 2019-05-12 jcs if (err)
5558 1e37a5c2 2019-05-12 jcs return err;
5559 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5560 e78dc838 2020-12-04 stsp view->focus_child = 1;
5561 1e37a5c2 2019-05-12 jcs } else
5562 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5563 152c1c93 2020-11-29 stsp break;
5564 152c1c93 2020-11-29 stsp case 'r':
5565 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5566 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5567 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5568 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5569 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5570 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5571 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5572 152c1c93 2020-11-29 stsp if (err) {
5573 152c1c93 2020-11-29 stsp view_close(ref_view);
5574 152c1c93 2020-11-29 stsp return err;
5575 152c1c93 2020-11-29 stsp }
5576 e78dc838 2020-12-04 stsp view->focussed = 0;
5577 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5578 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5579 152c1c93 2020-11-29 stsp err = view_close_child(view);
5580 152c1c93 2020-11-29 stsp if (err)
5581 152c1c93 2020-11-29 stsp return err;
5582 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5583 e78dc838 2020-12-04 stsp view->focus_child = 1;
5584 152c1c93 2020-11-29 stsp } else
5585 152c1c93 2020-11-29 stsp *new_view = ref_view;
5586 e4526bf5 2021-09-03 naddy break;
5587 e4526bf5 2021-09-03 naddy case 'g':
5588 e4526bf5 2021-09-03 naddy case KEY_HOME:
5589 e4526bf5 2021-09-03 naddy s->selected = 0;
5590 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5591 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5592 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5593 e4526bf5 2021-09-03 naddy else
5594 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5595 1e37a5c2 2019-05-12 jcs break;
5596 e4526bf5 2021-09-03 naddy case 'G':
5597 e4526bf5 2021-09-03 naddy case KEY_END:
5598 e4526bf5 2021-09-03 naddy s->selected = 0;
5599 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5600 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5601 e4526bf5 2021-09-03 naddy if (te == NULL) {
5602 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5603 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5604 e4526bf5 2021-09-03 naddy n++;
5605 e4526bf5 2021-09-03 naddy }
5606 e4526bf5 2021-09-03 naddy break;
5607 e4526bf5 2021-09-03 naddy }
5608 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5609 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5610 e4526bf5 2021-09-03 naddy }
5611 e4526bf5 2021-09-03 naddy if (n > 0)
5612 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5613 e4526bf5 2021-09-03 naddy break;
5614 1e37a5c2 2019-05-12 jcs case 'k':
5615 1e37a5c2 2019-05-12 jcs case KEY_UP:
5616 f7140bf5 2021-10-17 thomas case CTRL('p'):
5617 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5618 1e37a5c2 2019-05-12 jcs s->selected--;
5619 fa86c4bf 2020-11-29 stsp break;
5620 1e37a5c2 2019-05-12 jcs }
5621 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5622 1e37a5c2 2019-05-12 jcs break;
5623 70f17a53 2022-06-13 thomas case CTRL('u'):
5624 70f17a53 2022-06-13 thomas nscroll /= 2;
5625 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5626 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5627 ea025d1d 2020-02-22 naddy case CTRL('b'):
5628 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5629 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5630 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5631 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5632 fa86c4bf 2020-11-29 stsp } else {
5633 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5634 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
5635 fa86c4bf 2020-11-29 stsp }
5636 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
5637 1e37a5c2 2019-05-12 jcs break;
5638 1e37a5c2 2019-05-12 jcs case 'j':
5639 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5640 f7140bf5 2021-10-17 thomas case CTRL('n'):
5641 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5642 1e37a5c2 2019-05-12 jcs s->selected++;
5643 1e37a5c2 2019-05-12 jcs break;
5644 1e37a5c2 2019-05-12 jcs }
5645 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5646 56e0773d 2019-11-28 stsp == NULL)
5647 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5648 1e37a5c2 2019-05-12 jcs break;
5649 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5650 1e37a5c2 2019-05-12 jcs break;
5651 70f17a53 2022-06-13 thomas case CTRL('d'):
5652 70f17a53 2022-06-13 thomas nscroll /= 2;
5653 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5654 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5655 ea025d1d 2020-02-22 naddy case CTRL('f'):
5656 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5657 1e37a5c2 2019-05-12 jcs == NULL) {
5658 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5659 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5660 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
5661 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
5662 1e37a5c2 2019-05-12 jcs break;
5663 1e37a5c2 2019-05-12 jcs }
5664 70f17a53 2022-06-13 thomas tree_scroll_down(s, nscroll);
5665 1e37a5c2 2019-05-12 jcs break;
5666 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5667 1e37a5c2 2019-05-12 jcs case '\r':
5668 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5669 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5670 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5671 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5672 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5673 1e37a5c2 2019-05-12 jcs break;
5674 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5675 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5676 1e37a5c2 2019-05-12 jcs entry);
5677 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5678 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5679 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5680 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5681 1e37a5c2 2019-05-12 jcs s->selected_entry =
5682 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5683 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5684 1e37a5c2 2019-05-12 jcs free(parent);
5685 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5686 56e0773d 2019-11-28 stsp s->selected_entry))) {
5687 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5688 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5689 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5690 1e37a5c2 2019-05-12 jcs if (err)
5691 1e37a5c2 2019-05-12 jcs break;
5692 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5693 941e9f74 2019-05-21 stsp if (err) {
5694 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5695 1e37a5c2 2019-05-12 jcs break;
5696 1e37a5c2 2019-05-12 jcs }
5697 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5698 56e0773d 2019-11-28 stsp s->selected_entry))) {
5699 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5700 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5701 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5702 1e37a5c2 2019-05-12 jcs
5703 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5704 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5705 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5706 1e37a5c2 2019-05-12 jcs if (err)
5707 1e37a5c2 2019-05-12 jcs break;
5708 e78dc838 2020-12-04 stsp view->focussed = 0;
5709 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5710 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5711 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5712 669b5ffa 2018-10-07 stsp if (err)
5713 669b5ffa 2018-10-07 stsp return err;
5714 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5715 e78dc838 2020-12-04 stsp view->focus_child = 1;
5716 669b5ffa 2018-10-07 stsp } else
5717 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5718 1e37a5c2 2019-05-12 jcs }
5719 1e37a5c2 2019-05-12 jcs break;
5720 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5721 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5722 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5723 1e37a5c2 2019-05-12 jcs break;
5724 1e37a5c2 2019-05-12 jcs default:
5725 1e37a5c2 2019-05-12 jcs break;
5726 ffd1d5e5 2018-06-23 stsp }
5727 e5a0f69f 2018-08-18 stsp
5728 ffd1d5e5 2018-06-23 stsp return err;
5729 9f7d7167 2018-04-29 stsp }
5730 9f7d7167 2018-04-29 stsp
5731 ffd1d5e5 2018-06-23 stsp __dead static void
5732 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5733 ffd1d5e5 2018-06-23 stsp {
5734 ffd1d5e5 2018-06-23 stsp endwin();
5735 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5736 ffd1d5e5 2018-06-23 stsp getprogname());
5737 ffd1d5e5 2018-06-23 stsp exit(1);
5738 ffd1d5e5 2018-06-23 stsp }
5739 ffd1d5e5 2018-06-23 stsp
5740 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5741 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5742 ffd1d5e5 2018-06-23 stsp {
5743 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5744 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5745 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5746 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5747 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5748 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5749 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5750 4e97c21c 2020-12-06 stsp char *label = NULL;
5751 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5752 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5753 ffd1d5e5 2018-06-23 stsp int ch;
5754 5221c383 2018-08-01 stsp struct tog_view *view;
5755 70ac5f84 2019-03-28 stsp
5756 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5757 ffd1d5e5 2018-06-23 stsp switch (ch) {
5758 ffd1d5e5 2018-06-23 stsp case 'c':
5759 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5760 74283ab8 2020-02-07 stsp break;
5761 74283ab8 2020-02-07 stsp case 'r':
5762 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5763 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5764 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5765 74283ab8 2020-02-07 stsp optarg);
5766 ffd1d5e5 2018-06-23 stsp break;
5767 ffd1d5e5 2018-06-23 stsp default:
5768 e99e2d15 2020-11-24 naddy usage_tree();
5769 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5770 ffd1d5e5 2018-06-23 stsp }
5771 ffd1d5e5 2018-06-23 stsp }
5772 ffd1d5e5 2018-06-23 stsp
5773 ffd1d5e5 2018-06-23 stsp argc -= optind;
5774 ffd1d5e5 2018-06-23 stsp argv += optind;
5775 ffd1d5e5 2018-06-23 stsp
5776 55cccc34 2020-02-20 stsp if (argc > 1)
5777 e99e2d15 2020-11-24 naddy usage_tree();
5778 74283ab8 2020-02-07 stsp
5779 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5780 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5781 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5782 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5783 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5784 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5785 c156c7a4 2020-12-18 stsp goto done;
5786 55cccc34 2020-02-20 stsp if (worktree)
5787 52185f70 2019-02-05 stsp repo_path =
5788 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5789 55cccc34 2020-02-20 stsp else
5790 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5791 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5792 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5793 c156c7a4 2020-12-18 stsp goto done;
5794 c156c7a4 2020-12-18 stsp }
5795 55cccc34 2020-02-20 stsp }
5796 a915003a 2019-02-05 stsp
5797 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5798 c02c541e 2019-03-29 stsp if (error != NULL)
5799 52185f70 2019-02-05 stsp goto done;
5800 d188b9a6 2019-01-04 stsp
5801 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5802 55cccc34 2020-02-20 stsp repo, worktree);
5803 55cccc34 2020-02-20 stsp if (error)
5804 55cccc34 2020-02-20 stsp goto done;
5805 55cccc34 2020-02-20 stsp
5806 55cccc34 2020-02-20 stsp init_curses();
5807 55cccc34 2020-02-20 stsp
5808 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5809 c02c541e 2019-03-29 stsp if (error)
5810 52185f70 2019-02-05 stsp goto done;
5811 ffd1d5e5 2018-06-23 stsp
5812 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5813 51a10b52 2020-12-26 stsp if (error)
5814 51a10b52 2020-12-26 stsp goto done;
5815 51a10b52 2020-12-26 stsp
5816 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5817 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5818 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5819 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5820 4e97c21c 2020-12-06 stsp if (error)
5821 4e97c21c 2020-12-06 stsp goto done;
5822 4e97c21c 2020-12-06 stsp head_ref_name = label;
5823 4e97c21c 2020-12-06 stsp } else {
5824 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5825 4e97c21c 2020-12-06 stsp if (error == NULL)
5826 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5827 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5828 4e97c21c 2020-12-06 stsp goto done;
5829 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5830 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5831 4e97c21c 2020-12-06 stsp if (error)
5832 4e97c21c 2020-12-06 stsp goto done;
5833 4e97c21c 2020-12-06 stsp }
5834 ffd1d5e5 2018-06-23 stsp
5835 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5836 945f9229 2022-04-16 thomas if (error)
5837 945f9229 2022-04-16 thomas goto done;
5838 945f9229 2022-04-16 thomas
5839 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5840 5221c383 2018-08-01 stsp if (view == NULL) {
5841 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5842 5221c383 2018-08-01 stsp goto done;
5843 5221c383 2018-08-01 stsp }
5844 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5845 ad80ab7b 2018-08-04 stsp if (error)
5846 ad80ab7b 2018-08-04 stsp goto done;
5847 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5848 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5849 d91faf3b 2020-12-01 naddy in_repo_path);
5850 55cccc34 2020-02-20 stsp if (error)
5851 55cccc34 2020-02-20 stsp goto done;
5852 55cccc34 2020-02-20 stsp }
5853 55cccc34 2020-02-20 stsp
5854 55cccc34 2020-02-20 stsp if (worktree) {
5855 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5856 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5857 55cccc34 2020-02-20 stsp worktree = NULL;
5858 55cccc34 2020-02-20 stsp }
5859 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5860 ffd1d5e5 2018-06-23 stsp done:
5861 52185f70 2019-02-05 stsp free(repo_path);
5862 e4a0e26d 2020-02-20 stsp free(cwd);
5863 ffd1d5e5 2018-06-23 stsp free(commit_id);
5864 4e97c21c 2020-12-06 stsp free(label);
5865 486cd271 2020-12-06 stsp if (ref)
5866 486cd271 2020-12-06 stsp got_ref_close(ref);
5867 1d0f4054 2021-06-17 stsp if (repo) {
5868 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5869 1d0f4054 2021-06-17 stsp if (error == NULL)
5870 1d0f4054 2021-06-17 stsp error = close_err;
5871 1d0f4054 2021-06-17 stsp }
5872 51a10b52 2020-12-26 stsp tog_free_refs();
5873 ffd1d5e5 2018-06-23 stsp return error;
5874 6458efa5 2020-11-24 stsp }
5875 6458efa5 2020-11-24 stsp
5876 6458efa5 2020-11-24 stsp static const struct got_error *
5877 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5878 6458efa5 2020-11-24 stsp {
5879 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5880 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5881 6458efa5 2020-11-24 stsp
5882 6458efa5 2020-11-24 stsp s->nrefs = 0;
5883 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5884 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5885 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5886 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5887 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5888 6458efa5 2020-11-24 stsp continue;
5889 6458efa5 2020-11-24 stsp
5890 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5891 6458efa5 2020-11-24 stsp if (re == NULL)
5892 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5893 6458efa5 2020-11-24 stsp
5894 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5895 8924d611 2020-12-26 stsp if (re->ref == NULL)
5896 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5897 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5898 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5899 6458efa5 2020-11-24 stsp }
5900 6458efa5 2020-11-24 stsp
5901 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5902 6458efa5 2020-11-24 stsp return NULL;
5903 6458efa5 2020-11-24 stsp }
5904 6458efa5 2020-11-24 stsp
5905 6458efa5 2020-11-24 stsp void
5906 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5907 6458efa5 2020-11-24 stsp {
5908 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5909 6458efa5 2020-11-24 stsp
5910 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5911 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5912 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5913 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5914 6458efa5 2020-11-24 stsp free(re);
5915 6458efa5 2020-11-24 stsp }
5916 6458efa5 2020-11-24 stsp }
5917 6458efa5 2020-11-24 stsp
5918 6458efa5 2020-11-24 stsp static const struct got_error *
5919 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5920 6458efa5 2020-11-24 stsp {
5921 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5922 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5923 6458efa5 2020-11-24 stsp
5924 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5925 6458efa5 2020-11-24 stsp s->repo = repo;
5926 6458efa5 2020-11-24 stsp
5927 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5928 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5929 6458efa5 2020-11-24 stsp
5930 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5931 6458efa5 2020-11-24 stsp if (err)
5932 6458efa5 2020-11-24 stsp return err;
5933 34ba6917 2020-11-30 stsp
5934 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5935 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5936 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5937 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5938 6458efa5 2020-11-24 stsp if (err)
5939 6458efa5 2020-11-24 stsp goto done;
5940 6458efa5 2020-11-24 stsp
5941 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5942 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5943 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5944 6458efa5 2020-11-24 stsp if (err)
5945 6458efa5 2020-11-24 stsp goto done;
5946 6458efa5 2020-11-24 stsp
5947 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5948 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5949 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5950 2183bbf6 2022-01-23 thomas if (err)
5951 2183bbf6 2022-01-23 thomas goto done;
5952 2183bbf6 2022-01-23 thomas
5953 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5954 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5955 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5956 6458efa5 2020-11-24 stsp if (err)
5957 6458efa5 2020-11-24 stsp goto done;
5958 6458efa5 2020-11-24 stsp }
5959 6458efa5 2020-11-24 stsp
5960 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5961 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5962 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5963 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5964 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5965 6458efa5 2020-11-24 stsp done:
5966 6458efa5 2020-11-24 stsp if (err)
5967 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5968 6458efa5 2020-11-24 stsp return err;
5969 6458efa5 2020-11-24 stsp }
5970 6458efa5 2020-11-24 stsp
5971 6458efa5 2020-11-24 stsp static const struct got_error *
5972 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5973 6458efa5 2020-11-24 stsp {
5974 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5975 6458efa5 2020-11-24 stsp
5976 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5977 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5978 6458efa5 2020-11-24 stsp
5979 6458efa5 2020-11-24 stsp return NULL;
5980 9f7d7167 2018-04-29 stsp }
5981 ce5b7c56 2019-07-09 stsp
5982 6458efa5 2020-11-24 stsp static const struct got_error *
5983 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5984 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5985 6458efa5 2020-11-24 stsp {
5986 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5987 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5988 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5989 6458efa5 2020-11-24 stsp int obj_type;
5990 6458efa5 2020-11-24 stsp
5991 c42c9805 2020-11-24 stsp *commit_id = NULL;
5992 6458efa5 2020-11-24 stsp
5993 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5994 6458efa5 2020-11-24 stsp if (err)
5995 6458efa5 2020-11-24 stsp return err;
5996 6458efa5 2020-11-24 stsp
5997 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5998 6458efa5 2020-11-24 stsp if (err)
5999 6458efa5 2020-11-24 stsp goto done;
6000 6458efa5 2020-11-24 stsp
6001 6458efa5 2020-11-24 stsp switch (obj_type) {
6002 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6003 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6004 6458efa5 2020-11-24 stsp break;
6005 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6006 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6007 6458efa5 2020-11-24 stsp if (err)
6008 6458efa5 2020-11-24 stsp goto done;
6009 c42c9805 2020-11-24 stsp free(obj_id);
6010 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6011 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6012 c42c9805 2020-11-24 stsp if (err)
6013 6458efa5 2020-11-24 stsp goto done;
6014 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6015 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6016 c42c9805 2020-11-24 stsp goto done;
6017 c42c9805 2020-11-24 stsp }
6018 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6019 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6020 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6021 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6022 c42c9805 2020-11-24 stsp goto done;
6023 c42c9805 2020-11-24 stsp }
6024 6458efa5 2020-11-24 stsp break;
6025 6458efa5 2020-11-24 stsp default:
6026 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6027 c42c9805 2020-11-24 stsp break;
6028 6458efa5 2020-11-24 stsp }
6029 6458efa5 2020-11-24 stsp
6030 c42c9805 2020-11-24 stsp done:
6031 c42c9805 2020-11-24 stsp if (tag)
6032 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6033 c42c9805 2020-11-24 stsp if (err) {
6034 c42c9805 2020-11-24 stsp free(*commit_id);
6035 c42c9805 2020-11-24 stsp *commit_id = NULL;
6036 c42c9805 2020-11-24 stsp }
6037 c42c9805 2020-11-24 stsp return err;
6038 c42c9805 2020-11-24 stsp }
6039 c42c9805 2020-11-24 stsp
6040 c42c9805 2020-11-24 stsp static const struct got_error *
6041 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
6042 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6043 c42c9805 2020-11-24 stsp {
6044 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6045 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6046 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6047 c42c9805 2020-11-24 stsp
6048 c42c9805 2020-11-24 stsp *new_view = NULL;
6049 c42c9805 2020-11-24 stsp
6050 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6051 c42c9805 2020-11-24 stsp if (err) {
6052 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6053 c42c9805 2020-11-24 stsp return err;
6054 c42c9805 2020-11-24 stsp else
6055 c42c9805 2020-11-24 stsp return NULL;
6056 c42c9805 2020-11-24 stsp }
6057 c42c9805 2020-11-24 stsp
6058 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6059 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6060 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6061 6458efa5 2020-11-24 stsp goto done;
6062 6458efa5 2020-11-24 stsp }
6063 6458efa5 2020-11-24 stsp
6064 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6065 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6066 6458efa5 2020-11-24 stsp done:
6067 6458efa5 2020-11-24 stsp if (err)
6068 6458efa5 2020-11-24 stsp view_close(log_view);
6069 6458efa5 2020-11-24 stsp else
6070 6458efa5 2020-11-24 stsp *new_view = log_view;
6071 c42c9805 2020-11-24 stsp free(commit_id);
6072 6458efa5 2020-11-24 stsp return err;
6073 6458efa5 2020-11-24 stsp }
6074 6458efa5 2020-11-24 stsp
6075 ce5b7c56 2019-07-09 stsp static void
6076 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6077 6458efa5 2020-11-24 stsp {
6078 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6079 34ba6917 2020-11-30 stsp int i = 0;
6080 6458efa5 2020-11-24 stsp
6081 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6082 6458efa5 2020-11-24 stsp return;
6083 6458efa5 2020-11-24 stsp
6084 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6085 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6086 34ba6917 2020-11-30 stsp if (re == NULL)
6087 34ba6917 2020-11-30 stsp break;
6088 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6089 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6090 6458efa5 2020-11-24 stsp }
6091 6458efa5 2020-11-24 stsp }
6092 6458efa5 2020-11-24 stsp
6093 34ba6917 2020-11-30 stsp static void
6094 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6095 6458efa5 2020-11-24 stsp {
6096 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6097 6458efa5 2020-11-24 stsp int n = 0;
6098 6458efa5 2020-11-24 stsp
6099 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6100 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6101 6458efa5 2020-11-24 stsp else
6102 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6103 6458efa5 2020-11-24 stsp
6104 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6105 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6106 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6107 6458efa5 2020-11-24 stsp if (last) {
6108 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6109 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6110 6458efa5 2020-11-24 stsp }
6111 6458efa5 2020-11-24 stsp }
6112 6458efa5 2020-11-24 stsp }
6113 6458efa5 2020-11-24 stsp
6114 6458efa5 2020-11-24 stsp static const struct got_error *
6115 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6116 6458efa5 2020-11-24 stsp {
6117 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6118 6458efa5 2020-11-24 stsp
6119 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6120 6458efa5 2020-11-24 stsp return NULL;
6121 6458efa5 2020-11-24 stsp }
6122 6458efa5 2020-11-24 stsp
6123 6458efa5 2020-11-24 stsp static int
6124 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6125 6458efa5 2020-11-24 stsp {
6126 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6127 6458efa5 2020-11-24 stsp
6128 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6129 6458efa5 2020-11-24 stsp 0) == 0;
6130 6458efa5 2020-11-24 stsp }
6131 6458efa5 2020-11-24 stsp
6132 6458efa5 2020-11-24 stsp static const struct got_error *
6133 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6134 6458efa5 2020-11-24 stsp {
6135 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6136 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6137 6458efa5 2020-11-24 stsp
6138 6458efa5 2020-11-24 stsp if (!view->searching) {
6139 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6140 6458efa5 2020-11-24 stsp return NULL;
6141 6458efa5 2020-11-24 stsp }
6142 6458efa5 2020-11-24 stsp
6143 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6144 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6145 6458efa5 2020-11-24 stsp if (s->selected_entry)
6146 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6147 6458efa5 2020-11-24 stsp else
6148 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6149 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6150 6458efa5 2020-11-24 stsp } else {
6151 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6152 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6153 6458efa5 2020-11-24 stsp else
6154 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6155 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6156 6458efa5 2020-11-24 stsp }
6157 6458efa5 2020-11-24 stsp } else {
6158 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6159 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6160 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6161 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6162 6458efa5 2020-11-24 stsp else
6163 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6164 6458efa5 2020-11-24 stsp }
6165 6458efa5 2020-11-24 stsp
6166 6458efa5 2020-11-24 stsp while (1) {
6167 6458efa5 2020-11-24 stsp if (re == NULL) {
6168 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6169 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6170 6458efa5 2020-11-24 stsp return NULL;
6171 6458efa5 2020-11-24 stsp }
6172 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6173 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6174 6458efa5 2020-11-24 stsp else
6175 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6176 6458efa5 2020-11-24 stsp }
6177 6458efa5 2020-11-24 stsp
6178 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6179 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6180 6458efa5 2020-11-24 stsp s->matched_entry = re;
6181 6458efa5 2020-11-24 stsp break;
6182 6458efa5 2020-11-24 stsp }
6183 6458efa5 2020-11-24 stsp
6184 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6185 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6186 6458efa5 2020-11-24 stsp else
6187 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6188 6458efa5 2020-11-24 stsp }
6189 6458efa5 2020-11-24 stsp
6190 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6191 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6192 6458efa5 2020-11-24 stsp s->selected = 0;
6193 6458efa5 2020-11-24 stsp }
6194 6458efa5 2020-11-24 stsp
6195 6458efa5 2020-11-24 stsp return NULL;
6196 6458efa5 2020-11-24 stsp }
6197 6458efa5 2020-11-24 stsp
6198 6458efa5 2020-11-24 stsp static const struct got_error *
6199 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6200 6458efa5 2020-11-24 stsp {
6201 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6202 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6203 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6204 6458efa5 2020-11-24 stsp char *line = NULL;
6205 6458efa5 2020-11-24 stsp wchar_t *wline;
6206 6458efa5 2020-11-24 stsp struct tog_color *tc;
6207 6458efa5 2020-11-24 stsp int width, n;
6208 6458efa5 2020-11-24 stsp int limit = view->nlines;
6209 6458efa5 2020-11-24 stsp
6210 6458efa5 2020-11-24 stsp werase(view->window);
6211 6458efa5 2020-11-24 stsp
6212 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6213 6458efa5 2020-11-24 stsp
6214 6458efa5 2020-11-24 stsp if (limit == 0)
6215 6458efa5 2020-11-24 stsp return NULL;
6216 6458efa5 2020-11-24 stsp
6217 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6218 6458efa5 2020-11-24 stsp
6219 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6220 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6221 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6222 6458efa5 2020-11-24 stsp
6223 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6224 6458efa5 2020-11-24 stsp if (err) {
6225 6458efa5 2020-11-24 stsp free(line);
6226 6458efa5 2020-11-24 stsp return err;
6227 6458efa5 2020-11-24 stsp }
6228 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6229 6458efa5 2020-11-24 stsp wstandout(view->window);
6230 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6231 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6232 6458efa5 2020-11-24 stsp wstandend(view->window);
6233 6458efa5 2020-11-24 stsp free(wline);
6234 6458efa5 2020-11-24 stsp wline = NULL;
6235 6458efa5 2020-11-24 stsp free(line);
6236 6458efa5 2020-11-24 stsp line = NULL;
6237 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6238 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6239 6458efa5 2020-11-24 stsp if (--limit <= 0)
6240 6458efa5 2020-11-24 stsp return NULL;
6241 6458efa5 2020-11-24 stsp
6242 6458efa5 2020-11-24 stsp n = 0;
6243 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6244 6458efa5 2020-11-24 stsp char *line = NULL;
6245 6458efa5 2020-11-24 stsp
6246 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6247 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6248 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6249 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6250 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6251 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6252 6458efa5 2020-11-24 stsp struct got_object_id *id;
6253 6458efa5 2020-11-24 stsp char *id_str;
6254 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6255 6458efa5 2020-11-24 stsp if (err)
6256 6458efa5 2020-11-24 stsp return err;
6257 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6258 6458efa5 2020-11-24 stsp if (err) {
6259 6458efa5 2020-11-24 stsp free(id);
6260 6458efa5 2020-11-24 stsp return err;
6261 6458efa5 2020-11-24 stsp }
6262 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6263 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6264 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6265 6458efa5 2020-11-24 stsp free(id);
6266 6458efa5 2020-11-24 stsp free(id_str);
6267 6458efa5 2020-11-24 stsp return err;
6268 6458efa5 2020-11-24 stsp }
6269 6458efa5 2020-11-24 stsp free(id);
6270 6458efa5 2020-11-24 stsp free(id_str);
6271 6458efa5 2020-11-24 stsp } else {
6272 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6273 6458efa5 2020-11-24 stsp if (line == NULL)
6274 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6275 6458efa5 2020-11-24 stsp }
6276 6458efa5 2020-11-24 stsp
6277 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6278 6458efa5 2020-11-24 stsp if (err) {
6279 6458efa5 2020-11-24 stsp free(line);
6280 6458efa5 2020-11-24 stsp return err;
6281 6458efa5 2020-11-24 stsp }
6282 6458efa5 2020-11-24 stsp if (n == s->selected) {
6283 6458efa5 2020-11-24 stsp if (view->focussed)
6284 6458efa5 2020-11-24 stsp wstandout(view->window);
6285 6458efa5 2020-11-24 stsp s->selected_entry = re;
6286 6458efa5 2020-11-24 stsp }
6287 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6288 6458efa5 2020-11-24 stsp if (tc)
6289 6458efa5 2020-11-24 stsp wattr_on(view->window,
6290 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6291 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6292 6458efa5 2020-11-24 stsp if (tc)
6293 6458efa5 2020-11-24 stsp wattr_off(view->window,
6294 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6295 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6296 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6297 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6298 6458efa5 2020-11-24 stsp wstandend(view->window);
6299 6458efa5 2020-11-24 stsp free(line);
6300 6458efa5 2020-11-24 stsp free(wline);
6301 6458efa5 2020-11-24 stsp wline = NULL;
6302 6458efa5 2020-11-24 stsp n++;
6303 6458efa5 2020-11-24 stsp s->ndisplayed++;
6304 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6305 6458efa5 2020-11-24 stsp
6306 6458efa5 2020-11-24 stsp limit--;
6307 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6308 6458efa5 2020-11-24 stsp }
6309 6458efa5 2020-11-24 stsp
6310 6458efa5 2020-11-24 stsp view_vborder(view);
6311 6458efa5 2020-11-24 stsp return err;
6312 6458efa5 2020-11-24 stsp }
6313 6458efa5 2020-11-24 stsp
6314 6458efa5 2020-11-24 stsp static const struct got_error *
6315 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6316 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6317 c42c9805 2020-11-24 stsp {
6318 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6319 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6320 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6321 c42c9805 2020-11-24 stsp
6322 c42c9805 2020-11-24 stsp *new_view = NULL;
6323 c42c9805 2020-11-24 stsp
6324 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6325 c42c9805 2020-11-24 stsp if (err) {
6326 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6327 c42c9805 2020-11-24 stsp return err;
6328 c42c9805 2020-11-24 stsp else
6329 c42c9805 2020-11-24 stsp return NULL;
6330 c42c9805 2020-11-24 stsp }
6331 c42c9805 2020-11-24 stsp
6332 c42c9805 2020-11-24 stsp
6333 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6334 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6335 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6336 c42c9805 2020-11-24 stsp goto done;
6337 c42c9805 2020-11-24 stsp }
6338 c42c9805 2020-11-24 stsp
6339 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6340 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6341 c42c9805 2020-11-24 stsp if (err)
6342 c42c9805 2020-11-24 stsp goto done;
6343 c42c9805 2020-11-24 stsp
6344 c42c9805 2020-11-24 stsp *new_view = tree_view;
6345 c42c9805 2020-11-24 stsp done:
6346 c42c9805 2020-11-24 stsp free(commit_id);
6347 c42c9805 2020-11-24 stsp return err;
6348 c42c9805 2020-11-24 stsp }
6349 c42c9805 2020-11-24 stsp static const struct got_error *
6350 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6351 6458efa5 2020-11-24 stsp {
6352 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6353 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6354 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6355 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6356 70f17a53 2022-06-13 thomas int begin_x = 0, n, nscroll = view->nlines - 1;
6357 6458efa5 2020-11-24 stsp
6358 6458efa5 2020-11-24 stsp switch (ch) {
6359 6458efa5 2020-11-24 stsp case 'i':
6360 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6361 3bfadbd4 2021-11-20 thomas break;
6362 98182bd0 2021-11-20 thomas case 'o':
6363 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6364 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6365 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6366 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6367 c591440f 2021-11-20 thomas if (err)
6368 c591440f 2021-11-20 thomas break;
6369 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6370 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6371 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6372 3bfadbd4 2021-11-20 thomas if (err)
6373 3bfadbd4 2021-11-20 thomas break;
6374 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6375 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6376 6458efa5 2020-11-24 stsp break;
6377 6458efa5 2020-11-24 stsp case KEY_ENTER:
6378 6458efa5 2020-11-24 stsp case '\r':
6379 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6380 6458efa5 2020-11-24 stsp break;
6381 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6382 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6383 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6384 6458efa5 2020-11-24 stsp s->repo);
6385 e78dc838 2020-12-04 stsp view->focussed = 0;
6386 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6387 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6388 6458efa5 2020-11-24 stsp err = view_close_child(view);
6389 6458efa5 2020-11-24 stsp if (err)
6390 6458efa5 2020-11-24 stsp return err;
6391 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6392 e78dc838 2020-12-04 stsp view->focus_child = 1;
6393 6458efa5 2020-11-24 stsp } else
6394 6458efa5 2020-11-24 stsp *new_view = log_view;
6395 6458efa5 2020-11-24 stsp break;
6396 c42c9805 2020-11-24 stsp case 't':
6397 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6398 c42c9805 2020-11-24 stsp break;
6399 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6400 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6401 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6402 c42c9805 2020-11-24 stsp s->repo);
6403 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6404 c42c9805 2020-11-24 stsp break;
6405 e78dc838 2020-12-04 stsp view->focussed = 0;
6406 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6407 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6408 c42c9805 2020-11-24 stsp err = view_close_child(view);
6409 c42c9805 2020-11-24 stsp if (err)
6410 c42c9805 2020-11-24 stsp return err;
6411 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6412 e78dc838 2020-12-04 stsp view->focus_child = 1;
6413 c42c9805 2020-11-24 stsp } else
6414 c42c9805 2020-11-24 stsp *new_view = tree_view;
6415 c42c9805 2020-11-24 stsp break;
6416 e4526bf5 2021-09-03 naddy case 'g':
6417 e4526bf5 2021-09-03 naddy case KEY_HOME:
6418 e4526bf5 2021-09-03 naddy s->selected = 0;
6419 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6420 e4526bf5 2021-09-03 naddy break;
6421 e4526bf5 2021-09-03 naddy case 'G':
6422 e4526bf5 2021-09-03 naddy case KEY_END:
6423 e4526bf5 2021-09-03 naddy s->selected = 0;
6424 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6425 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6426 e4526bf5 2021-09-03 naddy if (re == NULL)
6427 e4526bf5 2021-09-03 naddy break;
6428 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6429 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6430 e4526bf5 2021-09-03 naddy }
6431 e4526bf5 2021-09-03 naddy if (n > 0)
6432 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6433 e4526bf5 2021-09-03 naddy break;
6434 6458efa5 2020-11-24 stsp case 'k':
6435 6458efa5 2020-11-24 stsp case KEY_UP:
6436 f7140bf5 2021-10-17 thomas case CTRL('p'):
6437 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6438 6458efa5 2020-11-24 stsp s->selected--;
6439 6458efa5 2020-11-24 stsp break;
6440 34ba6917 2020-11-30 stsp }
6441 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6442 6458efa5 2020-11-24 stsp break;
6443 70f17a53 2022-06-13 thomas case CTRL('u'):
6444 70f17a53 2022-06-13 thomas nscroll /= 2;
6445 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6446 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6447 6458efa5 2020-11-24 stsp case CTRL('b'):
6448 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6449 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
6450 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
6451 6458efa5 2020-11-24 stsp break;
6452 6458efa5 2020-11-24 stsp case 'j':
6453 6458efa5 2020-11-24 stsp case KEY_DOWN:
6454 f7140bf5 2021-10-17 thomas case CTRL('n'):
6455 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6456 6458efa5 2020-11-24 stsp s->selected++;
6457 6458efa5 2020-11-24 stsp break;
6458 6458efa5 2020-11-24 stsp }
6459 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6460 6458efa5 2020-11-24 stsp /* can't scroll any further */
6461 6458efa5 2020-11-24 stsp break;
6462 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6463 6458efa5 2020-11-24 stsp break;
6464 70f17a53 2022-06-13 thomas case CTRL('d'):
6465 70f17a53 2022-06-13 thomas nscroll /= 2;
6466 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6467 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6468 6458efa5 2020-11-24 stsp case CTRL('f'):
6469 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6470 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6471 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6472 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6473 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6474 6458efa5 2020-11-24 stsp break;
6475 6458efa5 2020-11-24 stsp }
6476 70f17a53 2022-06-13 thomas ref_scroll_down(s, nscroll);
6477 6458efa5 2020-11-24 stsp break;
6478 6458efa5 2020-11-24 stsp case CTRL('l'):
6479 8924d611 2020-12-26 stsp tog_free_refs();
6480 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6481 8924d611 2020-12-26 stsp if (err)
6482 8924d611 2020-12-26 stsp break;
6483 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6484 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6485 6458efa5 2020-11-24 stsp break;
6486 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6487 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6488 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6489 6458efa5 2020-11-24 stsp break;
6490 6458efa5 2020-11-24 stsp default:
6491 6458efa5 2020-11-24 stsp break;
6492 6458efa5 2020-11-24 stsp }
6493 6458efa5 2020-11-24 stsp
6494 6458efa5 2020-11-24 stsp return err;
6495 6458efa5 2020-11-24 stsp }
6496 6458efa5 2020-11-24 stsp
6497 6458efa5 2020-11-24 stsp __dead static void
6498 6458efa5 2020-11-24 stsp usage_ref(void)
6499 6458efa5 2020-11-24 stsp {
6500 6458efa5 2020-11-24 stsp endwin();
6501 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6502 6458efa5 2020-11-24 stsp getprogname());
6503 6458efa5 2020-11-24 stsp exit(1);
6504 6458efa5 2020-11-24 stsp }
6505 6458efa5 2020-11-24 stsp
6506 6458efa5 2020-11-24 stsp static const struct got_error *
6507 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6508 6458efa5 2020-11-24 stsp {
6509 6458efa5 2020-11-24 stsp const struct got_error *error;
6510 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6511 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6512 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6513 6458efa5 2020-11-24 stsp int ch;
6514 6458efa5 2020-11-24 stsp struct tog_view *view;
6515 6458efa5 2020-11-24 stsp
6516 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6517 6458efa5 2020-11-24 stsp switch (ch) {
6518 6458efa5 2020-11-24 stsp case 'r':
6519 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6520 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6521 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6522 6458efa5 2020-11-24 stsp optarg);
6523 6458efa5 2020-11-24 stsp break;
6524 6458efa5 2020-11-24 stsp default:
6525 e99e2d15 2020-11-24 naddy usage_ref();
6526 6458efa5 2020-11-24 stsp /* NOTREACHED */
6527 6458efa5 2020-11-24 stsp }
6528 6458efa5 2020-11-24 stsp }
6529 6458efa5 2020-11-24 stsp
6530 6458efa5 2020-11-24 stsp argc -= optind;
6531 6458efa5 2020-11-24 stsp argv += optind;
6532 6458efa5 2020-11-24 stsp
6533 6458efa5 2020-11-24 stsp if (argc > 1)
6534 e99e2d15 2020-11-24 naddy usage_ref();
6535 6458efa5 2020-11-24 stsp
6536 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6537 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6538 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6539 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6540 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6541 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6542 c156c7a4 2020-12-18 stsp goto done;
6543 6458efa5 2020-11-24 stsp if (worktree)
6544 6458efa5 2020-11-24 stsp repo_path =
6545 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6546 6458efa5 2020-11-24 stsp else
6547 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6548 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6549 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6550 c156c7a4 2020-12-18 stsp goto done;
6551 c156c7a4 2020-12-18 stsp }
6552 6458efa5 2020-11-24 stsp }
6553 6458efa5 2020-11-24 stsp
6554 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6555 6458efa5 2020-11-24 stsp if (error != NULL)
6556 6458efa5 2020-11-24 stsp goto done;
6557 6458efa5 2020-11-24 stsp
6558 6458efa5 2020-11-24 stsp init_curses();
6559 6458efa5 2020-11-24 stsp
6560 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6561 51a10b52 2020-12-26 stsp if (error)
6562 51a10b52 2020-12-26 stsp goto done;
6563 51a10b52 2020-12-26 stsp
6564 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6565 6458efa5 2020-11-24 stsp if (error)
6566 6458efa5 2020-11-24 stsp goto done;
6567 6458efa5 2020-11-24 stsp
6568 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6569 6458efa5 2020-11-24 stsp if (view == NULL) {
6570 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6571 6458efa5 2020-11-24 stsp goto done;
6572 6458efa5 2020-11-24 stsp }
6573 6458efa5 2020-11-24 stsp
6574 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6575 6458efa5 2020-11-24 stsp if (error)
6576 6458efa5 2020-11-24 stsp goto done;
6577 6458efa5 2020-11-24 stsp
6578 6458efa5 2020-11-24 stsp if (worktree) {
6579 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6580 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6581 6458efa5 2020-11-24 stsp worktree = NULL;
6582 6458efa5 2020-11-24 stsp }
6583 6458efa5 2020-11-24 stsp error = view_loop(view);
6584 6458efa5 2020-11-24 stsp done:
6585 6458efa5 2020-11-24 stsp free(repo_path);
6586 6458efa5 2020-11-24 stsp free(cwd);
6587 1d0f4054 2021-06-17 stsp if (repo) {
6588 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6589 1d0f4054 2021-06-17 stsp if (close_err)
6590 1d0f4054 2021-06-17 stsp error = close_err;
6591 1d0f4054 2021-06-17 stsp }
6592 51a10b52 2020-12-26 stsp tog_free_refs();
6593 6458efa5 2020-11-24 stsp return error;
6594 6458efa5 2020-11-24 stsp }
6595 6458efa5 2020-11-24 stsp
6596 6458efa5 2020-11-24 stsp static void
6597 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6598 ce5b7c56 2019-07-09 stsp {
6599 6059809a 2020-12-17 stsp size_t i;
6600 9f7d7167 2018-04-29 stsp
6601 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6602 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6603 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6604 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6605 ce5b7c56 2019-07-09 stsp }
6606 6879ba42 2020-10-01 naddy fputc('\n', fp);
6607 ce5b7c56 2019-07-09 stsp }
6608 ce5b7c56 2019-07-09 stsp
6609 4ed7e80c 2018-05-20 stsp __dead static void
6610 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6611 9f7d7167 2018-04-29 stsp {
6612 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6613 6879ba42 2020-10-01 naddy
6614 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6615 6879ba42 2020-10-01 naddy getprogname());
6616 6879ba42 2020-10-01 naddy if (hflag) {
6617 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6618 6879ba42 2020-10-01 naddy list_commands(fp);
6619 ee85c5e8 2020-02-29 stsp }
6620 6879ba42 2020-10-01 naddy exit(status);
6621 9f7d7167 2018-04-29 stsp }
6622 9f7d7167 2018-04-29 stsp
6623 c2301be8 2018-04-30 stsp static char **
6624 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6625 c2301be8 2018-04-30 stsp {
6626 ee85c5e8 2020-02-29 stsp va_list ap;
6627 c2301be8 2018-04-30 stsp char **argv;
6628 ee85c5e8 2020-02-29 stsp int i;
6629 c2301be8 2018-04-30 stsp
6630 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6631 ee85c5e8 2020-02-29 stsp
6632 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6633 c2301be8 2018-04-30 stsp if (argv == NULL)
6634 c2301be8 2018-04-30 stsp err(1, "calloc");
6635 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6636 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6637 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6638 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6639 c2301be8 2018-04-30 stsp }
6640 c2301be8 2018-04-30 stsp
6641 ee85c5e8 2020-02-29 stsp va_end(ap);
6642 c2301be8 2018-04-30 stsp return argv;
6643 ee85c5e8 2020-02-29 stsp }
6644 ee85c5e8 2020-02-29 stsp
6645 ee85c5e8 2020-02-29 stsp /*
6646 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6647 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6648 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6649 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6650 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6651 ee85c5e8 2020-02-29 stsp */
6652 ee85c5e8 2020-02-29 stsp static const struct got_error *
6653 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6654 ee85c5e8 2020-02-29 stsp {
6655 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6656 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6657 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6658 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6659 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6660 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6661 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6662 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6663 84de9106 2020-12-26 stsp
6664 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6665 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6666 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6667 ee85c5e8 2020-02-29 stsp
6668 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6669 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6670 ee85c5e8 2020-02-29 stsp goto done;
6671 ee85c5e8 2020-02-29 stsp
6672 ee85c5e8 2020-02-29 stsp if (worktree)
6673 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6674 ee85c5e8 2020-02-29 stsp else
6675 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6676 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6677 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6678 ee85c5e8 2020-02-29 stsp goto done;
6679 ee85c5e8 2020-02-29 stsp }
6680 ee85c5e8 2020-02-29 stsp
6681 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6682 ee85c5e8 2020-02-29 stsp if (error != NULL)
6683 ee85c5e8 2020-02-29 stsp goto done;
6684 ee85c5e8 2020-02-29 stsp
6685 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6686 ee85c5e8 2020-02-29 stsp repo, worktree);
6687 ee85c5e8 2020-02-29 stsp if (error)
6688 ee85c5e8 2020-02-29 stsp goto done;
6689 ee85c5e8 2020-02-29 stsp
6690 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6691 84de9106 2020-12-26 stsp if (error)
6692 84de9106 2020-12-26 stsp goto done;
6693 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6694 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6695 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6696 ee85c5e8 2020-02-29 stsp if (error)
6697 ee85c5e8 2020-02-29 stsp goto done;
6698 ee85c5e8 2020-02-29 stsp
6699 ee85c5e8 2020-02-29 stsp if (worktree) {
6700 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6701 ee85c5e8 2020-02-29 stsp worktree = NULL;
6702 ee85c5e8 2020-02-29 stsp }
6703 ee85c5e8 2020-02-29 stsp
6704 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6705 945f9229 2022-04-16 thomas if (error)
6706 945f9229 2022-04-16 thomas goto done;
6707 945f9229 2022-04-16 thomas
6708 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6709 ee85c5e8 2020-02-29 stsp if (error) {
6710 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6711 ee85c5e8 2020-02-29 stsp goto done;
6712 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6713 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6714 6879ba42 2020-10-01 naddy usage(1, 1);
6715 ee85c5e8 2020-02-29 stsp /* not reached */
6716 ee85c5e8 2020-02-29 stsp }
6717 ee85c5e8 2020-02-29 stsp
6718 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6719 1d0f4054 2021-06-17 stsp if (error == NULL)
6720 1d0f4054 2021-06-17 stsp error = close_err;
6721 ee85c5e8 2020-02-29 stsp repo = NULL;
6722 ee85c5e8 2020-02-29 stsp
6723 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6724 ee85c5e8 2020-02-29 stsp if (error)
6725 ee85c5e8 2020-02-29 stsp goto done;
6726 ee85c5e8 2020-02-29 stsp
6727 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6728 ee85c5e8 2020-02-29 stsp argc = 4;
6729 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6730 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6731 ee85c5e8 2020-02-29 stsp done:
6732 1d0f4054 2021-06-17 stsp if (repo) {
6733 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6734 1d0f4054 2021-06-17 stsp if (error == NULL)
6735 1d0f4054 2021-06-17 stsp error = close_err;
6736 1d0f4054 2021-06-17 stsp }
6737 945f9229 2022-04-16 thomas if (commit)
6738 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6739 ee85c5e8 2020-02-29 stsp if (worktree)
6740 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6741 ee85c5e8 2020-02-29 stsp free(id);
6742 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6743 ee85c5e8 2020-02-29 stsp free(commit_id);
6744 ee85c5e8 2020-02-29 stsp free(cwd);
6745 ee85c5e8 2020-02-29 stsp free(repo_path);
6746 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6747 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6748 ee85c5e8 2020-02-29 stsp int i;
6749 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6750 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6751 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6752 ee85c5e8 2020-02-29 stsp }
6753 87670572 2020-12-26 naddy tog_free_refs();
6754 ee85c5e8 2020-02-29 stsp return error;
6755 c2301be8 2018-04-30 stsp }
6756 c2301be8 2018-04-30 stsp
6757 9f7d7167 2018-04-29 stsp int
6758 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6759 9f7d7167 2018-04-29 stsp {
6760 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6761 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6762 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6763 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6764 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6765 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6766 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6767 83cd27f8 2020-01-13 stsp };
6768 9f7d7167 2018-04-29 stsp
6769 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6770 9f7d7167 2018-04-29 stsp
6771 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6772 9f7d7167 2018-04-29 stsp switch (ch) {
6773 9f7d7167 2018-04-29 stsp case 'h':
6774 9f7d7167 2018-04-29 stsp hflag = 1;
6775 9f7d7167 2018-04-29 stsp break;
6776 53ccebc2 2019-07-30 stsp case 'V':
6777 53ccebc2 2019-07-30 stsp Vflag = 1;
6778 53ccebc2 2019-07-30 stsp break;
6779 9f7d7167 2018-04-29 stsp default:
6780 6879ba42 2020-10-01 naddy usage(hflag, 1);
6781 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6782 9f7d7167 2018-04-29 stsp }
6783 9f7d7167 2018-04-29 stsp }
6784 9f7d7167 2018-04-29 stsp
6785 9f7d7167 2018-04-29 stsp argc -= optind;
6786 9f7d7167 2018-04-29 stsp argv += optind;
6787 9814e6a3 2020-09-27 naddy optind = 1;
6788 c2301be8 2018-04-30 stsp optreset = 1;
6789 9f7d7167 2018-04-29 stsp
6790 53ccebc2 2019-07-30 stsp if (Vflag) {
6791 53ccebc2 2019-07-30 stsp got_version_print_str();
6792 6879ba42 2020-10-01 naddy return 0;
6793 53ccebc2 2019-07-30 stsp }
6794 4010e238 2020-12-04 stsp
6795 4010e238 2020-12-04 stsp #ifndef PROFILE
6796 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6797 4010e238 2020-12-04 stsp NULL) == -1)
6798 4010e238 2020-12-04 stsp err(1, "pledge");
6799 4010e238 2020-12-04 stsp #endif
6800 53ccebc2 2019-07-30 stsp
6801 c2301be8 2018-04-30 stsp if (argc == 0) {
6802 f29d3e89 2018-06-23 stsp if (hflag)
6803 6879ba42 2020-10-01 naddy usage(hflag, 0);
6804 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6805 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6806 c2301be8 2018-04-30 stsp argc = 1;
6807 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6808 c2301be8 2018-04-30 stsp } else {
6809 6059809a 2020-12-17 stsp size_t i;
6810 9f7d7167 2018-04-29 stsp
6811 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6812 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6813 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6814 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6815 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6816 9f7d7167 2018-04-29 stsp break;
6817 9f7d7167 2018-04-29 stsp }
6818 9f7d7167 2018-04-29 stsp }
6819 ee85c5e8 2020-02-29 stsp }
6820 3642c4c6 2019-07-09 stsp
6821 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6822 ee85c5e8 2020-02-29 stsp if (argc != 1)
6823 6879ba42 2020-10-01 naddy usage(0, 1);
6824 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6825 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6826 ee85c5e8 2020-02-29 stsp } else {
6827 ee85c5e8 2020-02-29 stsp if (hflag)
6828 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6829 ee85c5e8 2020-02-29 stsp else
6830 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6831 9f7d7167 2018-04-29 stsp }
6832 9f7d7167 2018-04-29 stsp
6833 9f7d7167 2018-04-29 stsp endwin();
6834 b46c1e04 2020-09-20 naddy putchar('\n');
6835 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6836 a2f4a359 2020-02-28 stsp int i;
6837 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6838 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6839 a2f4a359 2020-02-28 stsp free(cmd_argv);
6840 a2f4a359 2020-02-28 stsp }
6841 a2f4a359 2020-02-28 stsp
6842 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6843 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6844 9f7d7167 2018-04-29 stsp return 0;
6845 9f7d7167 2018-04-29 stsp }