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 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.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 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
112 9b058f45 2022-06-30 mark };
113 9b058f45 2022-06-30 mark
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
115 9b058f45 2022-06-30 mark
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 d6b05b5b 2018-08-04 stsp
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
122 1a76625f 2018-10-22 stsp int idx;
123 ba4f502b 2018-08-04 stsp };
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
128 6d17833f 2019-11-08 stsp };
129 6d17833f 2019-11-08 stsp
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
134 15a087fe 2019-02-21 stsp };
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 11b20872 2019-11-08 stsp
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
140 cc488aa7 2022-01-23 stsp
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp {
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp *cmp = -1;
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp *cmp = 1;
157 cc488aa7 2022-01-23 stsp return NULL;
158 cc488aa7 2022-01-23 stsp }
159 cc488aa7 2022-01-23 stsp
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
162 cc488aa7 2022-01-23 stsp }
163 51a10b52 2020-12-26 stsp
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp {
167 51a10b52 2020-12-26 stsp const struct got_error *err;
168 51a10b52 2020-12-26 stsp
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 7f66531d 2021-11-16 stsp repo);
172 51a10b52 2020-12-26 stsp if (err)
173 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 51a10b52 2020-12-26 stsp repo);
177 51a10b52 2020-12-26 stsp }
178 51a10b52 2020-12-26 stsp
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp {
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp }
188 51a10b52 2020-12-26 stsp
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp {
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp free(tc);
216 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp }
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp }
222 11b20872 2019-11-08 stsp
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp {
226 11b20872 2019-11-08 stsp struct tog_color *tc;
227 11b20872 2019-11-08 stsp
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
232 11b20872 2019-11-08 stsp free(tc);
233 11b20872 2019-11-08 stsp }
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
243 11b20872 2019-11-08 stsp return tc;
244 11b20872 2019-11-08 stsp }
245 11b20872 2019-11-08 stsp
246 11b20872 2019-11-08 stsp return NULL;
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 11b20872 2019-11-08 stsp static int
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
282 11b20872 2019-11-08 stsp
283 11b20872 2019-11-08 stsp return -1;
284 11b20872 2019-11-08 stsp }
285 11b20872 2019-11-08 stsp
286 11b20872 2019-11-08 stsp static int
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp {
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 15a087fe 2019-02-21 stsp
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return -1;
312 11b20872 2019-11-08 stsp
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
314 11b20872 2019-11-08 stsp }
315 11b20872 2019-11-08 stsp
316 11b20872 2019-11-08 stsp
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int eof;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp
335 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
336 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
337 b01e7d3b 2018-08-04 stsp };
338 b01e7d3b 2018-08-04 stsp
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
341 1a76625f 2018-10-22 stsp
342 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
343 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
344 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
345 1a76625f 2018-10-22 stsp int commits_needed;
346 fb280deb 2021-08-30 stsp int load_all;
347 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
348 1a76625f 2018-10-22 stsp struct commit_queue *commits;
349 1a76625f 2018-10-22 stsp const char *in_repo_path;
350 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
351 1a76625f 2018-10-22 stsp struct got_repository *repo;
352 74467cc8 2022-06-15 stsp int *pack_fds;
353 1a76625f 2018-10-22 stsp int log_complete;
354 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
356 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
357 13add988 2019-10-15 stsp int *searching;
358 13add988 2019-10-15 stsp int *search_next_done;
359 13add988 2019-10-15 stsp regex_t *regex;
360 1a76625f 2018-10-22 stsp };
361 1a76625f 2018-10-22 stsp
362 1a76625f 2018-10-22 stsp struct tog_log_view_state {
363 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
366 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
367 b01e7d3b 2018-08-04 stsp int selected;
368 b01e7d3b 2018-08-04 stsp char *in_repo_path;
369 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
370 b672a97a 2020-01-27 stsp int log_branches;
371 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
372 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
373 1a76625f 2018-10-22 stsp sig_atomic_t quit;
374 1a76625f 2018-10-22 stsp pthread_t thread;
375 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
376 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
377 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
378 11b20872 2019-11-08 stsp struct tog_colors colors;
379 10aab77f 2022-07-19 op int use_committer;
380 ba4f502b 2018-08-04 stsp };
381 11b20872 2019-11-08 stsp
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
390 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
391 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
392 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
395 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
396 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
397 ba4f502b 2018-08-04 stsp
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
399 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
400 e9424729 2018-08-04 stsp int nlines;
401 e9424729 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_view *view;
403 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
404 e9424729 2018-08-04 stsp int *quit;
405 e9424729 2018-08-04 stsp };
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
408 e9424729 2018-08-04 stsp const char *path;
409 e9424729 2018-08-04 stsp struct got_repository *repo;
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
411 e9424729 2018-08-04 stsp int *complete;
412 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
413 fc06ba56 2019-08-22 stsp void *cancel_arg;
414 e9424729 2018-08-04 stsp };
415 e9424729 2018-08-04 stsp
416 e9424729 2018-08-04 stsp struct tog_blame {
417 e9424729 2018-08-04 stsp FILE *f;
418 be659d10 2020-11-18 stsp off_t filesize;
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
420 6fcac457 2018-11-19 stsp int nlines;
421 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
422 e9424729 2018-08-04 stsp pthread_t thread;
423 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
424 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
425 e9424729 2018-08-04 stsp const char *path;
426 0ae84acc 2022-06-15 tracey int *pack_fds;
427 e9424729 2018-08-04 stsp };
428 e9424729 2018-08-04 stsp
429 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
430 7cbe629d 2018-08-04 stsp int first_displayed_line;
431 7cbe629d 2018-08-04 stsp int last_displayed_line;
432 7cbe629d 2018-08-04 stsp int selected_line;
433 c0f61fa4 2022-07-11 mark int last_diffed_line;
434 7cbe629d 2018-08-04 stsp int blame_complete;
435 e5a0f69f 2018-08-18 stsp int eof;
436 e5a0f69f 2018-08-18 stsp int done;
437 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
438 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
439 e5a0f69f 2018-08-18 stsp char *path;
440 7cbe629d 2018-08-04 stsp struct got_repository *repo;
441 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
442 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
443 e9424729 2018-08-04 stsp struct tog_blame blame;
444 6c4c42e0 2019-06-24 stsp int matched_line;
445 11b20872 2019-11-08 stsp struct tog_colors colors;
446 ad80ab7b 2018-08-04 stsp };
447 ad80ab7b 2018-08-04 stsp
448 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
449 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
452 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
453 ad80ab7b 2018-08-04 stsp int selected;
454 ad80ab7b 2018-08-04 stsp };
455 ad80ab7b 2018-08-04 stsp
456 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 ad80ab7b 2018-08-04 stsp
458 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
459 ad80ab7b 2018-08-04 stsp char *tree_label;
460 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
462 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
465 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
466 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
467 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
469 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
470 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
471 6458efa5 2020-11-24 stsp struct tog_colors colors;
472 6458efa5 2020-11-24 stsp };
473 6458efa5 2020-11-24 stsp
474 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
475 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
476 6458efa5 2020-11-24 stsp struct got_reference *ref;
477 6458efa5 2020-11-24 stsp int idx;
478 6458efa5 2020-11-24 stsp };
479 6458efa5 2020-11-24 stsp
480 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 6458efa5 2020-11-24 stsp
482 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
483 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
487 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
488 6458efa5 2020-11-24 stsp struct got_repository *repo;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
490 bddb1296 2019-11-08 stsp struct tog_colors colors;
491 7cbe629d 2018-08-04 stsp };
492 7cbe629d 2018-08-04 stsp
493 669b5ffa 2018-10-07 stsp /*
494 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
495 669b5ffa 2018-10-07 stsp *
496 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
497 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
498 669b5ffa 2018-10-07 stsp * there is enough screen estate.
499 669b5ffa 2018-10-07 stsp *
500 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
501 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
502 669b5ffa 2018-10-07 stsp *
503 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
504 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
505 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
508 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
509 669b5ffa 2018-10-07 stsp */
510 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
511 669b5ffa 2018-10-07 stsp
512 cc3c9aac 2018-08-01 stsp struct tog_view {
513 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
514 26ed57b2 2018-05-19 stsp WINDOW *window;
515 26ed57b2 2018-05-19 stsp PANEL *panel;
516 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
517 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
518 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
519 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
520 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
521 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
522 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
523 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
524 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
525 9970f7fc 2020-12-03 stsp int dying;
526 669b5ffa 2018-10-07 stsp struct tog_view *parent;
527 669b5ffa 2018-10-07 stsp struct tog_view *child;
528 5dc9f4bc 2018-08-04 stsp
529 e78dc838 2020-12-04 stsp /*
530 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
531 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
532 e78dc838 2020-12-04 stsp * between parent and child.
533 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
534 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
535 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
536 e78dc838 2020-12-04 stsp * situations.
537 e78dc838 2020-12-04 stsp */
538 e78dc838 2020-12-04 stsp int focus_child;
539 e78dc838 2020-12-04 stsp
540 9b058f45 2022-06-30 mark enum tog_view_mode mode;
541 5dc9f4bc 2018-08-04 stsp /* type-specific state */
542 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
543 5dc9f4bc 2018-08-04 stsp union {
544 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
545 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
546 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
547 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
548 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
549 5dc9f4bc 2018-08-04 stsp } state;
550 e5a0f69f 2018-08-18 stsp
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
555 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
557 60493ae3 2019-06-20 stsp
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
559 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
560 c0c4acc8 2021-01-24 stsp int search_started;
561 60493ae3 2019-06-20 stsp int searching;
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
563 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
564 60493ae3 2019-06-20 stsp int search_next_done;
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
566 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
567 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
568 1803e47f 2019-06-22 stsp regex_t regex;
569 41605754 2020-11-12 stsp regmatch_t regmatch;
570 cc3c9aac 2018-08-01 stsp };
571 cd0acaa7 2018-05-20 stsp
572 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
573 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
574 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
575 78756c87 2020-11-24 stsp struct got_repository *);
576 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
582 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
583 e5a0f69f 2018-08-18 stsp
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
585 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
586 78756c87 2020-11-24 stsp const char *, const char *, int);
587 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
589 e78dc838 2020-12-04 stsp struct tog_view *, int);
590 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
591 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
593 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
594 e5a0f69f 2018-08-18 stsp
595 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
596 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
597 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
598 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
599 e78dc838 2020-12-04 stsp struct tog_view *, int);
600 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
603 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
604 e5a0f69f 2018-08-18 stsp
605 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
606 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
607 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
609 e78dc838 2020-12-04 stsp struct tog_view *, int);
610 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
612 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
613 6458efa5 2020-11-24 stsp
614 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
615 6458efa5 2020-11-24 stsp struct got_repository *);
616 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
617 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
618 e78dc838 2020-12-04 stsp struct tog_view *, int);
619 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
621 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
622 25791caa 2018-10-24 stsp
623 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
624 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
625 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
626 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
627 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
628 25791caa 2018-10-24 stsp
629 25791caa 2018-10-24 stsp static void
630 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
631 25791caa 2018-10-24 stsp {
632 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
633 83baff54 2019-08-12 stsp }
634 83baff54 2019-08-12 stsp
635 83baff54 2019-08-12 stsp static void
636 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
637 83baff54 2019-08-12 stsp {
638 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
639 25791caa 2018-10-24 stsp }
640 26ed57b2 2018-05-19 stsp
641 61266923 2020-01-14 stsp static void
642 61266923 2020-01-14 stsp tog_sigcont(int signo)
643 61266923 2020-01-14 stsp {
644 61266923 2020-01-14 stsp tog_sigcont_received = 1;
645 61266923 2020-01-14 stsp }
646 61266923 2020-01-14 stsp
647 2497f032 2022-05-31 stsp static void
648 2497f032 2022-05-31 stsp tog_sigint(int signo)
649 2497f032 2022-05-31 stsp {
650 2497f032 2022-05-31 stsp tog_sigint_received = 1;
651 2497f032 2022-05-31 stsp }
652 2497f032 2022-05-31 stsp
653 2497f032 2022-05-31 stsp static void
654 2497f032 2022-05-31 stsp tog_sigterm(int signo)
655 2497f032 2022-05-31 stsp {
656 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
657 2497f032 2022-05-31 stsp }
658 2497f032 2022-05-31 stsp
659 2497f032 2022-05-31 stsp static int
660 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
661 2497f032 2022-05-31 stsp {
662 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
663 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
664 2497f032 2022-05-31 stsp }
665 2497f032 2022-05-31 stsp
666 e5a0f69f 2018-08-18 stsp static const struct got_error *
667 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
668 ea5e7bb5 2018-08-01 stsp {
669 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
670 e5a0f69f 2018-08-18 stsp
671 669b5ffa 2018-10-07 stsp if (view->child) {
672 5629093a 2022-07-11 stsp child_err = view_close(view->child);
673 669b5ffa 2018-10-07 stsp view->child = NULL;
674 669b5ffa 2018-10-07 stsp }
675 e5a0f69f 2018-08-18 stsp if (view->close)
676 e5a0f69f 2018-08-18 stsp err = view->close(view);
677 ea5e7bb5 2018-08-01 stsp if (view->panel)
678 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
679 ea5e7bb5 2018-08-01 stsp if (view->window)
680 ea5e7bb5 2018-08-01 stsp delwin(view->window);
681 ea5e7bb5 2018-08-01 stsp free(view);
682 5629093a 2022-07-11 stsp return err ? err : child_err;
683 ea5e7bb5 2018-08-01 stsp }
684 ea5e7bb5 2018-08-01 stsp
685 ea5e7bb5 2018-08-01 stsp static struct tog_view *
686 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
687 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
688 ea5e7bb5 2018-08-01 stsp {
689 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
690 ea5e7bb5 2018-08-01 stsp
691 ea5e7bb5 2018-08-01 stsp if (view == NULL)
692 ea5e7bb5 2018-08-01 stsp return NULL;
693 ea5e7bb5 2018-08-01 stsp
694 d6b05b5b 2018-08-04 stsp view->type = type;
695 f7d12f7e 2018-08-01 stsp view->lines = LINES;
696 f7d12f7e 2018-08-01 stsp view->cols = COLS;
697 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
698 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
699 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
700 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
701 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
702 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
703 96a765a8 2018-08-04 stsp view_close(view);
704 ea5e7bb5 2018-08-01 stsp return NULL;
705 ea5e7bb5 2018-08-01 stsp }
706 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
707 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
708 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
709 96a765a8 2018-08-04 stsp view_close(view);
710 ea5e7bb5 2018-08-01 stsp return NULL;
711 ea5e7bb5 2018-08-01 stsp }
712 ea5e7bb5 2018-08-01 stsp
713 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
714 ea5e7bb5 2018-08-01 stsp return view;
715 cdf1ee82 2018-08-01 stsp }
716 cdf1ee82 2018-08-01 stsp
717 0cf4efb1 2018-09-29 stsp static int
718 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
719 0cf4efb1 2018-09-29 stsp {
720 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
721 0cf4efb1 2018-09-29 stsp return 0;
722 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
723 5c60c32a 2018-10-18 stsp }
724 5c60c32a 2018-10-18 stsp
725 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
726 9b058f45 2022-06-30 mark static int
727 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
728 9b058f45 2022-06-30 mark {
729 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
730 9b058f45 2022-06-30 mark }
731 9b058f45 2022-06-30 mark
732 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
733 5c60c32a 2018-10-18 stsp
734 5c60c32a 2018-10-18 stsp static const struct got_error *
735 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
736 5c60c32a 2018-10-18 stsp {
737 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
738 5c60c32a 2018-10-18 stsp
739 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
740 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
741 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
742 3c1dfe12 2022-07-08 mark else
743 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
744 9b058f45 2022-06-30 mark view->begin_x = 0;
745 571ccd73 2022-07-19 mark } else if (!view->resized) {
746 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
747 3c1dfe12 2022-07-08 mark view->cols > 119)
748 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
749 3c1dfe12 2022-07-08 mark else
750 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
751 9b058f45 2022-06-30 mark view->begin_y = 0;
752 9b058f45 2022-06-30 mark }
753 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
754 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
755 5c60c32a 2018-10-18 stsp view->lines = LINES;
756 5c60c32a 2018-10-18 stsp view->cols = COLS;
757 5c60c32a 2018-10-18 stsp err = view_resize(view);
758 5c60c32a 2018-10-18 stsp if (err)
759 5c60c32a 2018-10-18 stsp return err;
760 5c60c32a 2018-10-18 stsp
761 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
762 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
763 9b058f45 2022-06-30 mark
764 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
765 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
766 5c60c32a 2018-10-18 stsp
767 5c60c32a 2018-10-18 stsp return NULL;
768 5c60c32a 2018-10-18 stsp }
769 5c60c32a 2018-10-18 stsp
770 5c60c32a 2018-10-18 stsp static const struct got_error *
771 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
772 5c60c32a 2018-10-18 stsp {
773 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
774 5c60c32a 2018-10-18 stsp
775 5c60c32a 2018-10-18 stsp view->begin_x = 0;
776 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
777 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
778 5c60c32a 2018-10-18 stsp view->ncols = COLS;
779 5c60c32a 2018-10-18 stsp view->lines = LINES;
780 5c60c32a 2018-10-18 stsp view->cols = COLS;
781 5c60c32a 2018-10-18 stsp err = view_resize(view);
782 5c60c32a 2018-10-18 stsp if (err)
783 5c60c32a 2018-10-18 stsp return err;
784 5c60c32a 2018-10-18 stsp
785 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
786 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
787 5c60c32a 2018-10-18 stsp
788 5c60c32a 2018-10-18 stsp return NULL;
789 0cf4efb1 2018-09-29 stsp }
790 0cf4efb1 2018-09-29 stsp
791 5c60c32a 2018-10-18 stsp static int
792 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
793 5c60c32a 2018-10-18 stsp {
794 5c60c32a 2018-10-18 stsp return view->parent == NULL;
795 5c60c32a 2018-10-18 stsp }
796 5c60c32a 2018-10-18 stsp
797 6131ff18 2022-06-20 mark static int
798 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
799 6131ff18 2022-06-20 mark {
800 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
801 24b9cfdc 2022-06-30 stsp }
802 24b9cfdc 2022-06-30 stsp
803 24b9cfdc 2022-06-30 stsp static int
804 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
805 24b9cfdc 2022-06-30 stsp {
806 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
807 6131ff18 2022-06-20 mark }
808 6131ff18 2022-06-20 mark
809 49b24ee5 2022-07-03 mark static int
810 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
811 49b24ee5 2022-07-03 mark {
812 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
813 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
814 49b24ee5 2022-07-03 mark }
815 49b24ee5 2022-07-03 mark
816 9b058f45 2022-06-30 mark static void
817 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
818 9b058f45 2022-06-30 mark {
819 9b058f45 2022-06-30 mark PANEL *panel;
820 9b058f45 2022-06-30 mark const struct tog_view *view_above;
821 6131ff18 2022-06-20 mark
822 9b058f45 2022-06-30 mark if (view->parent)
823 9b058f45 2022-06-30 mark return view_border(view->parent);
824 9b058f45 2022-06-30 mark
825 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
826 9b058f45 2022-06-30 mark if (panel == NULL)
827 9b058f45 2022-06-30 mark return;
828 9b058f45 2022-06-30 mark
829 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
830 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
831 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
832 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
833 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
834 9b058f45 2022-06-30 mark else
835 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
836 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 9b058f45 2022-06-30 mark }
838 9b058f45 2022-06-30 mark
839 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
840 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
841 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
842 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
843 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
844 9b058f45 2022-06-30 mark
845 4d8c2215 2018-08-19 stsp static const struct got_error *
846 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
847 f7d12f7e 2018-08-01 stsp {
848 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
849 9b058f45 2022-06-30 mark int dif, nlines, ncols;
850 f7d12f7e 2018-08-01 stsp
851 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
852 9b058f45 2022-06-30 mark
853 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
854 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
855 0cf4efb1 2018-09-29 stsp else
856 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
857 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
858 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
859 0cf4efb1 2018-09-29 stsp else
860 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
861 6d0fee91 2018-08-01 stsp
862 4dd27a72 2022-06-29 stsp if (view->child) {
863 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
864 9b058f45 2022-06-30 mark
865 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
866 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
867 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
868 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
869 0dbbbe90 2022-06-17 op ncols = COLS;
870 0dbbbe90 2022-06-17 op
871 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
872 5c60c32a 2018-10-18 stsp if (view->child->focussed)
873 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
874 5c60c32a 2018-10-18 stsp else
875 5c60c32a 2018-10-18 stsp show_panel(view->panel);
876 5c60c32a 2018-10-18 stsp } else {
877 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
878 0dbbbe90 2022-06-17 op
879 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
880 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
881 9b058f45 2022-06-30 mark }
882 9b058f45 2022-06-30 mark /*
883 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
884 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
885 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
886 9b058f45 2022-06-30 mark */
887 9b058f45 2022-06-30 mark if (hs) {
888 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
889 9b058f45 2022-06-30 mark if (err)
890 9b058f45 2022-06-30 mark return err;
891 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
892 9b058f45 2022-06-30 mark err = offset_selection_down(view);
893 9b058f45 2022-06-30 mark if (err)
894 9b058f45 2022-06-30 mark return err;
895 9b058f45 2022-06-30 mark }
896 9b058f45 2022-06-30 mark view_border(view);
897 9b058f45 2022-06-30 mark update_panels();
898 9b058f45 2022-06-30 mark doupdate();
899 9b058f45 2022-06-30 mark show_panel(view->child->panel);
900 9b058f45 2022-06-30 mark nlines = view->nlines;
901 9b058f45 2022-06-30 mark }
902 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
903 0dbbbe90 2022-06-17 op ncols = COLS;
904 669b5ffa 2018-10-07 stsp
905 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
906 571ccd73 2022-07-19 mark err = view->resize(view, dif);
907 571ccd73 2022-07-19 mark if (err)
908 571ccd73 2022-07-19 mark return err;
909 571ccd73 2022-07-19 mark }
910 571ccd73 2022-07-19 mark
911 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
912 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
913 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
914 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
915 0dbbbe90 2022-06-17 op wclear(view->window);
916 0dbbbe90 2022-06-17 op
917 0dbbbe90 2022-06-17 op view->nlines = nlines;
918 0dbbbe90 2022-06-17 op view->ncols = ncols;
919 0dbbbe90 2022-06-17 op view->lines = LINES;
920 0dbbbe90 2022-06-17 op view->cols = COLS;
921 0dbbbe90 2022-06-17 op
922 5c60c32a 2018-10-18 stsp return NULL;
923 571ccd73 2022-07-19 mark }
924 571ccd73 2022-07-19 mark
925 571ccd73 2022-07-19 mark static const struct got_error *
926 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
927 571ccd73 2022-07-19 mark {
928 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
929 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
930 6fe51fee 2022-07-22 mark int n = 0;
931 571ccd73 2022-07-19 mark
932 6fe51fee 2022-07-22 mark if (s->selected_entry)
933 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
934 6fe51fee 2022-07-22 mark
935 571ccd73 2022-07-19 mark /*
936 571ccd73 2022-07-19 mark * Request commits to account for the increased
937 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
938 571ccd73 2022-07-19 mark */
939 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
940 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
941 571ccd73 2022-07-19 mark err = request_log_commits(view);
942 571ccd73 2022-07-19 mark }
943 571ccd73 2022-07-19 mark
944 571ccd73 2022-07-19 mark return err;
945 d9a7ab53 2022-07-11 mark }
946 d9a7ab53 2022-07-11 mark
947 d9a7ab53 2022-07-11 mark static void
948 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
949 d9a7ab53 2022-07-11 mark {
950 d9a7ab53 2022-07-11 mark if (n == 0)
951 d9a7ab53 2022-07-11 mark return;
952 d9a7ab53 2022-07-11 mark
953 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
954 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
955 d9a7ab53 2022-07-11 mark view->parent->offset += n;
956 d9a7ab53 2022-07-11 mark else
957 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
958 d9a7ab53 2022-07-11 mark } else if (view->offset) {
959 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
960 d9a7ab53 2022-07-11 mark view->offset -= n;
961 d9a7ab53 2022-07-11 mark else
962 d9a7ab53 2022-07-11 mark view->offset = 0;
963 d9a7ab53 2022-07-11 mark }
964 3c1dfe12 2022-07-08 mark }
965 3c1dfe12 2022-07-08 mark
966 3c1dfe12 2022-07-08 mark static const struct got_error *
967 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
968 3c1dfe12 2022-07-08 mark {
969 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
970 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
971 3c1dfe12 2022-07-08 mark
972 3c1dfe12 2022-07-08 mark if (view->parent)
973 3c1dfe12 2022-07-08 mark v = view->parent;
974 3c1dfe12 2022-07-08 mark else
975 3c1dfe12 2022-07-08 mark v = view;
976 3c1dfe12 2022-07-08 mark
977 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
978 3c1dfe12 2022-07-08 mark return NULL;
979 3c1dfe12 2022-07-08 mark
980 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
981 3c1dfe12 2022-07-08 mark
982 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
984 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
985 3c1dfe12 2022-07-08 mark if (view->parent)
986 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
987 3c1dfe12 2022-07-08 mark else
988 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
989 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
990 3c1dfe12 2022-07-08 mark view->count = 0;
991 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
992 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
993 3c1dfe12 2022-07-08 mark view->count = 0;
994 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
995 3c1dfe12 2022-07-08 mark }
996 3c1dfe12 2022-07-08 mark v->ncols = COLS;
997 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
998 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
999 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
1000 3c1dfe12 2022-07-08 mark if (err)
1001 3c1dfe12 2022-07-08 mark return err;
1002 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1003 3c1dfe12 2022-07-08 mark } else {
1004 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1005 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1006 3c1dfe12 2022-07-08 mark if (view->parent)
1007 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1008 3c1dfe12 2022-07-08 mark else
1009 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1010 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1011 3c1dfe12 2022-07-08 mark view->count = 0;
1012 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1013 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1014 3c1dfe12 2022-07-08 mark view->count = 0;
1015 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1016 3c1dfe12 2022-07-08 mark }
1017 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1018 3c1dfe12 2022-07-08 mark }
1019 3c1dfe12 2022-07-08 mark
1020 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1021 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1022 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1023 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1024 3c1dfe12 2022-07-08 mark
1025 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1026 3c1dfe12 2022-07-08 mark if (err)
1027 3c1dfe12 2022-07-08 mark return err;
1028 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1029 3c1dfe12 2022-07-08 mark if (err)
1030 3c1dfe12 2022-07-08 mark return err;
1031 3c1dfe12 2022-07-08 mark
1032 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1033 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1034 3c1dfe12 2022-07-08 mark if (err)
1035 3c1dfe12 2022-07-08 mark return err;
1036 3c1dfe12 2022-07-08 mark }
1037 3c1dfe12 2022-07-08 mark
1038 6fe51fee 2022-07-22 mark if (v->resize)
1039 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1040 6fe51fee 2022-07-22 mark else if (v->child->resize)
1041 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1042 3c1dfe12 2022-07-08 mark
1043 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1044 3c1dfe12 2022-07-08 mark
1045 3c1dfe12 2022-07-08 mark return err;
1046 3c1dfe12 2022-07-08 mark }
1047 3c1dfe12 2022-07-08 mark
1048 3c1dfe12 2022-07-08 mark static void
1049 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1050 3c1dfe12 2022-07-08 mark {
1051 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1052 3c1dfe12 2022-07-08 mark
1053 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1054 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1055 669b5ffa 2018-10-07 stsp }
1056 669b5ffa 2018-10-07 stsp
1057 669b5ffa 2018-10-07 stsp static const struct got_error *
1058 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1059 669b5ffa 2018-10-07 stsp {
1060 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1061 669b5ffa 2018-10-07 stsp
1062 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1063 669b5ffa 2018-10-07 stsp return NULL;
1064 669b5ffa 2018-10-07 stsp
1065 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1066 669b5ffa 2018-10-07 stsp view->child = NULL;
1067 669b5ffa 2018-10-07 stsp return err;
1068 669b5ffa 2018-10-07 stsp }
1069 669b5ffa 2018-10-07 stsp
1070 0dbbbe90 2022-06-17 op static const struct got_error *
1071 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1072 669b5ffa 2018-10-07 stsp {
1073 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1074 3c1dfe12 2022-07-08 mark
1075 669b5ffa 2018-10-07 stsp view->child = child;
1076 669b5ffa 2018-10-07 stsp child->parent = view;
1077 0dbbbe90 2022-06-17 op
1078 3c1dfe12 2022-07-08 mark err = view_resize(view);
1079 3c1dfe12 2022-07-08 mark if (err)
1080 3c1dfe12 2022-07-08 mark return err;
1081 3c1dfe12 2022-07-08 mark
1082 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1083 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1084 3c1dfe12 2022-07-08 mark
1085 3c1dfe12 2022-07-08 mark return err;
1086 bfddd0d9 2018-09-29 stsp }
1087 136e2bd4 2022-07-23 mark
1088 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1089 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1090 136e2bd4 2022-07-23 mark
1091 136e2bd4 2022-07-23 mark static const struct got_error *
1092 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1093 136e2bd4 2022-07-23 mark enum tog_view_type request)
1094 136e2bd4 2022-07-23 mark {
1095 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1096 136e2bd4 2022-07-23 mark const struct got_error *err;
1097 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1098 136e2bd4 2022-07-23 mark
1099 136e2bd4 2022-07-23 mark *requested = NULL;
1100 136e2bd4 2022-07-23 mark
1101 136e2bd4 2022-07-23 mark if (view_is_parent_view(view))
1102 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1103 bfddd0d9 2018-09-29 stsp
1104 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1105 136e2bd4 2022-07-23 mark if (err)
1106 136e2bd4 2022-07-23 mark return err;
1107 136e2bd4 2022-07-23 mark
1108 136e2bd4 2022-07-23 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1109 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1110 136e2bd4 2022-07-23 mark if (err)
1111 136e2bd4 2022-07-23 mark return err;
1112 136e2bd4 2022-07-23 mark }
1113 136e2bd4 2022-07-23 mark
1114 136e2bd4 2022-07-23 mark view->focussed = 0;
1115 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1116 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1117 136e2bd4 2022-07-23 mark new_view->nlines = view->lines - y;
1118 136e2bd4 2022-07-23 mark
1119 136e2bd4 2022-07-23 mark if (view_is_parent_view(view)) {
1120 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1121 136e2bd4 2022-07-23 mark err = view_close_child(view);
1122 136e2bd4 2022-07-23 mark if (err)
1123 136e2bd4 2022-07-23 mark return err;
1124 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1125 136e2bd4 2022-07-23 mark if (err)
1126 136e2bd4 2022-07-23 mark return err;
1127 136e2bd4 2022-07-23 mark view->focus_child = 1;
1128 136e2bd4 2022-07-23 mark } else
1129 136e2bd4 2022-07-23 mark *requested = new_view;
1130 136e2bd4 2022-07-23 mark
1131 136e2bd4 2022-07-23 mark return NULL;
1132 136e2bd4 2022-07-23 mark }
1133 136e2bd4 2022-07-23 mark
1134 34bc9ec9 2019-02-22 stsp static void
1135 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1136 25791caa 2018-10-24 stsp {
1137 25791caa 2018-10-24 stsp int cols, lines;
1138 25791caa 2018-10-24 stsp struct winsize size;
1139 25791caa 2018-10-24 stsp
1140 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1141 25791caa 2018-10-24 stsp cols = 80; /* Default */
1142 25791caa 2018-10-24 stsp lines = 24;
1143 25791caa 2018-10-24 stsp } else {
1144 25791caa 2018-10-24 stsp cols = size.ws_col;
1145 25791caa 2018-10-24 stsp lines = size.ws_row;
1146 25791caa 2018-10-24 stsp }
1147 25791caa 2018-10-24 stsp resize_term(lines, cols);
1148 2b49a8ae 2019-06-22 stsp }
1149 2b49a8ae 2019-06-22 stsp
1150 2b49a8ae 2019-06-22 stsp static const struct got_error *
1151 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1152 2b49a8ae 2019-06-22 stsp {
1153 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1154 9b058f45 2022-06-30 mark struct tog_view *v = view;
1155 2b49a8ae 2019-06-22 stsp char pattern[1024];
1156 2b49a8ae 2019-06-22 stsp int ret;
1157 c0c4acc8 2021-01-24 stsp
1158 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1159 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1160 c0c4acc8 2021-01-24 stsp view->searching = 0;
1161 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1162 c0c4acc8 2021-01-24 stsp }
1163 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1164 2b49a8ae 2019-06-22 stsp
1165 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1166 2b49a8ae 2019-06-22 stsp return NULL;
1167 2b49a8ae 2019-06-22 stsp
1168 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1169 9b058f45 2022-06-30 mark v = view->child;
1170 2b49a8ae 2019-06-22 stsp
1171 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1172 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1173 9b058f45 2022-06-30 mark
1174 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1175 2b49a8ae 2019-06-22 stsp nocbreak();
1176 2b49a8ae 2019-06-22 stsp echo();
1177 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1178 9b058f45 2022-06-30 mark wrefresh(v->window);
1179 2b49a8ae 2019-06-22 stsp cbreak();
1180 2b49a8ae 2019-06-22 stsp noecho();
1181 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1182 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1183 2b49a8ae 2019-06-22 stsp return NULL;
1184 2b49a8ae 2019-06-22 stsp
1185 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1186 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1187 7c32bd05 2019-06-22 stsp if (err) {
1188 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1189 7c32bd05 2019-06-22 stsp return err;
1190 7c32bd05 2019-06-22 stsp }
1191 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1192 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1193 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1194 2b49a8ae 2019-06-22 stsp view->search_next(view);
1195 2b49a8ae 2019-06-22 stsp }
1196 2b49a8ae 2019-06-22 stsp
1197 2b49a8ae 2019-06-22 stsp return NULL;
1198 d2366e29 2022-07-07 mark }
1199 d2366e29 2022-07-07 mark
1200 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1201 d2366e29 2022-07-07 mark static const struct got_error *
1202 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1203 d2366e29 2022-07-07 mark {
1204 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1205 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1206 d2366e29 2022-07-07 mark
1207 d2366e29 2022-07-07 mark if (view->parent)
1208 d2366e29 2022-07-07 mark v = view->parent;
1209 d2366e29 2022-07-07 mark else
1210 d2366e29 2022-07-07 mark v = view;
1211 d2366e29 2022-07-07 mark
1212 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1213 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1214 7532ccda 2022-07-11 mark else
1215 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1216 d2366e29 2022-07-07 mark
1217 7532ccda 2022-07-11 mark if (!v->child)
1218 7532ccda 2022-07-11 mark return NULL;
1219 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1220 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1221 7532ccda 2022-07-11 mark
1222 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1223 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1224 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1225 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1226 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1227 d2366e29 2022-07-07 mark
1228 7532ccda 2022-07-11 mark
1229 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1230 d2366e29 2022-07-07 mark v->ncols = COLS;
1231 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1232 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1233 d2366e29 2022-07-07 mark
1234 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1235 d2366e29 2022-07-07 mark if (err)
1236 d2366e29 2022-07-07 mark return err;
1237 d2366e29 2022-07-07 mark }
1238 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1239 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1240 d2366e29 2022-07-07 mark v->focus_child = 1;
1241 d2366e29 2022-07-07 mark
1242 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1243 d2366e29 2022-07-07 mark if (err)
1244 d2366e29 2022-07-07 mark return err;
1245 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1246 d2366e29 2022-07-07 mark if (err)
1247 d2366e29 2022-07-07 mark return err;
1248 d2366e29 2022-07-07 mark
1249 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1250 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1251 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1252 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1253 279d2047 2022-07-29 stsp if (err)
1254 279d2047 2022-07-29 stsp return err;
1255 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1256 279d2047 2022-07-29 stsp if (err)
1257 279d2047 2022-07-29 stsp return err;
1258 7532ccda 2022-07-11 mark } else {
1259 7532ccda 2022-07-11 mark offset_selection_up(v);
1260 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1261 d2366e29 2022-07-07 mark }
1262 dff91825 2022-07-22 mark if (v->resize)
1263 dff91825 2022-07-22 mark err = v->resize(v, 0);
1264 dff91825 2022-07-22 mark else if (v->child->resize)
1265 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1266 d2366e29 2022-07-07 mark
1267 d2366e29 2022-07-07 mark return err;
1268 0cf4efb1 2018-09-29 stsp }
1269 6d0fee91 2018-08-01 stsp
1270 640cd7ff 2022-06-22 mark /*
1271 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1272 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1273 640cd7ff 2022-06-22 mark */
1274 640cd7ff 2022-06-22 mark static int
1275 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1276 640cd7ff 2022-06-22 mark {
1277 9b058f45 2022-06-30 mark struct tog_view *v = view;
1278 9b058f45 2022-06-30 mark int x, n = 0;
1279 640cd7ff 2022-06-22 mark
1280 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1281 9b058f45 2022-06-30 mark v = view->child;
1282 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1283 9b058f45 2022-06-30 mark v = view->parent;
1284 9b058f45 2022-06-30 mark
1285 640cd7ff 2022-06-22 mark view->count = 0;
1286 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1287 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1288 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1289 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1290 9b058f45 2022-06-30 mark waddch(v->window, ':');
1291 640cd7ff 2022-06-22 mark
1292 640cd7ff 2022-06-22 mark do {
1293 9b058f45 2022-06-30 mark x = getcurx(v->window);
1294 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1295 9b058f45 2022-06-30 mark waddch(v->window, c);
1296 9b058f45 2022-06-30 mark wrefresh(v->window);
1297 9b058f45 2022-06-30 mark }
1298 9b058f45 2022-06-30 mark
1299 640cd7ff 2022-06-22 mark /*
1300 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1301 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1302 640cd7ff 2022-06-22 mark */
1303 640cd7ff 2022-06-22 mark if (n >= 9999999)
1304 640cd7ff 2022-06-22 mark n = 9999999;
1305 640cd7ff 2022-06-22 mark else
1306 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1307 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 640cd7ff 2022-06-22 mark
1309 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1310 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1311 94b80cfa 2022-08-01 mark n = 0;
1312 94b80cfa 2022-08-01 mark c = 0;
1313 94b80cfa 2022-08-01 mark }
1314 94b80cfa 2022-08-01 mark
1315 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1316 640cd7ff 2022-06-22 mark view->count = n;
1317 640cd7ff 2022-06-22 mark
1318 640cd7ff 2022-06-22 mark return c;
1319 640cd7ff 2022-06-22 mark }
1320 640cd7ff 2022-06-22 mark
1321 0cf4efb1 2018-09-29 stsp static const struct got_error *
1322 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1323 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1324 e5a0f69f 2018-08-18 stsp {
1325 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1326 669b5ffa 2018-10-07 stsp struct tog_view *v;
1327 1a76625f 2018-10-22 stsp int ch, errcode;
1328 e5a0f69f 2018-08-18 stsp
1329 e5a0f69f 2018-08-18 stsp *new = NULL;
1330 8f4ed634 2020-03-26 stsp
1331 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1332 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1333 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1334 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1335 640cd7ff 2022-06-22 mark view->count = 0;
1336 640cd7ff 2022-06-22 mark }
1337 e5a0f69f 2018-08-18 stsp
1338 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1339 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1340 82954512 2020-02-03 stsp if (errcode)
1341 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1342 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1343 3da8ef85 2021-09-21 stsp sched_yield();
1344 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1345 82954512 2020-02-03 stsp if (errcode)
1346 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1347 82954512 2020-02-03 stsp "pthread_mutex_lock");
1348 60493ae3 2019-06-20 stsp view->search_next(view);
1349 60493ae3 2019-06-20 stsp return NULL;
1350 60493ae3 2019-06-20 stsp }
1351 60493ae3 2019-06-20 stsp
1352 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1353 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1354 1a76625f 2018-10-22 stsp if (errcode)
1355 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1356 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1357 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1358 a6d37fac 2022-07-03 mark cbreak();
1359 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1360 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1361 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1362 a6d37fac 2022-07-03 mark view->count = 0;
1363 a6d37fac 2022-07-03 mark else
1364 a6d37fac 2022-07-03 mark ch = view->ch;
1365 a6d37fac 2022-07-03 mark } else {
1366 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1367 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1368 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1369 640cd7ff 2022-06-22 mark }
1370 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1371 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1372 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1373 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1374 1a76625f 2018-10-22 stsp if (errcode)
1375 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1376 25791caa 2018-10-24 stsp
1377 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1378 25791caa 2018-10-24 stsp tog_resizeterm();
1379 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1380 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1381 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1382 25791caa 2018-10-24 stsp err = view_resize(v);
1383 25791caa 2018-10-24 stsp if (err)
1384 25791caa 2018-10-24 stsp return err;
1385 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1386 25791caa 2018-10-24 stsp if (err)
1387 25791caa 2018-10-24 stsp return err;
1388 cdfcfb03 2020-12-06 stsp if (v->child) {
1389 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1390 cdfcfb03 2020-12-06 stsp if (err)
1391 cdfcfb03 2020-12-06 stsp return err;
1392 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1393 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1394 cdfcfb03 2020-12-06 stsp if (err)
1395 cdfcfb03 2020-12-06 stsp return err;
1396 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1397 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1398 3c1dfe12 2022-07-08 mark if (err)
1399 3c1dfe12 2022-07-08 mark return err;
1400 3c1dfe12 2022-07-08 mark }
1401 cdfcfb03 2020-12-06 stsp }
1402 25791caa 2018-10-24 stsp }
1403 25791caa 2018-10-24 stsp }
1404 25791caa 2018-10-24 stsp
1405 e5a0f69f 2018-08-18 stsp switch (ch) {
1406 1e37a5c2 2019-05-12 jcs case '\t':
1407 640cd7ff 2022-06-22 mark view->count = 0;
1408 1e37a5c2 2019-05-12 jcs if (view->child) {
1409 e78dc838 2020-12-04 stsp view->focussed = 0;
1410 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1411 e78dc838 2020-12-04 stsp view->focus_child = 1;
1412 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1413 e78dc838 2020-12-04 stsp view->focussed = 0;
1414 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1415 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1416 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1417 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1418 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1419 6fe51fee 2022-07-22 mark 0);
1420 9b058f45 2022-06-30 mark if (err)
1421 9b058f45 2022-06-30 mark return err;
1422 9b058f45 2022-06-30 mark }
1423 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1424 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1425 9b058f45 2022-06-30 mark if (err)
1426 9b058f45 2022-06-30 mark return err;
1427 9b058f45 2022-06-30 mark }
1428 1e37a5c2 2019-05-12 jcs }
1429 1e37a5c2 2019-05-12 jcs break;
1430 1e37a5c2 2019-05-12 jcs case 'q':
1431 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1432 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1433 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1434 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1435 9b058f45 2022-06-30 mark if (err)
1436 9b058f45 2022-06-30 mark break;
1437 9b058f45 2022-06-30 mark }
1438 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1439 9b058f45 2022-06-30 mark }
1440 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1441 9970f7fc 2020-12-03 stsp view->dying = 1;
1442 1e37a5c2 2019-05-12 jcs break;
1443 1e37a5c2 2019-05-12 jcs case 'Q':
1444 1e37a5c2 2019-05-12 jcs *done = 1;
1445 1e37a5c2 2019-05-12 jcs break;
1446 61417565 2022-06-20 mark case 'F':
1447 640cd7ff 2022-06-22 mark view->count = 0;
1448 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1449 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1450 1e37a5c2 2019-05-12 jcs break;
1451 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1452 e78dc838 2020-12-04 stsp view->focussed = 0;
1453 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1454 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1455 3c1dfe12 2022-07-08 mark } else {
1456 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1457 3c1dfe12 2022-07-08 mark if (!err)
1458 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1459 3c1dfe12 2022-07-08 mark }
1460 1e37a5c2 2019-05-12 jcs if (err)
1461 1e37a5c2 2019-05-12 jcs break;
1462 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1463 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1464 1e37a5c2 2019-05-12 jcs } else {
1465 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1466 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1467 e78dc838 2020-12-04 stsp view->focussed = 1;
1468 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1469 1e37a5c2 2019-05-12 jcs } else {
1470 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1471 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1472 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1473 3c1dfe12 2022-07-08 mark if (!err)
1474 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1475 669b5ffa 2018-10-07 stsp }
1476 1e37a5c2 2019-05-12 jcs if (err)
1477 1e37a5c2 2019-05-12 jcs break;
1478 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1479 1e37a5c2 2019-05-12 jcs }
1480 9b058f45 2022-06-30 mark if (err)
1481 9b058f45 2022-06-30 mark break;
1482 6fe51fee 2022-07-22 mark if (view->resize) {
1483 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1484 9b058f45 2022-06-30 mark if (err)
1485 9b058f45 2022-06-30 mark break;
1486 9b058f45 2022-06-30 mark }
1487 9b058f45 2022-06-30 mark if (view->parent)
1488 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1489 9b058f45 2022-06-30 mark if (!err)
1490 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1491 1e37a5c2 2019-05-12 jcs break;
1492 d2366e29 2022-07-07 mark case 'S':
1493 3c1dfe12 2022-07-08 mark view->count = 0;
1494 d2366e29 2022-07-07 mark err = switch_split(view);
1495 3c1dfe12 2022-07-08 mark break;
1496 3c1dfe12 2022-07-08 mark case '-':
1497 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1498 d2366e29 2022-07-07 mark break;
1499 3c1dfe12 2022-07-08 mark case '+':
1500 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1501 3c1dfe12 2022-07-08 mark break;
1502 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1503 60493ae3 2019-06-20 stsp break;
1504 60493ae3 2019-06-20 stsp case '/':
1505 640cd7ff 2022-06-22 mark view->count = 0;
1506 60493ae3 2019-06-20 stsp if (view->search_start)
1507 2b49a8ae 2019-06-22 stsp view_search_start(view);
1508 60493ae3 2019-06-20 stsp else
1509 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1510 1e37a5c2 2019-05-12 jcs break;
1511 b1bf1435 2019-06-21 stsp case 'N':
1512 60493ae3 2019-06-20 stsp case 'n':
1513 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1514 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1515 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1516 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1517 60493ae3 2019-06-20 stsp view->search_next(view);
1518 60493ae3 2019-06-20 stsp } else
1519 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1520 917d79a7 2022-07-01 stsp break;
1521 917d79a7 2022-07-01 stsp case 'A':
1522 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1523 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1524 917d79a7 2022-07-01 stsp else
1525 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1526 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1527 917d79a7 2022-07-01 stsp if (v->reset) {
1528 917d79a7 2022-07-01 stsp err = v->reset(v);
1529 917d79a7 2022-07-01 stsp if (err)
1530 917d79a7 2022-07-01 stsp return err;
1531 917d79a7 2022-07-01 stsp }
1532 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1533 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1534 917d79a7 2022-07-01 stsp if (err)
1535 917d79a7 2022-07-01 stsp return err;
1536 917d79a7 2022-07-01 stsp }
1537 917d79a7 2022-07-01 stsp }
1538 60493ae3 2019-06-20 stsp break;
1539 1e37a5c2 2019-05-12 jcs default:
1540 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1541 1e37a5c2 2019-05-12 jcs break;
1542 e5a0f69f 2018-08-18 stsp }
1543 e5a0f69f 2018-08-18 stsp
1544 e5a0f69f 2018-08-18 stsp return err;
1545 e5a0f69f 2018-08-18 stsp }
1546 e5a0f69f 2018-08-18 stsp
1547 336075a4 2022-06-25 op static int
1548 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1549 a3404814 2018-09-02 stsp {
1550 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1551 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1552 669b5ffa 2018-10-07 stsp return 0;
1553 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1554 669b5ffa 2018-10-07 stsp return 0;
1555 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1556 a3404814 2018-09-02 stsp return 0;
1557 a3404814 2018-09-02 stsp
1558 669b5ffa 2018-10-07 stsp return view->focussed;
1559 a3404814 2018-09-02 stsp }
1560 a3404814 2018-09-02 stsp
1561 bcbd79e2 2018-08-19 stsp static const struct got_error *
1562 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1563 e5a0f69f 2018-08-18 stsp {
1564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1565 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1566 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1567 d2366e29 2022-07-07 mark char *mode;
1568 fd823528 2018-10-22 stsp int fast_refresh = 10;
1569 1a76625f 2018-10-22 stsp int done = 0, errcode;
1570 e5a0f69f 2018-08-18 stsp
1571 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1572 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1573 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1574 d2366e29 2022-07-07 mark else
1575 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1576 d2366e29 2022-07-07 mark
1577 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1578 1a76625f 2018-10-22 stsp if (errcode)
1579 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1580 1a76625f 2018-10-22 stsp
1581 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1582 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1583 e5a0f69f 2018-08-18 stsp
1584 1004088d 2018-09-29 stsp view->focussed = 1;
1585 878940b7 2018-09-29 stsp err = view->show(view);
1586 0cf4efb1 2018-09-29 stsp if (err)
1587 0cf4efb1 2018-09-29 stsp return err;
1588 0cf4efb1 2018-09-29 stsp update_panels();
1589 0cf4efb1 2018-09-29 stsp doupdate();
1590 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1591 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1592 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1593 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1594 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1595 fd823528 2018-10-22 stsp
1596 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1597 e5a0f69f 2018-08-18 stsp if (err)
1598 e5a0f69f 2018-08-18 stsp break;
1599 9970f7fc 2020-12-03 stsp if (view->dying) {
1600 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1601 669b5ffa 2018-10-07 stsp
1602 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1603 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1604 9970f7fc 2020-12-03 stsp entry);
1605 e78dc838 2020-12-04 stsp else if (view->parent)
1606 669b5ffa 2018-10-07 stsp prev = view->parent;
1607 669b5ffa 2018-10-07 stsp
1608 e78dc838 2020-12-04 stsp if (view->parent) {
1609 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1610 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1611 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1612 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1613 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1614 0dbbbe90 2022-06-17 op if (err)
1615 0dbbbe90 2022-06-17 op break;
1616 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1617 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1618 e78dc838 2020-12-04 stsp } else
1619 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1620 669b5ffa 2018-10-07 stsp
1621 9970f7fc 2020-12-03 stsp err = view_close(view);
1622 fb59748f 2020-12-05 stsp if (err)
1623 e5a0f69f 2018-08-18 stsp goto done;
1624 669b5ffa 2018-10-07 stsp
1625 e78dc838 2020-12-04 stsp view = NULL;
1626 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1627 e78dc838 2020-12-04 stsp if (v->focussed)
1628 e78dc838 2020-12-04 stsp break;
1629 0cf4efb1 2018-09-29 stsp }
1630 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1631 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1632 e78dc838 2020-12-04 stsp if (prev)
1633 e78dc838 2020-12-04 stsp view = prev;
1634 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1635 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1636 e78dc838 2020-12-04 stsp tog_view_list_head);
1637 e78dc838 2020-12-04 stsp }
1638 e78dc838 2020-12-04 stsp if (view) {
1639 e78dc838 2020-12-04 stsp if (view->focus_child) {
1640 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1641 e78dc838 2020-12-04 stsp view = view->child;
1642 e78dc838 2020-12-04 stsp } else
1643 e78dc838 2020-12-04 stsp view->focussed = 1;
1644 e78dc838 2020-12-04 stsp }
1645 e78dc838 2020-12-04 stsp }
1646 e5a0f69f 2018-08-18 stsp }
1647 bcbd79e2 2018-08-19 stsp if (new_view) {
1648 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1649 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1650 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1651 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1652 86c66b02 2018-10-18 stsp continue;
1653 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1654 86c66b02 2018-10-18 stsp err = view_close(v);
1655 86c66b02 2018-10-18 stsp if (err)
1656 86c66b02 2018-10-18 stsp goto done;
1657 86c66b02 2018-10-18 stsp break;
1658 86c66b02 2018-10-18 stsp }
1659 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1660 fed7eaa8 2018-10-24 stsp view = new_view;
1661 0ae84acc 2022-06-15 tracey }
1662 669b5ffa 2018-10-07 stsp if (view) {
1663 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1664 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1665 e78dc838 2020-12-04 stsp view = view->child;
1666 e78dc838 2020-12-04 stsp } else {
1667 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1668 e78dc838 2020-12-04 stsp view = view->parent;
1669 1a76625f 2018-10-22 stsp }
1670 e78dc838 2020-12-04 stsp show_panel(view->panel);
1671 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1672 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1673 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1674 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1675 669b5ffa 2018-10-07 stsp if (err)
1676 1a76625f 2018-10-22 stsp goto done;
1677 669b5ffa 2018-10-07 stsp }
1678 669b5ffa 2018-10-07 stsp err = view->show(view);
1679 0cf4efb1 2018-09-29 stsp if (err)
1680 1a76625f 2018-10-22 stsp goto done;
1681 669b5ffa 2018-10-07 stsp if (view->child) {
1682 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1683 669b5ffa 2018-10-07 stsp if (err)
1684 1a76625f 2018-10-22 stsp goto done;
1685 669b5ffa 2018-10-07 stsp }
1686 1a76625f 2018-10-22 stsp update_panels();
1687 1a76625f 2018-10-22 stsp doupdate();
1688 0cf4efb1 2018-09-29 stsp }
1689 e5a0f69f 2018-08-18 stsp }
1690 e5a0f69f 2018-08-18 stsp done:
1691 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1692 5629093a 2022-07-11 stsp const struct got_error *close_err;
1693 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1694 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1695 5629093a 2022-07-11 stsp close_err = view_close(view);
1696 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1697 5629093a 2022-07-11 stsp err = close_err;
1698 e5a0f69f 2018-08-18 stsp }
1699 1a76625f 2018-10-22 stsp
1700 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1701 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1702 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1703 1a76625f 2018-10-22 stsp
1704 e5a0f69f 2018-08-18 stsp return err;
1705 ea5e7bb5 2018-08-01 stsp }
1706 ea5e7bb5 2018-08-01 stsp
1707 4ed7e80c 2018-05-20 stsp __dead static void
1708 9f7d7167 2018-04-29 stsp usage_log(void)
1709 9f7d7167 2018-04-29 stsp {
1710 80ddbec8 2018-04-29 stsp endwin();
1711 c70c5802 2018-08-01 stsp fprintf(stderr,
1712 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1713 9f7d7167 2018-04-29 stsp getprogname());
1714 9f7d7167 2018-04-29 stsp exit(1);
1715 80ddbec8 2018-04-29 stsp }
1716 80ddbec8 2018-04-29 stsp
1717 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1718 80ddbec8 2018-04-29 stsp static const struct got_error *
1719 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1720 963b370f 2018-05-20 stsp {
1721 00dfcb92 2018-06-11 stsp char *vis = NULL;
1722 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1723 963b370f 2018-05-20 stsp
1724 963b370f 2018-05-20 stsp *ws = NULL;
1725 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1726 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1727 00dfcb92 2018-06-11 stsp int vislen;
1728 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1729 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1730 00dfcb92 2018-06-11 stsp
1731 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1732 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1733 00dfcb92 2018-06-11 stsp if (err)
1734 00dfcb92 2018-06-11 stsp return err;
1735 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1736 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1737 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1738 a7f50699 2018-06-11 stsp goto done;
1739 a7f50699 2018-06-11 stsp }
1740 00dfcb92 2018-06-11 stsp }
1741 963b370f 2018-05-20 stsp
1742 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1743 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1744 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1745 a7f50699 2018-06-11 stsp goto done;
1746 a7f50699 2018-06-11 stsp }
1747 963b370f 2018-05-20 stsp
1748 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1750 a7f50699 2018-06-11 stsp done:
1751 00dfcb92 2018-06-11 stsp free(vis);
1752 963b370f 2018-05-20 stsp if (err) {
1753 963b370f 2018-05-20 stsp free(*ws);
1754 963b370f 2018-05-20 stsp *ws = NULL;
1755 963b370f 2018-05-20 stsp *wlen = 0;
1756 963b370f 2018-05-20 stsp }
1757 963b370f 2018-05-20 stsp return err;
1758 145b6838 2022-06-16 stsp }
1759 145b6838 2022-06-16 stsp
1760 145b6838 2022-06-16 stsp static const struct got_error *
1761 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1762 145b6838 2022-06-16 stsp {
1763 145b6838 2022-06-16 stsp char *dst;
1764 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1765 145b6838 2022-06-16 stsp
1766 145b6838 2022-06-16 stsp *ptr = NULL;
1767 145b6838 2022-06-16 stsp n = len = strlen(src);
1768 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1769 145b6838 2022-06-16 stsp if (dst == NULL)
1770 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1771 145b6838 2022-06-16 stsp
1772 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1773 145b6838 2022-06-16 stsp const char c = src[idx];
1774 145b6838 2022-06-16 stsp
1775 145b6838 2022-06-16 stsp if (c == '\t') {
1776 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1777 367ddf28 2022-06-16 mark char *p;
1778 367ddf28 2022-06-16 mark
1779 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1780 6e1c41ad 2022-06-16 mark if (p == NULL) {
1781 6e1c41ad 2022-06-16 mark free(dst);
1782 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1783 6e1c41ad 2022-06-16 mark
1784 6e1c41ad 2022-06-16 mark }
1785 6e1c41ad 2022-06-16 mark dst = p;
1786 145b6838 2022-06-16 stsp n += nb;
1787 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1788 145b6838 2022-06-16 stsp sz += nb;
1789 145b6838 2022-06-16 stsp } else
1790 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1791 145b6838 2022-06-16 stsp ++idx;
1792 145b6838 2022-06-16 stsp }
1793 145b6838 2022-06-16 stsp
1794 145b6838 2022-06-16 stsp dst[sz] = '\0';
1795 145b6838 2022-06-16 stsp *ptr = dst;
1796 145b6838 2022-06-16 stsp return NULL;
1797 963b370f 2018-05-20 stsp }
1798 963b370f 2018-05-20 stsp
1799 4e4a9ac8 2022-06-17 op /*
1800 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1801 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1802 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1803 4e4a9ac8 2022-06-17 op * *rcol.
1804 ccda2f4d 2022-06-16 stsp */
1805 4e4a9ac8 2022-06-17 op static int
1806 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1807 4e4a9ac8 2022-06-17 op {
1808 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1809 ccda2f4d 2022-06-16 stsp
1810 4e4a9ac8 2022-06-17 op if (n == 0) {
1811 4e4a9ac8 2022-06-17 op *rcol = cols;
1812 4e4a9ac8 2022-06-17 op return off;
1813 4e4a9ac8 2022-06-17 op }
1814 ccda2f4d 2022-06-16 stsp
1815 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1816 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1817 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1818 4e4a9ac8 2022-06-17 op else
1819 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1820 ccda2f4d 2022-06-16 stsp
1821 4e4a9ac8 2022-06-17 op if (width == -1) {
1822 4e4a9ac8 2022-06-17 op width = 1;
1823 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1824 ccda2f4d 2022-06-16 stsp }
1825 ccda2f4d 2022-06-16 stsp
1826 4e4a9ac8 2022-06-17 op if (cols + width > n)
1827 4e4a9ac8 2022-06-17 op break;
1828 4e4a9ac8 2022-06-17 op cols += width;
1829 ccda2f4d 2022-06-16 stsp }
1830 ccda2f4d 2022-06-16 stsp
1831 4e4a9ac8 2022-06-17 op *rcol = cols;
1832 4e4a9ac8 2022-06-17 op return i;
1833 ccda2f4d 2022-06-16 stsp }
1834 ccda2f4d 2022-06-16 stsp
1835 ccda2f4d 2022-06-16 stsp /*
1836 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1837 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1838 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1839 ccda2f4d 2022-06-16 stsp */
1840 ccda2f4d 2022-06-16 stsp static const struct got_error *
1841 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1842 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1843 ccda2f4d 2022-06-16 stsp {
1844 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1845 4e4a9ac8 2022-06-17 op int cols;
1846 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1847 145b6838 2022-06-16 stsp char *exstr = NULL;
1848 963b370f 2018-05-20 stsp size_t wlen;
1849 4e4a9ac8 2022-06-17 op int i, scrollx;
1850 963b370f 2018-05-20 stsp
1851 963b370f 2018-05-20 stsp *wlinep = NULL;
1852 b700b5d6 2018-07-10 stsp *widthp = 0;
1853 963b370f 2018-05-20 stsp
1854 145b6838 2022-06-16 stsp if (expand) {
1855 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1856 145b6838 2022-06-16 stsp if (err)
1857 145b6838 2022-06-16 stsp return err;
1858 145b6838 2022-06-16 stsp }
1859 145b6838 2022-06-16 stsp
1860 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1861 145b6838 2022-06-16 stsp free(exstr);
1862 963b370f 2018-05-20 stsp if (err)
1863 963b370f 2018-05-20 stsp return err;
1864 963b370f 2018-05-20 stsp
1865 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1866 ccda2f4d 2022-06-16 stsp
1867 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1868 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1869 3f670bfb 2020-12-10 stsp wlen--;
1870 3f670bfb 2020-12-10 stsp }
1871 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1872 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1873 3f670bfb 2020-12-10 stsp wlen--;
1874 3f670bfb 2020-12-10 stsp }
1875 3f670bfb 2020-12-10 stsp
1876 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1877 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1878 27a741e5 2019-09-11 stsp
1879 b700b5d6 2018-07-10 stsp if (widthp)
1880 b700b5d6 2018-07-10 stsp *widthp = cols;
1881 ccda2f4d 2022-06-16 stsp if (scrollxp)
1882 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1883 963b370f 2018-05-20 stsp if (err)
1884 963b370f 2018-05-20 stsp free(wline);
1885 963b370f 2018-05-20 stsp else
1886 963b370f 2018-05-20 stsp *wlinep = wline;
1887 963b370f 2018-05-20 stsp return err;
1888 963b370f 2018-05-20 stsp }
1889 963b370f 2018-05-20 stsp
1890 8b473291 2019-02-21 stsp static const struct got_error*
1891 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1892 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1893 8b473291 2019-02-21 stsp {
1894 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1895 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1896 8b473291 2019-02-21 stsp char *s;
1897 8b473291 2019-02-21 stsp const char *name;
1898 8b473291 2019-02-21 stsp
1899 8b473291 2019-02-21 stsp *refs_str = NULL;
1900 8b473291 2019-02-21 stsp
1901 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1902 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1903 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1904 52b5abe1 2019-08-13 stsp int cmp;
1905 52b5abe1 2019-08-13 stsp
1906 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1907 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1908 8b473291 2019-02-21 stsp continue;
1909 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1910 8b473291 2019-02-21 stsp name += 5;
1911 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1912 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1913 7143d404 2019-03-12 stsp continue;
1914 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1915 8b473291 2019-02-21 stsp name += 6;
1916 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1917 8b473291 2019-02-21 stsp name += 8;
1918 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1919 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1920 79cc719f 2020-04-24 stsp continue;
1921 79cc719f 2020-04-24 stsp }
1922 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1923 48cae60d 2020-09-22 stsp if (err)
1924 48cae60d 2020-09-22 stsp break;
1925 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1926 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1927 5d844a1e 2019-08-13 stsp if (err) {
1928 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1929 48cae60d 2020-09-22 stsp free(ref_id);
1930 5d844a1e 2019-08-13 stsp break;
1931 48cae60d 2020-09-22 stsp }
1932 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1933 5d844a1e 2019-08-13 stsp err = NULL;
1934 5d844a1e 2019-08-13 stsp tag = NULL;
1935 5d844a1e 2019-08-13 stsp }
1936 52b5abe1 2019-08-13 stsp }
1937 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1938 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1939 48cae60d 2020-09-22 stsp free(ref_id);
1940 52b5abe1 2019-08-13 stsp if (tag)
1941 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1942 52b5abe1 2019-08-13 stsp if (cmp != 0)
1943 52b5abe1 2019-08-13 stsp continue;
1944 8b473291 2019-02-21 stsp s = *refs_str;
1945 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1946 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1947 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1948 8b473291 2019-02-21 stsp free(s);
1949 8b473291 2019-02-21 stsp *refs_str = NULL;
1950 8b473291 2019-02-21 stsp break;
1951 8b473291 2019-02-21 stsp }
1952 8b473291 2019-02-21 stsp free(s);
1953 8b473291 2019-02-21 stsp }
1954 8b473291 2019-02-21 stsp
1955 8b473291 2019-02-21 stsp return err;
1956 8b473291 2019-02-21 stsp }
1957 8b473291 2019-02-21 stsp
1958 963b370f 2018-05-20 stsp static const struct got_error *
1959 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1960 27a741e5 2019-09-11 stsp int col_tab_align)
1961 5813d178 2019-03-09 stsp {
1962 e6b8b890 2020-12-29 naddy char *smallerthan;
1963 5813d178 2019-03-09 stsp
1964 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1965 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1966 5813d178 2019-03-09 stsp author = smallerthan + 1;
1967 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1968 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1969 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1970 5813d178 2019-03-09 stsp }
1971 5813d178 2019-03-09 stsp
1972 5813d178 2019-03-09 stsp static const struct got_error *
1973 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1974 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1975 8fdc79fe 2020-12-01 naddy int author_display_cols)
1976 80ddbec8 2018-04-29 stsp {
1977 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1978 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1979 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1980 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1981 5813d178 2019-03-09 stsp char *author = NULL;
1982 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1983 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1984 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1985 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1986 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1987 ccb26ccd 2018-11-05 stsp struct tm tm;
1988 45d799e2 2018-12-23 stsp time_t committer_time;
1989 11b20872 2019-11-08 stsp struct tog_color *tc;
1990 80ddbec8 2018-04-29 stsp
1991 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1992 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1993 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1994 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1995 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1996 b39d25c7 2018-07-10 stsp
1997 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1998 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1999 b39d25c7 2018-07-10 stsp else
2000 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2001 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2002 11b20872 2019-11-08 stsp if (tc)
2003 11b20872 2019-11-08 stsp wattr_on(view->window,
2004 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2005 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_off(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 27a741e5 2019-09-11 stsp col = limit;
2010 b39d25c7 2018-07-10 stsp if (col > avail)
2011 b39d25c7 2018-07-10 stsp goto done;
2012 6570a66d 2019-11-08 stsp
2013 6570a66d 2019-11-08 stsp if (avail >= 120) {
2014 6570a66d 2019-11-08 stsp char *id_str;
2015 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2016 6570a66d 2019-11-08 stsp if (err)
2017 6570a66d 2019-11-08 stsp goto done;
2018 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2019 11b20872 2019-11-08 stsp if (tc)
2020 11b20872 2019-11-08 stsp wattr_on(view->window,
2021 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2022 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2023 11b20872 2019-11-08 stsp if (tc)
2024 11b20872 2019-11-08 stsp wattr_off(view->window,
2025 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2026 6570a66d 2019-11-08 stsp free(id_str);
2027 6570a66d 2019-11-08 stsp col += 9;
2028 6570a66d 2019-11-08 stsp if (col > avail)
2029 6570a66d 2019-11-08 stsp goto done;
2030 6570a66d 2019-11-08 stsp }
2031 b39d25c7 2018-07-10 stsp
2032 10aab77f 2022-07-19 op if (s->use_committer)
2033 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2034 10aab77f 2022-07-19 op else
2035 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2036 5813d178 2019-03-09 stsp if (author == NULL) {
2037 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2038 80ddbec8 2018-04-29 stsp goto done;
2039 80ddbec8 2018-04-29 stsp }
2040 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2041 bb737323 2018-05-20 stsp if (err)
2042 bb737323 2018-05-20 stsp goto done;
2043 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2044 11b20872 2019-11-08 stsp if (tc)
2045 11b20872 2019-11-08 stsp wattr_on(view->window,
2046 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2047 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2048 11b20872 2019-11-08 stsp if (tc)
2049 11b20872 2019-11-08 stsp wattr_off(view->window,
2050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2051 bb737323 2018-05-20 stsp col += author_width;
2052 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2053 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2054 bb737323 2018-05-20 stsp col++;
2055 bb737323 2018-05-20 stsp author_width++;
2056 bb737323 2018-05-20 stsp }
2057 9c2eaf34 2018-05-20 stsp if (col > avail)
2058 9c2eaf34 2018-05-20 stsp goto done;
2059 80ddbec8 2018-04-29 stsp
2060 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2061 5943eee2 2019-08-13 stsp if (err)
2062 6d9fbc00 2018-04-29 stsp goto done;
2063 bb737323 2018-05-20 stsp logmsg = logmsg0;
2064 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2065 bb737323 2018-05-20 stsp logmsg++;
2066 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2067 bb737323 2018-05-20 stsp if (newline)
2068 bb737323 2018-05-20 stsp *newline = '\0';
2069 ccda2f4d 2022-06-16 stsp limit = avail - col;
2070 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2071 4d1f6af3 2022-06-17 op limit--; /* for the border */
2072 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2073 ccda2f4d 2022-06-16 stsp limit, col, 1);
2074 29688b02 2022-06-16 stsp if (err)
2075 29688b02 2022-06-16 stsp goto done;
2076 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2077 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2078 27a741e5 2019-09-11 stsp while (col < avail) {
2079 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2080 bb737323 2018-05-20 stsp col++;
2081 881b2d3e 2018-04-30 stsp }
2082 80ddbec8 2018-04-29 stsp done:
2083 80ddbec8 2018-04-29 stsp free(logmsg0);
2084 bb737323 2018-05-20 stsp free(wlogmsg);
2085 5813d178 2019-03-09 stsp free(author);
2086 bb737323 2018-05-20 stsp free(wauthor);
2087 80ddbec8 2018-04-29 stsp free(line);
2088 80ddbec8 2018-04-29 stsp return err;
2089 80ddbec8 2018-04-29 stsp }
2090 26ed57b2 2018-05-19 stsp
2091 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2092 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2093 899d86c2 2018-05-10 stsp struct got_object_id *id)
2094 80ddbec8 2018-04-29 stsp {
2095 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2096 80ddbec8 2018-04-29 stsp
2097 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2098 80ddbec8 2018-04-29 stsp if (entry == NULL)
2099 899d86c2 2018-05-10 stsp return NULL;
2100 99db9666 2018-05-07 stsp
2101 899d86c2 2018-05-10 stsp entry->id = id;
2102 99db9666 2018-05-07 stsp entry->commit = commit;
2103 899d86c2 2018-05-10 stsp return entry;
2104 99db9666 2018-05-07 stsp }
2105 80ddbec8 2018-04-29 stsp
2106 99db9666 2018-05-07 stsp static void
2107 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2108 99db9666 2018-05-07 stsp {
2109 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2110 99db9666 2018-05-07 stsp
2111 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2112 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2113 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2114 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2115 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2116 99db9666 2018-05-07 stsp free(entry);
2117 99db9666 2018-05-07 stsp }
2118 99db9666 2018-05-07 stsp
2119 99db9666 2018-05-07 stsp static void
2120 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2121 99db9666 2018-05-07 stsp {
2122 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2123 99db9666 2018-05-07 stsp pop_commit(commits);
2124 c4972b91 2018-05-07 stsp }
2125 c4972b91 2018-05-07 stsp
2126 c4972b91 2018-05-07 stsp static const struct got_error *
2127 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2128 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2129 13add988 2019-10-15 stsp {
2130 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2131 13add988 2019-10-15 stsp regmatch_t regmatch;
2132 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2133 13add988 2019-10-15 stsp
2134 13add988 2019-10-15 stsp *have_match = 0;
2135 13add988 2019-10-15 stsp
2136 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2137 13add988 2019-10-15 stsp if (err)
2138 13add988 2019-10-15 stsp return err;
2139 13add988 2019-10-15 stsp
2140 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2141 13add988 2019-10-15 stsp if (err)
2142 13add988 2019-10-15 stsp goto done;
2143 13add988 2019-10-15 stsp
2144 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2145 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2146 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2147 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2148 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2149 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2150 13add988 2019-10-15 stsp *have_match = 1;
2151 13add988 2019-10-15 stsp done:
2152 13add988 2019-10-15 stsp free(id_str);
2153 13add988 2019-10-15 stsp free(logmsg);
2154 13add988 2019-10-15 stsp return err;
2155 13add988 2019-10-15 stsp }
2156 13add988 2019-10-15 stsp
2157 13add988 2019-10-15 stsp static const struct got_error *
2158 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2159 c4972b91 2018-05-07 stsp {
2160 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2161 9ba79e04 2018-06-11 stsp
2162 1a76625f 2018-10-22 stsp /*
2163 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2164 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2165 1a76625f 2018-10-22 stsp * while updating the display.
2166 1a76625f 2018-10-22 stsp */
2167 4e0d2870 2020-12-07 naddy do {
2168 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2169 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2170 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2171 1a76625f 2018-10-22 stsp int errcode;
2172 899d86c2 2018-05-10 stsp
2173 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2174 4e0d2870 2020-12-07 naddy NULL, NULL);
2175 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2176 ecb28ae0 2018-07-16 stsp break;
2177 899d86c2 2018-05-10 stsp
2178 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2179 9ba79e04 2018-06-11 stsp if (err)
2180 9ba79e04 2018-06-11 stsp break;
2181 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2182 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2183 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2184 9ba79e04 2018-06-11 stsp break;
2185 9ba79e04 2018-06-11 stsp }
2186 93e45b7c 2018-09-24 stsp
2187 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2188 1a76625f 2018-10-22 stsp if (errcode) {
2189 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2190 13add988 2019-10-15 stsp "pthread_mutex_lock");
2191 1a76625f 2018-10-22 stsp break;
2192 1a76625f 2018-10-22 stsp }
2193 1a76625f 2018-10-22 stsp
2194 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2195 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2196 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2197 1a76625f 2018-10-22 stsp
2198 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2199 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2200 7c1452c1 2020-03-26 stsp int have_match;
2201 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2202 7c1452c1 2020-03-26 stsp if (err)
2203 7c1452c1 2020-03-26 stsp break;
2204 7c1452c1 2020-03-26 stsp if (have_match)
2205 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2206 13add988 2019-10-15 stsp }
2207 13add988 2019-10-15 stsp
2208 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2209 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2210 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2211 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2212 7c1452c1 2020-03-26 stsp if (err)
2213 13add988 2019-10-15 stsp break;
2214 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2215 899d86c2 2018-05-10 stsp
2216 9ba79e04 2018-06-11 stsp return err;
2217 0553a4e3 2018-04-30 stsp }
2218 0553a4e3 2018-04-30 stsp
2219 2b779855 2020-12-05 naddy static void
2220 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2221 2b779855 2020-12-05 naddy {
2222 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2223 2b779855 2020-12-05 naddy int ncommits = 0;
2224 2b779855 2020-12-05 naddy
2225 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2226 2b779855 2020-12-05 naddy while (entry) {
2227 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2228 2b779855 2020-12-05 naddy s->selected_entry = entry;
2229 2b779855 2020-12-05 naddy break;
2230 2b779855 2020-12-05 naddy }
2231 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2232 2b779855 2020-12-05 naddy ncommits++;
2233 2b779855 2020-12-05 naddy }
2234 2b779855 2020-12-05 naddy }
2235 2b779855 2020-12-05 naddy
2236 0553a4e3 2018-04-30 stsp static const struct got_error *
2237 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2238 0553a4e3 2018-04-30 stsp {
2239 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2240 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2241 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2242 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2243 60493ae3 2019-06-20 stsp int width;
2244 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2245 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2246 8b473291 2019-02-21 stsp char *refs_str = NULL;
2247 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2248 11b20872 2019-11-08 stsp struct tog_color *tc;
2249 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2250 0553a4e3 2018-04-30 stsp
2251 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2252 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2253 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2254 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2255 1a76625f 2018-10-22 stsp if (err)
2256 ecb28ae0 2018-07-16 stsp return err;
2257 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2258 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2259 d2075bf3 2020-12-25 stsp if (refs) {
2260 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2261 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2262 d2075bf3 2020-12-25 stsp if (err)
2263 d2075bf3 2020-12-25 stsp goto done;
2264 d2075bf3 2020-12-25 stsp }
2265 867c6645 2018-07-10 stsp }
2266 359bfafd 2019-02-22 stsp
2267 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2268 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2269 1a76625f 2018-10-22 stsp
2270 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2271 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2272 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2273 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2274 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2275 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2276 8f4ed634 2020-03-26 stsp goto done;
2277 8f4ed634 2020-03-26 stsp }
2278 8f4ed634 2020-03-26 stsp } else {
2279 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2280 f9686aa5 2020-03-27 stsp
2281 f9686aa5 2020-03-27 stsp if (view->searching) {
2282 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2283 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2284 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2285 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2286 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2287 f9686aa5 2020-03-27 stsp search_str = "searching...";
2288 f9686aa5 2020-03-27 stsp }
2289 f9686aa5 2020-03-27 stsp
2290 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2291 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2292 f9686aa5 2020-03-27 stsp search_str ? search_str :
2293 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2294 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2295 8f4ed634 2020-03-26 stsp goto done;
2296 8f4ed634 2020-03-26 stsp }
2297 8b473291 2019-02-21 stsp }
2298 1a76625f 2018-10-22 stsp
2299 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2300 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2301 87411fa9 2022-06-16 stsp "........................................",
2302 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2304 1a76625f 2018-10-22 stsp header = NULL;
2305 1a76625f 2018-10-22 stsp goto done;
2306 1a76625f 2018-10-22 stsp }
2307 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2308 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2309 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2311 1a76625f 2018-10-22 stsp header = NULL;
2312 1a76625f 2018-10-22 stsp goto done;
2313 ecb28ae0 2018-07-16 stsp }
2314 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2315 1a76625f 2018-10-22 stsp if (err)
2316 1a76625f 2018-10-22 stsp goto done;
2317 867c6645 2018-07-10 stsp
2318 2814baeb 2018-08-01 stsp werase(view->window);
2319 867c6645 2018-07-10 stsp
2320 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2321 a3404814 2018-09-02 stsp wstandout(view->window);
2322 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2323 11b20872 2019-11-08 stsp if (tc)
2324 11b20872 2019-11-08 stsp wattr_on(view->window,
2325 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2326 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2327 11b20872 2019-11-08 stsp if (tc)
2328 11b20872 2019-11-08 stsp wattr_off(view->window,
2329 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2330 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2331 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2332 1a76625f 2018-10-22 stsp width++;
2333 1a76625f 2018-10-22 stsp }
2334 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2335 a3404814 2018-09-02 stsp wstandend(view->window);
2336 ecb28ae0 2018-07-16 stsp free(wline);
2337 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2338 1a76625f 2018-10-22 stsp goto done;
2339 0553a4e3 2018-04-30 stsp
2340 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2341 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2342 5813d178 2019-03-09 stsp ncommits = 0;
2343 145b6838 2022-06-16 stsp view->maxx = 0;
2344 5813d178 2019-03-09 stsp while (entry) {
2345 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2346 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2347 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2348 5813d178 2019-03-09 stsp int width;
2349 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2350 5813d178 2019-03-09 stsp break;
2351 10aab77f 2022-07-19 op if (s->use_committer)
2352 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2353 10aab77f 2022-07-19 op else
2354 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2355 5813d178 2019-03-09 stsp if (author == NULL) {
2356 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2357 5813d178 2019-03-09 stsp goto done;
2358 5813d178 2019-03-09 stsp }
2359 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2360 27a741e5 2019-09-11 stsp date_display_cols);
2361 5813d178 2019-03-09 stsp if (author_cols < width)
2362 5813d178 2019-03-09 stsp author_cols = width;
2363 5813d178 2019-03-09 stsp free(wauthor);
2364 5813d178 2019-03-09 stsp free(author);
2365 a310d9c3 2022-07-21 florian if (err)
2366 a310d9c3 2022-07-21 florian goto done;
2367 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2368 145b6838 2022-06-16 stsp if (err)
2369 145b6838 2022-06-16 stsp goto done;
2370 145b6838 2022-06-16 stsp msg = msg0;
2371 145b6838 2022-06-16 stsp while (*msg == '\n')
2372 145b6838 2022-06-16 stsp ++msg;
2373 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2374 29688b02 2022-06-16 stsp *eol = '\0';
2375 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2376 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2377 29688b02 2022-06-16 stsp if (err)
2378 29688b02 2022-06-16 stsp goto done;
2379 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2380 145b6838 2022-06-16 stsp free(msg0);
2381 29688b02 2022-06-16 stsp free(wmsg);
2382 7ca04879 2019-10-19 stsp ncommits++;
2383 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2384 5813d178 2019-03-09 stsp }
2385 5813d178 2019-03-09 stsp
2386 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2387 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2388 867c6645 2018-07-10 stsp ncommits = 0;
2389 899d86c2 2018-05-10 stsp while (entry) {
2390 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2391 899d86c2 2018-05-10 stsp break;
2392 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2393 2814baeb 2018-08-01 stsp wstandout(view->window);
2394 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2395 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2396 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2397 2814baeb 2018-08-01 stsp wstandend(view->window);
2398 0553a4e3 2018-04-30 stsp if (err)
2399 60493ae3 2019-06-20 stsp goto done;
2400 0553a4e3 2018-04-30 stsp ncommits++;
2401 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2402 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2403 80ddbec8 2018-04-29 stsp }
2404 80ddbec8 2018-04-29 stsp
2405 9b058f45 2022-06-30 mark view_border(view);
2406 1a76625f 2018-10-22 stsp done:
2407 1a76625f 2018-10-22 stsp free(id_str);
2408 8b473291 2019-02-21 stsp free(refs_str);
2409 1a76625f 2018-10-22 stsp free(ncommits_str);
2410 1a76625f 2018-10-22 stsp free(header);
2411 80ddbec8 2018-04-29 stsp return err;
2412 9f7d7167 2018-04-29 stsp }
2413 07b55e75 2018-05-10 stsp
2414 07b55e75 2018-05-10 stsp static void
2415 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2416 07b55e75 2018-05-10 stsp {
2417 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2418 07b55e75 2018-05-10 stsp int nscrolled = 0;
2419 07b55e75 2018-05-10 stsp
2420 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2421 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2422 07b55e75 2018-05-10 stsp return;
2423 9f7d7167 2018-04-29 stsp
2424 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2425 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2426 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2427 07b55e75 2018-05-10 stsp if (entry) {
2428 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2429 07b55e75 2018-05-10 stsp nscrolled++;
2430 07b55e75 2018-05-10 stsp }
2431 07b55e75 2018-05-10 stsp }
2432 aa075928 2018-05-10 stsp }
2433 aa075928 2018-05-10 stsp
2434 aa075928 2018-05-10 stsp static const struct got_error *
2435 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2436 aa075928 2018-05-10 stsp {
2437 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2438 5e224a3e 2019-02-22 stsp int errcode;
2439 8a42fca8 2019-02-22 stsp
2440 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2441 aa075928 2018-05-10 stsp
2442 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2443 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2444 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2445 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2446 7aafa0d1 2019-02-22 stsp if (errcode)
2447 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2448 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2449 7c1452c1 2020-03-26 stsp
2450 7c1452c1 2020-03-26 stsp /*
2451 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2452 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2453 7c1452c1 2020-03-26 stsp */
2454 7c1452c1 2020-03-26 stsp if (!wait)
2455 7c1452c1 2020-03-26 stsp break;
2456 7c1452c1 2020-03-26 stsp
2457 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2458 ffe38506 2020-12-01 naddy show_log_view(view);
2459 7c1452c1 2020-03-26 stsp update_panels();
2460 7c1452c1 2020-03-26 stsp doupdate();
2461 7c1452c1 2020-03-26 stsp
2462 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2463 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2464 82954512 2020-02-03 stsp if (errcode)
2465 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2466 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2467 82954512 2020-02-03 stsp
2468 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2469 ffe38506 2020-12-01 naddy show_log_view(view);
2470 7c1452c1 2020-03-26 stsp update_panels();
2471 7c1452c1 2020-03-26 stsp doupdate();
2472 5e224a3e 2019-02-22 stsp }
2473 5e224a3e 2019-02-22 stsp
2474 5e224a3e 2019-02-22 stsp return NULL;
2475 5e224a3e 2019-02-22 stsp }
2476 5e224a3e 2019-02-22 stsp
2477 5e224a3e 2019-02-22 stsp static const struct got_error *
2478 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2479 9b058f45 2022-06-30 mark {
2480 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2481 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2482 2525dccb 2022-07-13 mark
2483 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2484 2525dccb 2022-07-13 mark return NULL;
2485 9b058f45 2022-06-30 mark
2486 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2487 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2488 9b058f45 2022-06-30 mark view->nscrolled = 0;
2489 9b058f45 2022-06-30 mark
2490 9b058f45 2022-06-30 mark return err;
2491 9b058f45 2022-06-30 mark }
2492 9b058f45 2022-06-30 mark
2493 9b058f45 2022-06-30 mark static const struct got_error *
2494 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2495 5e224a3e 2019-02-22 stsp {
2496 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2497 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2498 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2499 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2500 5e224a3e 2019-02-22 stsp
2501 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2502 5e224a3e 2019-02-22 stsp return NULL;
2503 5e224a3e 2019-02-22 stsp
2504 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2505 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2506 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2507 08ebd0a9 2019-02-22 stsp /*
2508 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2509 08ebd0a9 2019-02-22 stsp */
2510 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2511 94b80cfa 2022-08-01 mark ncommits_needed - s->commits.ncommits;
2512 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2513 5e224a3e 2019-02-22 stsp if (err)
2514 5e224a3e 2019-02-22 stsp return err;
2515 7aafa0d1 2019-02-22 stsp }
2516 b295e71b 2019-02-22 stsp
2517 7aafa0d1 2019-02-22 stsp do {
2518 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2519 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2520 88048b54 2019-02-21 stsp break;
2521 88048b54 2019-02-21 stsp
2522 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2523 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2524 aa075928 2018-05-10 stsp
2525 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2526 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2527 dd0a52c1 2018-05-20 stsp break;
2528 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2529 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2530 aa075928 2018-05-10 stsp
2531 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2532 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2533 9b058f45 2022-06-30 mark else
2534 9b058f45 2022-06-30 mark view->nscrolled = 0;
2535 9b058f45 2022-06-30 mark
2536 dd0a52c1 2018-05-20 stsp return err;
2537 07b55e75 2018-05-10 stsp }
2538 4a7f7875 2018-05-10 stsp
2539 cd0acaa7 2018-05-20 stsp static const struct got_error *
2540 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2541 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2542 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2543 cd0acaa7 2018-05-20 stsp {
2544 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2545 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2546 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2547 cd0acaa7 2018-05-20 stsp
2548 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2549 15a94983 2018-12-23 stsp if (diff_view == NULL)
2550 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2551 ea5e7bb5 2018-08-01 stsp
2552 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2553 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2554 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2555 e5a0f69f 2018-08-18 stsp if (err == NULL)
2556 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2557 cd0acaa7 2018-05-20 stsp return err;
2558 4a7f7875 2018-05-10 stsp }
2559 4a7f7875 2018-05-10 stsp
2560 80ddbec8 2018-04-29 stsp static const struct got_error *
2561 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2562 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2563 9343a5fb 2018-06-23 stsp {
2564 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2565 941e9f74 2019-05-21 stsp
2566 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2567 941e9f74 2019-05-21 stsp if (parent == NULL)
2568 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2569 941e9f74 2019-05-21 stsp
2570 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2571 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2572 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2573 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2574 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2575 941e9f74 2019-05-21 stsp s->tree = subtree;
2576 941e9f74 2019-05-21 stsp s->selected = 0;
2577 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2578 941e9f74 2019-05-21 stsp return NULL;
2579 941e9f74 2019-05-21 stsp }
2580 941e9f74 2019-05-21 stsp
2581 941e9f74 2019-05-21 stsp static const struct got_error *
2582 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2583 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2584 941e9f74 2019-05-21 stsp {
2585 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2586 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2587 941e9f74 2019-05-21 stsp const char *p;
2588 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2589 9343a5fb 2018-06-23 stsp
2590 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2591 941e9f74 2019-05-21 stsp p = path;
2592 941e9f74 2019-05-21 stsp while (*p) {
2593 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2594 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2595 56e0773d 2019-11-28 stsp char *te_name;
2596 33cbf02b 2020-01-12 stsp
2597 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2598 33cbf02b 2020-01-12 stsp p++;
2599 941e9f74 2019-05-21 stsp
2600 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2601 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2602 941e9f74 2019-05-21 stsp if (slash == NULL)
2603 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2604 33cbf02b 2020-01-12 stsp else
2605 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2606 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2607 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2608 56e0773d 2019-11-28 stsp break;
2609 941e9f74 2019-05-21 stsp }
2610 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2611 56e0773d 2019-11-28 stsp if (te == NULL) {
2612 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2613 56e0773d 2019-11-28 stsp free(te_name);
2614 941e9f74 2019-05-21 stsp break;
2615 941e9f74 2019-05-21 stsp }
2616 56e0773d 2019-11-28 stsp free(te_name);
2617 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2618 941e9f74 2019-05-21 stsp
2619 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2620 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2621 b03c880f 2019-05-21 stsp
2622 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2623 941e9f74 2019-05-21 stsp if (slash)
2624 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2625 941e9f74 2019-05-21 stsp else
2626 941e9f74 2019-05-21 stsp subpath = strdup(path);
2627 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2628 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2629 941e9f74 2019-05-21 stsp break;
2630 941e9f74 2019-05-21 stsp }
2631 941e9f74 2019-05-21 stsp
2632 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2633 941e9f74 2019-05-21 stsp subpath);
2634 941e9f74 2019-05-21 stsp if (err)
2635 941e9f74 2019-05-21 stsp break;
2636 941e9f74 2019-05-21 stsp
2637 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2638 941e9f74 2019-05-21 stsp free(tree_id);
2639 941e9f74 2019-05-21 stsp if (err)
2640 941e9f74 2019-05-21 stsp break;
2641 941e9f74 2019-05-21 stsp
2642 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2643 941e9f74 2019-05-21 stsp if (err) {
2644 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2645 941e9f74 2019-05-21 stsp break;
2646 941e9f74 2019-05-21 stsp }
2647 941e9f74 2019-05-21 stsp if (slash == NULL)
2648 941e9f74 2019-05-21 stsp break;
2649 941e9f74 2019-05-21 stsp free(subpath);
2650 941e9f74 2019-05-21 stsp subpath = NULL;
2651 941e9f74 2019-05-21 stsp p = slash;
2652 941e9f74 2019-05-21 stsp }
2653 941e9f74 2019-05-21 stsp
2654 941e9f74 2019-05-21 stsp free(subpath);
2655 1a76625f 2018-10-22 stsp return err;
2656 61266923 2020-01-14 stsp }
2657 61266923 2020-01-14 stsp
2658 61266923 2020-01-14 stsp static const struct got_error *
2659 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2660 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2661 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2662 55cccc34 2020-02-20 stsp {
2663 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2664 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2665 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2666 55cccc34 2020-02-20 stsp
2667 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2668 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2669 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2670 55cccc34 2020-02-20 stsp
2671 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2672 bc573f3b 2021-07-10 stsp if (err)
2673 55cccc34 2020-02-20 stsp return err;
2674 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2675 55cccc34 2020-02-20 stsp
2676 55cccc34 2020-02-20 stsp *new_view = tree_view;
2677 55cccc34 2020-02-20 stsp
2678 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2679 55cccc34 2020-02-20 stsp return NULL;
2680 55cccc34 2020-02-20 stsp
2681 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2682 55cccc34 2020-02-20 stsp }
2683 55cccc34 2020-02-20 stsp
2684 55cccc34 2020-02-20 stsp static const struct got_error *
2685 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2686 61266923 2020-01-14 stsp {
2687 61266923 2020-01-14 stsp sigset_t sigset;
2688 61266923 2020-01-14 stsp int errcode;
2689 61266923 2020-01-14 stsp
2690 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2691 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2692 61266923 2020-01-14 stsp
2693 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2694 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2695 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2696 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2697 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2698 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2699 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2700 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2701 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2702 61266923 2020-01-14 stsp
2703 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2704 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2705 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2706 61266923 2020-01-14 stsp
2707 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2708 61266923 2020-01-14 stsp if (errcode)
2709 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2710 61266923 2020-01-14 stsp
2711 61266923 2020-01-14 stsp return NULL;
2712 1a76625f 2018-10-22 stsp }
2713 1a76625f 2018-10-22 stsp
2714 1a76625f 2018-10-22 stsp static void *
2715 1a76625f 2018-10-22 stsp log_thread(void *arg)
2716 1a76625f 2018-10-22 stsp {
2717 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2718 1a76625f 2018-10-22 stsp int errcode = 0;
2719 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2720 1a76625f 2018-10-22 stsp int done = 0;
2721 61266923 2020-01-14 stsp
2722 5629093a 2022-07-11 stsp /*
2723 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2724 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2725 5629093a 2022-07-11 stsp */
2726 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2727 5629093a 2022-07-11 stsp if (errcode) {
2728 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2729 61266923 2020-01-14 stsp return (void *)err;
2730 5629093a 2022-07-11 stsp }
2731 1a76625f 2018-10-22 stsp
2732 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2733 5629093a 2022-07-11 stsp if (err) {
2734 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2735 5629093a 2022-07-11 stsp goto done;
2736 5629093a 2022-07-11 stsp }
2737 5629093a 2022-07-11 stsp
2738 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2739 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2740 5629093a 2022-07-11 stsp if (errcode) {
2741 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2742 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2743 5629093a 2022-07-11 stsp goto done;
2744 5629093a 2022-07-11 stsp }
2745 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2746 1a76625f 2018-10-22 stsp if (err) {
2747 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2748 5629093a 2022-07-11 stsp goto done;
2749 1a76625f 2018-10-22 stsp err = NULL;
2750 1a76625f 2018-10-22 stsp done = 1;
2751 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2752 1a76625f 2018-10-22 stsp a->commits_needed--;
2753 1a76625f 2018-10-22 stsp
2754 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2755 3abe8080 2019-04-10 stsp if (errcode) {
2756 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2757 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2758 5629093a 2022-07-11 stsp goto done;
2759 3abe8080 2019-04-10 stsp } else if (*a->quit)
2760 1a76625f 2018-10-22 stsp done = 1;
2761 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2762 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2763 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2764 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2765 1a76625f 2018-10-22 stsp }
2766 1a76625f 2018-10-22 stsp
2767 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2768 7c1452c1 2020-03-26 stsp if (errcode) {
2769 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2770 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2771 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2772 5629093a 2022-07-11 stsp goto done;
2773 7c1452c1 2020-03-26 stsp }
2774 7c1452c1 2020-03-26 stsp
2775 1a76625f 2018-10-22 stsp if (done)
2776 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2777 7c1452c1 2020-03-26 stsp else {
2778 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2779 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2780 7c1452c1 2020-03-26 stsp &tog_mutex);
2781 5629093a 2022-07-11 stsp if (errcode) {
2782 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2783 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2784 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2785 5629093a 2022-07-11 stsp goto done;
2786 5629093a 2022-07-11 stsp }
2787 21355643 2020-12-06 stsp if (*a->quit)
2788 21355643 2020-12-06 stsp done = 1;
2789 7c1452c1 2020-03-26 stsp }
2790 1a76625f 2018-10-22 stsp }
2791 1a76625f 2018-10-22 stsp }
2792 3abe8080 2019-04-10 stsp a->log_complete = 1;
2793 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2794 5629093a 2022-07-11 stsp if (errcode)
2795 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2796 5629093a 2022-07-11 stsp done:
2797 5629093a 2022-07-11 stsp if (err) {
2798 5629093a 2022-07-11 stsp tog_thread_error = 1;
2799 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2800 5629093a 2022-07-11 stsp }
2801 1a76625f 2018-10-22 stsp return (void *)err;
2802 1a76625f 2018-10-22 stsp }
2803 1a76625f 2018-10-22 stsp
2804 1a76625f 2018-10-22 stsp static const struct got_error *
2805 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2806 1a76625f 2018-10-22 stsp {
2807 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2808 1a76625f 2018-10-22 stsp int errcode;
2809 1a76625f 2018-10-22 stsp
2810 1a76625f 2018-10-22 stsp if (s->thread) {
2811 1a76625f 2018-10-22 stsp s->quit = 1;
2812 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2813 1a76625f 2018-10-22 stsp if (errcode)
2814 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2815 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2816 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2817 1a76625f 2018-10-22 stsp if (errcode)
2818 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2819 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2820 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2821 1a76625f 2018-10-22 stsp if (errcode)
2822 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2823 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2824 1a76625f 2018-10-22 stsp if (errcode)
2825 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2826 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2827 1a76625f 2018-10-22 stsp s->thread = NULL;
2828 1a76625f 2018-10-22 stsp }
2829 1a76625f 2018-10-22 stsp
2830 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2831 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2832 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2833 74467cc8 2022-06-15 stsp }
2834 74467cc8 2022-06-15 stsp
2835 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2836 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2837 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2838 74467cc8 2022-06-15 stsp if (err == NULL)
2839 74467cc8 2022-06-15 stsp err = pack_err;
2840 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2841 1a76625f 2018-10-22 stsp }
2842 1a76625f 2018-10-22 stsp
2843 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2844 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2845 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2846 1a76625f 2018-10-22 stsp }
2847 1a76625f 2018-10-22 stsp
2848 5629093a 2022-07-11 stsp return err ? err : thread_err;
2849 9343a5fb 2018-06-23 stsp }
2850 9343a5fb 2018-06-23 stsp
2851 9343a5fb 2018-06-23 stsp static const struct got_error *
2852 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2853 1a76625f 2018-10-22 stsp {
2854 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2855 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2856 276b94a1 2020-11-13 naddy int errcode;
2857 1a76625f 2018-10-22 stsp
2858 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2859 276b94a1 2020-11-13 naddy
2860 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2861 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2862 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2863 276b94a1 2020-11-13 naddy
2864 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2865 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2866 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2867 276b94a1 2020-11-13 naddy
2868 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2869 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2870 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2871 1a76625f 2018-10-22 stsp free(s->start_id);
2872 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2873 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2874 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2875 1a76625f 2018-10-22 stsp return err;
2876 1a76625f 2018-10-22 stsp }
2877 1a76625f 2018-10-22 stsp
2878 1a76625f 2018-10-22 stsp static const struct got_error *
2879 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2880 60493ae3 2019-06-20 stsp {
2881 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2882 60493ae3 2019-06-20 stsp
2883 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2884 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2885 60493ae3 2019-06-20 stsp return NULL;
2886 60493ae3 2019-06-20 stsp }
2887 60493ae3 2019-06-20 stsp
2888 60493ae3 2019-06-20 stsp static const struct got_error *
2889 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2890 60493ae3 2019-06-20 stsp {
2891 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2892 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2893 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2894 60493ae3 2019-06-20 stsp
2895 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2896 f9686aa5 2020-03-27 stsp show_log_view(view);
2897 f9686aa5 2020-03-27 stsp update_panels();
2898 f9686aa5 2020-03-27 stsp doupdate();
2899 f9686aa5 2020-03-27 stsp
2900 96e2b566 2019-07-08 stsp if (s->search_entry) {
2901 13add988 2019-10-15 stsp int errcode, ch;
2902 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2903 13add988 2019-10-15 stsp if (errcode)
2904 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2905 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2906 13add988 2019-10-15 stsp ch = wgetch(view->window);
2907 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2908 13add988 2019-10-15 stsp if (errcode)
2909 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2910 13add988 2019-10-15 stsp "pthread_mutex_lock");
2911 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2912 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2913 678cbce5 2019-07-28 stsp return NULL;
2914 678cbce5 2019-07-28 stsp }
2915 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2916 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2917 96e2b566 2019-07-08 stsp else
2918 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2919 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2920 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2921 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2922 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2923 364ac6fd 2022-06-18 stsp
2924 364ac6fd 2022-06-18 stsp /*
2925 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2926 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2927 f704b35c 2022-06-18 stsp * might have changed.
2928 364ac6fd 2022-06-18 stsp */
2929 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2930 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2931 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2932 364ac6fd 2022-06-18 stsp else
2933 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2934 4bfe9f0a 2022-06-18 stsp } else {
2935 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2936 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2937 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2938 364ac6fd 2022-06-18 stsp else
2939 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2940 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2941 4bfe9f0a 2022-06-18 stsp }
2942 20be8d96 2019-06-21 stsp } else {
2943 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2944 20be8d96 2019-06-21 stsp }
2945 60493ae3 2019-06-20 stsp
2946 60493ae3 2019-06-20 stsp while (1) {
2947 13add988 2019-10-15 stsp int have_match = 0;
2948 13add988 2019-10-15 stsp
2949 60493ae3 2019-06-20 stsp if (entry == NULL) {
2950 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2951 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2952 f9967bca 2020-03-27 stsp view->search_next_done =
2953 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2954 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2955 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2956 f801134a 2019-06-25 stsp return NULL;
2957 60493ae3 2019-06-20 stsp }
2958 96e2b566 2019-07-08 stsp /*
2959 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2960 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2961 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2962 96e2b566 2019-07-08 stsp */
2963 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2964 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2965 60493ae3 2019-06-20 stsp }
2966 60493ae3 2019-06-20 stsp
2967 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2968 13add988 2019-10-15 stsp &view->regex);
2969 5943eee2 2019-08-13 stsp if (err)
2970 13add988 2019-10-15 stsp break;
2971 13add988 2019-10-15 stsp if (have_match) {
2972 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2973 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2974 60493ae3 2019-06-20 stsp break;
2975 60493ae3 2019-06-20 stsp }
2976 13add988 2019-10-15 stsp
2977 96e2b566 2019-07-08 stsp s->search_entry = entry;
2978 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2979 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2980 b1bf1435 2019-06-21 stsp else
2981 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2982 60493ae3 2019-06-20 stsp }
2983 60493ae3 2019-06-20 stsp
2984 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2985 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2986 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2987 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2988 60493ae3 2019-06-20 stsp if (err)
2989 60493ae3 2019-06-20 stsp return err;
2990 ead14cbe 2019-06-21 stsp cur++;
2991 ead14cbe 2019-06-21 stsp }
2992 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2993 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2994 60493ae3 2019-06-20 stsp if (err)
2995 60493ae3 2019-06-20 stsp return err;
2996 ead14cbe 2019-06-21 stsp cur--;
2997 60493ae3 2019-06-20 stsp }
2998 60493ae3 2019-06-20 stsp }
2999 60493ae3 2019-06-20 stsp
3000 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3001 96e2b566 2019-07-08 stsp
3002 60493ae3 2019-06-20 stsp return NULL;
3003 60493ae3 2019-06-20 stsp }
3004 60493ae3 2019-06-20 stsp
3005 60493ae3 2019-06-20 stsp static const struct got_error *
3006 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3007 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3008 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3009 80ddbec8 2018-04-29 stsp {
3010 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3011 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3012 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3013 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3014 1a76625f 2018-10-22 stsp int errcode;
3015 80ddbec8 2018-04-29 stsp
3016 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3017 f135c941 2020-02-20 stsp free(s->in_repo_path);
3018 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3019 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3020 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3021 f135c941 2020-02-20 stsp }
3022 ecb28ae0 2018-07-16 stsp
3023 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3024 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3025 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3026 78756c87 2020-11-24 stsp
3027 fb2756b9 2018-08-04 stsp s->repo = repo;
3028 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3029 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3030 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3031 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3032 9cd7cbd1 2020-12-07 stsp goto done;
3033 9cd7cbd1 2020-12-07 stsp }
3034 9cd7cbd1 2020-12-07 stsp }
3035 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3036 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3037 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3038 5036bf37 2018-09-24 stsp goto done;
3039 5036bf37 2018-09-24 stsp }
3040 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3041 e5a0f69f 2018-08-18 stsp
3042 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3043 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3044 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3045 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3046 11b20872 2019-11-08 stsp if (err)
3047 11b20872 2019-11-08 stsp goto done;
3048 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3049 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3050 11b20872 2019-11-08 stsp if (err) {
3051 11b20872 2019-11-08 stsp free_colors(&s->colors);
3052 11b20872 2019-11-08 stsp goto done;
3053 11b20872 2019-11-08 stsp }
3054 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3055 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3056 11b20872 2019-11-08 stsp if (err) {
3057 11b20872 2019-11-08 stsp free_colors(&s->colors);
3058 11b20872 2019-11-08 stsp goto done;
3059 11b20872 2019-11-08 stsp }
3060 11b20872 2019-11-08 stsp }
3061 11b20872 2019-11-08 stsp
3062 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3063 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3064 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3065 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3066 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3067 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3068 1a76625f 2018-10-22 stsp
3069 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3070 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3071 74467cc8 2022-06-15 stsp if (err)
3072 74467cc8 2022-06-15 stsp goto done;
3073 74467cc8 2022-06-15 stsp }
3074 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3075 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3076 0ae84acc 2022-06-15 tracey if (err)
3077 0ae84acc 2022-06-15 tracey goto done;
3078 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3079 b672a97a 2020-01-27 stsp !s->log_branches);
3080 1a76625f 2018-10-22 stsp if (err)
3081 1a76625f 2018-10-22 stsp goto done;
3082 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3083 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3084 c5b78334 2020-01-12 stsp if (err)
3085 c5b78334 2020-01-12 stsp goto done;
3086 1a76625f 2018-10-22 stsp
3087 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3088 1a76625f 2018-10-22 stsp if (errcode) {
3089 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3090 1a76625f 2018-10-22 stsp goto done;
3091 1a76625f 2018-10-22 stsp }
3092 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3093 7c1452c1 2020-03-26 stsp if (errcode) {
3094 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3095 7c1452c1 2020-03-26 stsp goto done;
3096 7c1452c1 2020-03-26 stsp }
3097 1a76625f 2018-10-22 stsp
3098 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3099 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3100 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3101 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3102 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3103 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3104 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3105 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3106 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3107 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3108 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3109 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3110 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3111 ba4f502b 2018-08-04 stsp done:
3112 1a76625f 2018-10-22 stsp if (err)
3113 1a76625f 2018-10-22 stsp close_log_view(view);
3114 ba4f502b 2018-08-04 stsp return err;
3115 ba4f502b 2018-08-04 stsp }
3116 ba4f502b 2018-08-04 stsp
3117 e5a0f69f 2018-08-18 stsp static const struct got_error *
3118 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3119 ba4f502b 2018-08-04 stsp {
3120 f2f6d207 2020-11-24 stsp const struct got_error *err;
3121 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3122 ba4f502b 2018-08-04 stsp
3123 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3124 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3125 2b380cc8 2018-10-24 stsp &s->thread_args);
3126 2b380cc8 2018-10-24 stsp if (errcode)
3127 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3128 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3129 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3130 f2f6d207 2020-11-24 stsp if (err)
3131 f2f6d207 2020-11-24 stsp return err;
3132 f2f6d207 2020-11-24 stsp }
3133 2b380cc8 2018-10-24 stsp }
3134 2b380cc8 2018-10-24 stsp
3135 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3136 b880cc75 2022-06-30 mark }
3137 b880cc75 2022-06-30 mark
3138 b880cc75 2022-06-30 mark static void
3139 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3140 b880cc75 2022-06-30 mark {
3141 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3142 b880cc75 2022-06-30 mark
3143 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3144 b880cc75 2022-06-30 mark view->count = 0;
3145 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3146 b880cc75 2022-06-30 mark return;
3147 b880cc75 2022-06-30 mark
3148 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3149 b880cc75 2022-06-30 mark || home)
3150 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3151 b880cc75 2022-06-30 mark
3152 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3153 b880cc75 2022-06-30 mark --s->selected;
3154 b880cc75 2022-06-30 mark else
3155 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3156 b880cc75 2022-06-30 mark
3157 b880cc75 2022-06-30 mark select_commit(s);
3158 b880cc75 2022-06-30 mark return;
3159 b880cc75 2022-06-30 mark }
3160 b880cc75 2022-06-30 mark
3161 b880cc75 2022-06-30 mark static const struct got_error *
3162 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3163 b880cc75 2022-06-30 mark {
3164 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3165 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3166 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3167 b880cc75 2022-06-30 mark
3168 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3169 b880cc75 2022-06-30 mark if (first == NULL) {
3170 b880cc75 2022-06-30 mark view->count = 0;
3171 b880cc75 2022-06-30 mark return NULL;
3172 b880cc75 2022-06-30 mark }
3173 b880cc75 2022-06-30 mark
3174 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3175 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3176 b880cc75 2022-06-30 mark return NULL;
3177 b880cc75 2022-06-30 mark
3178 b880cc75 2022-06-30 mark if (!page) {
3179 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3180 b880cc75 2022-06-30 mark
3181 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3182 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3183 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3184 b880cc75 2022-06-30 mark ++s->selected;
3185 b880cc75 2022-06-30 mark else
3186 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3187 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3188 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3189 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3190 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3191 b880cc75 2022-06-30 mark else
3192 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3193 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3194 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3195 b880cc75 2022-06-30 mark } else {
3196 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3197 b880cc75 2022-06-30 mark if (err)
3198 b880cc75 2022-06-30 mark return err;
3199 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3200 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3201 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3202 b880cc75 2022-06-30 mark }
3203 b880cc75 2022-06-30 mark }
3204 b880cc75 2022-06-30 mark if (err)
3205 b880cc75 2022-06-30 mark return err;
3206 b880cc75 2022-06-30 mark
3207 9b058f45 2022-06-30 mark /*
3208 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3209 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3210 9b058f45 2022-06-30 mark */
3211 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3212 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3213 9b058f45 2022-06-30 mark
3214 b880cc75 2022-06-30 mark select_commit(s);
3215 b880cc75 2022-06-30 mark
3216 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3217 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3218 b880cc75 2022-06-30 mark view->count = 0;
3219 b880cc75 2022-06-30 mark
3220 b880cc75 2022-06-30 mark return NULL;
3221 e5a0f69f 2018-08-18 stsp }
3222 04cc582a 2018-08-01 stsp
3223 9b058f45 2022-06-30 mark static void
3224 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3225 9b058f45 2022-06-30 mark {
3226 76364b2d 2022-07-02 mark *x = 0;
3227 76364b2d 2022-07-02 mark *y = 0;
3228 76364b2d 2022-07-02 mark
3229 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3230 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3231 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3232 7532ccda 2022-07-11 mark else if (view->resized_y)
3233 7532ccda 2022-07-11 mark *y = view->resized_y;
3234 3c1dfe12 2022-07-08 mark else
3235 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3236 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3237 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3238 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3239 7532ccda 2022-07-11 mark else if (view->resized_x)
3240 7532ccda 2022-07-11 mark *x = view->resized_x;
3241 3c1dfe12 2022-07-08 mark else
3242 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3243 3c1dfe12 2022-07-08 mark }
3244 9b058f45 2022-06-30 mark }
3245 9b058f45 2022-06-30 mark
3246 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3247 e5a0f69f 2018-08-18 stsp static const struct got_error *
3248 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3249 9b058f45 2022-06-30 mark {
3250 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3251 9b058f45 2022-06-30 mark
3252 9b058f45 2022-06-30 mark view->nlines = y;
3253 d2366e29 2022-07-07 mark view->ncols = COLS;
3254 9b058f45 2022-06-30 mark err = view_resize(view);
3255 9b058f45 2022-06-30 mark if (err)
3256 9b058f45 2022-06-30 mark return err;
3257 9b058f45 2022-06-30 mark
3258 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3259 9b058f45 2022-06-30 mark
3260 9b058f45 2022-06-30 mark return err;
3261 94b80cfa 2022-08-01 mark }
3262 94b80cfa 2022-08-01 mark
3263 94b80cfa 2022-08-01 mark static const struct got_error *
3264 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3265 94b80cfa 2022-08-01 mark {
3266 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3267 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3268 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3269 94b80cfa 2022-08-01 mark
3270 94b80cfa 2022-08-01 mark g = view->gline;
3271 94b80cfa 2022-08-01 mark view->gline = 0;
3272 94b80cfa 2022-08-01 mark
3273 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3274 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3275 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3276 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3277 94b80cfa 2022-08-01 mark select_commit(s);
3278 94b80cfa 2022-08-01 mark return NULL;
3279 94b80cfa 2022-08-01 mark }
3280 94b80cfa 2022-08-01 mark
3281 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3282 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3283 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3284 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3285 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3286 94b80cfa 2022-08-01 mark if (err)
3287 94b80cfa 2022-08-01 mark return err;
3288 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3289 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3290 94b80cfa 2022-08-01 mark
3291 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3292 94b80cfa 2022-08-01 mark s->selected = g - 1;
3293 94b80cfa 2022-08-01 mark
3294 94b80cfa 2022-08-01 mark select_commit(s);
3295 94b80cfa 2022-08-01 mark return NULL;
3296 94b80cfa 2022-08-01 mark
3297 9b058f45 2022-06-30 mark }
3298 9b058f45 2022-06-30 mark
3299 9b058f45 2022-06-30 mark static const struct got_error *
3300 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3301 e5a0f69f 2018-08-18 stsp {
3302 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3303 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3304 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3305 136e2bd4 2022-07-23 mark int eos, n, nscroll;
3306 80ddbec8 2018-04-29 stsp
3307 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3308 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3309 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3310 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3311 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3312 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3313 fb280deb 2021-08-30 stsp }
3314 b880cc75 2022-06-30 mark return err;
3315 528dedf3 2021-08-30 stsp }
3316 0dca135e 2022-06-30 mark
3317 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3318 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3319 0dca135e 2022-06-30 mark --eos; /* border */
3320 94b80cfa 2022-08-01 mark
3321 94b80cfa 2022-08-01 mark if (view->gline)
3322 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3323 0dca135e 2022-06-30 mark
3324 528dedf3 2021-08-30 stsp switch (ch) {
3325 1e37a5c2 2019-05-12 jcs case 'q':
3326 1e37a5c2 2019-05-12 jcs s->quit = 1;
3327 145b6838 2022-06-16 stsp break;
3328 145b6838 2022-06-16 stsp case '0':
3329 145b6838 2022-06-16 stsp view->x = 0;
3330 145b6838 2022-06-16 stsp break;
3331 145b6838 2022-06-16 stsp case '$':
3332 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3333 640cd7ff 2022-06-22 mark view->count = 0;
3334 145b6838 2022-06-16 stsp break;
3335 145b6838 2022-06-16 stsp case KEY_RIGHT:
3336 145b6838 2022-06-16 stsp case 'l':
3337 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3338 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3339 640cd7ff 2022-06-22 mark else
3340 640cd7ff 2022-06-22 mark view->count = 0;
3341 1e37a5c2 2019-05-12 jcs break;
3342 145b6838 2022-06-16 stsp case KEY_LEFT:
3343 145b6838 2022-06-16 stsp case 'h':
3344 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3345 640cd7ff 2022-06-22 mark if (view->x <= 0)
3346 640cd7ff 2022-06-22 mark view->count = 0;
3347 145b6838 2022-06-16 stsp break;
3348 1e37a5c2 2019-05-12 jcs case 'k':
3349 1e37a5c2 2019-05-12 jcs case KEY_UP:
3350 1e37a5c2 2019-05-12 jcs case '<':
3351 1e37a5c2 2019-05-12 jcs case ',':
3352 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3353 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3354 912a3f79 2021-08-30 j break;
3355 912a3f79 2021-08-30 j case 'g':
3356 912a3f79 2021-08-30 j case KEY_HOME:
3357 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3358 640cd7ff 2022-06-22 mark view->count = 0;
3359 1e37a5c2 2019-05-12 jcs break;
3360 83cc4199 2022-06-13 stsp case CTRL('u'):
3361 33c3719a 2022-06-15 stsp case 'u':
3362 83cc4199 2022-06-13 stsp nscroll /= 2;
3363 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3364 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3365 a4292ac5 2019-05-12 jcs case CTRL('b'):
3366 61417565 2022-06-20 mark case 'b':
3367 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3368 1e37a5c2 2019-05-12 jcs break;
3369 1e37a5c2 2019-05-12 jcs case 'j':
3370 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3371 1e37a5c2 2019-05-12 jcs case '>':
3372 1e37a5c2 2019-05-12 jcs case '.':
3373 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3374 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3375 912a3f79 2021-08-30 j break;
3376 10aab77f 2022-07-19 op case '@':
3377 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3378 10aab77f 2022-07-19 op break;
3379 912a3f79 2021-08-30 j case 'G':
3380 912a3f79 2021-08-30 j case KEY_END: {
3381 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3382 912a3f79 2021-08-30 j * traverse them all. */
3383 640cd7ff 2022-06-22 mark view->count = 0;
3384 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3385 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3386 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3387 80ddbec8 2018-04-29 stsp }
3388 912a3f79 2021-08-30 j
3389 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3390 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3391 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3392 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3393 f3bc9f1d 2021-09-05 naddy break;
3394 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3395 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3396 f3bc9f1d 2021-09-05 naddy }
3397 f3bc9f1d 2021-09-05 naddy if (n > 0)
3398 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3399 2b779855 2020-12-05 naddy select_commit(s);
3400 1e37a5c2 2019-05-12 jcs break;
3401 912a3f79 2021-08-30 j }
3402 80b7e8da 2022-06-11 stsp case CTRL('d'):
3403 33c3719a 2022-06-15 stsp case 'd':
3404 83cc4199 2022-06-13 stsp nscroll /= 2;
3405 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3406 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3407 61417565 2022-06-20 mark case CTRL('f'):
3408 48bb96f0 2022-06-20 naddy case 'f':
3409 b880cc75 2022-06-30 mark case ' ':
3410 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3411 1e37a5c2 2019-05-12 jcs break;
3412 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3413 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3414 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3415 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3416 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3417 2b779855 2020-12-05 naddy select_commit(s);
3418 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3419 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3420 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3421 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3422 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3423 0bf7f153 2020-12-02 naddy }
3424 1e37a5c2 2019-05-12 jcs break;
3425 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3426 49b24ee5 2022-07-03 mark case '\r':
3427 640cd7ff 2022-06-22 mark view->count = 0;
3428 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3429 e5a0f69f 2018-08-18 stsp break;
3430 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3431 1e37a5c2 2019-05-12 jcs break;
3432 5e98fb33 2022-07-22 mark case 'T':
3433 640cd7ff 2022-06-22 mark view->count = 0;
3434 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3435 5036bf37 2018-09-24 stsp break;
3436 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3437 1e37a5c2 2019-05-12 jcs break;
3438 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3439 21355643 2020-12-06 stsp case CTRL('l'):
3440 21355643 2020-12-06 stsp case 'B':
3441 640cd7ff 2022-06-22 mark view->count = 0;
3442 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3443 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3444 1e37a5c2 2019-05-12 jcs break;
3445 21355643 2020-12-06 stsp err = stop_log_thread(s);
3446 74cfe85e 2020-10-20 stsp if (err)
3447 74cfe85e 2020-10-20 stsp return err;
3448 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3449 21355643 2020-12-06 stsp char *parent_path;
3450 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3451 21355643 2020-12-06 stsp if (err)
3452 21355643 2020-12-06 stsp return err;
3453 21355643 2020-12-06 stsp free(s->in_repo_path);
3454 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3455 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3456 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3457 21355643 2020-12-06 stsp struct got_object_id *start_id;
3458 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3459 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3460 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3461 21355643 2020-12-06 stsp if (err)
3462 21355643 2020-12-06 stsp return err;
3463 21355643 2020-12-06 stsp free(s->start_id);
3464 21355643 2020-12-06 stsp s->start_id = start_id;
3465 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3466 21355643 2020-12-06 stsp } else /* 'B' */
3467 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3468 21355643 2020-12-06 stsp
3469 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3470 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3471 b0dd8d27 2022-06-16 stsp if (err)
3472 b0dd8d27 2022-06-16 stsp return err;
3473 b0dd8d27 2022-06-16 stsp }
3474 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3475 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3476 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3477 74cfe85e 2020-10-20 stsp if (err)
3478 21355643 2020-12-06 stsp return err;
3479 51a10b52 2020-12-26 stsp tog_free_refs();
3480 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3481 ca51c541 2020-12-07 stsp if (err)
3482 ca51c541 2020-12-07 stsp return err;
3483 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3484 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3485 d01904d4 2019-06-25 stsp if (err)
3486 d01904d4 2019-06-25 stsp return err;
3487 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3488 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3489 b672a97a 2020-01-27 stsp if (err)
3490 b672a97a 2020-01-27 stsp return err;
3491 21355643 2020-12-06 stsp free_commits(&s->commits);
3492 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3493 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3494 21355643 2020-12-06 stsp s->selected_entry = NULL;
3495 21355643 2020-12-06 stsp s->selected = 0;
3496 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3497 21355643 2020-12-06 stsp s->quit = 0;
3498 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3499 dfee752e 2022-06-18 op s->matched_entry = NULL;
3500 dfee752e 2022-06-18 op s->search_entry = NULL;
3501 01a7bcaf 2022-07-22 mark view->offset = 0;
3502 d01904d4 2019-06-25 stsp break;
3503 5e98fb33 2022-07-22 mark case 'R':
3504 640cd7ff 2022-06-22 mark view->count = 0;
3505 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3506 6458efa5 2020-11-24 stsp break;
3507 1e37a5c2 2019-05-12 jcs default:
3508 640cd7ff 2022-06-22 mark view->count = 0;
3509 1e37a5c2 2019-05-12 jcs break;
3510 899d86c2 2018-05-10 stsp }
3511 e5a0f69f 2018-08-18 stsp
3512 80ddbec8 2018-04-29 stsp return err;
3513 80ddbec8 2018-04-29 stsp }
3514 80ddbec8 2018-04-29 stsp
3515 4ed7e80c 2018-05-20 stsp static const struct got_error *
3516 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3517 c2db6724 2019-01-04 stsp {
3518 c2db6724 2019-01-04 stsp const struct got_error *error;
3519 c2db6724 2019-01-04 stsp
3520 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3521 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3522 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3523 37c06ea4 2019-07-15 stsp #endif
3524 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3525 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3526 c2db6724 2019-01-04 stsp
3527 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3528 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3529 c2db6724 2019-01-04 stsp
3530 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3531 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3532 c2db6724 2019-01-04 stsp
3533 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3534 c2db6724 2019-01-04 stsp if (error != NULL)
3535 c2db6724 2019-01-04 stsp return error;
3536 c2db6724 2019-01-04 stsp
3537 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3538 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3539 c2db6724 2019-01-04 stsp
3540 c2db6724 2019-01-04 stsp return NULL;
3541 c2db6724 2019-01-04 stsp }
3542 c2db6724 2019-01-04 stsp
3543 a915003a 2019-02-05 stsp static void
3544 a915003a 2019-02-05 stsp init_curses(void)
3545 a915003a 2019-02-05 stsp {
3546 2497f032 2022-05-31 stsp /*
3547 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3548 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3549 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3550 2497f032 2022-05-31 stsp */
3551 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3552 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3553 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3554 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3555 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3556 2497f032 2022-05-31 stsp
3557 a915003a 2019-02-05 stsp initscr();
3558 a915003a 2019-02-05 stsp cbreak();
3559 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3560 a915003a 2019-02-05 stsp noecho();
3561 a915003a 2019-02-05 stsp nonl();
3562 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3563 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3564 a915003a 2019-02-05 stsp curs_set(0);
3565 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3566 6d17833f 2019-11-08 stsp start_color();
3567 6d17833f 2019-11-08 stsp use_default_colors();
3568 6d17833f 2019-11-08 stsp }
3569 a915003a 2019-02-05 stsp }
3570 a915003a 2019-02-05 stsp
3571 c2db6724 2019-01-04 stsp static const struct got_error *
3572 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3573 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3574 f135c941 2020-02-20 stsp {
3575 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3576 f135c941 2020-02-20 stsp
3577 f135c941 2020-02-20 stsp if (argc == 0) {
3578 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3579 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3580 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3581 f135c941 2020-02-20 stsp return NULL;
3582 f135c941 2020-02-20 stsp }
3583 f135c941 2020-02-20 stsp
3584 f135c941 2020-02-20 stsp if (worktree) {
3585 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3586 bfd61697 2020-11-14 stsp char *p;
3587 f135c941 2020-02-20 stsp
3588 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3589 f135c941 2020-02-20 stsp if (err)
3590 f135c941 2020-02-20 stsp return err;
3591 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3592 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3593 bfd61697 2020-11-14 stsp p) == -1) {
3594 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3595 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3596 f135c941 2020-02-20 stsp }
3597 f135c941 2020-02-20 stsp free(p);
3598 f135c941 2020-02-20 stsp } else
3599 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3600 f135c941 2020-02-20 stsp
3601 f135c941 2020-02-20 stsp return err;
3602 f135c941 2020-02-20 stsp }
3603 f135c941 2020-02-20 stsp
3604 f135c941 2020-02-20 stsp static const struct got_error *
3605 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3606 9f7d7167 2018-04-29 stsp {
3607 80ddbec8 2018-04-29 stsp const struct got_error *error;
3608 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3609 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3610 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3611 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3612 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3613 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3614 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3615 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3616 04cc582a 2018-08-01 stsp struct tog_view *view;
3617 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3618 80ddbec8 2018-04-29 stsp
3619 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3620 80ddbec8 2018-04-29 stsp switch (ch) {
3621 b672a97a 2020-01-27 stsp case 'b':
3622 b672a97a 2020-01-27 stsp log_branches = 1;
3623 b672a97a 2020-01-27 stsp break;
3624 80ddbec8 2018-04-29 stsp case 'c':
3625 80ddbec8 2018-04-29 stsp start_commit = optarg;
3626 80ddbec8 2018-04-29 stsp break;
3627 ecb28ae0 2018-07-16 stsp case 'r':
3628 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3629 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3630 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3631 9ba1d308 2019-10-21 stsp optarg);
3632 ecb28ae0 2018-07-16 stsp break;
3633 80ddbec8 2018-04-29 stsp default:
3634 17020d27 2019-03-07 stsp usage_log();
3635 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3636 80ddbec8 2018-04-29 stsp }
3637 80ddbec8 2018-04-29 stsp }
3638 80ddbec8 2018-04-29 stsp
3639 80ddbec8 2018-04-29 stsp argc -= optind;
3640 80ddbec8 2018-04-29 stsp argv += optind;
3641 80ddbec8 2018-04-29 stsp
3642 f135c941 2020-02-20 stsp if (argc > 1)
3643 f135c941 2020-02-20 stsp usage_log();
3644 963f97a1 2019-03-18 stsp
3645 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3646 0ae84acc 2022-06-15 tracey if (error != NULL)
3647 0ae84acc 2022-06-15 tracey goto done;
3648 0ae84acc 2022-06-15 tracey
3649 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3650 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3651 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3652 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3653 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3654 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3655 c156c7a4 2020-12-18 stsp goto done;
3656 a1fbf39a 2019-08-11 stsp if (worktree)
3657 6962eb72 2020-02-20 stsp repo_path =
3658 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3659 a1fbf39a 2019-08-11 stsp else
3660 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3661 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3662 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3663 c156c7a4 2020-12-18 stsp goto done;
3664 c156c7a4 2020-12-18 stsp }
3665 ecb28ae0 2018-07-16 stsp }
3666 ecb28ae0 2018-07-16 stsp
3667 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3668 80ddbec8 2018-04-29 stsp if (error != NULL)
3669 ecb28ae0 2018-07-16 stsp goto done;
3670 80ddbec8 2018-04-29 stsp
3671 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3672 f135c941 2020-02-20 stsp repo, worktree);
3673 f135c941 2020-02-20 stsp if (error)
3674 f135c941 2020-02-20 stsp goto done;
3675 f135c941 2020-02-20 stsp
3676 f135c941 2020-02-20 stsp init_curses();
3677 f135c941 2020-02-20 stsp
3678 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3679 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3680 c02c541e 2019-03-29 stsp if (error)
3681 c02c541e 2019-03-29 stsp goto done;
3682 c02c541e 2019-03-29 stsp
3683 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3684 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3685 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3686 87670572 2020-12-26 naddy if (error)
3687 87670572 2020-12-26 naddy goto done;
3688 87670572 2020-12-26 naddy }
3689 51a10b52 2020-12-26 stsp
3690 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3691 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3692 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3693 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3694 d8f38dc4 2020-12-05 stsp if (error)
3695 d8f38dc4 2020-12-05 stsp goto done;
3696 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3697 d8f38dc4 2020-12-05 stsp } else {
3698 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3699 d8f38dc4 2020-12-05 stsp if (error == NULL)
3700 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3701 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3702 d8f38dc4 2020-12-05 stsp goto done;
3703 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3704 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3705 d8f38dc4 2020-12-05 stsp if (error)
3706 d8f38dc4 2020-12-05 stsp goto done;
3707 d8f38dc4 2020-12-05 stsp }
3708 8b473291 2019-02-21 stsp
3709 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3710 04cc582a 2018-08-01 stsp if (view == NULL) {
3711 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3712 04cc582a 2018-08-01 stsp goto done;
3713 04cc582a 2018-08-01 stsp }
3714 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3715 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3716 ba4f502b 2018-08-04 stsp if (error)
3717 ba4f502b 2018-08-04 stsp goto done;
3718 2fc00ff4 2019-08-31 stsp if (worktree) {
3719 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3720 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3721 2fc00ff4 2019-08-31 stsp worktree = NULL;
3722 2fc00ff4 2019-08-31 stsp }
3723 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3724 ecb28ae0 2018-07-16 stsp done:
3725 f135c941 2020-02-20 stsp free(in_repo_path);
3726 ecb28ae0 2018-07-16 stsp free(repo_path);
3727 ecb28ae0 2018-07-16 stsp free(cwd);
3728 899d86c2 2018-05-10 stsp free(start_id);
3729 d8f38dc4 2020-12-05 stsp free(label);
3730 d8f38dc4 2020-12-05 stsp if (ref)
3731 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3732 1d0f4054 2021-06-17 stsp if (repo) {
3733 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3734 1d0f4054 2021-06-17 stsp if (error == NULL)
3735 1d0f4054 2021-06-17 stsp error = close_err;
3736 1d0f4054 2021-06-17 stsp }
3737 ec142235 2019-03-07 stsp if (worktree)
3738 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3739 0ae84acc 2022-06-15 tracey if (pack_fds) {
3740 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3741 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3742 0ae84acc 2022-06-15 tracey if (error == NULL)
3743 0ae84acc 2022-06-15 tracey error = pack_err;
3744 0ae84acc 2022-06-15 tracey }
3745 51a10b52 2020-12-26 stsp tog_free_refs();
3746 80ddbec8 2018-04-29 stsp return error;
3747 9f7d7167 2018-04-29 stsp }
3748 9f7d7167 2018-04-29 stsp
3749 4ed7e80c 2018-05-20 stsp __dead static void
3750 9f7d7167 2018-04-29 stsp usage_diff(void)
3751 9f7d7167 2018-04-29 stsp {
3752 80ddbec8 2018-04-29 stsp endwin();
3753 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3754 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3755 9f7d7167 2018-04-29 stsp exit(1);
3756 b304db33 2018-05-20 stsp }
3757 b304db33 2018-05-20 stsp
3758 6d17833f 2019-11-08 stsp static int
3759 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3760 41605754 2020-11-12 stsp regmatch_t *regmatch)
3761 6d17833f 2019-11-08 stsp {
3762 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3763 6d17833f 2019-11-08 stsp }
3764 6d17833f 2019-11-08 stsp
3765 336075a4 2022-06-25 op static struct tog_color *
3766 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3767 6d17833f 2019-11-08 stsp {
3768 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3769 6d17833f 2019-11-08 stsp
3770 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3771 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3772 6d17833f 2019-11-08 stsp return tc;
3773 6d17833f 2019-11-08 stsp }
3774 6d17833f 2019-11-08 stsp
3775 6d17833f 2019-11-08 stsp return NULL;
3776 6d17833f 2019-11-08 stsp }
3777 6d17833f 2019-11-08 stsp
3778 4ed7e80c 2018-05-20 stsp static const struct got_error *
3779 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3780 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3781 41605754 2020-11-12 stsp {
3782 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3783 44a87665 2022-06-16 stsp char *exstr = NULL;
3784 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3785 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3786 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3787 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3788 41605754 2020-11-12 stsp
3789 41605754 2020-11-12 stsp *wtotal = 0;
3790 1853e0f4 2022-06-16 stsp
3791 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3792 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3793 41605754 2020-11-12 stsp
3794 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3795 44a87665 2022-06-16 stsp if (err)
3796 44a87665 2022-06-16 stsp return err;
3797 44a87665 2022-06-16 stsp
3798 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3799 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3800 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3801 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3802 44a87665 2022-06-16 stsp goto done;
3803 44a87665 2022-06-16 stsp }
3804 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3805 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3806 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3807 1853e0f4 2022-06-16 stsp goto done;
3808 1853e0f4 2022-06-16 stsp }
3809 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3810 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3811 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3812 1853e0f4 2022-06-16 stsp goto done;
3813 1853e0f4 2022-06-16 stsp }
3814 145b6838 2022-06-16 stsp
3815 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3816 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3817 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3818 1853e0f4 2022-06-16 stsp if (err)
3819 1853e0f4 2022-06-16 stsp goto done;
3820 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3821 145b6838 2022-06-16 stsp if (n) {
3822 1853e0f4 2022-06-16 stsp free(wline);
3823 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3824 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3825 1853e0f4 2022-06-16 stsp if (err)
3826 1853e0f4 2022-06-16 stsp goto done;
3827 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3828 1853e0f4 2022-06-16 stsp wlimit -= width;
3829 1853e0f4 2022-06-16 stsp *wtotal += width;
3830 41605754 2020-11-12 stsp }
3831 41605754 2020-11-12 stsp
3832 41605754 2020-11-12 stsp if (wlimit > 0) {
3833 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3834 1853e0f4 2022-06-16 stsp size_t wlen;
3835 1853e0f4 2022-06-16 stsp
3836 1853e0f4 2022-06-16 stsp free(wline);
3837 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3838 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3839 1853e0f4 2022-06-16 stsp if (err)
3840 1853e0f4 2022-06-16 stsp goto done;
3841 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3842 1853e0f4 2022-06-16 stsp while (i < wlen) {
3843 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3844 1853e0f4 2022-06-16 stsp if (width == -1) {
3845 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3846 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3847 1853e0f4 2022-06-16 stsp goto done;
3848 1853e0f4 2022-06-16 stsp }
3849 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3850 1853e0f4 2022-06-16 stsp break;
3851 61417565 2022-06-20 mark w += width;
3852 1853e0f4 2022-06-16 stsp i++;
3853 41605754 2020-11-12 stsp }
3854 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3855 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3856 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3857 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3858 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3859 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3860 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3861 41605754 2020-11-12 stsp }
3862 41605754 2020-11-12 stsp }
3863 41605754 2020-11-12 stsp
3864 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3865 1853e0f4 2022-06-16 stsp free(wline);
3866 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3867 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3868 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3869 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3870 1853e0f4 2022-06-16 stsp if (err)
3871 1853e0f4 2022-06-16 stsp goto done;
3872 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3873 1853e0f4 2022-06-16 stsp } else {
3874 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3875 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3876 1853e0f4 2022-06-16 stsp if (err)
3877 1853e0f4 2022-06-16 stsp goto done;
3878 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3879 1853e0f4 2022-06-16 stsp }
3880 1853e0f4 2022-06-16 stsp *wtotal += width2;
3881 41605754 2020-11-12 stsp }
3882 1853e0f4 2022-06-16 stsp done:
3883 145b6838 2022-06-16 stsp free(wline);
3884 44a87665 2022-06-16 stsp free(exstr);
3885 1853e0f4 2022-06-16 stsp free(seg0);
3886 1853e0f4 2022-06-16 stsp free(seg1);
3887 1853e0f4 2022-06-16 stsp free(seg2);
3888 1853e0f4 2022-06-16 stsp return err;
3889 41605754 2020-11-12 stsp }
3890 94b80cfa 2022-08-01 mark
3891 94b80cfa 2022-08-01 mark static int
3892 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
3893 94b80cfa 2022-08-01 mark {
3894 94b80cfa 2022-08-01 mark FILE *f = NULL;
3895 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
3896 94b80cfa 2022-08-01 mark
3897 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
3898 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
3899 41605754 2020-11-12 stsp
3900 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3901 94b80cfa 2022-08-01 mark selected = first;
3902 94b80cfa 2022-08-01 mark eof = &s->eof;
3903 94b80cfa 2022-08-01 mark f = s->f;
3904 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
3905 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
3906 94b80cfa 2022-08-01 mark
3907 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3908 94b80cfa 2022-08-01 mark selected = &s->selected_line;
3909 94b80cfa 2022-08-01 mark eof = &s->eof;
3910 94b80cfa 2022-08-01 mark f = s->blame.f;
3911 94b80cfa 2022-08-01 mark } else
3912 94b80cfa 2022-08-01 mark return 0;
3913 94b80cfa 2022-08-01 mark
3914 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
3915 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
3916 94b80cfa 2022-08-01 mark return 0;
3917 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3918 94b80cfa 2022-08-01 mark rewind(f);
3919 94b80cfa 2022-08-01 mark *eof = 0;
3920 94b80cfa 2022-08-01 mark *first = 1;
3921 94b80cfa 2022-08-01 mark *lineno = 0;
3922 94b80cfa 2022-08-01 mark *nprinted = 0;
3923 94b80cfa 2022-08-01 mark return 0;
3924 94b80cfa 2022-08-01 mark }
3925 94b80cfa 2022-08-01 mark
3926 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
3927 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
3928 94b80cfa 2022-08-01 mark view->gline = 0;
3929 94b80cfa 2022-08-01 mark
3930 94b80cfa 2022-08-01 mark return 1;
3931 94b80cfa 2022-08-01 mark }
3932 94b80cfa 2022-08-01 mark
3933 41605754 2020-11-12 stsp static const struct got_error *
3934 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3935 26ed57b2 2018-05-19 stsp {
3936 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3937 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3938 61e69b96 2018-05-20 stsp const struct got_error *err;
3939 94b80cfa 2022-08-01 mark int lineno, nprinted = 0;
3940 b304db33 2018-05-20 stsp char *line;
3941 826082fe 2020-12-10 stsp size_t linesize = 0;
3942 826082fe 2020-12-10 stsp ssize_t linelen;
3943 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3944 61e69b96 2018-05-20 stsp wchar_t *wline;
3945 e0b650dd 2018-05-20 stsp int width;
3946 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3947 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3948 fe621944 2020-11-10 stsp off_t line_offset;
3949 94b80cfa 2022-08-01 mark attr_t attr;
3950 26ed57b2 2018-05-19 stsp
3951 94b80cfa 2022-08-01 mark lineno = s->first_displayed_line - 1;
3952 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3953 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3954 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3955 fe621944 2020-11-10 stsp
3956 f7d12f7e 2018-08-01 stsp werase(view->window);
3957 a3404814 2018-09-02 stsp
3958 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
3959 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
3960 94b80cfa 2022-08-01 mark
3961 a3404814 2018-09-02 stsp if (header) {
3962 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3963 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
3964 94b80cfa 2022-08-01 mark lineno + s->selected_line;
3965 94b80cfa 2022-08-01 mark
3966 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3967 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3968 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3969 ccda2f4d 2022-06-16 stsp 0, 0);
3970 135a2da0 2020-11-11 stsp free(line);
3971 135a2da0 2020-11-11 stsp if (err)
3972 a3404814 2018-09-02 stsp return err;
3973 a3404814 2018-09-02 stsp
3974 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3975 a3404814 2018-09-02 stsp wstandout(view->window);
3976 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3977 e54cc94a 2020-11-11 stsp free(wline);
3978 e54cc94a 2020-11-11 stsp wline = NULL;
3979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3980 a3404814 2018-09-02 stsp wstandend(view->window);
3981 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3982 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3983 26ed57b2 2018-05-19 stsp
3984 a3404814 2018-09-02 stsp if (max_lines <= 1)
3985 a3404814 2018-09-02 stsp return NULL;
3986 a3404814 2018-09-02 stsp max_lines--;
3987 a3404814 2018-09-02 stsp }
3988 a3404814 2018-09-02 stsp
3989 89f1a395 2020-12-01 naddy s->eof = 0;
3990 145b6838 2022-06-16 stsp view->maxx = 0;
3991 826082fe 2020-12-10 stsp line = NULL;
3992 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3993 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3994 826082fe 2020-12-10 stsp if (linelen == -1) {
3995 826082fe 2020-12-10 stsp if (feof(s->f)) {
3996 826082fe 2020-12-10 stsp s->eof = 1;
3997 826082fe 2020-12-10 stsp break;
3998 826082fe 2020-12-10 stsp }
3999 826082fe 2020-12-10 stsp free(line);
4000 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4001 61e69b96 2018-05-20 stsp }
4002 145b6838 2022-06-16 stsp
4003 94b80cfa 2022-08-01 mark attr = 0;
4004 94b80cfa 2022-08-01 mark if (++lineno < s->first_displayed_line)
4005 94b80cfa 2022-08-01 mark continue;
4006 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
4007 94b80cfa 2022-08-01 mark continue;
4008 94b80cfa 2022-08-01 mark if (lineno == view->hiline)
4009 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4010 94b80cfa 2022-08-01 mark
4011 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4012 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4013 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4014 1853e0f4 2022-06-16 stsp if (err) {
4015 1853e0f4 2022-06-16 stsp free(line);
4016 1853e0f4 2022-06-16 stsp return err;
4017 1853e0f4 2022-06-16 stsp }
4018 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4019 1853e0f4 2022-06-16 stsp free(wline);
4020 1853e0f4 2022-06-16 stsp wline = NULL;
4021 1853e0f4 2022-06-16 stsp
4022 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
4023 f26dddb7 2019-11-08 stsp if (tc)
4024 94b80cfa 2022-08-01 mark attr |= COLOR_PAIR(tc->colorpair);
4025 94b80cfa 2022-08-01 mark if (attr)
4026 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4027 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4028 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4029 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4030 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4031 41605754 2020-11-12 stsp if (err) {
4032 41605754 2020-11-12 stsp free(line);
4033 41605754 2020-11-12 stsp return err;
4034 41605754 2020-11-12 stsp }
4035 41605754 2020-11-12 stsp } else {
4036 969c159c 2022-06-16 stsp int skip;
4037 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4038 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4039 969c159c 2022-06-16 stsp if (err) {
4040 969c159c 2022-06-16 stsp free(line);
4041 969c159c 2022-06-16 stsp return err;
4042 969c159c 2022-06-16 stsp }
4043 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4044 969c159c 2022-06-16 stsp free(wline);
4045 41605754 2020-11-12 stsp wline = NULL;
4046 41605754 2020-11-12 stsp }
4047 94b80cfa 2022-08-01 mark if (lineno == view->hiline) {
4048 94b80cfa 2022-08-01 mark /* highlight full gline length */
4049 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4050 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4051 94b80cfa 2022-08-01 mark } else {
4052 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4053 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4054 94b80cfa 2022-08-01 mark }
4055 94b80cfa 2022-08-01 mark if (attr)
4056 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4057 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4058 94b80cfa 2022-08-01 mark s->first_displayed_line = lineno;
4059 826082fe 2020-12-10 stsp }
4060 826082fe 2020-12-10 stsp free(line);
4061 fe621944 2020-11-10 stsp if (nprinted >= 1)
4062 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4063 89f1a395 2020-12-01 naddy (nprinted - 1);
4064 fe621944 2020-11-10 stsp else
4065 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4066 26ed57b2 2018-05-19 stsp
4067 9b058f45 2022-06-30 mark view_border(view);
4068 c3e9aa98 2019-05-13 jcs
4069 89f1a395 2020-12-01 naddy if (s->eof) {
4070 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4071 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4072 c3e9aa98 2019-05-13 jcs nprinted++;
4073 c3e9aa98 2019-05-13 jcs }
4074 c3e9aa98 2019-05-13 jcs
4075 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4076 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4077 c3e9aa98 2019-05-13 jcs if (err) {
4078 c3e9aa98 2019-05-13 jcs return err;
4079 c3e9aa98 2019-05-13 jcs }
4080 26ed57b2 2018-05-19 stsp
4081 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4082 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4083 e54cc94a 2020-11-11 stsp free(wline);
4084 e54cc94a 2020-11-11 stsp wline = NULL;
4085 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4086 c3e9aa98 2019-05-13 jcs }
4087 c3e9aa98 2019-05-13 jcs
4088 26ed57b2 2018-05-19 stsp return NULL;
4089 abd2672a 2018-12-23 stsp }
4090 abd2672a 2018-12-23 stsp
4091 abd2672a 2018-12-23 stsp static char *
4092 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4093 abd2672a 2018-12-23 stsp {
4094 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4095 09867e48 2019-08-13 stsp char *p, *s;
4096 09867e48 2019-08-13 stsp
4097 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4098 09867e48 2019-08-13 stsp if (tm == NULL)
4099 09867e48 2019-08-13 stsp return NULL;
4100 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4101 09867e48 2019-08-13 stsp if (s == NULL)
4102 09867e48 2019-08-13 stsp return NULL;
4103 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4104 abd2672a 2018-12-23 stsp if (p)
4105 abd2672a 2018-12-23 stsp *p = '\0';
4106 abd2672a 2018-12-23 stsp return s;
4107 9f7d7167 2018-04-29 stsp }
4108 9f7d7167 2018-04-29 stsp
4109 4ed7e80c 2018-05-20 stsp static const struct got_error *
4110 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4111 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4112 0208f208 2020-05-05 stsp {
4113 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4114 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4115 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4116 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4117 0208f208 2020-05-05 stsp
4118 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4119 0208f208 2020-05-05 stsp if (qid != NULL) {
4120 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4121 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4122 d7b5a0e8 2022-04-20 stsp &qid->id);
4123 0208f208 2020-05-05 stsp if (err)
4124 0208f208 2020-05-05 stsp return err;
4125 0208f208 2020-05-05 stsp
4126 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4127 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4128 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4129 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4130 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4131 aa8b5dd0 2021-08-01 stsp }
4132 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4133 0208f208 2020-05-05 stsp
4134 0208f208 2020-05-05 stsp }
4135 0208f208 2020-05-05 stsp
4136 0208f208 2020-05-05 stsp if (tree_id1) {
4137 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4138 0208f208 2020-05-05 stsp if (err)
4139 0208f208 2020-05-05 stsp goto done;
4140 0208f208 2020-05-05 stsp }
4141 0208f208 2020-05-05 stsp
4142 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4143 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4144 0208f208 2020-05-05 stsp if (err)
4145 0208f208 2020-05-05 stsp goto done;
4146 0208f208 2020-05-05 stsp
4147 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4148 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4149 0208f208 2020-05-05 stsp done:
4150 0208f208 2020-05-05 stsp if (tree1)
4151 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4152 0208f208 2020-05-05 stsp if (tree2)
4153 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4154 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4155 0208f208 2020-05-05 stsp return err;
4156 0208f208 2020-05-05 stsp }
4157 0208f208 2020-05-05 stsp
4158 0208f208 2020-05-05 stsp static const struct got_error *
4159 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4160 abd2672a 2018-12-23 stsp {
4161 fe621944 2020-11-10 stsp off_t *p;
4162 fe621944 2020-11-10 stsp
4163 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4164 fe621944 2020-11-10 stsp if (p == NULL)
4165 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4166 fe621944 2020-11-10 stsp *line_offsets = p;
4167 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4168 fe621944 2020-11-10 stsp (*nlines)++;
4169 fe621944 2020-11-10 stsp return NULL;
4170 fe621944 2020-11-10 stsp }
4171 fe621944 2020-11-10 stsp
4172 fe621944 2020-11-10 stsp static const struct got_error *
4173 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4174 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4175 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4176 fe621944 2020-11-10 stsp {
4177 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4178 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4179 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4180 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4181 45d799e2 2018-12-23 stsp time_t committer_time;
4182 45d799e2 2018-12-23 stsp const char *author, *committer;
4183 8b473291 2019-02-21 stsp char *refs_str = NULL;
4184 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4185 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4186 fe621944 2020-11-10 stsp off_t outoff = 0;
4187 fe621944 2020-11-10 stsp int n;
4188 abd2672a 2018-12-23 stsp
4189 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4190 0208f208 2020-05-05 stsp
4191 8b473291 2019-02-21 stsp if (refs) {
4192 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4193 8b473291 2019-02-21 stsp if (err)
4194 8b473291 2019-02-21 stsp return err;
4195 8b473291 2019-02-21 stsp }
4196 8b473291 2019-02-21 stsp
4197 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4198 abd2672a 2018-12-23 stsp if (err)
4199 abd2672a 2018-12-23 stsp return err;
4200 abd2672a 2018-12-23 stsp
4201 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4202 15a94983 2018-12-23 stsp if (err) {
4203 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4204 15a94983 2018-12-23 stsp goto done;
4205 15a94983 2018-12-23 stsp }
4206 abd2672a 2018-12-23 stsp
4207 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4208 fe621944 2020-11-10 stsp if (err)
4209 fe621944 2020-11-10 stsp goto done;
4210 fe621944 2020-11-10 stsp
4211 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4212 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4213 fe621944 2020-11-10 stsp if (n < 0) {
4214 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4215 abd2672a 2018-12-23 stsp goto done;
4216 abd2672a 2018-12-23 stsp }
4217 fe621944 2020-11-10 stsp outoff += n;
4218 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4219 fe621944 2020-11-10 stsp if (err)
4220 fe621944 2020-11-10 stsp goto done;
4221 fe621944 2020-11-10 stsp
4222 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4223 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4224 fe621944 2020-11-10 stsp if (n < 0) {
4225 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4226 abd2672a 2018-12-23 stsp goto done;
4227 abd2672a 2018-12-23 stsp }
4228 fe621944 2020-11-10 stsp outoff += n;
4229 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4230 fe621944 2020-11-10 stsp if (err)
4231 fe621944 2020-11-10 stsp goto done;
4232 fe621944 2020-11-10 stsp
4233 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4234 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4235 fe621944 2020-11-10 stsp if (datestr) {
4236 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4237 fe621944 2020-11-10 stsp if (n < 0) {
4238 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4239 fe621944 2020-11-10 stsp goto done;
4240 fe621944 2020-11-10 stsp }
4241 fe621944 2020-11-10 stsp outoff += n;
4242 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4243 fe621944 2020-11-10 stsp if (err)
4244 fe621944 2020-11-10 stsp goto done;
4245 abd2672a 2018-12-23 stsp }
4246 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4247 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4248 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4249 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4250 fe621944 2020-11-10 stsp if (n < 0) {
4251 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4252 fe621944 2020-11-10 stsp goto done;
4253 fe621944 2020-11-10 stsp }
4254 fe621944 2020-11-10 stsp outoff += n;
4255 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4256 fe621944 2020-11-10 stsp if (err)
4257 fe621944 2020-11-10 stsp goto done;
4258 abd2672a 2018-12-23 stsp }
4259 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4260 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4261 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4262 9f98ca05 2021-09-24 stsp int pn = 1;
4263 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4264 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4265 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4266 9f98ca05 2021-09-24 stsp if (err)
4267 9f98ca05 2021-09-24 stsp goto done;
4268 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4269 9f98ca05 2021-09-24 stsp if (n < 0) {
4270 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4271 9f98ca05 2021-09-24 stsp goto done;
4272 9f98ca05 2021-09-24 stsp }
4273 9f98ca05 2021-09-24 stsp outoff += n;
4274 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4275 9f98ca05 2021-09-24 stsp if (err)
4276 9f98ca05 2021-09-24 stsp goto done;
4277 9f98ca05 2021-09-24 stsp free(id_str);
4278 9f98ca05 2021-09-24 stsp id_str = NULL;
4279 9f98ca05 2021-09-24 stsp }
4280 9f98ca05 2021-09-24 stsp }
4281 9f98ca05 2021-09-24 stsp
4282 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4283 5943eee2 2019-08-13 stsp if (err)
4284 5943eee2 2019-08-13 stsp goto done;
4285 fe621944 2020-11-10 stsp s = logmsg;
4286 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4287 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4288 fe621944 2020-11-10 stsp if (n < 0) {
4289 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4290 fe621944 2020-11-10 stsp goto done;
4291 fe621944 2020-11-10 stsp }
4292 fe621944 2020-11-10 stsp outoff += n;
4293 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4294 fe621944 2020-11-10 stsp if (err)
4295 fe621944 2020-11-10 stsp goto done;
4296 abd2672a 2018-12-23 stsp }
4297 fe621944 2020-11-10 stsp
4298 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4299 0208f208 2020-05-05 stsp if (err)
4300 0208f208 2020-05-05 stsp goto done;
4301 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4302 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4303 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4304 fe621944 2020-11-10 stsp if (n < 0) {
4305 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4306 fe621944 2020-11-10 stsp goto done;
4307 fe621944 2020-11-10 stsp }
4308 fe621944 2020-11-10 stsp outoff += n;
4309 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4310 fe621944 2020-11-10 stsp if (err)
4311 fe621944 2020-11-10 stsp goto done;
4312 0208f208 2020-05-05 stsp free((char *)pe->path);
4313 0208f208 2020-05-05 stsp free(pe->data);
4314 0208f208 2020-05-05 stsp }
4315 fe621944 2020-11-10 stsp
4316 0208f208 2020-05-05 stsp fputc('\n', outfile);
4317 fe621944 2020-11-10 stsp outoff++;
4318 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4319 abd2672a 2018-12-23 stsp done:
4320 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4321 abd2672a 2018-12-23 stsp free(id_str);
4322 5943eee2 2019-08-13 stsp free(logmsg);
4323 8b473291 2019-02-21 stsp free(refs_str);
4324 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4325 7510f233 2020-08-09 stsp if (err) {
4326 ae6a6978 2020-08-09 stsp free(*line_offsets);
4327 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4328 ae6a6978 2020-08-09 stsp *nlines = 0;
4329 7510f233 2020-08-09 stsp }
4330 fe621944 2020-11-10 stsp return err;
4331 abd2672a 2018-12-23 stsp }
4332 abd2672a 2018-12-23 stsp
4333 abd2672a 2018-12-23 stsp static const struct got_error *
4334 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4335 26ed57b2 2018-05-19 stsp {
4336 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4337 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4338 15a94983 2018-12-23 stsp int obj_type;
4339 fe621944 2020-11-10 stsp
4340 fe621944 2020-11-10 stsp free(s->line_offsets);
4341 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4342 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4343 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4344 fe621944 2020-11-10 stsp s->nlines = 0;
4345 26ed57b2 2018-05-19 stsp
4346 511a516b 2018-05-19 stsp f = got_opentemp();
4347 48ae06ee 2018-10-18 stsp if (f == NULL) {
4348 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4349 48ae06ee 2018-10-18 stsp goto done;
4350 48ae06ee 2018-10-18 stsp }
4351 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4353 fb43ecf1 2019-02-11 stsp goto done;
4354 fb43ecf1 2019-02-11 stsp }
4355 48ae06ee 2018-10-18 stsp s->f = f;
4356 26ed57b2 2018-05-19 stsp
4357 15a94983 2018-12-23 stsp if (s->id1)
4358 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4359 15a94983 2018-12-23 stsp else
4360 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4361 15a94983 2018-12-23 stsp if (err)
4362 15a94983 2018-12-23 stsp goto done;
4363 15a94983 2018-12-23 stsp
4364 15a94983 2018-12-23 stsp switch (obj_type) {
4365 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4366 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4367 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4368 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4369 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4370 26ed57b2 2018-05-19 stsp break;
4371 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4372 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4373 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4374 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4375 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4376 26ed57b2 2018-05-19 stsp break;
4377 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4378 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4379 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4380 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4381 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4382 abd2672a 2018-12-23 stsp
4383 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4384 abd2672a 2018-12-23 stsp if (err)
4385 3ffacbe1 2020-02-02 tracey goto done;
4386 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4387 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4388 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4389 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4390 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4391 f44b1f58 2020-02-02 tracey if (err)
4392 f44b1f58 2020-02-02 tracey goto done;
4393 f44b1f58 2020-02-02 tracey } else {
4394 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4395 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4396 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4397 fe621944 2020-11-10 stsp err = write_commit_info(
4398 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4399 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4400 f44b1f58 2020-02-02 tracey if (err)
4401 f44b1f58 2020-02-02 tracey goto done;
4402 f5404e4e 2020-02-02 tracey break;
4403 15a087fe 2019-02-21 stsp }
4404 abd2672a 2018-12-23 stsp }
4405 abd2672a 2018-12-23 stsp }
4406 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4407 abd2672a 2018-12-23 stsp
4408 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4409 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4410 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4411 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4412 26ed57b2 2018-05-19 stsp break;
4413 abd2672a 2018-12-23 stsp }
4414 26ed57b2 2018-05-19 stsp default:
4415 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4416 48ae06ee 2018-10-18 stsp break;
4417 26ed57b2 2018-05-19 stsp }
4418 f44b1f58 2020-02-02 tracey if (err)
4419 f44b1f58 2020-02-02 tracey goto done;
4420 48ae06ee 2018-10-18 stsp done:
4421 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4422 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4423 48ae06ee 2018-10-18 stsp return err;
4424 48ae06ee 2018-10-18 stsp }
4425 26ed57b2 2018-05-19 stsp
4426 f5215bb9 2019-02-22 stsp static void
4427 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4428 f5215bb9 2019-02-22 stsp {
4429 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4430 f5215bb9 2019-02-22 stsp update_panels();
4431 f5215bb9 2019-02-22 stsp doupdate();
4432 f44b1f58 2020-02-02 tracey }
4433 f44b1f58 2020-02-02 tracey
4434 f44b1f58 2020-02-02 tracey static const struct got_error *
4435 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4436 f44b1f58 2020-02-02 tracey {
4437 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4438 f44b1f58 2020-02-02 tracey
4439 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4440 f44b1f58 2020-02-02 tracey return NULL;
4441 f44b1f58 2020-02-02 tracey }
4442 f44b1f58 2020-02-02 tracey
4443 f44b1f58 2020-02-02 tracey static const struct got_error *
4444 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4445 f44b1f58 2020-02-02 tracey {
4446 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4447 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4448 f44b1f58 2020-02-02 tracey int lineno;
4449 cb713507 2022-06-16 stsp char *line = NULL;
4450 826082fe 2020-12-10 stsp size_t linesize = 0;
4451 826082fe 2020-12-10 stsp ssize_t linelen;
4452 f44b1f58 2020-02-02 tracey
4453 f44b1f58 2020-02-02 tracey if (!view->searching) {
4454 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4455 f44b1f58 2020-02-02 tracey return NULL;
4456 f44b1f58 2020-02-02 tracey }
4457 f44b1f58 2020-02-02 tracey
4458 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4459 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4460 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4461 f44b1f58 2020-02-02 tracey else
4462 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4463 487cd7d2 2021-12-17 stsp } else
4464 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4465 f44b1f58 2020-02-02 tracey
4466 f44b1f58 2020-02-02 tracey while (1) {
4467 f44b1f58 2020-02-02 tracey off_t offset;
4468 f44b1f58 2020-02-02 tracey
4469 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4470 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4471 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4472 f44b1f58 2020-02-02 tracey break;
4473 f44b1f58 2020-02-02 tracey }
4474 f44b1f58 2020-02-02 tracey
4475 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4476 f44b1f58 2020-02-02 tracey lineno = 1;
4477 f44b1f58 2020-02-02 tracey else
4478 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4479 f44b1f58 2020-02-02 tracey }
4480 f44b1f58 2020-02-02 tracey
4481 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4482 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4483 f44b1f58 2020-02-02 tracey free(line);
4484 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4485 f44b1f58 2020-02-02 tracey }
4486 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4487 cb713507 2022-06-16 stsp if (linelen != -1) {
4488 cb713507 2022-06-16 stsp char *exstr;
4489 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4490 cb713507 2022-06-16 stsp if (err)
4491 cb713507 2022-06-16 stsp break;
4492 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4493 cb713507 2022-06-16 stsp &view->regmatch)) {
4494 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4495 cb713507 2022-06-16 stsp s->matched_line = lineno;
4496 cb713507 2022-06-16 stsp free(exstr);
4497 cb713507 2022-06-16 stsp break;
4498 cb713507 2022-06-16 stsp }
4499 cb713507 2022-06-16 stsp free(exstr);
4500 f44b1f58 2020-02-02 tracey }
4501 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4502 f44b1f58 2020-02-02 tracey lineno++;
4503 f44b1f58 2020-02-02 tracey else
4504 f44b1f58 2020-02-02 tracey lineno--;
4505 f44b1f58 2020-02-02 tracey }
4506 826082fe 2020-12-10 stsp free(line);
4507 f44b1f58 2020-02-02 tracey
4508 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4509 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4510 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4511 f44b1f58 2020-02-02 tracey }
4512 f44b1f58 2020-02-02 tracey
4513 145b6838 2022-06-16 stsp return err;
4514 f5215bb9 2019-02-22 stsp }
4515 f5215bb9 2019-02-22 stsp
4516 48ae06ee 2018-10-18 stsp static const struct got_error *
4517 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4518 b72706c3 2022-06-01 stsp {
4519 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4520 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4521 b72706c3 2022-06-01 stsp
4522 b72706c3 2022-06-01 stsp free(s->id1);
4523 b72706c3 2022-06-01 stsp s->id1 = NULL;
4524 b72706c3 2022-06-01 stsp free(s->id2);
4525 b72706c3 2022-06-01 stsp s->id2 = NULL;
4526 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4527 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4528 b72706c3 2022-06-01 stsp s->f = NULL;
4529 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4530 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4531 b72706c3 2022-06-01 stsp s->f1 = NULL;
4532 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4533 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4534 b72706c3 2022-06-01 stsp s->f2 = NULL;
4535 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4536 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4537 f9d37699 2022-06-28 stsp s->fd1 = -1;
4538 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4539 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4540 f9d37699 2022-06-28 stsp s->fd2 = -1;
4541 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4542 b72706c3 2022-06-01 stsp free(s->line_offsets);
4543 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4544 b72706c3 2022-06-01 stsp s->nlines = 0;
4545 b72706c3 2022-06-01 stsp return err;
4546 b72706c3 2022-06-01 stsp }
4547 b72706c3 2022-06-01 stsp
4548 b72706c3 2022-06-01 stsp static const struct got_error *
4549 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4550 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4551 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4552 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4553 48ae06ee 2018-10-18 stsp {
4554 48ae06ee 2018-10-18 stsp const struct got_error *err;
4555 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4556 5dc9f4bc 2018-08-04 stsp
4557 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4558 f9d37699 2022-06-28 stsp s->fd1 = -1;
4559 f9d37699 2022-06-28 stsp s->fd2 = -1;
4560 b72706c3 2022-06-01 stsp
4561 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4562 15a94983 2018-12-23 stsp int type1, type2;
4563 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4564 15a94983 2018-12-23 stsp if (err)
4565 15a94983 2018-12-23 stsp return err;
4566 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4567 15a94983 2018-12-23 stsp if (err)
4568 15a94983 2018-12-23 stsp return err;
4569 15a94983 2018-12-23 stsp
4570 15a94983 2018-12-23 stsp if (type1 != type2)
4571 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4572 15a94983 2018-12-23 stsp }
4573 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4574 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4575 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4576 f44b1f58 2020-02-02 tracey s->repo = repo;
4577 f44b1f58 2020-02-02 tracey s->id1 = id1;
4578 f44b1f58 2020-02-02 tracey s->id2 = id2;
4579 3dbaef42 2020-11-24 stsp s->label1 = label1;
4580 3dbaef42 2020-11-24 stsp s->label2 = label2;
4581 48ae06ee 2018-10-18 stsp
4582 15a94983 2018-12-23 stsp if (id1) {
4583 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4584 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4585 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4586 48ae06ee 2018-10-18 stsp } else
4587 5465d566 2020-02-01 tracey s->id1 = NULL;
4588 48ae06ee 2018-10-18 stsp
4589 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4590 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4591 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4592 b72706c3 2022-06-01 stsp goto done;
4593 48ae06ee 2018-10-18 stsp }
4594 b72706c3 2022-06-01 stsp
4595 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4596 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4597 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4598 a00719e9 2022-06-17 stsp goto done;
4599 a00719e9 2022-06-17 stsp }
4600 a00719e9 2022-06-17 stsp
4601 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4602 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4603 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4604 b72706c3 2022-06-01 stsp goto done;
4605 b72706c3 2022-06-01 stsp }
4606 b72706c3 2022-06-01 stsp
4607 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4608 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4609 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4610 f9d37699 2022-06-28 stsp goto done;
4611 f9d37699 2022-06-28 stsp }
4612 f9d37699 2022-06-28 stsp
4613 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4614 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4615 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4616 f9d37699 2022-06-28 stsp goto done;
4617 f9d37699 2022-06-28 stsp }
4618 f9d37699 2022-06-28 stsp
4619 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4620 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4621 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4622 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4623 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4624 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4625 5465d566 2020-02-01 tracey s->repo = repo;
4626 6d17833f 2019-11-08 stsp
4627 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4628 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4629 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4630 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4631 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4632 6d17833f 2019-11-08 stsp if (err)
4633 b72706c3 2022-06-01 stsp goto done;
4634 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4635 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4637 b72706c3 2022-06-01 stsp if (err)
4638 b72706c3 2022-06-01 stsp goto done;
4639 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4640 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4641 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4642 b72706c3 2022-06-01 stsp if (err)
4643 b72706c3 2022-06-01 stsp goto done;
4644 6d17833f 2019-11-08 stsp
4645 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4646 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4647 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4648 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4649 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4650 b72706c3 2022-06-01 stsp if (err)
4651 b72706c3 2022-06-01 stsp goto done;
4652 11b20872 2019-11-08 stsp
4653 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4654 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4655 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4656 b72706c3 2022-06-01 stsp if (err)
4657 b72706c3 2022-06-01 stsp goto done;
4658 11b20872 2019-11-08 stsp
4659 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4660 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4661 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4662 b72706c3 2022-06-01 stsp if (err)
4663 b72706c3 2022-06-01 stsp goto done;
4664 6d17833f 2019-11-08 stsp }
4665 5dc9f4bc 2018-08-04 stsp
4666 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4667 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4668 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4669 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4670 f5215bb9 2019-02-22 stsp
4671 5465d566 2020-02-01 tracey err = create_diff(s);
4672 48ae06ee 2018-10-18 stsp
4673 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4674 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4675 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4676 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4677 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4678 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4679 b72706c3 2022-06-01 stsp done:
4680 b72706c3 2022-06-01 stsp if (err)
4681 b72706c3 2022-06-01 stsp close_diff_view(view);
4682 e5a0f69f 2018-08-18 stsp return err;
4683 5dc9f4bc 2018-08-04 stsp }
4684 5dc9f4bc 2018-08-04 stsp
4685 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4686 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4687 5dc9f4bc 2018-08-04 stsp {
4688 a3404814 2018-09-02 stsp const struct got_error *err;
4689 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4690 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4691 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4692 a3404814 2018-09-02 stsp
4693 a3404814 2018-09-02 stsp if (s->id1) {
4694 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4695 a3404814 2018-09-02 stsp if (err)
4696 a3404814 2018-09-02 stsp return err;
4697 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4698 3dbaef42 2020-11-24 stsp } else
4699 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4700 3dbaef42 2020-11-24 stsp
4701 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4702 a3404814 2018-09-02 stsp if (err)
4703 a3404814 2018-09-02 stsp return err;
4704 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4705 26ed57b2 2018-05-19 stsp
4706 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4707 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4708 a3404814 2018-09-02 stsp free(id_str1);
4709 a3404814 2018-09-02 stsp free(id_str2);
4710 a3404814 2018-09-02 stsp return err;
4711 a3404814 2018-09-02 stsp }
4712 a3404814 2018-09-02 stsp free(id_str1);
4713 a3404814 2018-09-02 stsp free(id_str2);
4714 a3404814 2018-09-02 stsp
4715 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4716 267bb3b8 2021-08-01 stsp free(header);
4717 267bb3b8 2021-08-01 stsp return err;
4718 15a087fe 2019-02-21 stsp }
4719 15a087fe 2019-02-21 stsp
4720 15a087fe 2019-02-21 stsp static const struct got_error *
4721 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4722 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4723 15a087fe 2019-02-21 stsp {
4724 d7a04538 2019-02-21 stsp const struct got_error *err;
4725 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4726 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4727 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4728 15a087fe 2019-02-21 stsp
4729 15a087fe 2019-02-21 stsp free(s->id2);
4730 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4731 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4732 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4733 15a087fe 2019-02-21 stsp
4734 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4735 d7a04538 2019-02-21 stsp if (err)
4736 d7a04538 2019-02-21 stsp return err;
4737 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4738 15a087fe 2019-02-21 stsp free(s->id1);
4739 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4740 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4741 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4742 15a087fe 2019-02-21 stsp return NULL;
4743 0cf4efb1 2018-09-29 stsp }
4744 0cf4efb1 2018-09-29 stsp
4745 0cf4efb1 2018-09-29 stsp static const struct got_error *
4746 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4747 917d79a7 2022-07-01 stsp {
4748 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4749 917d79a7 2022-07-01 stsp
4750 917d79a7 2022-07-01 stsp view->count = 0;
4751 917d79a7 2022-07-01 stsp wclear(view->window);
4752 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4753 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4754 917d79a7 2022-07-01 stsp s->matched_line = 0;
4755 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4756 917d79a7 2022-07-01 stsp return create_diff(s);
4757 917d79a7 2022-07-01 stsp }
4758 917d79a7 2022-07-01 stsp
4759 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4760 c0f61fa4 2022-07-11 mark int, int, int);
4761 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4762 c0f61fa4 2022-07-11 mark int, int);
4763 c0f61fa4 2022-07-11 mark
4764 917d79a7 2022-07-01 stsp static const struct got_error *
4765 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4766 e5a0f69f 2018-08-18 stsp {
4767 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4768 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4769 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4770 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4771 826082fe 2020-12-10 stsp char *line = NULL;
4772 826082fe 2020-12-10 stsp size_t linesize = 0;
4773 826082fe 2020-12-10 stsp ssize_t linelen;
4774 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4775 e5a0f69f 2018-08-18 stsp
4776 e5a0f69f 2018-08-18 stsp switch (ch) {
4777 145b6838 2022-06-16 stsp case '0':
4778 145b6838 2022-06-16 stsp view->x = 0;
4779 145b6838 2022-06-16 stsp break;
4780 145b6838 2022-06-16 stsp case '$':
4781 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4782 640cd7ff 2022-06-22 mark view->count = 0;
4783 145b6838 2022-06-16 stsp break;
4784 145b6838 2022-06-16 stsp case KEY_RIGHT:
4785 145b6838 2022-06-16 stsp case 'l':
4786 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4787 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4788 640cd7ff 2022-06-22 mark else
4789 640cd7ff 2022-06-22 mark view->count = 0;
4790 145b6838 2022-06-16 stsp break;
4791 145b6838 2022-06-16 stsp case KEY_LEFT:
4792 145b6838 2022-06-16 stsp case 'h':
4793 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4794 640cd7ff 2022-06-22 mark if (view->x <= 0)
4795 640cd7ff 2022-06-22 mark view->count = 0;
4796 145b6838 2022-06-16 stsp break;
4797 64453f7e 2020-11-21 stsp case 'a':
4798 3dbaef42 2020-11-24 stsp case 'w':
4799 3dbaef42 2020-11-24 stsp if (ch == 'a')
4800 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4801 3dbaef42 2020-11-24 stsp if (ch == 'w')
4802 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4803 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4804 912a3f79 2021-08-30 j break;
4805 912a3f79 2021-08-30 j case 'g':
4806 912a3f79 2021-08-30 j case KEY_HOME:
4807 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4808 640cd7ff 2022-06-22 mark view->count = 0;
4809 64453f7e 2020-11-21 stsp break;
4810 912a3f79 2021-08-30 j case 'G':
4811 912a3f79 2021-08-30 j case KEY_END:
4812 640cd7ff 2022-06-22 mark view->count = 0;
4813 912a3f79 2021-08-30 j if (s->eof)
4814 912a3f79 2021-08-30 j break;
4815 912a3f79 2021-08-30 j
4816 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4817 912a3f79 2021-08-30 j s->eof = 1;
4818 912a3f79 2021-08-30 j break;
4819 1e37a5c2 2019-05-12 jcs case 'k':
4820 1e37a5c2 2019-05-12 jcs case KEY_UP:
4821 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4822 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4823 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4824 640cd7ff 2022-06-22 mark else
4825 640cd7ff 2022-06-22 mark view->count = 0;
4826 1e37a5c2 2019-05-12 jcs break;
4827 83cc4199 2022-06-13 stsp case CTRL('u'):
4828 33c3719a 2022-06-15 stsp case 'u':
4829 83cc4199 2022-06-13 stsp nscroll /= 2;
4830 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4831 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4832 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4833 61417565 2022-06-20 mark case 'b':
4834 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4835 640cd7ff 2022-06-22 mark view->count = 0;
4836 26ed57b2 2018-05-19 stsp break;
4837 640cd7ff 2022-06-22 mark }
4838 1e37a5c2 2019-05-12 jcs i = 0;
4839 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4840 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4841 1e37a5c2 2019-05-12 jcs break;
4842 1e37a5c2 2019-05-12 jcs case 'j':
4843 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4844 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4845 1e37a5c2 2019-05-12 jcs if (!s->eof)
4846 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4847 640cd7ff 2022-06-22 mark else
4848 640cd7ff 2022-06-22 mark view->count = 0;
4849 1e37a5c2 2019-05-12 jcs break;
4850 83cc4199 2022-06-13 stsp case CTRL('d'):
4851 33c3719a 2022-06-15 stsp case 'd':
4852 83cc4199 2022-06-13 stsp nscroll /= 2;
4853 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4854 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4855 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4856 61417565 2022-06-20 mark case 'f':
4857 1e37a5c2 2019-05-12 jcs case ' ':
4858 640cd7ff 2022-06-22 mark if (s->eof) {
4859 640cd7ff 2022-06-22 mark view->count = 0;
4860 1e37a5c2 2019-05-12 jcs break;
4861 640cd7ff 2022-06-22 mark }
4862 1e37a5c2 2019-05-12 jcs i = 0;
4863 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4864 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4865 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4866 826082fe 2020-12-10 stsp if (linelen == -1) {
4867 826082fe 2020-12-10 stsp if (feof(s->f)) {
4868 826082fe 2020-12-10 stsp s->eof = 1;
4869 826082fe 2020-12-10 stsp } else
4870 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4871 34bc9ec9 2019-02-22 stsp break;
4872 826082fe 2020-12-10 stsp }
4873 1e37a5c2 2019-05-12 jcs }
4874 826082fe 2020-12-10 stsp free(line);
4875 1e37a5c2 2019-05-12 jcs break;
4876 1e37a5c2 2019-05-12 jcs case '[':
4877 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4878 1e37a5c2 2019-05-12 jcs s->diff_context--;
4879 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4880 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4881 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4882 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4883 27829c9e 2020-11-21 stsp s->nlines) {
4884 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4885 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4886 27829c9e 2020-11-21 stsp }
4887 640cd7ff 2022-06-22 mark } else
4888 640cd7ff 2022-06-22 mark view->count = 0;
4889 1e37a5c2 2019-05-12 jcs break;
4890 1e37a5c2 2019-05-12 jcs case ']':
4891 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4892 1e37a5c2 2019-05-12 jcs s->diff_context++;
4893 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4894 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4895 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4896 640cd7ff 2022-06-22 mark } else
4897 640cd7ff 2022-06-22 mark view->count = 0;
4898 1e37a5c2 2019-05-12 jcs break;
4899 1e37a5c2 2019-05-12 jcs case '<':
4900 1e37a5c2 2019-05-12 jcs case ',':
4901 2b3e6702 2022-07-20 mark case 'K':
4902 c0f61fa4 2022-07-11 mark up = 1;
4903 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4904 c0f61fa4 2022-07-11 mark case '>':
4905 c0f61fa4 2022-07-11 mark case '.':
4906 2b3e6702 2022-07-20 mark case 'J':
4907 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4908 640cd7ff 2022-06-22 mark view->count = 0;
4909 48ae06ee 2018-10-18 stsp break;
4910 640cd7ff 2022-06-22 mark }
4911 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4912 6524637e 2019-02-21 stsp
4913 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4914 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4915 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4916 15a087fe 2019-02-21 stsp
4917 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4918 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4919 c0f61fa4 2022-07-11 mark if (err)
4920 c0f61fa4 2022-07-11 mark break;
4921 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4922 15a087fe 2019-02-21 stsp
4923 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4924 c0f61fa4 2022-07-11 mark break;
4925 15a087fe 2019-02-21 stsp
4926 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4927 c0f61fa4 2022-07-11 mark if (err)
4928 c0f61fa4 2022-07-11 mark break;
4929 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4930 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4931 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4932 5e224a3e 2019-02-22 stsp
4933 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4934 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4935 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4936 c0f61fa4 2022-07-11 mark
4937 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4938 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4939 c0f61fa4 2022-07-11 mark if (err)
4940 c0f61fa4 2022-07-11 mark break;
4941 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4942 5a8b5076 2020-12-05 stsp
4943 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4944 c0f61fa4 2022-07-11 mark break;
4945 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4946 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4947 c0f61fa4 2022-07-11 mark bs->selected_line);
4948 c0f61fa4 2022-07-11 mark if (id == NULL)
4949 c0f61fa4 2022-07-11 mark break;
4950 15a087fe 2019-02-21 stsp
4951 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4952 c0f61fa4 2022-07-11 mark break;
4953 15a087fe 2019-02-21 stsp
4954 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4955 c0f61fa4 2022-07-11 mark if (err)
4956 c0f61fa4 2022-07-11 mark break;
4957 c0f61fa4 2022-07-11 mark }
4958 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4959 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4960 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4961 145b6838 2022-06-16 stsp view->x = 0;
4962 1e37a5c2 2019-05-12 jcs
4963 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4964 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4965 1e37a5c2 2019-05-12 jcs break;
4966 1e37a5c2 2019-05-12 jcs default:
4967 640cd7ff 2022-06-22 mark view->count = 0;
4968 1e37a5c2 2019-05-12 jcs break;
4969 26ed57b2 2018-05-19 stsp }
4970 e5a0f69f 2018-08-18 stsp
4971 bcbd79e2 2018-08-19 stsp return err;
4972 26ed57b2 2018-05-19 stsp }
4973 26ed57b2 2018-05-19 stsp
4974 4ed7e80c 2018-05-20 stsp static const struct got_error *
4975 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4976 9f7d7167 2018-04-29 stsp {
4977 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4978 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4979 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4980 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4981 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4982 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4983 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4984 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4985 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4986 3dbaef42 2020-11-24 stsp const char *errstr;
4987 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4988 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4989 70ac5f84 2019-03-28 stsp
4990 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4991 26ed57b2 2018-05-19 stsp switch (ch) {
4992 64453f7e 2020-11-21 stsp case 'a':
4993 64453f7e 2020-11-21 stsp force_text_diff = 1;
4994 3dbaef42 2020-11-24 stsp break;
4995 3dbaef42 2020-11-24 stsp case 'C':
4996 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4997 3dbaef42 2020-11-24 stsp &errstr);
4998 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4999 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5000 5a20d08d 2022-02-09 op errstr, errstr);
5001 64453f7e 2020-11-21 stsp break;
5002 09b5bff8 2020-02-23 naddy case 'r':
5003 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5004 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5005 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5006 09b5bff8 2020-02-23 naddy optarg);
5007 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5008 09b5bff8 2020-02-23 naddy break;
5009 3dbaef42 2020-11-24 stsp case 'w':
5010 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5011 3dbaef42 2020-11-24 stsp break;
5012 26ed57b2 2018-05-19 stsp default:
5013 17020d27 2019-03-07 stsp usage_diff();
5014 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5015 26ed57b2 2018-05-19 stsp }
5016 26ed57b2 2018-05-19 stsp }
5017 26ed57b2 2018-05-19 stsp
5018 26ed57b2 2018-05-19 stsp argc -= optind;
5019 26ed57b2 2018-05-19 stsp argv += optind;
5020 26ed57b2 2018-05-19 stsp
5021 26ed57b2 2018-05-19 stsp if (argc == 0) {
5022 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5023 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5024 15a94983 2018-12-23 stsp id_str1 = argv[0];
5025 15a94983 2018-12-23 stsp id_str2 = argv[1];
5026 26ed57b2 2018-05-19 stsp } else
5027 26ed57b2 2018-05-19 stsp usage_diff();
5028 eb6600df 2019-01-04 stsp
5029 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5030 0ae84acc 2022-06-15 tracey if (error)
5031 0ae84acc 2022-06-15 tracey goto done;
5032 0ae84acc 2022-06-15 tracey
5033 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5034 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5035 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5036 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5037 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5038 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5039 c156c7a4 2020-12-18 stsp goto done;
5040 a273ac94 2020-02-23 naddy if (worktree)
5041 a273ac94 2020-02-23 naddy repo_path =
5042 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5043 a273ac94 2020-02-23 naddy else
5044 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5045 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5046 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5047 c156c7a4 2020-12-18 stsp goto done;
5048 c156c7a4 2020-12-18 stsp }
5049 a273ac94 2020-02-23 naddy }
5050 a273ac94 2020-02-23 naddy
5051 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5052 eb6600df 2019-01-04 stsp if (error)
5053 eb6600df 2019-01-04 stsp goto done;
5054 26ed57b2 2018-05-19 stsp
5055 a273ac94 2020-02-23 naddy init_curses();
5056 a273ac94 2020-02-23 naddy
5057 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5058 51a10b52 2020-12-26 stsp if (error)
5059 51a10b52 2020-12-26 stsp goto done;
5060 51a10b52 2020-12-26 stsp
5061 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5062 26ed57b2 2018-05-19 stsp if (error)
5063 26ed57b2 2018-05-19 stsp goto done;
5064 26ed57b2 2018-05-19 stsp
5065 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5066 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5067 26ed57b2 2018-05-19 stsp if (error)
5068 26ed57b2 2018-05-19 stsp goto done;
5069 26ed57b2 2018-05-19 stsp
5070 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5071 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5072 26ed57b2 2018-05-19 stsp if (error)
5073 26ed57b2 2018-05-19 stsp goto done;
5074 26ed57b2 2018-05-19 stsp
5075 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5076 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5077 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5078 ea5e7bb5 2018-08-01 stsp goto done;
5079 ea5e7bb5 2018-08-01 stsp }
5080 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5081 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5082 5dc9f4bc 2018-08-04 stsp if (error)
5083 5dc9f4bc 2018-08-04 stsp goto done;
5084 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5085 26ed57b2 2018-05-19 stsp done:
5086 3dbaef42 2020-11-24 stsp free(label1);
5087 3dbaef42 2020-11-24 stsp free(label2);
5088 c02c541e 2019-03-29 stsp free(repo_path);
5089 a273ac94 2020-02-23 naddy free(cwd);
5090 1d0f4054 2021-06-17 stsp if (repo) {
5091 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5092 1d0f4054 2021-06-17 stsp if (error == NULL)
5093 1d0f4054 2021-06-17 stsp error = close_err;
5094 1d0f4054 2021-06-17 stsp }
5095 a273ac94 2020-02-23 naddy if (worktree)
5096 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5097 0ae84acc 2022-06-15 tracey if (pack_fds) {
5098 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5099 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5100 0ae84acc 2022-06-15 tracey if (error == NULL)
5101 0ae84acc 2022-06-15 tracey error = pack_err;
5102 0ae84acc 2022-06-15 tracey }
5103 51a10b52 2020-12-26 stsp tog_free_refs();
5104 26ed57b2 2018-05-19 stsp return error;
5105 9f7d7167 2018-04-29 stsp }
5106 9f7d7167 2018-04-29 stsp
5107 4ed7e80c 2018-05-20 stsp __dead static void
5108 9f7d7167 2018-04-29 stsp usage_blame(void)
5109 9f7d7167 2018-04-29 stsp {
5110 80ddbec8 2018-04-29 stsp endwin();
5111 87411fa9 2022-06-16 stsp fprintf(stderr,
5112 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5113 9f7d7167 2018-04-29 stsp getprogname());
5114 9f7d7167 2018-04-29 stsp exit(1);
5115 9f7d7167 2018-04-29 stsp }
5116 84451b3e 2018-07-10 stsp
5117 84451b3e 2018-07-10 stsp struct tog_blame_line {
5118 84451b3e 2018-07-10 stsp int annotated;
5119 84451b3e 2018-07-10 stsp struct got_object_id *id;
5120 84451b3e 2018-07-10 stsp };
5121 9f7d7167 2018-04-29 stsp
5122 4ed7e80c 2018-05-20 stsp static const struct got_error *
5123 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5124 84451b3e 2018-07-10 stsp {
5125 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5126 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5127 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5128 84451b3e 2018-07-10 stsp const struct got_error *err;
5129 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5130 826082fe 2020-12-10 stsp char *line = NULL;
5131 826082fe 2020-12-10 stsp size_t linesize = 0;
5132 826082fe 2020-12-10 stsp ssize_t linelen;
5133 84451b3e 2018-07-10 stsp wchar_t *wline;
5134 27a741e5 2019-09-11 stsp int width;
5135 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5136 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5137 ab089a2a 2018-07-12 stsp char *id_str;
5138 11b20872 2019-11-08 stsp struct tog_color *tc;
5139 ab089a2a 2018-07-12 stsp
5140 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5141 ab089a2a 2018-07-12 stsp if (err)
5142 ab089a2a 2018-07-12 stsp return err;
5143 84451b3e 2018-07-10 stsp
5144 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5145 f7d12f7e 2018-08-01 stsp werase(view->window);
5146 84451b3e 2018-07-10 stsp
5147 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5148 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5149 ab089a2a 2018-07-12 stsp free(id_str);
5150 ab089a2a 2018-07-12 stsp return err;
5151 ab089a2a 2018-07-12 stsp }
5152 ab089a2a 2018-07-12 stsp
5153 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5154 ab089a2a 2018-07-12 stsp free(line);
5155 2550e4c3 2018-07-13 stsp line = NULL;
5156 1cae65b4 2019-09-22 stsp if (err)
5157 1cae65b4 2019-09-22 stsp return err;
5158 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5159 a3404814 2018-09-02 stsp wstandout(view->window);
5160 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5161 11b20872 2019-11-08 stsp if (tc)
5162 11b20872 2019-11-08 stsp wattr_on(view->window,
5163 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5164 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5165 11b20872 2019-11-08 stsp if (tc)
5166 11b20872 2019-11-08 stsp wattr_off(view->window,
5167 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5168 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5169 a3404814 2018-09-02 stsp wstandend(view->window);
5170 2550e4c3 2018-07-13 stsp free(wline);
5171 2550e4c3 2018-07-13 stsp wline = NULL;
5172 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5173 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5174 ab089a2a 2018-07-12 stsp
5175 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5176 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5177 94b80cfa 2022-08-01 mark
5178 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5179 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5180 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5181 ab089a2a 2018-07-12 stsp free(id_str);
5182 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5183 ab089a2a 2018-07-12 stsp }
5184 ab089a2a 2018-07-12 stsp free(id_str);
5185 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5186 3f60a8ef 2018-07-10 stsp free(line);
5187 2550e4c3 2018-07-13 stsp line = NULL;
5188 3f60a8ef 2018-07-10 stsp if (err)
5189 3f60a8ef 2018-07-10 stsp return err;
5190 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5191 2550e4c3 2018-07-13 stsp free(wline);
5192 2550e4c3 2018-07-13 stsp wline = NULL;
5193 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5194 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5195 3f60a8ef 2018-07-10 stsp
5196 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5197 145b6838 2022-06-16 stsp view->maxx = 0;
5198 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5199 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5200 826082fe 2020-12-10 stsp if (linelen == -1) {
5201 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5202 826082fe 2020-12-10 stsp s->eof = 1;
5203 826082fe 2020-12-10 stsp break;
5204 826082fe 2020-12-10 stsp }
5205 84451b3e 2018-07-10 stsp free(line);
5206 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5207 84451b3e 2018-07-10 stsp }
5208 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5209 94b80cfa 2022-08-01 mark continue;
5210 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5211 826082fe 2020-12-10 stsp continue;
5212 1853e0f4 2022-06-16 stsp
5213 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5214 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5215 1853e0f4 2022-06-16 stsp if (err) {
5216 1853e0f4 2022-06-16 stsp free(line);
5217 1853e0f4 2022-06-16 stsp return err;
5218 1853e0f4 2022-06-16 stsp }
5219 1853e0f4 2022-06-16 stsp free(wline);
5220 1853e0f4 2022-06-16 stsp wline = NULL;
5221 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5222 84451b3e 2018-07-10 stsp
5223 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5224 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5225 b700b5d6 2018-07-10 stsp
5226 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5227 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5228 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5229 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5230 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5231 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5232 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5233 8d0fe45a 2019-08-12 stsp char *id_str;
5234 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5235 87411fa9 2022-06-16 stsp blame_line->id);
5236 8d0fe45a 2019-08-12 stsp if (err) {
5237 8d0fe45a 2019-08-12 stsp free(line);
5238 8d0fe45a 2019-08-12 stsp return err;
5239 8d0fe45a 2019-08-12 stsp }
5240 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5241 11b20872 2019-11-08 stsp if (tc)
5242 11b20872 2019-11-08 stsp wattr_on(view->window,
5243 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5244 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5245 11b20872 2019-11-08 stsp if (tc)
5246 11b20872 2019-11-08 stsp wattr_off(view->window,
5247 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5248 8d0fe45a 2019-08-12 stsp free(id_str);
5249 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5250 8d0fe45a 2019-08-12 stsp } else {
5251 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5252 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5253 84451b3e 2018-07-10 stsp }
5254 ee41ec32 2018-07-10 stsp } else {
5255 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5256 ee41ec32 2018-07-10 stsp prev_id = NULL;
5257 ee41ec32 2018-07-10 stsp }
5258 84451b3e 2018-07-10 stsp
5259 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5260 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5261 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5262 27a741e5 2019-09-11 stsp
5263 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5264 41605754 2020-11-12 stsp width = 9;
5265 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5266 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5267 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5268 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5269 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5270 41605754 2020-11-12 stsp if (err) {
5271 41605754 2020-11-12 stsp free(line);
5272 41605754 2020-11-12 stsp return err;
5273 41605754 2020-11-12 stsp }
5274 41605754 2020-11-12 stsp width += 9;
5275 41605754 2020-11-12 stsp } else {
5276 4cb9d869 2022-06-16 stsp int skip;
5277 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5278 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5279 4cb9d869 2022-06-16 stsp if (err) {
5280 4cb9d869 2022-06-16 stsp free(line);
5281 4cb9d869 2022-06-16 stsp return err;
5282 145b6838 2022-06-16 stsp }
5283 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5284 a75b3e2d 2022-06-16 stsp width += 9;
5285 41605754 2020-11-12 stsp free(wline);
5286 41605754 2020-11-12 stsp wline = NULL;
5287 41605754 2020-11-12 stsp }
5288 41605754 2020-11-12 stsp
5289 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5290 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5291 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5292 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5293 84451b3e 2018-07-10 stsp }
5294 826082fe 2020-12-10 stsp free(line);
5295 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5296 84451b3e 2018-07-10 stsp
5297 9b058f45 2022-06-30 mark view_border(view);
5298 84451b3e 2018-07-10 stsp
5299 84451b3e 2018-07-10 stsp return NULL;
5300 84451b3e 2018-07-10 stsp }
5301 84451b3e 2018-07-10 stsp
5302 84451b3e 2018-07-10 stsp static const struct got_error *
5303 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5304 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5305 84451b3e 2018-07-10 stsp {
5306 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5307 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5308 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5309 1a76625f 2018-10-22 stsp int errcode;
5310 84451b3e 2018-07-10 stsp
5311 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5312 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5313 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5314 84451b3e 2018-07-10 stsp
5315 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5316 1a76625f 2018-10-22 stsp if (errcode)
5317 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5318 84451b3e 2018-07-10 stsp
5319 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5320 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5321 d68a0a7d 2018-07-10 stsp goto done;
5322 d68a0a7d 2018-07-10 stsp }
5323 d68a0a7d 2018-07-10 stsp
5324 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5325 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5326 d68a0a7d 2018-07-10 stsp
5327 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5328 d68a0a7d 2018-07-10 stsp if (line->annotated)
5329 d68a0a7d 2018-07-10 stsp goto done;
5330 d68a0a7d 2018-07-10 stsp
5331 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5332 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5333 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5334 84451b3e 2018-07-10 stsp goto done;
5335 84451b3e 2018-07-10 stsp }
5336 84451b3e 2018-07-10 stsp line->annotated = 1;
5337 84451b3e 2018-07-10 stsp done:
5338 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5339 1a76625f 2018-10-22 stsp if (errcode)
5340 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5341 84451b3e 2018-07-10 stsp return err;
5342 84451b3e 2018-07-10 stsp }
5343 84451b3e 2018-07-10 stsp
5344 84451b3e 2018-07-10 stsp static void *
5345 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5346 84451b3e 2018-07-10 stsp {
5347 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5348 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5349 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5350 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5351 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5352 1b484788 2022-06-28 tracey
5353 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5354 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5355 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5356 e6e73e55 2022-06-30 tracey
5357 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5358 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5359 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5360 e6e73e55 2022-06-30 tracey goto done;
5361 e6e73e55 2022-06-30 tracey }
5362 18430de3 2018-07-10 stsp
5363 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5364 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5365 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5366 e6e73e55 2022-06-30 tracey goto done;
5367 e6e73e55 2022-06-30 tracey }
5368 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5369 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5370 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5371 e6e73e55 2022-06-30 tracey goto done;
5372 e6e73e55 2022-06-30 tracey }
5373 e6e73e55 2022-06-30 tracey
5374 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5375 61266923 2020-01-14 stsp if (err)
5376 e6e73e55 2022-06-30 tracey goto done;
5377 61266923 2020-01-14 stsp
5378 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5379 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5380 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5381 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5382 fc06ba56 2019-08-22 stsp err = NULL;
5383 18430de3 2018-07-10 stsp
5384 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5385 e6e73e55 2022-06-30 tracey if (errcode) {
5386 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5387 e6e73e55 2022-06-30 tracey goto done;
5388 e6e73e55 2022-06-30 tracey }
5389 18430de3 2018-07-10 stsp
5390 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5391 1d0f4054 2021-06-17 stsp if (err == NULL)
5392 1d0f4054 2021-06-17 stsp err = close_err;
5393 c9beca56 2018-07-22 stsp ta->repo = NULL;
5394 c9beca56 2018-07-22 stsp *ta->complete = 1;
5395 18430de3 2018-07-10 stsp
5396 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5397 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5398 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5399 18430de3 2018-07-10 stsp
5400 e6e73e55 2022-06-30 tracey done:
5401 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5402 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5403 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5404 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5405 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5406 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5407 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5408 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5409 1b484788 2022-06-28 tracey
5410 18430de3 2018-07-10 stsp return (void *)err;
5411 84451b3e 2018-07-10 stsp }
5412 84451b3e 2018-07-10 stsp
5413 245d91c1 2018-07-12 stsp static struct got_object_id *
5414 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5415 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5416 245d91c1 2018-07-12 stsp {
5417 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5418 8d0fe45a 2019-08-12 stsp
5419 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5420 8d0fe45a 2019-08-12 stsp return NULL;
5421 b880a918 2018-07-10 stsp
5422 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5423 c0f61fa4 2022-07-11 mark if (!line->annotated)
5424 c0f61fa4 2022-07-11 mark return NULL;
5425 c0f61fa4 2022-07-11 mark
5426 c0f61fa4 2022-07-11 mark return line->id;
5427 c0f61fa4 2022-07-11 mark }
5428 c0f61fa4 2022-07-11 mark
5429 c0f61fa4 2022-07-11 mark static struct got_object_id *
5430 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5431 c0f61fa4 2022-07-11 mark int lineno)
5432 c0f61fa4 2022-07-11 mark {
5433 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5434 c0f61fa4 2022-07-11 mark
5435 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5436 c0f61fa4 2022-07-11 mark return NULL;
5437 c0f61fa4 2022-07-11 mark
5438 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5439 245d91c1 2018-07-12 stsp if (!line->annotated)
5440 245d91c1 2018-07-12 stsp return NULL;
5441 245d91c1 2018-07-12 stsp
5442 245d91c1 2018-07-12 stsp return line->id;
5443 b880a918 2018-07-10 stsp }
5444 245d91c1 2018-07-12 stsp
5445 b880a918 2018-07-10 stsp static const struct got_error *
5446 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5447 a70480e0 2018-06-23 stsp {
5448 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5449 245d91c1 2018-07-12 stsp int i;
5450 245d91c1 2018-07-12 stsp
5451 245d91c1 2018-07-12 stsp if (blame->thread) {
5452 1a76625f 2018-10-22 stsp int errcode;
5453 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5454 1a76625f 2018-10-22 stsp if (errcode)
5455 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5456 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5457 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5458 1a76625f 2018-10-22 stsp if (errcode)
5459 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5460 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5461 1a76625f 2018-10-22 stsp if (errcode)
5462 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5463 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5464 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5465 245d91c1 2018-07-12 stsp err = NULL;
5466 245d91c1 2018-07-12 stsp blame->thread = NULL;
5467 245d91c1 2018-07-12 stsp }
5468 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5469 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5470 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5471 1d0f4054 2021-06-17 stsp if (err == NULL)
5472 1d0f4054 2021-06-17 stsp err = close_err;
5473 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5474 245d91c1 2018-07-12 stsp }
5475 245d91c1 2018-07-12 stsp if (blame->f) {
5476 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5477 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5478 245d91c1 2018-07-12 stsp blame->f = NULL;
5479 245d91c1 2018-07-12 stsp }
5480 57670559 2018-12-23 stsp if (blame->lines) {
5481 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5482 57670559 2018-12-23 stsp free(blame->lines[i].id);
5483 57670559 2018-12-23 stsp free(blame->lines);
5484 57670559 2018-12-23 stsp blame->lines = NULL;
5485 57670559 2018-12-23 stsp }
5486 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5487 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5488 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5489 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5490 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5491 0ae84acc 2022-06-15 tracey if (err == NULL)
5492 0ae84acc 2022-06-15 tracey err = pack_err;
5493 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5494 0ae84acc 2022-06-15 tracey }
5495 245d91c1 2018-07-12 stsp return err;
5496 245d91c1 2018-07-12 stsp }
5497 245d91c1 2018-07-12 stsp
5498 245d91c1 2018-07-12 stsp static const struct got_error *
5499 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5500 fc06ba56 2019-08-22 stsp {
5501 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5502 fc06ba56 2019-08-22 stsp int *done = arg;
5503 fc06ba56 2019-08-22 stsp int errcode;
5504 fc06ba56 2019-08-22 stsp
5505 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5506 fc06ba56 2019-08-22 stsp if (errcode)
5507 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5508 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5509 fc06ba56 2019-08-22 stsp
5510 fc06ba56 2019-08-22 stsp if (*done)
5511 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5512 fc06ba56 2019-08-22 stsp
5513 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5514 fc06ba56 2019-08-22 stsp if (errcode)
5515 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5516 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5517 fc06ba56 2019-08-22 stsp
5518 fc06ba56 2019-08-22 stsp return err;
5519 fc06ba56 2019-08-22 stsp }
5520 fc06ba56 2019-08-22 stsp
5521 fc06ba56 2019-08-22 stsp static const struct got_error *
5522 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5523 245d91c1 2018-07-12 stsp {
5524 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5525 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5526 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5527 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5528 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5529 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5530 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5531 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5532 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5533 a70480e0 2018-06-23 stsp
5534 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5535 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5536 27d434c2 2018-09-15 stsp if (err)
5537 15a94983 2018-12-23 stsp return err;
5538 eb81bc23 2022-06-28 tracey
5539 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5540 eb81bc23 2022-06-28 tracey if (fd == -1) {
5541 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5542 eb81bc23 2022-06-28 tracey goto done;
5543 eb81bc23 2022-06-28 tracey }
5544 a44927cc 2022-04-07 stsp
5545 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5546 a44927cc 2022-04-07 stsp if (err)
5547 a44927cc 2022-04-07 stsp goto done;
5548 27d434c2 2018-09-15 stsp
5549 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5550 84451b3e 2018-07-10 stsp if (err)
5551 84451b3e 2018-07-10 stsp goto done;
5552 27d434c2 2018-09-15 stsp
5553 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5554 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5555 84451b3e 2018-07-10 stsp goto done;
5556 84451b3e 2018-07-10 stsp }
5557 a70480e0 2018-06-23 stsp
5558 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5559 a70480e0 2018-06-23 stsp if (err)
5560 a70480e0 2018-06-23 stsp goto done;
5561 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5562 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5563 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5564 84451b3e 2018-07-10 stsp goto done;
5565 84451b3e 2018-07-10 stsp }
5566 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5567 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5568 1fddf795 2021-01-20 stsp if (err)
5569 1fddf795 2021-01-20 stsp goto done;
5570 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5571 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5572 84451b3e 2018-07-10 stsp goto done;
5573 1fddf795 2021-01-20 stsp }
5574 b02560ec 2019-08-19 stsp
5575 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5576 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5577 b02560ec 2019-08-19 stsp blame->nlines--;
5578 a70480e0 2018-06-23 stsp
5579 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5580 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5581 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5582 84451b3e 2018-07-10 stsp goto done;
5583 84451b3e 2018-07-10 stsp }
5584 a70480e0 2018-06-23 stsp
5585 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5586 bd24772e 2018-07-11 stsp if (err)
5587 bd24772e 2018-07-11 stsp goto done;
5588 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5589 0ae84acc 2022-06-15 tracey pack_fds);
5590 0ae84acc 2022-06-15 tracey if (err)
5591 0ae84acc 2022-06-15 tracey goto done;
5592 bd24772e 2018-07-11 stsp
5593 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5594 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5595 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5596 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5597 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5598 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5599 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5600 245d91c1 2018-07-12 stsp goto done;
5601 245d91c1 2018-07-12 stsp }
5602 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5603 245d91c1 2018-07-12 stsp
5604 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5605 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5606 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5607 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5608 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5609 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5610 a5388363 2020-12-01 naddy s->blame_complete = 0;
5611 f5a09613 2020-12-13 naddy
5612 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5613 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5614 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5615 f5a09613 2020-12-13 naddy s->selected_line = 1;
5616 f5a09613 2020-12-13 naddy }
5617 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5618 245d91c1 2018-07-12 stsp
5619 245d91c1 2018-07-12 stsp done:
5620 a44927cc 2022-04-07 stsp if (commit)
5621 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5622 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5623 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5624 245d91c1 2018-07-12 stsp if (blob)
5625 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5626 27d434c2 2018-09-15 stsp free(obj_id);
5627 245d91c1 2018-07-12 stsp if (err)
5628 245d91c1 2018-07-12 stsp stop_blame(blame);
5629 245d91c1 2018-07-12 stsp return err;
5630 245d91c1 2018-07-12 stsp }
5631 245d91c1 2018-07-12 stsp
5632 245d91c1 2018-07-12 stsp static const struct got_error *
5633 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5634 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5635 245d91c1 2018-07-12 stsp {
5636 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5637 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5638 dbc6a6b6 2018-07-12 stsp
5639 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5640 245d91c1 2018-07-12 stsp
5641 c4843652 2019-08-12 stsp s->path = strdup(path);
5642 c4843652 2019-08-12 stsp if (s->path == NULL)
5643 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5644 c4843652 2019-08-12 stsp
5645 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5646 c4843652 2019-08-12 stsp if (err) {
5647 c4843652 2019-08-12 stsp free(s->path);
5648 7cbe629d 2018-08-04 stsp return err;
5649 c4843652 2019-08-12 stsp }
5650 245d91c1 2018-07-12 stsp
5651 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5652 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5653 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5654 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5655 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5656 fb2756b9 2018-08-04 stsp s->repo = repo;
5657 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5658 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5659 7cbe629d 2018-08-04 stsp
5660 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5661 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5662 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5663 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5664 11b20872 2019-11-08 stsp if (err)
5665 11b20872 2019-11-08 stsp return err;
5666 11b20872 2019-11-08 stsp }
5667 11b20872 2019-11-08 stsp
5668 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5669 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5670 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5671 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5672 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5673 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5674 e5a0f69f 2018-08-18 stsp
5675 a5388363 2020-12-01 naddy return run_blame(view);
5676 7cbe629d 2018-08-04 stsp }
5677 7cbe629d 2018-08-04 stsp
5678 e5a0f69f 2018-08-18 stsp static const struct got_error *
5679 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5680 7cbe629d 2018-08-04 stsp {
5681 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5682 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5683 7cbe629d 2018-08-04 stsp
5684 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5685 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5686 e5a0f69f 2018-08-18 stsp
5687 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5688 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5689 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5690 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5691 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5692 7cbe629d 2018-08-04 stsp }
5693 e5a0f69f 2018-08-18 stsp
5694 e5a0f69f 2018-08-18 stsp free(s->path);
5695 11b20872 2019-11-08 stsp free_colors(&s->colors);
5696 e5a0f69f 2018-08-18 stsp return err;
5697 7cbe629d 2018-08-04 stsp }
5698 7cbe629d 2018-08-04 stsp
5699 7cbe629d 2018-08-04 stsp static const struct got_error *
5700 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5701 6c4c42e0 2019-06-24 stsp {
5702 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5703 6c4c42e0 2019-06-24 stsp
5704 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5705 6c4c42e0 2019-06-24 stsp return NULL;
5706 6c4c42e0 2019-06-24 stsp }
5707 6c4c42e0 2019-06-24 stsp
5708 6c4c42e0 2019-06-24 stsp static const struct got_error *
5709 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5710 6c4c42e0 2019-06-24 stsp {
5711 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5712 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5713 6c4c42e0 2019-06-24 stsp int lineno;
5714 cb713507 2022-06-16 stsp char *line = NULL;
5715 826082fe 2020-12-10 stsp size_t linesize = 0;
5716 826082fe 2020-12-10 stsp ssize_t linelen;
5717 6c4c42e0 2019-06-24 stsp
5718 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5719 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5720 6c4c42e0 2019-06-24 stsp return NULL;
5721 6c4c42e0 2019-06-24 stsp }
5722 6c4c42e0 2019-06-24 stsp
5723 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5724 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5725 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5726 6c4c42e0 2019-06-24 stsp else
5727 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5728 487cd7d2 2021-12-17 stsp } else
5729 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5730 6c4c42e0 2019-06-24 stsp
5731 6c4c42e0 2019-06-24 stsp while (1) {
5732 6c4c42e0 2019-06-24 stsp off_t offset;
5733 6c4c42e0 2019-06-24 stsp
5734 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5735 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5736 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5737 6c4c42e0 2019-06-24 stsp break;
5738 6c4c42e0 2019-06-24 stsp }
5739 2246482e 2019-06-25 stsp
5740 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5741 6c4c42e0 2019-06-24 stsp lineno = 1;
5742 6c4c42e0 2019-06-24 stsp else
5743 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5744 6c4c42e0 2019-06-24 stsp }
5745 6c4c42e0 2019-06-24 stsp
5746 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5747 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5748 6c4c42e0 2019-06-24 stsp free(line);
5749 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5750 6c4c42e0 2019-06-24 stsp }
5751 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5752 cb713507 2022-06-16 stsp if (linelen != -1) {
5753 cb713507 2022-06-16 stsp char *exstr;
5754 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5755 cb713507 2022-06-16 stsp if (err)
5756 cb713507 2022-06-16 stsp break;
5757 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5758 cb713507 2022-06-16 stsp &view->regmatch)) {
5759 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5760 cb713507 2022-06-16 stsp s->matched_line = lineno;
5761 cb713507 2022-06-16 stsp free(exstr);
5762 cb713507 2022-06-16 stsp break;
5763 cb713507 2022-06-16 stsp }
5764 cb713507 2022-06-16 stsp free(exstr);
5765 6c4c42e0 2019-06-24 stsp }
5766 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5767 6c4c42e0 2019-06-24 stsp lineno++;
5768 6c4c42e0 2019-06-24 stsp else
5769 6c4c42e0 2019-06-24 stsp lineno--;
5770 6c4c42e0 2019-06-24 stsp }
5771 826082fe 2020-12-10 stsp free(line);
5772 6c4c42e0 2019-06-24 stsp
5773 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5774 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5775 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5776 6c4c42e0 2019-06-24 stsp }
5777 6c4c42e0 2019-06-24 stsp
5778 145b6838 2022-06-16 stsp return err;
5779 6c4c42e0 2019-06-24 stsp }
5780 6c4c42e0 2019-06-24 stsp
5781 6c4c42e0 2019-06-24 stsp static const struct got_error *
5782 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5783 7cbe629d 2018-08-04 stsp {
5784 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5785 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5786 2b380cc8 2018-10-24 stsp int errcode;
5787 2b380cc8 2018-10-24 stsp
5788 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5789 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5790 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5791 2b380cc8 2018-10-24 stsp if (errcode)
5792 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5793 51fe7530 2019-08-19 stsp
5794 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5795 2b380cc8 2018-10-24 stsp }
5796 e5a0f69f 2018-08-18 stsp
5797 51fe7530 2019-08-19 stsp if (s->blame_complete)
5798 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5799 51fe7530 2019-08-19 stsp
5800 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5801 e5a0f69f 2018-08-18 stsp
5802 9b058f45 2022-06-30 mark view_border(view);
5803 e5a0f69f 2018-08-18 stsp return err;
5804 e5a0f69f 2018-08-18 stsp }
5805 e5a0f69f 2018-08-18 stsp
5806 e5a0f69f 2018-08-18 stsp static const struct got_error *
5807 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5808 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5809 05f04cdf 2022-07-20 mark {
5810 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5811 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5812 05f04cdf 2022-07-20 mark
5813 05f04cdf 2022-07-20 mark *new_view = NULL;
5814 05f04cdf 2022-07-20 mark
5815 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5816 05f04cdf 2022-07-20 mark if (log_view == NULL)
5817 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5818 05f04cdf 2022-07-20 mark
5819 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5820 05f04cdf 2022-07-20 mark if (err)
5821 05f04cdf 2022-07-20 mark view_close(log_view);
5822 05f04cdf 2022-07-20 mark else
5823 05f04cdf 2022-07-20 mark *new_view = log_view;
5824 05f04cdf 2022-07-20 mark
5825 05f04cdf 2022-07-20 mark return err;
5826 05f04cdf 2022-07-20 mark }
5827 05f04cdf 2022-07-20 mark
5828 05f04cdf 2022-07-20 mark static const struct got_error *
5829 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5830 e5a0f69f 2018-08-18 stsp {
5831 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5832 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
5833 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5834 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5835 9b058f45 2022-06-30 mark
5836 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5837 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5838 9b058f45 2022-06-30 mark --eos; /* border */
5839 7cbe629d 2018-08-04 stsp
5840 e5a0f69f 2018-08-18 stsp switch (ch) {
5841 145b6838 2022-06-16 stsp case '0':
5842 145b6838 2022-06-16 stsp view->x = 0;
5843 145b6838 2022-06-16 stsp break;
5844 145b6838 2022-06-16 stsp case '$':
5845 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5846 640cd7ff 2022-06-22 mark view->count = 0;
5847 145b6838 2022-06-16 stsp break;
5848 145b6838 2022-06-16 stsp case KEY_RIGHT:
5849 145b6838 2022-06-16 stsp case 'l':
5850 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5851 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5852 640cd7ff 2022-06-22 mark else
5853 640cd7ff 2022-06-22 mark view->count = 0;
5854 145b6838 2022-06-16 stsp break;
5855 145b6838 2022-06-16 stsp case KEY_LEFT:
5856 145b6838 2022-06-16 stsp case 'h':
5857 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5858 640cd7ff 2022-06-22 mark if (view->x <= 0)
5859 640cd7ff 2022-06-22 mark view->count = 0;
5860 145b6838 2022-06-16 stsp break;
5861 1e37a5c2 2019-05-12 jcs case 'q':
5862 1e37a5c2 2019-05-12 jcs s->done = 1;
5863 4deef56f 2021-09-02 naddy break;
5864 4deef56f 2021-09-02 naddy case 'g':
5865 4deef56f 2021-09-02 naddy case KEY_HOME:
5866 4deef56f 2021-09-02 naddy s->selected_line = 1;
5867 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5868 640cd7ff 2022-06-22 mark view->count = 0;
5869 4deef56f 2021-09-02 naddy break;
5870 4deef56f 2021-09-02 naddy case 'G':
5871 4deef56f 2021-09-02 naddy case KEY_END:
5872 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5873 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5874 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5875 4deef56f 2021-09-02 naddy } else {
5876 9b058f45 2022-06-30 mark s->selected_line = eos;
5877 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5878 4deef56f 2021-09-02 naddy }
5879 640cd7ff 2022-06-22 mark view->count = 0;
5880 1e37a5c2 2019-05-12 jcs break;
5881 1e37a5c2 2019-05-12 jcs case 'k':
5882 1e37a5c2 2019-05-12 jcs case KEY_UP:
5883 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5884 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5885 1e37a5c2 2019-05-12 jcs s->selected_line--;
5886 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5887 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5888 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5889 640cd7ff 2022-06-22 mark else
5890 640cd7ff 2022-06-22 mark view->count = 0;
5891 1e37a5c2 2019-05-12 jcs break;
5892 83cc4199 2022-06-13 stsp case CTRL('u'):
5893 33c3719a 2022-06-15 stsp case 'u':
5894 83cc4199 2022-06-13 stsp nscroll /= 2;
5895 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5896 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5897 ea025d1d 2020-02-22 naddy case CTRL('b'):
5898 61417565 2022-06-20 mark case 'b':
5899 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5900 640cd7ff 2022-06-22 mark if (view->count > 1)
5901 640cd7ff 2022-06-22 mark nscroll += nscroll;
5902 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5903 640cd7ff 2022-06-22 mark view->count = 0;
5904 e5a0f69f 2018-08-18 stsp break;
5905 1e37a5c2 2019-05-12 jcs }
5906 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5907 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5908 1e37a5c2 2019-05-12 jcs else
5909 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5910 1e37a5c2 2019-05-12 jcs break;
5911 1e37a5c2 2019-05-12 jcs case 'j':
5912 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5913 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5914 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5915 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5916 1e37a5c2 2019-05-12 jcs s->selected_line++;
5917 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5918 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5919 640cd7ff 2022-06-22 mark else
5920 640cd7ff 2022-06-22 mark view->count = 0;
5921 1e37a5c2 2019-05-12 jcs break;
5922 61417565 2022-06-20 mark case 'c':
5923 1e37a5c2 2019-05-12 jcs case 'p': {
5924 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5925 640cd7ff 2022-06-22 mark
5926 640cd7ff 2022-06-22 mark view->count = 0;
5927 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5928 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5929 1e37a5c2 2019-05-12 jcs if (id == NULL)
5930 e5a0f69f 2018-08-18 stsp break;
5931 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5932 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5933 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5934 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5935 1e37a5c2 2019-05-12 jcs int obj_type;
5936 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5937 1e37a5c2 2019-05-12 jcs s->repo, id);
5938 e5a0f69f 2018-08-18 stsp if (err)
5939 e5a0f69f 2018-08-18 stsp break;
5940 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5941 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5942 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5943 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5944 e5a0f69f 2018-08-18 stsp break;
5945 e5a0f69f 2018-08-18 stsp }
5946 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5947 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5948 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5949 a44927cc 2022-04-07 stsp if (err)
5950 a44927cc 2022-04-07 stsp break;
5951 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5952 a44927cc 2022-04-07 stsp pcommit, s->path);
5953 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5954 e5a0f69f 2018-08-18 stsp if (err) {
5955 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5956 1e37a5c2 2019-05-12 jcs err = NULL;
5957 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5958 e5a0f69f 2018-08-18 stsp break;
5959 e5a0f69f 2018-08-18 stsp }
5960 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5961 1e37a5c2 2019-05-12 jcs blob_id);
5962 1e37a5c2 2019-05-12 jcs free(blob_id);
5963 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5964 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5965 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5966 e5a0f69f 2018-08-18 stsp break;
5967 1e37a5c2 2019-05-12 jcs }
5968 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5969 d7b5a0e8 2022-04-20 stsp &pid->id);
5970 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5971 1e37a5c2 2019-05-12 jcs } else {
5972 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5973 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5974 1e37a5c2 2019-05-12 jcs break;
5975 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5976 1e37a5c2 2019-05-12 jcs id);
5977 1e37a5c2 2019-05-12 jcs }
5978 1e37a5c2 2019-05-12 jcs if (err)
5979 e5a0f69f 2018-08-18 stsp break;
5980 1e37a5c2 2019-05-12 jcs s->done = 1;
5981 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5982 1e37a5c2 2019-05-12 jcs s->done = 0;
5983 1e37a5c2 2019-05-12 jcs if (thread_err)
5984 1e37a5c2 2019-05-12 jcs break;
5985 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5986 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5987 a5388363 2020-12-01 naddy err = run_blame(view);
5988 1e37a5c2 2019-05-12 jcs if (err)
5989 1e37a5c2 2019-05-12 jcs break;
5990 1e37a5c2 2019-05-12 jcs break;
5991 1e37a5c2 2019-05-12 jcs }
5992 61417565 2022-06-20 mark case 'C': {
5993 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5994 640cd7ff 2022-06-22 mark
5995 640cd7ff 2022-06-22 mark view->count = 0;
5996 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5997 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5998 1e37a5c2 2019-05-12 jcs break;
5999 1e37a5c2 2019-05-12 jcs s->done = 1;
6000 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6001 1e37a5c2 2019-05-12 jcs s->done = 0;
6002 1e37a5c2 2019-05-12 jcs if (thread_err)
6003 1e37a5c2 2019-05-12 jcs break;
6004 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6005 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6006 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6007 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6008 a5388363 2020-12-01 naddy err = run_blame(view);
6009 1e37a5c2 2019-05-12 jcs if (err)
6010 1e37a5c2 2019-05-12 jcs break;
6011 1e37a5c2 2019-05-12 jcs break;
6012 1e37a5c2 2019-05-12 jcs }
6013 136e2bd4 2022-07-23 mark case 'L':
6014 05f04cdf 2022-07-20 mark view->count = 0;
6015 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6016 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6017 136e2bd4 2022-07-23 mark if (s->id_to_log)
6018 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6019 05f04cdf 2022-07-20 mark break;
6020 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6021 1e37a5c2 2019-05-12 jcs case '\r': {
6022 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6023 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6024 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6025 640cd7ff 2022-06-22 mark
6026 640cd7ff 2022-06-22 mark view->count = 0;
6027 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6028 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6029 1e37a5c2 2019-05-12 jcs if (id == NULL)
6030 1e37a5c2 2019-05-12 jcs break;
6031 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6032 1e37a5c2 2019-05-12 jcs if (err)
6033 1e37a5c2 2019-05-12 jcs break;
6034 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6035 c0f61fa4 2022-07-11 mark if (*new_view) {
6036 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6037 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6038 c0f61fa4 2022-07-11 mark if (err)
6039 c0f61fa4 2022-07-11 mark break;
6040 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6041 c0f61fa4 2022-07-11 mark } else {
6042 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6043 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6044 9b058f45 2022-06-30 mark
6045 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6046 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6047 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6048 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6049 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6050 c0f61fa4 2022-07-11 mark break;
6051 c0f61fa4 2022-07-11 mark }
6052 15a94983 2018-12-23 stsp }
6053 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6054 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6055 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6056 1e37a5c2 2019-05-12 jcs if (err) {
6057 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6058 1e37a5c2 2019-05-12 jcs break;
6059 1e37a5c2 2019-05-12 jcs }
6060 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6061 c0f61fa4 2022-07-11 mark s->selected_line;
6062 c0f61fa4 2022-07-11 mark if (*new_view)
6063 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6064 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6065 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6066 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6067 9b058f45 2022-06-30 mark if (err)
6068 9b058f45 2022-06-30 mark break;
6069 9b058f45 2022-06-30 mark }
6070 9b058f45 2022-06-30 mark
6071 e78dc838 2020-12-04 stsp view->focussed = 0;
6072 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6073 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6074 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6075 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6076 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6077 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6078 1e37a5c2 2019-05-12 jcs if (err)
6079 34bc9ec9 2019-02-22 stsp break;
6080 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6081 0dbbbe90 2022-06-17 op if (err)
6082 0dbbbe90 2022-06-17 op break;
6083 e78dc838 2020-12-04 stsp view->focus_child = 1;
6084 1e37a5c2 2019-05-12 jcs } else
6085 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6086 1e37a5c2 2019-05-12 jcs if (err)
6087 e5a0f69f 2018-08-18 stsp break;
6088 1e37a5c2 2019-05-12 jcs break;
6089 1e37a5c2 2019-05-12 jcs }
6090 83cc4199 2022-06-13 stsp case CTRL('d'):
6091 33c3719a 2022-06-15 stsp case 'd':
6092 83cc4199 2022-06-13 stsp nscroll /= 2;
6093 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6094 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6095 ea025d1d 2020-02-22 naddy case CTRL('f'):
6096 61417565 2022-06-20 mark case 'f':
6097 1e37a5c2 2019-05-12 jcs case ' ':
6098 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6099 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6100 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6101 640cd7ff 2022-06-22 mark view->count = 0;
6102 e5a0f69f 2018-08-18 stsp break;
6103 1e37a5c2 2019-05-12 jcs }
6104 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6105 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6106 83cc4199 2022-06-13 stsp s->selected_line +=
6107 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6108 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6109 1e37a5c2 2019-05-12 jcs }
6110 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6111 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6112 1e37a5c2 2019-05-12 jcs else
6113 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6114 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6115 1e37a5c2 2019-05-12 jcs break;
6116 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6117 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6118 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6119 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6120 1e37a5c2 2019-05-12 jcs }
6121 1e37a5c2 2019-05-12 jcs break;
6122 1e37a5c2 2019-05-12 jcs default:
6123 640cd7ff 2022-06-22 mark view->count = 0;
6124 1e37a5c2 2019-05-12 jcs break;
6125 a70480e0 2018-06-23 stsp }
6126 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6127 917d79a7 2022-07-01 stsp }
6128 917d79a7 2022-07-01 stsp
6129 917d79a7 2022-07-01 stsp static const struct got_error *
6130 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6131 917d79a7 2022-07-01 stsp {
6132 917d79a7 2022-07-01 stsp const struct got_error *err;
6133 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6134 917d79a7 2022-07-01 stsp
6135 917d79a7 2022-07-01 stsp view->count = 0;
6136 917d79a7 2022-07-01 stsp s->done = 1;
6137 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6138 917d79a7 2022-07-01 stsp s->done = 0;
6139 917d79a7 2022-07-01 stsp if (err)
6140 917d79a7 2022-07-01 stsp return err;
6141 917d79a7 2022-07-01 stsp return run_blame(view);
6142 a70480e0 2018-06-23 stsp }
6143 a70480e0 2018-06-23 stsp
6144 a70480e0 2018-06-23 stsp static const struct got_error *
6145 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6146 9f7d7167 2018-04-29 stsp {
6147 a70480e0 2018-06-23 stsp const struct got_error *error;
6148 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6149 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6150 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6151 0587e10c 2020-07-23 stsp char *link_target = NULL;
6152 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6153 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6154 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6155 a70480e0 2018-06-23 stsp int ch;
6156 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6157 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6158 a70480e0 2018-06-23 stsp
6159 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6160 a70480e0 2018-06-23 stsp switch (ch) {
6161 a70480e0 2018-06-23 stsp case 'c':
6162 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6163 a70480e0 2018-06-23 stsp break;
6164 69069811 2018-08-02 stsp case 'r':
6165 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6166 69069811 2018-08-02 stsp if (repo_path == NULL)
6167 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6168 9ba1d308 2019-10-21 stsp optarg);
6169 69069811 2018-08-02 stsp break;
6170 a70480e0 2018-06-23 stsp default:
6171 17020d27 2019-03-07 stsp usage_blame();
6172 a70480e0 2018-06-23 stsp /* NOTREACHED */
6173 a70480e0 2018-06-23 stsp }
6174 a70480e0 2018-06-23 stsp }
6175 a70480e0 2018-06-23 stsp
6176 a70480e0 2018-06-23 stsp argc -= optind;
6177 a70480e0 2018-06-23 stsp argv += optind;
6178 a70480e0 2018-06-23 stsp
6179 f135c941 2020-02-20 stsp if (argc != 1)
6180 a70480e0 2018-06-23 stsp usage_blame();
6181 6962eb72 2020-02-20 stsp
6182 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6183 0ae84acc 2022-06-15 tracey if (error != NULL)
6184 0ae84acc 2022-06-15 tracey goto done;
6185 0ae84acc 2022-06-15 tracey
6186 69069811 2018-08-02 stsp if (repo_path == NULL) {
6187 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6188 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6189 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6190 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6191 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6192 c156c7a4 2020-12-18 stsp goto done;
6193 f135c941 2020-02-20 stsp if (worktree)
6194 eb41ed75 2019-02-05 stsp repo_path =
6195 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6196 f135c941 2020-02-20 stsp else
6197 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6198 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6199 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6200 c156c7a4 2020-12-18 stsp goto done;
6201 c156c7a4 2020-12-18 stsp }
6202 f135c941 2020-02-20 stsp }
6203 a915003a 2019-02-05 stsp
6204 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6205 c02c541e 2019-03-29 stsp if (error != NULL)
6206 8e94dd5b 2019-01-04 stsp goto done;
6207 69069811 2018-08-02 stsp
6208 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6209 f135c941 2020-02-20 stsp worktree);
6210 c02c541e 2019-03-29 stsp if (error)
6211 92205607 2019-01-04 stsp goto done;
6212 69069811 2018-08-02 stsp
6213 f135c941 2020-02-20 stsp init_curses();
6214 f135c941 2020-02-20 stsp
6215 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6216 51a10b52 2020-12-26 stsp if (error)
6217 51a10b52 2020-12-26 stsp goto done;
6218 51a10b52 2020-12-26 stsp
6219 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6220 eb41ed75 2019-02-05 stsp if (error)
6221 69069811 2018-08-02 stsp goto done;
6222 a70480e0 2018-06-23 stsp
6223 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6224 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6225 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6226 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6227 a70480e0 2018-06-23 stsp if (error != NULL)
6228 66b4983c 2018-06-23 stsp goto done;
6229 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6230 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6231 a70480e0 2018-06-23 stsp } else {
6232 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6233 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6234 a70480e0 2018-06-23 stsp }
6235 a19e88aa 2018-06-23 stsp if (error != NULL)
6236 8b473291 2019-02-21 stsp goto done;
6237 8b473291 2019-02-21 stsp
6238 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6239 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6240 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6241 e1cd8fed 2018-08-01 stsp goto done;
6242 e1cd8fed 2018-08-01 stsp }
6243 0587e10c 2020-07-23 stsp
6244 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6245 a44927cc 2022-04-07 stsp if (error)
6246 a44927cc 2022-04-07 stsp goto done;
6247 a44927cc 2022-04-07 stsp
6248 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6249 a44927cc 2022-04-07 stsp commit, repo);
6250 7cbe629d 2018-08-04 stsp if (error)
6251 7cbe629d 2018-08-04 stsp goto done;
6252 0587e10c 2020-07-23 stsp
6253 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6254 78756c87 2020-11-24 stsp commit_id, repo);
6255 0587e10c 2020-07-23 stsp if (error)
6256 0587e10c 2020-07-23 stsp goto done;
6257 12314ad4 2019-08-31 stsp if (worktree) {
6258 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6259 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6260 12314ad4 2019-08-31 stsp worktree = NULL;
6261 12314ad4 2019-08-31 stsp }
6262 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6263 a70480e0 2018-06-23 stsp done:
6264 69069811 2018-08-02 stsp free(repo_path);
6265 f135c941 2020-02-20 stsp free(in_repo_path);
6266 0587e10c 2020-07-23 stsp free(link_target);
6267 69069811 2018-08-02 stsp free(cwd);
6268 a70480e0 2018-06-23 stsp free(commit_id);
6269 a44927cc 2022-04-07 stsp if (commit)
6270 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6271 eb41ed75 2019-02-05 stsp if (worktree)
6272 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6273 1d0f4054 2021-06-17 stsp if (repo) {
6274 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6275 1d0f4054 2021-06-17 stsp if (error == NULL)
6276 1d0f4054 2021-06-17 stsp error = close_err;
6277 1d0f4054 2021-06-17 stsp }
6278 0ae84acc 2022-06-15 tracey if (pack_fds) {
6279 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6280 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6281 0ae84acc 2022-06-15 tracey if (error == NULL)
6282 0ae84acc 2022-06-15 tracey error = pack_err;
6283 0ae84acc 2022-06-15 tracey }
6284 51a10b52 2020-12-26 stsp tog_free_refs();
6285 a70480e0 2018-06-23 stsp return error;
6286 ffd1d5e5 2018-06-23 stsp }
6287 ffd1d5e5 2018-06-23 stsp
6288 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6289 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6290 ffd1d5e5 2018-06-23 stsp {
6291 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6292 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6293 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6294 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6295 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6296 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6297 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6298 ffd1d5e5 2018-06-23 stsp
6299 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6300 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6301 9b058f45 2022-06-30 mark --limit; /* border */
6302 ffd1d5e5 2018-06-23 stsp
6303 f7d12f7e 2018-08-01 stsp werase(view->window);
6304 ffd1d5e5 2018-06-23 stsp
6305 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6306 ffd1d5e5 2018-06-23 stsp return NULL;
6307 ffd1d5e5 2018-06-23 stsp
6308 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6309 ccda2f4d 2022-06-16 stsp 0, 0);
6310 ffd1d5e5 2018-06-23 stsp if (err)
6311 ffd1d5e5 2018-06-23 stsp return err;
6312 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6313 a3404814 2018-09-02 stsp wstandout(view->window);
6314 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6315 11b20872 2019-11-08 stsp if (tc)
6316 11b20872 2019-11-08 stsp wattr_on(view->window,
6317 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6318 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6319 11b20872 2019-11-08 stsp if (tc)
6320 11b20872 2019-11-08 stsp wattr_off(view->window,
6321 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6322 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6323 a3404814 2018-09-02 stsp wstandend(view->window);
6324 2550e4c3 2018-07-13 stsp free(wline);
6325 2550e4c3 2018-07-13 stsp wline = NULL;
6326 94b80cfa 2022-08-01 mark
6327 94b80cfa 2022-08-01 mark if (s->selected_entry) {
6328 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(s->selected_entry);
6329 94b80cfa 2022-08-01 mark i += s->tree == s->root ? 1 : 2; /* account for ".." entry */
6330 94b80cfa 2022-08-01 mark }
6331 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6332 94b80cfa 2022-08-01 mark wprintw(view->window, " [%d/%d]", i,
6333 94b80cfa 2022-08-01 mark nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6334 94b80cfa 2022-08-01 mark
6335 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6336 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6337 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6338 ffd1d5e5 2018-06-23 stsp return NULL;
6339 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6340 ccda2f4d 2022-06-16 stsp 0, 0);
6341 ce52c690 2018-06-23 stsp if (err)
6342 ce52c690 2018-06-23 stsp return err;
6343 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6344 2550e4c3 2018-07-13 stsp free(wline);
6345 2550e4c3 2018-07-13 stsp wline = NULL;
6346 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6347 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6348 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6349 ffd1d5e5 2018-06-23 stsp return NULL;
6350 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6351 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6352 a1eca9bb 2018-06-23 stsp return NULL;
6353 ffd1d5e5 2018-06-23 stsp
6354 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6355 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6356 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6357 0cf4efb1 2018-09-29 stsp if (view->focussed)
6358 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6359 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6360 ffd1d5e5 2018-06-23 stsp }
6361 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6362 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6363 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6364 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6365 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6366 ffd1d5e5 2018-06-23 stsp return NULL;
6367 ffd1d5e5 2018-06-23 stsp n = 1;
6368 ffd1d5e5 2018-06-23 stsp } else {
6369 ffd1d5e5 2018-06-23 stsp n = 0;
6370 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6371 ffd1d5e5 2018-06-23 stsp }
6372 ffd1d5e5 2018-06-23 stsp
6373 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6374 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6375 848d6979 2019-08-12 stsp const char *modestr = "";
6376 56e0773d 2019-11-28 stsp mode_t mode;
6377 1d13200f 2018-07-12 stsp
6378 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6379 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6380 56e0773d 2019-11-28 stsp
6381 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6382 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6383 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6384 1d13200f 2018-07-12 stsp if (err)
6385 638f9024 2019-05-13 stsp return got_error_from_errno(
6386 230a42bd 2019-05-11 jcs "got_object_id_str");
6387 1d13200f 2018-07-12 stsp }
6388 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6389 63c5ca5d 2019-08-24 stsp modestr = "$";
6390 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6391 0d6c6ee3 2020-05-20 stsp int i;
6392 0d6c6ee3 2020-05-20 stsp
6393 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6394 d86d3b18 2020-12-01 naddy te, s->repo);
6395 0d6c6ee3 2020-05-20 stsp if (err) {
6396 0d6c6ee3 2020-05-20 stsp free(id_str);
6397 0d6c6ee3 2020-05-20 stsp return err;
6398 0d6c6ee3 2020-05-20 stsp }
6399 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6400 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6401 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6402 0d6c6ee3 2020-05-20 stsp }
6403 848d6979 2019-08-12 stsp modestr = "@";
6404 0d6c6ee3 2020-05-20 stsp }
6405 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6406 848d6979 2019-08-12 stsp modestr = "/";
6407 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6408 848d6979 2019-08-12 stsp modestr = "*";
6409 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6410 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6411 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6412 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6413 1d13200f 2018-07-12 stsp free(id_str);
6414 0d6c6ee3 2020-05-20 stsp free(link_target);
6415 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6416 1d13200f 2018-07-12 stsp }
6417 1d13200f 2018-07-12 stsp free(id_str);
6418 0d6c6ee3 2020-05-20 stsp free(link_target);
6419 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6420 ccda2f4d 2022-06-16 stsp 0, 0);
6421 ffd1d5e5 2018-06-23 stsp if (err) {
6422 ffd1d5e5 2018-06-23 stsp free(line);
6423 ffd1d5e5 2018-06-23 stsp break;
6424 ffd1d5e5 2018-06-23 stsp }
6425 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6426 0cf4efb1 2018-09-29 stsp if (view->focussed)
6427 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6428 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6429 ffd1d5e5 2018-06-23 stsp }
6430 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6431 f26dddb7 2019-11-08 stsp if (tc)
6432 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6433 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6434 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6435 f26dddb7 2019-11-08 stsp if (tc)
6436 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6437 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6438 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6439 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6440 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6441 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6442 ffd1d5e5 2018-06-23 stsp free(line);
6443 2550e4c3 2018-07-13 stsp free(wline);
6444 2550e4c3 2018-07-13 stsp wline = NULL;
6445 ffd1d5e5 2018-06-23 stsp n++;
6446 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6447 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6448 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6449 ffd1d5e5 2018-06-23 stsp break;
6450 ffd1d5e5 2018-06-23 stsp }
6451 ffd1d5e5 2018-06-23 stsp
6452 ffd1d5e5 2018-06-23 stsp return err;
6453 ffd1d5e5 2018-06-23 stsp }
6454 ffd1d5e5 2018-06-23 stsp
6455 ffd1d5e5 2018-06-23 stsp static void
6456 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6457 ffd1d5e5 2018-06-23 stsp {
6458 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6459 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6460 fa86c4bf 2020-11-29 stsp int i = 0;
6461 ffd1d5e5 2018-06-23 stsp
6462 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6463 ffd1d5e5 2018-06-23 stsp return;
6464 ffd1d5e5 2018-06-23 stsp
6465 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6466 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6467 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6468 fa86c4bf 2020-11-29 stsp if (!isroot)
6469 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6470 fa86c4bf 2020-11-29 stsp break;
6471 fa86c4bf 2020-11-29 stsp }
6472 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6473 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6474 ffd1d5e5 2018-06-23 stsp }
6475 ffd1d5e5 2018-06-23 stsp }
6476 ffd1d5e5 2018-06-23 stsp
6477 9b058f45 2022-06-30 mark static const struct got_error *
6478 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6479 ffd1d5e5 2018-06-23 stsp {
6480 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6481 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6482 ffd1d5e5 2018-06-23 stsp int n = 0;
6483 ffd1d5e5 2018-06-23 stsp
6484 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6485 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6486 694d3271 2020-12-01 naddy s->first_displayed_entry);
6487 694d3271 2020-12-01 naddy else
6488 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6489 56e0773d 2019-11-28 stsp
6490 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6491 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6492 94b80cfa 2022-08-01 mark if (last) {
6493 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6494 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6495 94b80cfa 2022-08-01 mark }
6496 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6497 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6498 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6499 768394f3 2019-01-24 stsp }
6500 ffd1d5e5 2018-06-23 stsp }
6501 9b058f45 2022-06-30 mark
6502 9b058f45 2022-06-30 mark return NULL;
6503 ffd1d5e5 2018-06-23 stsp }
6504 ffd1d5e5 2018-06-23 stsp
6505 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6506 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6507 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6508 ffd1d5e5 2018-06-23 stsp {
6509 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6510 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6511 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6512 ffd1d5e5 2018-06-23 stsp
6513 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6514 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6515 56e0773d 2019-11-28 stsp + 1 /* slash */;
6516 ce52c690 2018-06-23 stsp if (te)
6517 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6518 ce52c690 2018-06-23 stsp
6519 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6520 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6521 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6522 ffd1d5e5 2018-06-23 stsp
6523 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6524 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6525 d9765a41 2018-06-23 stsp while (pt) {
6526 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6527 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6528 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6529 cb2ebc8a 2018-06-23 stsp goto done;
6530 cb2ebc8a 2018-06-23 stsp }
6531 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6532 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6533 cb2ebc8a 2018-06-23 stsp goto done;
6534 cb2ebc8a 2018-06-23 stsp }
6535 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6536 ffd1d5e5 2018-06-23 stsp }
6537 ce52c690 2018-06-23 stsp if (te) {
6538 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6539 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6540 ce52c690 2018-06-23 stsp goto done;
6541 ce52c690 2018-06-23 stsp }
6542 cb2ebc8a 2018-06-23 stsp }
6543 ce52c690 2018-06-23 stsp done:
6544 ce52c690 2018-06-23 stsp if (err) {
6545 ce52c690 2018-06-23 stsp free(*path);
6546 ce52c690 2018-06-23 stsp *path = NULL;
6547 ce52c690 2018-06-23 stsp }
6548 ce52c690 2018-06-23 stsp return err;
6549 ce52c690 2018-06-23 stsp }
6550 ce52c690 2018-06-23 stsp
6551 ce52c690 2018-06-23 stsp static const struct got_error *
6552 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6553 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6554 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6555 ce52c690 2018-06-23 stsp {
6556 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6557 ce52c690 2018-06-23 stsp char *path;
6558 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6559 a0de39f3 2019-08-09 stsp
6560 a0de39f3 2019-08-09 stsp *new_view = NULL;
6561 69efd4c4 2018-07-18 stsp
6562 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6563 ce52c690 2018-06-23 stsp if (err)
6564 ce52c690 2018-06-23 stsp return err;
6565 ffd1d5e5 2018-06-23 stsp
6566 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6567 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6568 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6569 83ce39e3 2019-08-12 stsp goto done;
6570 83ce39e3 2019-08-12 stsp }
6571 cdf1ee82 2018-08-01 stsp
6572 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6573 e5a0f69f 2018-08-18 stsp if (err) {
6574 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6575 fc06ba56 2019-08-22 stsp err = NULL;
6576 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6577 e5a0f69f 2018-08-18 stsp } else
6578 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6579 83ce39e3 2019-08-12 stsp done:
6580 83ce39e3 2019-08-12 stsp free(path);
6581 69efd4c4 2018-07-18 stsp return err;
6582 69efd4c4 2018-07-18 stsp }
6583 69efd4c4 2018-07-18 stsp
6584 69efd4c4 2018-07-18 stsp static const struct got_error *
6585 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6586 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6587 69efd4c4 2018-07-18 stsp {
6588 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6589 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6590 69efd4c4 2018-07-18 stsp char *path;
6591 a0de39f3 2019-08-09 stsp
6592 a0de39f3 2019-08-09 stsp *new_view = NULL;
6593 69efd4c4 2018-07-18 stsp
6594 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6595 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6596 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6597 e5a0f69f 2018-08-18 stsp
6598 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6599 69efd4c4 2018-07-18 stsp if (err)
6600 69efd4c4 2018-07-18 stsp return err;
6601 69efd4c4 2018-07-18 stsp
6602 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6603 4e97c21c 2020-12-06 stsp path, 0);
6604 ba4f502b 2018-08-04 stsp if (err)
6605 e5a0f69f 2018-08-18 stsp view_close(log_view);
6606 e5a0f69f 2018-08-18 stsp else
6607 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6608 cb2ebc8a 2018-06-23 stsp free(path);
6609 cb2ebc8a 2018-06-23 stsp return err;
6610 ffd1d5e5 2018-06-23 stsp }
6611 ffd1d5e5 2018-06-23 stsp
6612 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6613 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6614 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6615 ffd1d5e5 2018-06-23 stsp {
6616 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6617 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6618 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6619 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6620 ffd1d5e5 2018-06-23 stsp
6621 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6622 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6623 bc573f3b 2021-07-10 stsp
6624 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6625 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6626 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6627 ffd1d5e5 2018-06-23 stsp
6628 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6629 bc573f3b 2021-07-10 stsp if (err)
6630 bc573f3b 2021-07-10 stsp goto done;
6631 bc573f3b 2021-07-10 stsp
6632 bc573f3b 2021-07-10 stsp /*
6633 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6634 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6635 bc573f3b 2021-07-10 stsp * closed on demand.
6636 bc573f3b 2021-07-10 stsp */
6637 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6638 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6639 bc573f3b 2021-07-10 stsp if (err)
6640 bc573f3b 2021-07-10 stsp goto done;
6641 bc573f3b 2021-07-10 stsp s->tree = s->root;
6642 bc573f3b 2021-07-10 stsp
6643 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6644 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6645 ffd1d5e5 2018-06-23 stsp goto done;
6646 ffd1d5e5 2018-06-23 stsp
6647 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6648 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6649 ffd1d5e5 2018-06-23 stsp goto done;
6650 ffd1d5e5 2018-06-23 stsp }
6651 ffd1d5e5 2018-06-23 stsp
6652 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6653 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6654 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6655 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6656 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6657 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6658 9cd7cbd1 2020-12-07 stsp goto done;
6659 9cd7cbd1 2020-12-07 stsp }
6660 9cd7cbd1 2020-12-07 stsp }
6661 fb2756b9 2018-08-04 stsp s->repo = repo;
6662 c0b01bdb 2019-11-08 stsp
6663 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6664 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6665 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6666 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6667 c0b01bdb 2019-11-08 stsp if (err)
6668 c0b01bdb 2019-11-08 stsp goto done;
6669 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6670 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6671 bc573f3b 2021-07-10 stsp if (err)
6672 c0b01bdb 2019-11-08 stsp goto done;
6673 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6674 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6675 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6676 bc573f3b 2021-07-10 stsp if (err)
6677 c0b01bdb 2019-11-08 stsp goto done;
6678 e5a0f69f 2018-08-18 stsp
6679 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6680 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6681 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6682 bc573f3b 2021-07-10 stsp if (err)
6683 11b20872 2019-11-08 stsp goto done;
6684 11b20872 2019-11-08 stsp
6685 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6686 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6687 bc573f3b 2021-07-10 stsp if (err)
6688 c0b01bdb 2019-11-08 stsp goto done;
6689 c0b01bdb 2019-11-08 stsp }
6690 c0b01bdb 2019-11-08 stsp
6691 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6692 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6693 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6694 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6695 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6696 ad80ab7b 2018-08-04 stsp done:
6697 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6698 bc573f3b 2021-07-10 stsp if (commit)
6699 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6700 bc573f3b 2021-07-10 stsp if (err)
6701 bc573f3b 2021-07-10 stsp close_tree_view(view);
6702 ad80ab7b 2018-08-04 stsp return err;
6703 ad80ab7b 2018-08-04 stsp }
6704 ad80ab7b 2018-08-04 stsp
6705 e5a0f69f 2018-08-18 stsp static const struct got_error *
6706 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6707 ad80ab7b 2018-08-04 stsp {
6708 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6709 ad80ab7b 2018-08-04 stsp
6710 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6711 fb2756b9 2018-08-04 stsp free(s->tree_label);
6712 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6713 6484ec90 2018-09-29 stsp free(s->commit_id);
6714 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6715 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6716 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6717 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6718 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6719 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6720 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6721 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6722 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6723 ad80ab7b 2018-08-04 stsp free(parent);
6724 ad80ab7b 2018-08-04 stsp
6725 ad80ab7b 2018-08-04 stsp }
6726 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6727 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6728 bc573f3b 2021-07-10 stsp if (s->root)
6729 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6730 7c32bd05 2019-06-22 stsp return NULL;
6731 7c32bd05 2019-06-22 stsp }
6732 7c32bd05 2019-06-22 stsp
6733 7c32bd05 2019-06-22 stsp static const struct got_error *
6734 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6735 7c32bd05 2019-06-22 stsp {
6736 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6737 7c32bd05 2019-06-22 stsp
6738 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6739 7c32bd05 2019-06-22 stsp return NULL;
6740 7c32bd05 2019-06-22 stsp }
6741 7c32bd05 2019-06-22 stsp
6742 7c32bd05 2019-06-22 stsp static int
6743 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6744 7c32bd05 2019-06-22 stsp {
6745 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6746 7c32bd05 2019-06-22 stsp
6747 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6748 56e0773d 2019-11-28 stsp 0) == 0;
6749 7c32bd05 2019-06-22 stsp }
6750 7c32bd05 2019-06-22 stsp
6751 7c32bd05 2019-06-22 stsp static const struct got_error *
6752 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6753 7c32bd05 2019-06-22 stsp {
6754 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6755 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6756 7c32bd05 2019-06-22 stsp
6757 7c32bd05 2019-06-22 stsp if (!view->searching) {
6758 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6759 7c32bd05 2019-06-22 stsp return NULL;
6760 7c32bd05 2019-06-22 stsp }
6761 7c32bd05 2019-06-22 stsp
6762 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6763 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6764 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6765 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6766 56e0773d 2019-11-28 stsp s->selected_entry);
6767 7c32bd05 2019-06-22 stsp else
6768 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6769 56e0773d 2019-11-28 stsp } else {
6770 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6771 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6772 56e0773d 2019-11-28 stsp else
6773 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6774 56e0773d 2019-11-28 stsp s->selected_entry);
6775 7c32bd05 2019-06-22 stsp }
6776 7c32bd05 2019-06-22 stsp } else {
6777 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6778 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6779 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6780 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6781 56e0773d 2019-11-28 stsp else
6782 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6783 7c32bd05 2019-06-22 stsp }
6784 7c32bd05 2019-06-22 stsp
6785 7c32bd05 2019-06-22 stsp while (1) {
6786 56e0773d 2019-11-28 stsp if (te == NULL) {
6787 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6788 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6789 ac66afb8 2019-06-24 stsp return NULL;
6790 ac66afb8 2019-06-24 stsp }
6791 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6792 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6793 56e0773d 2019-11-28 stsp else
6794 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6795 7c32bd05 2019-06-22 stsp }
6796 7c32bd05 2019-06-22 stsp
6797 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6798 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6799 56e0773d 2019-11-28 stsp s->matched_entry = te;
6800 7c32bd05 2019-06-22 stsp break;
6801 7c32bd05 2019-06-22 stsp }
6802 7c32bd05 2019-06-22 stsp
6803 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6804 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6805 56e0773d 2019-11-28 stsp else
6806 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6807 7c32bd05 2019-06-22 stsp }
6808 e5a0f69f 2018-08-18 stsp
6809 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6810 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6811 7c32bd05 2019-06-22 stsp s->selected = 0;
6812 7c32bd05 2019-06-22 stsp }
6813 7c32bd05 2019-06-22 stsp
6814 e5a0f69f 2018-08-18 stsp return NULL;
6815 ad80ab7b 2018-08-04 stsp }
6816 ad80ab7b 2018-08-04 stsp
6817 ad80ab7b 2018-08-04 stsp static const struct got_error *
6818 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6819 ad80ab7b 2018-08-04 stsp {
6820 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6821 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6822 e5a0f69f 2018-08-18 stsp char *parent_path;
6823 ad80ab7b 2018-08-04 stsp
6824 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6825 e5a0f69f 2018-08-18 stsp if (err)
6826 e5a0f69f 2018-08-18 stsp return err;
6827 ffd1d5e5 2018-06-23 stsp
6828 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6829 e5a0f69f 2018-08-18 stsp free(parent_path);
6830 669b5ffa 2018-10-07 stsp
6831 9b058f45 2022-06-30 mark view_border(view);
6832 e5a0f69f 2018-08-18 stsp return err;
6833 94b80cfa 2022-08-01 mark }
6834 94b80cfa 2022-08-01 mark
6835 94b80cfa 2022-08-01 mark static const struct got_error *
6836 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
6837 94b80cfa 2022-08-01 mark {
6838 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
6839 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
6840 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
6841 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
6842 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
6843 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
6844 94b80cfa 2022-08-01 mark
6845 94b80cfa 2022-08-01 mark g = view->gline;
6846 94b80cfa 2022-08-01 mark view->gline = 0;
6847 94b80cfa 2022-08-01 mark
6848 94b80cfa 2022-08-01 mark if (g == 0)
6849 94b80cfa 2022-08-01 mark g = 1;
6850 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
6851 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6852 94b80cfa 2022-08-01 mark
6853 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
6854 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
6855 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
6856 94b80cfa 2022-08-01 mark
6857 94b80cfa 2022-08-01 mark if (*fte != NULL) {
6858 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6859 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
6860 94b80cfa 2022-08-01 mark }
6861 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
6862 94b80cfa 2022-08-01 mark last += off;
6863 94b80cfa 2022-08-01 mark
6864 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
6865 94b80cfa 2022-08-01 mark s->selected = g - first;
6866 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
6867 94b80cfa 2022-08-01 mark }
6868 94b80cfa 2022-08-01 mark
6869 94b80cfa 2022-08-01 mark if (*ste != NULL) {
6870 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
6871 94b80cfa 2022-08-01 mark i += off;
6872 94b80cfa 2022-08-01 mark }
6873 94b80cfa 2022-08-01 mark
6874 94b80cfa 2022-08-01 mark if (i < g) {
6875 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
6876 94b80cfa 2022-08-01 mark if (err)
6877 94b80cfa 2022-08-01 mark return err;
6878 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
6879 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
6880 94b80cfa 2022-08-01 mark first + s->selected < g &&
6881 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
6882 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6883 94b80cfa 2022-08-01 mark first += off;
6884 94b80cfa 2022-08-01 mark s->selected = g - first;
6885 94b80cfa 2022-08-01 mark }
6886 94b80cfa 2022-08-01 mark } else if (i > g)
6887 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
6888 94b80cfa 2022-08-01 mark
6889 94b80cfa 2022-08-01 mark if (g < nlines &&
6890 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6891 94b80cfa 2022-08-01 mark s->selected = g - 1;
6892 94b80cfa 2022-08-01 mark
6893 94b80cfa 2022-08-01 mark return NULL;
6894 e5a0f69f 2018-08-18 stsp }
6895 ce52c690 2018-06-23 stsp
6896 e5a0f69f 2018-08-18 stsp static const struct got_error *
6897 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6898 e5a0f69f 2018-08-18 stsp {
6899 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6900 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6901 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6902 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
6903 ffd1d5e5 2018-06-23 stsp
6904 94b80cfa 2022-08-01 mark if (view->gline)
6905 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
6906 94b80cfa 2022-08-01 mark
6907 e5a0f69f 2018-08-18 stsp switch (ch) {
6908 1e37a5c2 2019-05-12 jcs case 'i':
6909 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6910 640cd7ff 2022-06-22 mark view->count = 0;
6911 1e37a5c2 2019-05-12 jcs break;
6912 5e98fb33 2022-07-22 mark case 'L':
6913 640cd7ff 2022-06-22 mark view->count = 0;
6914 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6915 ffd1d5e5 2018-06-23 stsp break;
6916 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6917 152c1c93 2020-11-29 stsp break;
6918 5e98fb33 2022-07-22 mark case 'R':
6919 640cd7ff 2022-06-22 mark view->count = 0;
6920 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
6921 e4526bf5 2021-09-03 naddy break;
6922 e4526bf5 2021-09-03 naddy case 'g':
6923 e4526bf5 2021-09-03 naddy case KEY_HOME:
6924 e4526bf5 2021-09-03 naddy s->selected = 0;
6925 640cd7ff 2022-06-22 mark view->count = 0;
6926 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6927 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6928 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6929 e4526bf5 2021-09-03 naddy else
6930 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6931 1e37a5c2 2019-05-12 jcs break;
6932 e4526bf5 2021-09-03 naddy case 'G':
6933 9b058f45 2022-06-30 mark case KEY_END: {
6934 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6935 9b058f45 2022-06-30 mark
6936 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6937 9b058f45 2022-06-30 mark --eos; /* border */
6938 e4526bf5 2021-09-03 naddy s->selected = 0;
6939 640cd7ff 2022-06-22 mark view->count = 0;
6940 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6941 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6942 e4526bf5 2021-09-03 naddy if (te == NULL) {
6943 ad055527 2022-07-16 mark if (s->tree != s->root) {
6944 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6945 e4526bf5 2021-09-03 naddy n++;
6946 e4526bf5 2021-09-03 naddy }
6947 e4526bf5 2021-09-03 naddy break;
6948 e4526bf5 2021-09-03 naddy }
6949 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6950 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6951 e4526bf5 2021-09-03 naddy }
6952 e4526bf5 2021-09-03 naddy if (n > 0)
6953 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6954 e4526bf5 2021-09-03 naddy break;
6955 9b058f45 2022-06-30 mark }
6956 1e37a5c2 2019-05-12 jcs case 'k':
6957 1e37a5c2 2019-05-12 jcs case KEY_UP:
6958 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6959 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6960 1e37a5c2 2019-05-12 jcs s->selected--;
6961 fa86c4bf 2020-11-29 stsp break;
6962 1e37a5c2 2019-05-12 jcs }
6963 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6964 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6965 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6966 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6967 640cd7ff 2022-06-22 mark view->count = 0;
6968 1e37a5c2 2019-05-12 jcs break;
6969 83cc4199 2022-06-13 stsp case CTRL('u'):
6970 33c3719a 2022-06-15 stsp case 'u':
6971 83cc4199 2022-06-13 stsp nscroll /= 2;
6972 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6973 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6974 ea025d1d 2020-02-22 naddy case CTRL('b'):
6975 61417565 2022-06-20 mark case 'b':
6976 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6977 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6978 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6979 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6980 fa86c4bf 2020-11-29 stsp } else {
6981 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6982 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6983 fa86c4bf 2020-11-29 stsp }
6984 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6985 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6986 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6987 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6988 640cd7ff 2022-06-22 mark view->count = 0;
6989 1e37a5c2 2019-05-12 jcs break;
6990 1e37a5c2 2019-05-12 jcs case 'j':
6991 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6992 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6993 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6994 1e37a5c2 2019-05-12 jcs s->selected++;
6995 1e37a5c2 2019-05-12 jcs break;
6996 1e37a5c2 2019-05-12 jcs }
6997 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6998 640cd7ff 2022-06-22 mark == NULL) {
6999 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7000 640cd7ff 2022-06-22 mark view->count = 0;
7001 1e37a5c2 2019-05-12 jcs break;
7002 640cd7ff 2022-06-22 mark }
7003 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7004 1e37a5c2 2019-05-12 jcs break;
7005 83cc4199 2022-06-13 stsp case CTRL('d'):
7006 33c3719a 2022-06-15 stsp case 'd':
7007 83cc4199 2022-06-13 stsp nscroll /= 2;
7008 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7009 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7010 ea025d1d 2020-02-22 naddy case CTRL('f'):
7011 61417565 2022-06-20 mark case 'f':
7012 48bb96f0 2022-06-20 naddy case ' ':
7013 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7014 1e37a5c2 2019-05-12 jcs == NULL) {
7015 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7016 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7017 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7018 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7019 640cd7ff 2022-06-22 mark else
7020 640cd7ff 2022-06-22 mark view->count = 0;
7021 1e37a5c2 2019-05-12 jcs break;
7022 1e37a5c2 2019-05-12 jcs }
7023 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7024 1e37a5c2 2019-05-12 jcs break;
7025 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7026 1e37a5c2 2019-05-12 jcs case '\r':
7027 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7028 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7029 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7030 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7031 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7032 640cd7ff 2022-06-22 mark view->count = 0;
7033 1e37a5c2 2019-05-12 jcs break;
7034 640cd7ff 2022-06-22 mark }
7035 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7036 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7037 1e37a5c2 2019-05-12 jcs entry);
7038 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7039 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7040 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7041 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7042 1e37a5c2 2019-05-12 jcs s->selected_entry =
7043 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7044 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7045 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7046 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7047 9b058f45 2022-06-30 mark if (err)
7048 9b058f45 2022-06-30 mark break;
7049 9b058f45 2022-06-30 mark }
7050 1e37a5c2 2019-05-12 jcs free(parent);
7051 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7052 56e0773d 2019-11-28 stsp s->selected_entry))) {
7053 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7054 640cd7ff 2022-06-22 mark view->count = 0;
7055 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7056 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7057 1e37a5c2 2019-05-12 jcs if (err)
7058 1e37a5c2 2019-05-12 jcs break;
7059 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7060 941e9f74 2019-05-21 stsp if (err) {
7061 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7062 1e37a5c2 2019-05-12 jcs break;
7063 1e37a5c2 2019-05-12 jcs }
7064 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7065 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7066 1e37a5c2 2019-05-12 jcs break;
7067 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7068 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7069 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7070 640cd7ff 2022-06-22 mark view->count = 0;
7071 1e37a5c2 2019-05-12 jcs break;
7072 1e37a5c2 2019-05-12 jcs default:
7073 640cd7ff 2022-06-22 mark view->count = 0;
7074 1e37a5c2 2019-05-12 jcs break;
7075 ffd1d5e5 2018-06-23 stsp }
7076 e5a0f69f 2018-08-18 stsp
7077 ffd1d5e5 2018-06-23 stsp return err;
7078 9f7d7167 2018-04-29 stsp }
7079 9f7d7167 2018-04-29 stsp
7080 ffd1d5e5 2018-06-23 stsp __dead static void
7081 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7082 ffd1d5e5 2018-06-23 stsp {
7083 ffd1d5e5 2018-06-23 stsp endwin();
7084 87411fa9 2022-06-16 stsp fprintf(stderr,
7085 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7086 ffd1d5e5 2018-06-23 stsp getprogname());
7087 ffd1d5e5 2018-06-23 stsp exit(1);
7088 ffd1d5e5 2018-06-23 stsp }
7089 ffd1d5e5 2018-06-23 stsp
7090 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7091 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7092 ffd1d5e5 2018-06-23 stsp {
7093 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7094 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7095 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7096 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7097 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7098 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7099 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7100 4e97c21c 2020-12-06 stsp char *label = NULL;
7101 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7102 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7103 ffd1d5e5 2018-06-23 stsp int ch;
7104 5221c383 2018-08-01 stsp struct tog_view *view;
7105 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7106 70ac5f84 2019-03-28 stsp
7107 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7108 ffd1d5e5 2018-06-23 stsp switch (ch) {
7109 ffd1d5e5 2018-06-23 stsp case 'c':
7110 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7111 74283ab8 2020-02-07 stsp break;
7112 74283ab8 2020-02-07 stsp case 'r':
7113 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7114 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7115 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7116 74283ab8 2020-02-07 stsp optarg);
7117 ffd1d5e5 2018-06-23 stsp break;
7118 ffd1d5e5 2018-06-23 stsp default:
7119 e99e2d15 2020-11-24 naddy usage_tree();
7120 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7121 ffd1d5e5 2018-06-23 stsp }
7122 ffd1d5e5 2018-06-23 stsp }
7123 ffd1d5e5 2018-06-23 stsp
7124 ffd1d5e5 2018-06-23 stsp argc -= optind;
7125 ffd1d5e5 2018-06-23 stsp argv += optind;
7126 ffd1d5e5 2018-06-23 stsp
7127 55cccc34 2020-02-20 stsp if (argc > 1)
7128 e99e2d15 2020-11-24 naddy usage_tree();
7129 0ae84acc 2022-06-15 tracey
7130 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7131 0ae84acc 2022-06-15 tracey if (error != NULL)
7132 0ae84acc 2022-06-15 tracey goto done;
7133 74283ab8 2020-02-07 stsp
7134 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7135 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7136 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7137 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7138 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7139 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7140 c156c7a4 2020-12-18 stsp goto done;
7141 55cccc34 2020-02-20 stsp if (worktree)
7142 52185f70 2019-02-05 stsp repo_path =
7143 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7144 55cccc34 2020-02-20 stsp else
7145 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7146 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7147 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7148 c156c7a4 2020-12-18 stsp goto done;
7149 c156c7a4 2020-12-18 stsp }
7150 55cccc34 2020-02-20 stsp }
7151 a915003a 2019-02-05 stsp
7152 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7153 c02c541e 2019-03-29 stsp if (error != NULL)
7154 52185f70 2019-02-05 stsp goto done;
7155 d188b9a6 2019-01-04 stsp
7156 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7157 55cccc34 2020-02-20 stsp repo, worktree);
7158 55cccc34 2020-02-20 stsp if (error)
7159 55cccc34 2020-02-20 stsp goto done;
7160 55cccc34 2020-02-20 stsp
7161 55cccc34 2020-02-20 stsp init_curses();
7162 55cccc34 2020-02-20 stsp
7163 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7164 c02c541e 2019-03-29 stsp if (error)
7165 52185f70 2019-02-05 stsp goto done;
7166 ffd1d5e5 2018-06-23 stsp
7167 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7168 51a10b52 2020-12-26 stsp if (error)
7169 51a10b52 2020-12-26 stsp goto done;
7170 51a10b52 2020-12-26 stsp
7171 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7172 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7173 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7174 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7175 4e97c21c 2020-12-06 stsp if (error)
7176 4e97c21c 2020-12-06 stsp goto done;
7177 4e97c21c 2020-12-06 stsp head_ref_name = label;
7178 4e97c21c 2020-12-06 stsp } else {
7179 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7180 4e97c21c 2020-12-06 stsp if (error == NULL)
7181 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7182 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7183 4e97c21c 2020-12-06 stsp goto done;
7184 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7185 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7186 4e97c21c 2020-12-06 stsp if (error)
7187 4e97c21c 2020-12-06 stsp goto done;
7188 4e97c21c 2020-12-06 stsp }
7189 ffd1d5e5 2018-06-23 stsp
7190 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7191 a44927cc 2022-04-07 stsp if (error)
7192 a44927cc 2022-04-07 stsp goto done;
7193 a44927cc 2022-04-07 stsp
7194 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7195 5221c383 2018-08-01 stsp if (view == NULL) {
7196 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7197 5221c383 2018-08-01 stsp goto done;
7198 5221c383 2018-08-01 stsp }
7199 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7200 ad80ab7b 2018-08-04 stsp if (error)
7201 ad80ab7b 2018-08-04 stsp goto done;
7202 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7203 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7204 d91faf3b 2020-12-01 naddy in_repo_path);
7205 55cccc34 2020-02-20 stsp if (error)
7206 55cccc34 2020-02-20 stsp goto done;
7207 55cccc34 2020-02-20 stsp }
7208 55cccc34 2020-02-20 stsp
7209 55cccc34 2020-02-20 stsp if (worktree) {
7210 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7211 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7212 55cccc34 2020-02-20 stsp worktree = NULL;
7213 55cccc34 2020-02-20 stsp }
7214 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7215 ffd1d5e5 2018-06-23 stsp done:
7216 52185f70 2019-02-05 stsp free(repo_path);
7217 e4a0e26d 2020-02-20 stsp free(cwd);
7218 ffd1d5e5 2018-06-23 stsp free(commit_id);
7219 4e97c21c 2020-12-06 stsp free(label);
7220 486cd271 2020-12-06 stsp if (ref)
7221 486cd271 2020-12-06 stsp got_ref_close(ref);
7222 1d0f4054 2021-06-17 stsp if (repo) {
7223 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7224 1d0f4054 2021-06-17 stsp if (error == NULL)
7225 1d0f4054 2021-06-17 stsp error = close_err;
7226 1d0f4054 2021-06-17 stsp }
7227 0ae84acc 2022-06-15 tracey if (pack_fds) {
7228 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7229 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7230 0ae84acc 2022-06-15 tracey if (error == NULL)
7231 0ae84acc 2022-06-15 tracey error = pack_err;
7232 0ae84acc 2022-06-15 tracey }
7233 51a10b52 2020-12-26 stsp tog_free_refs();
7234 ffd1d5e5 2018-06-23 stsp return error;
7235 6458efa5 2020-11-24 stsp }
7236 6458efa5 2020-11-24 stsp
7237 6458efa5 2020-11-24 stsp static const struct got_error *
7238 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7239 6458efa5 2020-11-24 stsp {
7240 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7241 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7242 6458efa5 2020-11-24 stsp
7243 6458efa5 2020-11-24 stsp s->nrefs = 0;
7244 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7245 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7246 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7247 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7248 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7249 6458efa5 2020-11-24 stsp continue;
7250 6458efa5 2020-11-24 stsp
7251 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7252 6458efa5 2020-11-24 stsp if (re == NULL)
7253 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7254 6458efa5 2020-11-24 stsp
7255 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7256 8924d611 2020-12-26 stsp if (re->ref == NULL)
7257 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7258 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7259 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7260 6458efa5 2020-11-24 stsp }
7261 6458efa5 2020-11-24 stsp
7262 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7263 6458efa5 2020-11-24 stsp return NULL;
7264 6458efa5 2020-11-24 stsp }
7265 6458efa5 2020-11-24 stsp
7266 336075a4 2022-06-25 op static void
7267 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7268 6458efa5 2020-11-24 stsp {
7269 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7270 6458efa5 2020-11-24 stsp
7271 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7272 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7273 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7274 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7275 6458efa5 2020-11-24 stsp free(re);
7276 6458efa5 2020-11-24 stsp }
7277 6458efa5 2020-11-24 stsp }
7278 6458efa5 2020-11-24 stsp
7279 6458efa5 2020-11-24 stsp static const struct got_error *
7280 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7281 6458efa5 2020-11-24 stsp {
7282 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7283 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7284 6458efa5 2020-11-24 stsp
7285 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7286 6458efa5 2020-11-24 stsp s->repo = repo;
7287 6458efa5 2020-11-24 stsp
7288 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7289 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7290 6458efa5 2020-11-24 stsp
7291 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7292 6458efa5 2020-11-24 stsp if (err)
7293 6458efa5 2020-11-24 stsp return err;
7294 34ba6917 2020-11-30 stsp
7295 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7296 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7297 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7298 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7299 6458efa5 2020-11-24 stsp if (err)
7300 6458efa5 2020-11-24 stsp goto done;
7301 6458efa5 2020-11-24 stsp
7302 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7303 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7304 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7305 6458efa5 2020-11-24 stsp if (err)
7306 6458efa5 2020-11-24 stsp goto done;
7307 6458efa5 2020-11-24 stsp
7308 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7309 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7310 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7311 cc488aa7 2022-01-23 stsp if (err)
7312 cc488aa7 2022-01-23 stsp goto done;
7313 cc488aa7 2022-01-23 stsp
7314 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7315 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7316 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7317 6458efa5 2020-11-24 stsp if (err)
7318 6458efa5 2020-11-24 stsp goto done;
7319 6458efa5 2020-11-24 stsp }
7320 6458efa5 2020-11-24 stsp
7321 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7322 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7323 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7324 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7325 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7326 6458efa5 2020-11-24 stsp done:
7327 6458efa5 2020-11-24 stsp if (err)
7328 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7329 6458efa5 2020-11-24 stsp return err;
7330 6458efa5 2020-11-24 stsp }
7331 6458efa5 2020-11-24 stsp
7332 6458efa5 2020-11-24 stsp static const struct got_error *
7333 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7334 6458efa5 2020-11-24 stsp {
7335 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7336 6458efa5 2020-11-24 stsp
7337 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7338 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7339 6458efa5 2020-11-24 stsp
7340 6458efa5 2020-11-24 stsp return NULL;
7341 9f7d7167 2018-04-29 stsp }
7342 ce5b7c56 2019-07-09 stsp
7343 6458efa5 2020-11-24 stsp static const struct got_error *
7344 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7345 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7346 6458efa5 2020-11-24 stsp {
7347 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7348 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7349 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7350 6458efa5 2020-11-24 stsp int obj_type;
7351 6458efa5 2020-11-24 stsp
7352 c42c9805 2020-11-24 stsp *commit_id = NULL;
7353 6458efa5 2020-11-24 stsp
7354 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7355 6458efa5 2020-11-24 stsp if (err)
7356 6458efa5 2020-11-24 stsp return err;
7357 6458efa5 2020-11-24 stsp
7358 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7359 6458efa5 2020-11-24 stsp if (err)
7360 6458efa5 2020-11-24 stsp goto done;
7361 6458efa5 2020-11-24 stsp
7362 6458efa5 2020-11-24 stsp switch (obj_type) {
7363 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7364 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7365 6458efa5 2020-11-24 stsp break;
7366 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7367 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7368 6458efa5 2020-11-24 stsp if (err)
7369 6458efa5 2020-11-24 stsp goto done;
7370 c42c9805 2020-11-24 stsp free(obj_id);
7371 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7372 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7373 c42c9805 2020-11-24 stsp if (err)
7374 6458efa5 2020-11-24 stsp goto done;
7375 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7376 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7377 c42c9805 2020-11-24 stsp goto done;
7378 c42c9805 2020-11-24 stsp }
7379 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7380 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7381 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7382 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7383 c42c9805 2020-11-24 stsp goto done;
7384 c42c9805 2020-11-24 stsp }
7385 6458efa5 2020-11-24 stsp break;
7386 6458efa5 2020-11-24 stsp default:
7387 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7388 c42c9805 2020-11-24 stsp break;
7389 6458efa5 2020-11-24 stsp }
7390 6458efa5 2020-11-24 stsp
7391 c42c9805 2020-11-24 stsp done:
7392 c42c9805 2020-11-24 stsp if (tag)
7393 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7394 c42c9805 2020-11-24 stsp if (err) {
7395 c42c9805 2020-11-24 stsp free(*commit_id);
7396 c42c9805 2020-11-24 stsp *commit_id = NULL;
7397 c42c9805 2020-11-24 stsp }
7398 c42c9805 2020-11-24 stsp return err;
7399 c42c9805 2020-11-24 stsp }
7400 c42c9805 2020-11-24 stsp
7401 c42c9805 2020-11-24 stsp static const struct got_error *
7402 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7403 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7404 c42c9805 2020-11-24 stsp {
7405 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7406 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7407 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7408 c42c9805 2020-11-24 stsp
7409 c42c9805 2020-11-24 stsp *new_view = NULL;
7410 c42c9805 2020-11-24 stsp
7411 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7412 c42c9805 2020-11-24 stsp if (err) {
7413 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7414 c42c9805 2020-11-24 stsp return err;
7415 c42c9805 2020-11-24 stsp else
7416 c42c9805 2020-11-24 stsp return NULL;
7417 c42c9805 2020-11-24 stsp }
7418 c42c9805 2020-11-24 stsp
7419 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7420 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7421 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7422 6458efa5 2020-11-24 stsp goto done;
7423 6458efa5 2020-11-24 stsp }
7424 6458efa5 2020-11-24 stsp
7425 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7426 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7427 6458efa5 2020-11-24 stsp done:
7428 6458efa5 2020-11-24 stsp if (err)
7429 6458efa5 2020-11-24 stsp view_close(log_view);
7430 6458efa5 2020-11-24 stsp else
7431 6458efa5 2020-11-24 stsp *new_view = log_view;
7432 c42c9805 2020-11-24 stsp free(commit_id);
7433 6458efa5 2020-11-24 stsp return err;
7434 6458efa5 2020-11-24 stsp }
7435 6458efa5 2020-11-24 stsp
7436 ce5b7c56 2019-07-09 stsp static void
7437 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7438 6458efa5 2020-11-24 stsp {
7439 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7440 34ba6917 2020-11-30 stsp int i = 0;
7441 6458efa5 2020-11-24 stsp
7442 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7443 6458efa5 2020-11-24 stsp return;
7444 6458efa5 2020-11-24 stsp
7445 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7446 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7447 34ba6917 2020-11-30 stsp if (re == NULL)
7448 34ba6917 2020-11-30 stsp break;
7449 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7450 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7451 6458efa5 2020-11-24 stsp }
7452 6458efa5 2020-11-24 stsp }
7453 6458efa5 2020-11-24 stsp
7454 9b058f45 2022-06-30 mark static const struct got_error *
7455 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7456 6458efa5 2020-11-24 stsp {
7457 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7458 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7459 6458efa5 2020-11-24 stsp int n = 0;
7460 6458efa5 2020-11-24 stsp
7461 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7462 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7463 6458efa5 2020-11-24 stsp else
7464 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7465 6458efa5 2020-11-24 stsp
7466 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7467 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7468 94b80cfa 2022-08-01 mark if (last) {
7469 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7470 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7471 94b80cfa 2022-08-01 mark }
7472 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7473 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7474 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7475 6458efa5 2020-11-24 stsp }
7476 6458efa5 2020-11-24 stsp }
7477 9b058f45 2022-06-30 mark
7478 9b058f45 2022-06-30 mark return NULL;
7479 6458efa5 2020-11-24 stsp }
7480 6458efa5 2020-11-24 stsp
7481 6458efa5 2020-11-24 stsp static const struct got_error *
7482 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7483 6458efa5 2020-11-24 stsp {
7484 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7485 6458efa5 2020-11-24 stsp
7486 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7487 6458efa5 2020-11-24 stsp return NULL;
7488 6458efa5 2020-11-24 stsp }
7489 6458efa5 2020-11-24 stsp
7490 6458efa5 2020-11-24 stsp static int
7491 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7492 6458efa5 2020-11-24 stsp {
7493 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7494 6458efa5 2020-11-24 stsp
7495 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7496 6458efa5 2020-11-24 stsp 0) == 0;
7497 6458efa5 2020-11-24 stsp }
7498 6458efa5 2020-11-24 stsp
7499 6458efa5 2020-11-24 stsp static const struct got_error *
7500 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7501 6458efa5 2020-11-24 stsp {
7502 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7503 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7504 6458efa5 2020-11-24 stsp
7505 6458efa5 2020-11-24 stsp if (!view->searching) {
7506 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7507 6458efa5 2020-11-24 stsp return NULL;
7508 6458efa5 2020-11-24 stsp }
7509 6458efa5 2020-11-24 stsp
7510 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7511 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7512 6458efa5 2020-11-24 stsp if (s->selected_entry)
7513 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7514 6458efa5 2020-11-24 stsp else
7515 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7516 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7517 6458efa5 2020-11-24 stsp } else {
7518 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7519 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7520 6458efa5 2020-11-24 stsp else
7521 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7522 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7523 6458efa5 2020-11-24 stsp }
7524 6458efa5 2020-11-24 stsp } else {
7525 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7526 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7527 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7528 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7529 6458efa5 2020-11-24 stsp else
7530 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7531 6458efa5 2020-11-24 stsp }
7532 6458efa5 2020-11-24 stsp
7533 6458efa5 2020-11-24 stsp while (1) {
7534 6458efa5 2020-11-24 stsp if (re == NULL) {
7535 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7536 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7537 6458efa5 2020-11-24 stsp return NULL;
7538 6458efa5 2020-11-24 stsp }
7539 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7540 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7541 6458efa5 2020-11-24 stsp else
7542 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7543 6458efa5 2020-11-24 stsp }
7544 6458efa5 2020-11-24 stsp
7545 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7546 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7547 6458efa5 2020-11-24 stsp s->matched_entry = re;
7548 6458efa5 2020-11-24 stsp break;
7549 6458efa5 2020-11-24 stsp }
7550 6458efa5 2020-11-24 stsp
7551 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7552 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7553 6458efa5 2020-11-24 stsp else
7554 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7555 6458efa5 2020-11-24 stsp }
7556 6458efa5 2020-11-24 stsp
7557 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7558 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7559 6458efa5 2020-11-24 stsp s->selected = 0;
7560 6458efa5 2020-11-24 stsp }
7561 6458efa5 2020-11-24 stsp
7562 6458efa5 2020-11-24 stsp return NULL;
7563 6458efa5 2020-11-24 stsp }
7564 6458efa5 2020-11-24 stsp
7565 6458efa5 2020-11-24 stsp static const struct got_error *
7566 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7567 6458efa5 2020-11-24 stsp {
7568 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7569 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7570 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7571 6458efa5 2020-11-24 stsp char *line = NULL;
7572 6458efa5 2020-11-24 stsp wchar_t *wline;
7573 6458efa5 2020-11-24 stsp struct tog_color *tc;
7574 6458efa5 2020-11-24 stsp int width, n;
7575 6458efa5 2020-11-24 stsp int limit = view->nlines;
7576 6458efa5 2020-11-24 stsp
7577 6458efa5 2020-11-24 stsp werase(view->window);
7578 6458efa5 2020-11-24 stsp
7579 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7580 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7581 9b058f45 2022-06-30 mark --limit; /* border */
7582 6458efa5 2020-11-24 stsp
7583 6458efa5 2020-11-24 stsp if (limit == 0)
7584 6458efa5 2020-11-24 stsp return NULL;
7585 6458efa5 2020-11-24 stsp
7586 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7587 6458efa5 2020-11-24 stsp
7588 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7589 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7590 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7591 6458efa5 2020-11-24 stsp
7592 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7593 6458efa5 2020-11-24 stsp if (err) {
7594 6458efa5 2020-11-24 stsp free(line);
7595 6458efa5 2020-11-24 stsp return err;
7596 6458efa5 2020-11-24 stsp }
7597 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7598 6458efa5 2020-11-24 stsp wstandout(view->window);
7599 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7600 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7601 6458efa5 2020-11-24 stsp wstandend(view->window);
7602 6458efa5 2020-11-24 stsp free(wline);
7603 6458efa5 2020-11-24 stsp wline = NULL;
7604 6458efa5 2020-11-24 stsp free(line);
7605 6458efa5 2020-11-24 stsp line = NULL;
7606 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7607 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7608 6458efa5 2020-11-24 stsp if (--limit <= 0)
7609 6458efa5 2020-11-24 stsp return NULL;
7610 6458efa5 2020-11-24 stsp
7611 6458efa5 2020-11-24 stsp n = 0;
7612 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7613 6458efa5 2020-11-24 stsp char *line = NULL;
7614 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7615 6458efa5 2020-11-24 stsp
7616 b4996bee 2022-06-16 stsp if (s->show_date) {
7617 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7618 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7619 b4996bee 2022-06-16 stsp struct got_object_id *id;
7620 b4996bee 2022-06-16 stsp struct tm tm;
7621 b4996bee 2022-06-16 stsp time_t t;
7622 b4996bee 2022-06-16 stsp
7623 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7624 b4996bee 2022-06-16 stsp if (err)
7625 b4996bee 2022-06-16 stsp return err;
7626 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7627 b4996bee 2022-06-16 stsp if (err) {
7628 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7629 b4996bee 2022-06-16 stsp free(id);
7630 b4996bee 2022-06-16 stsp return err;
7631 b4996bee 2022-06-16 stsp }
7632 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7633 b4996bee 2022-06-16 stsp id);
7634 b4996bee 2022-06-16 stsp if (err) {
7635 b4996bee 2022-06-16 stsp free(id);
7636 b4996bee 2022-06-16 stsp return err;
7637 b4996bee 2022-06-16 stsp }
7638 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7639 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7640 b4996bee 2022-06-16 stsp } else {
7641 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7642 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7643 b4996bee 2022-06-16 stsp }
7644 b4996bee 2022-06-16 stsp free(id);
7645 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7646 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7647 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7648 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7649 b4996bee 2022-06-16 stsp }
7650 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7651 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7652 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7653 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7654 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7655 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7656 6458efa5 2020-11-24 stsp struct got_object_id *id;
7657 6458efa5 2020-11-24 stsp char *id_str;
7658 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7659 6458efa5 2020-11-24 stsp if (err)
7660 6458efa5 2020-11-24 stsp return err;
7661 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7662 6458efa5 2020-11-24 stsp if (err) {
7663 6458efa5 2020-11-24 stsp free(id);
7664 6458efa5 2020-11-24 stsp return err;
7665 6458efa5 2020-11-24 stsp }
7666 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7667 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7668 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7669 6458efa5 2020-11-24 stsp free(id);
7670 6458efa5 2020-11-24 stsp free(id_str);
7671 6458efa5 2020-11-24 stsp return err;
7672 6458efa5 2020-11-24 stsp }
7673 6458efa5 2020-11-24 stsp free(id);
7674 6458efa5 2020-11-24 stsp free(id_str);
7675 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7676 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7677 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7678 6458efa5 2020-11-24 stsp
7679 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7680 ccda2f4d 2022-06-16 stsp 0, 0);
7681 6458efa5 2020-11-24 stsp if (err) {
7682 6458efa5 2020-11-24 stsp free(line);
7683 6458efa5 2020-11-24 stsp return err;
7684 6458efa5 2020-11-24 stsp }
7685 6458efa5 2020-11-24 stsp if (n == s->selected) {
7686 6458efa5 2020-11-24 stsp if (view->focussed)
7687 6458efa5 2020-11-24 stsp wstandout(view->window);
7688 6458efa5 2020-11-24 stsp s->selected_entry = re;
7689 6458efa5 2020-11-24 stsp }
7690 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7691 6458efa5 2020-11-24 stsp if (tc)
7692 6458efa5 2020-11-24 stsp wattr_on(view->window,
7693 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7694 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7695 6458efa5 2020-11-24 stsp if (tc)
7696 6458efa5 2020-11-24 stsp wattr_off(view->window,
7697 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7698 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7699 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7700 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7701 6458efa5 2020-11-24 stsp wstandend(view->window);
7702 6458efa5 2020-11-24 stsp free(line);
7703 6458efa5 2020-11-24 stsp free(wline);
7704 6458efa5 2020-11-24 stsp wline = NULL;
7705 6458efa5 2020-11-24 stsp n++;
7706 6458efa5 2020-11-24 stsp s->ndisplayed++;
7707 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7708 6458efa5 2020-11-24 stsp
7709 6458efa5 2020-11-24 stsp limit--;
7710 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7711 6458efa5 2020-11-24 stsp }
7712 6458efa5 2020-11-24 stsp
7713 9b058f45 2022-06-30 mark view_border(view);
7714 6458efa5 2020-11-24 stsp return err;
7715 6458efa5 2020-11-24 stsp }
7716 6458efa5 2020-11-24 stsp
7717 6458efa5 2020-11-24 stsp static const struct got_error *
7718 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7719 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7720 c42c9805 2020-11-24 stsp {
7721 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7722 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7723 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7724 c42c9805 2020-11-24 stsp
7725 c42c9805 2020-11-24 stsp *new_view = NULL;
7726 c42c9805 2020-11-24 stsp
7727 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7728 c42c9805 2020-11-24 stsp if (err) {
7729 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7730 c42c9805 2020-11-24 stsp return err;
7731 c42c9805 2020-11-24 stsp else
7732 c42c9805 2020-11-24 stsp return NULL;
7733 c42c9805 2020-11-24 stsp }
7734 c42c9805 2020-11-24 stsp
7735 c42c9805 2020-11-24 stsp
7736 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7737 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7738 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7739 c42c9805 2020-11-24 stsp goto done;
7740 c42c9805 2020-11-24 stsp }
7741 c42c9805 2020-11-24 stsp
7742 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7743 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7744 c42c9805 2020-11-24 stsp if (err)
7745 c42c9805 2020-11-24 stsp goto done;
7746 c42c9805 2020-11-24 stsp
7747 c42c9805 2020-11-24 stsp *new_view = tree_view;
7748 c42c9805 2020-11-24 stsp done:
7749 c42c9805 2020-11-24 stsp free(commit_id);
7750 c42c9805 2020-11-24 stsp return err;
7751 94b80cfa 2022-08-01 mark }
7752 94b80cfa 2022-08-01 mark
7753 94b80cfa 2022-08-01 mark static const struct got_error *
7754 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
7755 94b80cfa 2022-08-01 mark {
7756 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7757 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
7758 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
7759 94b80cfa 2022-08-01 mark
7760 94b80cfa 2022-08-01 mark g = view->gline;
7761 94b80cfa 2022-08-01 mark view->gline = 0;
7762 94b80cfa 2022-08-01 mark
7763 94b80cfa 2022-08-01 mark if (g == 0)
7764 94b80cfa 2022-08-01 mark g = 1;
7765 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
7766 94b80cfa 2022-08-01 mark g = s->nrefs;
7767 94b80cfa 2022-08-01 mark
7768 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
7769 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
7770 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
7771 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7772 94b80cfa 2022-08-01 mark return NULL;
7773 94b80cfa 2022-08-01 mark }
7774 94b80cfa 2022-08-01 mark
7775 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
7776 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
7777 94b80cfa 2022-08-01 mark if (err)
7778 94b80cfa 2022-08-01 mark return err;
7779 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7780 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
7781 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
7782 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7783 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
7784 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
7785 94b80cfa 2022-08-01 mark
7786 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
7787 94b80cfa 2022-08-01 mark s->selected = g - 1;
7788 94b80cfa 2022-08-01 mark
7789 94b80cfa 2022-08-01 mark return NULL;
7790 94b80cfa 2022-08-01 mark
7791 c42c9805 2020-11-24 stsp }
7792 94b80cfa 2022-08-01 mark
7793 c42c9805 2020-11-24 stsp static const struct got_error *
7794 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7795 6458efa5 2020-11-24 stsp {
7796 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7797 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7798 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7799 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
7800 6458efa5 2020-11-24 stsp
7801 94b80cfa 2022-08-01 mark if (view->gline)
7802 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
7803 94b80cfa 2022-08-01 mark
7804 6458efa5 2020-11-24 stsp switch (ch) {
7805 6458efa5 2020-11-24 stsp case 'i':
7806 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7807 640cd7ff 2022-06-22 mark view->count = 0;
7808 7f66531d 2021-11-16 stsp break;
7809 b4996bee 2022-06-16 stsp case 'm':
7810 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7811 640cd7ff 2022-06-22 mark view->count = 0;
7812 b4996bee 2022-06-16 stsp break;
7813 07a065fe 2021-11-20 stsp case 'o':
7814 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7815 640cd7ff 2022-06-22 mark view->count = 0;
7816 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7817 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7818 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7819 50617b77 2021-11-20 stsp if (err)
7820 50617b77 2021-11-20 stsp break;
7821 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7822 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7823 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7824 7f66531d 2021-11-16 stsp if (err)
7825 7f66531d 2021-11-16 stsp break;
7826 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7827 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7828 6458efa5 2020-11-24 stsp break;
7829 6458efa5 2020-11-24 stsp case KEY_ENTER:
7830 6458efa5 2020-11-24 stsp case '\r':
7831 640cd7ff 2022-06-22 mark view->count = 0;
7832 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7833 9b058f45 2022-06-30 mark break;
7834 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7835 6458efa5 2020-11-24 stsp break;
7836 5e98fb33 2022-07-22 mark case 'T':
7837 640cd7ff 2022-06-22 mark view->count = 0;
7838 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7839 c42c9805 2020-11-24 stsp break;
7840 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
7841 c42c9805 2020-11-24 stsp break;
7842 e4526bf5 2021-09-03 naddy case 'g':
7843 e4526bf5 2021-09-03 naddy case KEY_HOME:
7844 e4526bf5 2021-09-03 naddy s->selected = 0;
7845 640cd7ff 2022-06-22 mark view->count = 0;
7846 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7847 e4526bf5 2021-09-03 naddy break;
7848 e4526bf5 2021-09-03 naddy case 'G':
7849 9b058f45 2022-06-30 mark case KEY_END: {
7850 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7851 9b058f45 2022-06-30 mark
7852 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7853 9b058f45 2022-06-30 mark --eos; /* border */
7854 e4526bf5 2021-09-03 naddy s->selected = 0;
7855 640cd7ff 2022-06-22 mark view->count = 0;
7856 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7857 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7858 e4526bf5 2021-09-03 naddy if (re == NULL)
7859 e4526bf5 2021-09-03 naddy break;
7860 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7861 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7862 e4526bf5 2021-09-03 naddy }
7863 e4526bf5 2021-09-03 naddy if (n > 0)
7864 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7865 e4526bf5 2021-09-03 naddy break;
7866 9b058f45 2022-06-30 mark }
7867 6458efa5 2020-11-24 stsp case 'k':
7868 6458efa5 2020-11-24 stsp case KEY_UP:
7869 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7870 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7871 6458efa5 2020-11-24 stsp s->selected--;
7872 6458efa5 2020-11-24 stsp break;
7873 34ba6917 2020-11-30 stsp }
7874 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7875 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7876 640cd7ff 2022-06-22 mark view->count = 0;
7877 6458efa5 2020-11-24 stsp break;
7878 83cc4199 2022-06-13 stsp case CTRL('u'):
7879 33c3719a 2022-06-15 stsp case 'u':
7880 83cc4199 2022-06-13 stsp nscroll /= 2;
7881 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7882 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7883 6458efa5 2020-11-24 stsp case CTRL('b'):
7884 61417565 2022-06-20 mark case 'b':
7885 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7886 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7887 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7888 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7889 640cd7ff 2022-06-22 mark view->count = 0;
7890 6458efa5 2020-11-24 stsp break;
7891 6458efa5 2020-11-24 stsp case 'j':
7892 6458efa5 2020-11-24 stsp case KEY_DOWN:
7893 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7894 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7895 6458efa5 2020-11-24 stsp s->selected++;
7896 6458efa5 2020-11-24 stsp break;
7897 6458efa5 2020-11-24 stsp }
7898 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7899 6458efa5 2020-11-24 stsp /* can't scroll any further */
7900 640cd7ff 2022-06-22 mark view->count = 0;
7901 6458efa5 2020-11-24 stsp break;
7902 640cd7ff 2022-06-22 mark }
7903 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7904 6458efa5 2020-11-24 stsp break;
7905 83cc4199 2022-06-13 stsp case CTRL('d'):
7906 33c3719a 2022-06-15 stsp case 'd':
7907 83cc4199 2022-06-13 stsp nscroll /= 2;
7908 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7909 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7910 6458efa5 2020-11-24 stsp case CTRL('f'):
7911 61417565 2022-06-20 mark case 'f':
7912 48bb96f0 2022-06-20 naddy case ' ':
7913 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7914 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7915 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7916 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7917 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7918 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7919 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7920 640cd7ff 2022-06-22 mark view->count = 0;
7921 6458efa5 2020-11-24 stsp break;
7922 6458efa5 2020-11-24 stsp }
7923 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7924 6458efa5 2020-11-24 stsp break;
7925 6458efa5 2020-11-24 stsp case CTRL('l'):
7926 640cd7ff 2022-06-22 mark view->count = 0;
7927 8924d611 2020-12-26 stsp tog_free_refs();
7928 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7929 8924d611 2020-12-26 stsp if (err)
7930 8924d611 2020-12-26 stsp break;
7931 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7932 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7933 6458efa5 2020-11-24 stsp break;
7934 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7935 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7936 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7937 6458efa5 2020-11-24 stsp break;
7938 6458efa5 2020-11-24 stsp default:
7939 640cd7ff 2022-06-22 mark view->count = 0;
7940 6458efa5 2020-11-24 stsp break;
7941 6458efa5 2020-11-24 stsp }
7942 6458efa5 2020-11-24 stsp
7943 6458efa5 2020-11-24 stsp return err;
7944 6458efa5 2020-11-24 stsp }
7945 6458efa5 2020-11-24 stsp
7946 6458efa5 2020-11-24 stsp __dead static void
7947 6458efa5 2020-11-24 stsp usage_ref(void)
7948 6458efa5 2020-11-24 stsp {
7949 6458efa5 2020-11-24 stsp endwin();
7950 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7951 6458efa5 2020-11-24 stsp getprogname());
7952 6458efa5 2020-11-24 stsp exit(1);
7953 6458efa5 2020-11-24 stsp }
7954 6458efa5 2020-11-24 stsp
7955 6458efa5 2020-11-24 stsp static const struct got_error *
7956 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7957 6458efa5 2020-11-24 stsp {
7958 6458efa5 2020-11-24 stsp const struct got_error *error;
7959 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7960 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7961 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7962 6458efa5 2020-11-24 stsp int ch;
7963 6458efa5 2020-11-24 stsp struct tog_view *view;
7964 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7965 6458efa5 2020-11-24 stsp
7966 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7967 6458efa5 2020-11-24 stsp switch (ch) {
7968 6458efa5 2020-11-24 stsp case 'r':
7969 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7970 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7971 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7972 6458efa5 2020-11-24 stsp optarg);
7973 6458efa5 2020-11-24 stsp break;
7974 6458efa5 2020-11-24 stsp default:
7975 e99e2d15 2020-11-24 naddy usage_ref();
7976 6458efa5 2020-11-24 stsp /* NOTREACHED */
7977 6458efa5 2020-11-24 stsp }
7978 6458efa5 2020-11-24 stsp }
7979 6458efa5 2020-11-24 stsp
7980 6458efa5 2020-11-24 stsp argc -= optind;
7981 6458efa5 2020-11-24 stsp argv += optind;
7982 6458efa5 2020-11-24 stsp
7983 6458efa5 2020-11-24 stsp if (argc > 1)
7984 e99e2d15 2020-11-24 naddy usage_ref();
7985 0ae84acc 2022-06-15 tracey
7986 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7987 0ae84acc 2022-06-15 tracey if (error != NULL)
7988 0ae84acc 2022-06-15 tracey goto done;
7989 6458efa5 2020-11-24 stsp
7990 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7991 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7992 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7993 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7994 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7995 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7996 c156c7a4 2020-12-18 stsp goto done;
7997 6458efa5 2020-11-24 stsp if (worktree)
7998 6458efa5 2020-11-24 stsp repo_path =
7999 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8000 6458efa5 2020-11-24 stsp else
8001 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8002 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8003 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8004 c156c7a4 2020-12-18 stsp goto done;
8005 c156c7a4 2020-12-18 stsp }
8006 6458efa5 2020-11-24 stsp }
8007 6458efa5 2020-11-24 stsp
8008 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8009 6458efa5 2020-11-24 stsp if (error != NULL)
8010 6458efa5 2020-11-24 stsp goto done;
8011 6458efa5 2020-11-24 stsp
8012 6458efa5 2020-11-24 stsp init_curses();
8013 6458efa5 2020-11-24 stsp
8014 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8015 51a10b52 2020-12-26 stsp if (error)
8016 51a10b52 2020-12-26 stsp goto done;
8017 51a10b52 2020-12-26 stsp
8018 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8019 6458efa5 2020-11-24 stsp if (error)
8020 6458efa5 2020-11-24 stsp goto done;
8021 6458efa5 2020-11-24 stsp
8022 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8023 6458efa5 2020-11-24 stsp if (view == NULL) {
8024 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8025 6458efa5 2020-11-24 stsp goto done;
8026 6458efa5 2020-11-24 stsp }
8027 6458efa5 2020-11-24 stsp
8028 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8029 6458efa5 2020-11-24 stsp if (error)
8030 6458efa5 2020-11-24 stsp goto done;
8031 6458efa5 2020-11-24 stsp
8032 6458efa5 2020-11-24 stsp if (worktree) {
8033 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8034 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8035 6458efa5 2020-11-24 stsp worktree = NULL;
8036 6458efa5 2020-11-24 stsp }
8037 6458efa5 2020-11-24 stsp error = view_loop(view);
8038 6458efa5 2020-11-24 stsp done:
8039 6458efa5 2020-11-24 stsp free(repo_path);
8040 6458efa5 2020-11-24 stsp free(cwd);
8041 1d0f4054 2021-06-17 stsp if (repo) {
8042 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8043 1d0f4054 2021-06-17 stsp if (close_err)
8044 1d0f4054 2021-06-17 stsp error = close_err;
8045 1d0f4054 2021-06-17 stsp }
8046 0ae84acc 2022-06-15 tracey if (pack_fds) {
8047 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8048 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8049 0ae84acc 2022-06-15 tracey if (error == NULL)
8050 0ae84acc 2022-06-15 tracey error = pack_err;
8051 0ae84acc 2022-06-15 tracey }
8052 51a10b52 2020-12-26 stsp tog_free_refs();
8053 6458efa5 2020-11-24 stsp return error;
8054 6458efa5 2020-11-24 stsp }
8055 6458efa5 2020-11-24 stsp
8056 136e2bd4 2022-07-23 mark static const struct got_error *
8057 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8058 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8059 136e2bd4 2022-07-23 mark {
8060 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8061 136e2bd4 2022-07-23 mark
8062 136e2bd4 2022-07-23 mark *new_view = NULL;
8063 136e2bd4 2022-07-23 mark
8064 136e2bd4 2022-07-23 mark switch (request) {
8065 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8066 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8067 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8068 136e2bd4 2022-07-23 mark
8069 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8070 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8071 136e2bd4 2022-07-23 mark view, s->repo);
8072 136e2bd4 2022-07-23 mark } else
8073 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8074 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8075 136e2bd4 2022-07-23 mark break;
8076 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8077 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8078 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8079 136e2bd4 2022-07-23 mark
8080 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8081 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8082 136e2bd4 2022-07-23 mark s->repo);
8083 136e2bd4 2022-07-23 mark } else
8084 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8085 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8086 136e2bd4 2022-07-23 mark break;
8087 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8088 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8089 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8090 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8091 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8092 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8093 136e2bd4 2022-07-23 mark &view->state.tree);
8094 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8095 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8096 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8097 136e2bd4 2022-07-23 mark view->state.ref.repo);
8098 136e2bd4 2022-07-23 mark else
8099 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8100 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8101 136e2bd4 2022-07-23 mark break;
8102 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8103 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8104 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8105 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8106 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8107 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8108 136e2bd4 2022-07-23 mark view->state.log.repo);
8109 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8110 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8111 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8112 136e2bd4 2022-07-23 mark view->state.ref.repo);
8113 136e2bd4 2022-07-23 mark else
8114 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8115 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8116 136e2bd4 2022-07-23 mark break;
8117 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8118 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8119 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8120 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
8121 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8122 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
8123 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8124 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
8125 136e2bd4 2022-07-23 mark else
8126 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
8127 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8128 136e2bd4 2022-07-23 mark if (err)
8129 136e2bd4 2022-07-23 mark view_close(*new_view);
8130 136e2bd4 2022-07-23 mark break;
8131 136e2bd4 2022-07-23 mark default:
8132 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8133 136e2bd4 2022-07-23 mark }
8134 136e2bd4 2022-07-23 mark
8135 136e2bd4 2022-07-23 mark return err;
8136 136e2bd4 2022-07-23 mark }
8137 136e2bd4 2022-07-23 mark
8138 9b058f45 2022-06-30 mark /*
8139 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8140 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8141 9b058f45 2022-06-30 mark */
8142 6458efa5 2020-11-24 stsp static void
8143 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8144 9b058f45 2022-06-30 mark {
8145 9b058f45 2022-06-30 mark switch (view->type) {
8146 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8147 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8148 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8149 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8150 9b058f45 2022-06-30 mark 1);
8151 9b058f45 2022-06-30 mark break;
8152 9b058f45 2022-06-30 mark }
8153 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8154 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8155 9b058f45 2022-06-30 mark else
8156 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8157 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8158 9b058f45 2022-06-30 mark break;
8159 9b058f45 2022-06-30 mark }
8160 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8161 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8162 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8163 9b058f45 2022-06-30 mark break;
8164 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8165 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8166 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8167 9b058f45 2022-06-30 mark break;
8168 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8169 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8170 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8171 9b058f45 2022-06-30 mark break;
8172 9b058f45 2022-06-30 mark default:
8173 9b058f45 2022-06-30 mark break;
8174 9b058f45 2022-06-30 mark }
8175 9b058f45 2022-06-30 mark
8176 9b058f45 2022-06-30 mark view->offset = 0;
8177 9b058f45 2022-06-30 mark }
8178 9b058f45 2022-06-30 mark
8179 9b058f45 2022-06-30 mark /*
8180 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8181 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8182 9b058f45 2022-06-30 mark */
8183 9b058f45 2022-06-30 mark static const struct got_error *
8184 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8185 9b058f45 2022-06-30 mark {
8186 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8187 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8188 9b058f45 2022-06-30 mark int *selected = NULL;
8189 9b058f45 2022-06-30 mark int header, offset;
8190 9b058f45 2022-06-30 mark
8191 9b058f45 2022-06-30 mark switch (view->type) {
8192 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8193 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8194 9b058f45 2022-06-30 mark header = 3;
8195 9b058f45 2022-06-30 mark scrolld = NULL;
8196 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8197 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8198 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8199 9b058f45 2022-06-30 mark s->selected_line -= offset;
8200 9b058f45 2022-06-30 mark view->offset = offset;
8201 9b058f45 2022-06-30 mark }
8202 9b058f45 2022-06-30 mark break;
8203 9b058f45 2022-06-30 mark }
8204 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8205 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8206 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8207 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8208 9b058f45 2022-06-30 mark selected = &s->selected;
8209 9b058f45 2022-06-30 mark break;
8210 9b058f45 2022-06-30 mark }
8211 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8212 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8213 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8214 9b058f45 2022-06-30 mark header = 3;
8215 9b058f45 2022-06-30 mark selected = &s->selected;
8216 9b058f45 2022-06-30 mark break;
8217 9b058f45 2022-06-30 mark }
8218 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8219 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8220 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8221 9b058f45 2022-06-30 mark header = 5;
8222 9b058f45 2022-06-30 mark selected = &s->selected;
8223 9b058f45 2022-06-30 mark break;
8224 9b058f45 2022-06-30 mark }
8225 9b058f45 2022-06-30 mark default:
8226 9b058f45 2022-06-30 mark selected = NULL;
8227 9b058f45 2022-06-30 mark scrolld = NULL;
8228 9b058f45 2022-06-30 mark header = 0;
8229 9b058f45 2022-06-30 mark break;
8230 9b058f45 2022-06-30 mark }
8231 9b058f45 2022-06-30 mark
8232 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8233 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8234 9b058f45 2022-06-30 mark view->offset = offset;
8235 9b058f45 2022-06-30 mark if (scrolld && offset) {
8236 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8237 9b058f45 2022-06-30 mark *selected -= offset;
8238 9b058f45 2022-06-30 mark }
8239 9b058f45 2022-06-30 mark }
8240 9b058f45 2022-06-30 mark
8241 9b058f45 2022-06-30 mark return err;
8242 9b058f45 2022-06-30 mark }
8243 9b058f45 2022-06-30 mark
8244 9b058f45 2022-06-30 mark static void
8245 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8246 ce5b7c56 2019-07-09 stsp {
8247 6059809a 2020-12-17 stsp size_t i;
8248 9f7d7167 2018-04-29 stsp
8249 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8250 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8251 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8252 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8253 ce5b7c56 2019-07-09 stsp }
8254 6879ba42 2020-10-01 naddy fputc('\n', fp);
8255 ce5b7c56 2019-07-09 stsp }
8256 ce5b7c56 2019-07-09 stsp
8257 4ed7e80c 2018-05-20 stsp __dead static void
8258 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8259 9f7d7167 2018-04-29 stsp {
8260 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8261 6879ba42 2020-10-01 naddy
8262 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8263 6879ba42 2020-10-01 naddy getprogname());
8264 6879ba42 2020-10-01 naddy if (hflag) {
8265 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8266 6879ba42 2020-10-01 naddy list_commands(fp);
8267 ee85c5e8 2020-02-29 stsp }
8268 6879ba42 2020-10-01 naddy exit(status);
8269 9f7d7167 2018-04-29 stsp }
8270 9f7d7167 2018-04-29 stsp
8271 c2301be8 2018-04-30 stsp static char **
8272 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8273 c2301be8 2018-04-30 stsp {
8274 ee85c5e8 2020-02-29 stsp va_list ap;
8275 c2301be8 2018-04-30 stsp char **argv;
8276 ee85c5e8 2020-02-29 stsp int i;
8277 c2301be8 2018-04-30 stsp
8278 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8279 ee85c5e8 2020-02-29 stsp
8280 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8281 c2301be8 2018-04-30 stsp if (argv == NULL)
8282 c2301be8 2018-04-30 stsp err(1, "calloc");
8283 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8284 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8285 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8286 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8287 c2301be8 2018-04-30 stsp }
8288 c2301be8 2018-04-30 stsp
8289 ee85c5e8 2020-02-29 stsp va_end(ap);
8290 c2301be8 2018-04-30 stsp return argv;
8291 ee85c5e8 2020-02-29 stsp }
8292 ee85c5e8 2020-02-29 stsp
8293 ee85c5e8 2020-02-29 stsp /*
8294 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8295 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8296 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8297 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8298 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8299 ee85c5e8 2020-02-29 stsp */
8300 ee85c5e8 2020-02-29 stsp static const struct got_error *
8301 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8302 ee85c5e8 2020-02-29 stsp {
8303 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8304 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8305 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8306 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8307 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8308 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8309 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8310 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8311 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8312 84de9106 2020-12-26 stsp
8313 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8314 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8315 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8316 ee85c5e8 2020-02-29 stsp
8317 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8318 0ae84acc 2022-06-15 tracey if (error != NULL)
8319 0ae84acc 2022-06-15 tracey goto done;
8320 0ae84acc 2022-06-15 tracey
8321 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8322 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8323 ee85c5e8 2020-02-29 stsp goto done;
8324 ee85c5e8 2020-02-29 stsp
8325 ee85c5e8 2020-02-29 stsp if (worktree)
8326 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8327 ee85c5e8 2020-02-29 stsp else
8328 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8329 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8330 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8331 ee85c5e8 2020-02-29 stsp goto done;
8332 ee85c5e8 2020-02-29 stsp }
8333 ee85c5e8 2020-02-29 stsp
8334 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8335 ee85c5e8 2020-02-29 stsp if (error != NULL)
8336 ee85c5e8 2020-02-29 stsp goto done;
8337 ee85c5e8 2020-02-29 stsp
8338 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8339 ee85c5e8 2020-02-29 stsp repo, worktree);
8340 ee85c5e8 2020-02-29 stsp if (error)
8341 ee85c5e8 2020-02-29 stsp goto done;
8342 ee85c5e8 2020-02-29 stsp
8343 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8344 84de9106 2020-12-26 stsp if (error)
8345 84de9106 2020-12-26 stsp goto done;
8346 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8347 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8348 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8349 ee85c5e8 2020-02-29 stsp if (error)
8350 ee85c5e8 2020-02-29 stsp goto done;
8351 ee85c5e8 2020-02-29 stsp
8352 ee85c5e8 2020-02-29 stsp if (worktree) {
8353 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8354 ee85c5e8 2020-02-29 stsp worktree = NULL;
8355 ee85c5e8 2020-02-29 stsp }
8356 ee85c5e8 2020-02-29 stsp
8357 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8358 a44927cc 2022-04-07 stsp if (error)
8359 a44927cc 2022-04-07 stsp goto done;
8360 a44927cc 2022-04-07 stsp
8361 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8362 ee85c5e8 2020-02-29 stsp if (error) {
8363 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8364 ee85c5e8 2020-02-29 stsp goto done;
8365 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8366 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8367 6879ba42 2020-10-01 naddy usage(1, 1);
8368 ee85c5e8 2020-02-29 stsp /* not reached */
8369 ee85c5e8 2020-02-29 stsp }
8370 ee85c5e8 2020-02-29 stsp
8371 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8372 ee85c5e8 2020-02-29 stsp if (error)
8373 ee85c5e8 2020-02-29 stsp goto done;
8374 ee85c5e8 2020-02-29 stsp
8375 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8376 ee85c5e8 2020-02-29 stsp argc = 4;
8377 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8378 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8379 ee85c5e8 2020-02-29 stsp done:
8380 1d0f4054 2021-06-17 stsp if (repo) {
8381 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8382 1d0f4054 2021-06-17 stsp if (error == NULL)
8383 1d0f4054 2021-06-17 stsp error = close_err;
8384 1d0f4054 2021-06-17 stsp }
8385 a44927cc 2022-04-07 stsp if (commit)
8386 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8387 ee85c5e8 2020-02-29 stsp if (worktree)
8388 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8389 0ae84acc 2022-06-15 tracey if (pack_fds) {
8390 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8391 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8392 0ae84acc 2022-06-15 tracey if (error == NULL)
8393 0ae84acc 2022-06-15 tracey error = pack_err;
8394 0ae84acc 2022-06-15 tracey }
8395 ee85c5e8 2020-02-29 stsp free(id);
8396 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8397 ee85c5e8 2020-02-29 stsp free(commit_id);
8398 ee85c5e8 2020-02-29 stsp free(cwd);
8399 ee85c5e8 2020-02-29 stsp free(repo_path);
8400 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8401 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8402 ee85c5e8 2020-02-29 stsp int i;
8403 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8404 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8405 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8406 ee85c5e8 2020-02-29 stsp }
8407 87670572 2020-12-26 naddy tog_free_refs();
8408 ee85c5e8 2020-02-29 stsp return error;
8409 c2301be8 2018-04-30 stsp }
8410 c2301be8 2018-04-30 stsp
8411 9f7d7167 2018-04-29 stsp int
8412 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8413 9f7d7167 2018-04-29 stsp {
8414 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8415 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8416 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8417 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8418 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8419 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8420 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8421 83cd27f8 2020-01-13 stsp };
8422 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8423 9f7d7167 2018-04-29 stsp
8424 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8425 9f7d7167 2018-04-29 stsp
8426 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8427 9f7d7167 2018-04-29 stsp switch (ch) {
8428 9f7d7167 2018-04-29 stsp case 'h':
8429 9f7d7167 2018-04-29 stsp hflag = 1;
8430 9f7d7167 2018-04-29 stsp break;
8431 53ccebc2 2019-07-30 stsp case 'V':
8432 53ccebc2 2019-07-30 stsp Vflag = 1;
8433 53ccebc2 2019-07-30 stsp break;
8434 9f7d7167 2018-04-29 stsp default:
8435 6879ba42 2020-10-01 naddy usage(hflag, 1);
8436 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8437 9f7d7167 2018-04-29 stsp }
8438 9f7d7167 2018-04-29 stsp }
8439 9f7d7167 2018-04-29 stsp
8440 9f7d7167 2018-04-29 stsp argc -= optind;
8441 9f7d7167 2018-04-29 stsp argv += optind;
8442 9814e6a3 2020-09-27 naddy optind = 1;
8443 c2301be8 2018-04-30 stsp optreset = 1;
8444 9f7d7167 2018-04-29 stsp
8445 53ccebc2 2019-07-30 stsp if (Vflag) {
8446 53ccebc2 2019-07-30 stsp got_version_print_str();
8447 6879ba42 2020-10-01 naddy return 0;
8448 53ccebc2 2019-07-30 stsp }
8449 4010e238 2020-12-04 stsp
8450 4010e238 2020-12-04 stsp #ifndef PROFILE
8451 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8452 4010e238 2020-12-04 stsp NULL) == -1)
8453 4010e238 2020-12-04 stsp err(1, "pledge");
8454 4010e238 2020-12-04 stsp #endif
8455 53ccebc2 2019-07-30 stsp
8456 c2301be8 2018-04-30 stsp if (argc == 0) {
8457 f29d3e89 2018-06-23 stsp if (hflag)
8458 6879ba42 2020-10-01 naddy usage(hflag, 0);
8459 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8460 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8461 c2301be8 2018-04-30 stsp argc = 1;
8462 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8463 c2301be8 2018-04-30 stsp } else {
8464 6059809a 2020-12-17 stsp size_t i;
8465 9f7d7167 2018-04-29 stsp
8466 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8467 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8468 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8469 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8470 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8471 9f7d7167 2018-04-29 stsp break;
8472 9f7d7167 2018-04-29 stsp }
8473 9f7d7167 2018-04-29 stsp }
8474 ee85c5e8 2020-02-29 stsp }
8475 3642c4c6 2019-07-09 stsp
8476 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8477 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8478 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8479 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8480 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8481 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8482 917d79a7 2022-07-01 stsp }
8483 917d79a7 2022-07-01 stsp
8484 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8485 ee85c5e8 2020-02-29 stsp if (argc != 1)
8486 6879ba42 2020-10-01 naddy usage(0, 1);
8487 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8488 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8489 ee85c5e8 2020-02-29 stsp } else {
8490 ee85c5e8 2020-02-29 stsp if (hflag)
8491 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8492 ee85c5e8 2020-02-29 stsp else
8493 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8494 9f7d7167 2018-04-29 stsp }
8495 9f7d7167 2018-04-29 stsp
8496 9f7d7167 2018-04-29 stsp endwin();
8497 b46c1e04 2020-09-20 naddy putchar('\n');
8498 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8499 a2f4a359 2020-02-28 stsp int i;
8500 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8501 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8502 a2f4a359 2020-02-28 stsp free(cmd_argv);
8503 a2f4a359 2020-02-28 stsp }
8504 a2f4a359 2020-02-28 stsp
8505 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8506 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8507 9f7d7167 2018-04-29 stsp return 0;
8508 9f7d7167 2018-04-29 stsp }