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 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
317 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
318 3dbaef42 2020-11-24 stsp const char *label1, *label2;
319 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
320 f9d37699 2022-06-28 stsp int fd1, fd2;
321 c7d5c43c 2022-08-04 mark int lineno;
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 c7d5c43c 2022-08-04 mark struct got_diff_line *lines;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey int matched_line;
332 f44b1f58 2020-02-02 tracey int selected_line;
333 15a087fe 2019-02-21 stsp
334 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
335 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
336 b01e7d3b 2018-08-04 stsp };
337 b01e7d3b 2018-08-04 stsp
338 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
340 1a76625f 2018-10-22 stsp
341 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
342 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
343 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
344 1a76625f 2018-10-22 stsp int commits_needed;
345 fb280deb 2021-08-30 stsp int load_all;
346 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
347 1a76625f 2018-10-22 stsp struct commit_queue *commits;
348 1a76625f 2018-10-22 stsp const char *in_repo_path;
349 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
350 1a76625f 2018-10-22 stsp struct got_repository *repo;
351 74467cc8 2022-06-15 stsp int *pack_fds;
352 1a76625f 2018-10-22 stsp int log_complete;
353 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
356 13add988 2019-10-15 stsp int *searching;
357 13add988 2019-10-15 stsp int *search_next_done;
358 13add988 2019-10-15 stsp regex_t *regex;
359 1a76625f 2018-10-22 stsp };
360 1a76625f 2018-10-22 stsp
361 1a76625f 2018-10-22 stsp struct tog_log_view_state {
362 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
366 b01e7d3b 2018-08-04 stsp int selected;
367 b01e7d3b 2018-08-04 stsp char *in_repo_path;
368 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
369 b672a97a 2020-01-27 stsp int log_branches;
370 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
371 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
372 1a76625f 2018-10-22 stsp sig_atomic_t quit;
373 1a76625f 2018-10-22 stsp pthread_t thread;
374 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
375 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
376 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
377 11b20872 2019-11-08 stsp struct tog_colors colors;
378 10aab77f 2022-07-19 op int use_committer;
379 ba4f502b 2018-08-04 stsp };
380 11b20872 2019-11-08 stsp
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
389 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
390 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
391 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
395 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
396 ba4f502b 2018-08-04 stsp
397 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
398 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
399 e9424729 2018-08-04 stsp int nlines;
400 e9424729 2018-08-04 stsp
401 e9424729 2018-08-04 stsp struct tog_view *view;
402 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
403 e9424729 2018-08-04 stsp int *quit;
404 e9424729 2018-08-04 stsp };
405 e9424729 2018-08-04 stsp
406 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
407 e9424729 2018-08-04 stsp const char *path;
408 e9424729 2018-08-04 stsp struct got_repository *repo;
409 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
410 e9424729 2018-08-04 stsp int *complete;
411 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
412 fc06ba56 2019-08-22 stsp void *cancel_arg;
413 e9424729 2018-08-04 stsp };
414 e9424729 2018-08-04 stsp
415 e9424729 2018-08-04 stsp struct tog_blame {
416 e9424729 2018-08-04 stsp FILE *f;
417 be659d10 2020-11-18 stsp off_t filesize;
418 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
419 6fcac457 2018-11-19 stsp int nlines;
420 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
421 e9424729 2018-08-04 stsp pthread_t thread;
422 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
423 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
424 e9424729 2018-08-04 stsp const char *path;
425 0ae84acc 2022-06-15 tracey int *pack_fds;
426 e9424729 2018-08-04 stsp };
427 e9424729 2018-08-04 stsp
428 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
429 7cbe629d 2018-08-04 stsp int first_displayed_line;
430 7cbe629d 2018-08-04 stsp int last_displayed_line;
431 7cbe629d 2018-08-04 stsp int selected_line;
432 c0f61fa4 2022-07-11 mark int last_diffed_line;
433 7cbe629d 2018-08-04 stsp int blame_complete;
434 e5a0f69f 2018-08-18 stsp int eof;
435 e5a0f69f 2018-08-18 stsp int done;
436 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
437 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
438 e5a0f69f 2018-08-18 stsp char *path;
439 7cbe629d 2018-08-04 stsp struct got_repository *repo;
440 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
441 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
442 e9424729 2018-08-04 stsp struct tog_blame blame;
443 6c4c42e0 2019-06-24 stsp int matched_line;
444 11b20872 2019-11-08 stsp struct tog_colors colors;
445 ad80ab7b 2018-08-04 stsp };
446 ad80ab7b 2018-08-04 stsp
447 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
448 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
449 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
450 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
452 ad80ab7b 2018-08-04 stsp int selected;
453 ad80ab7b 2018-08-04 stsp };
454 ad80ab7b 2018-08-04 stsp
455 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
456 ad80ab7b 2018-08-04 stsp
457 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
458 ad80ab7b 2018-08-04 stsp char *tree_label;
459 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
460 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
466 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
468 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
469 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
470 6458efa5 2020-11-24 stsp struct tog_colors colors;
471 6458efa5 2020-11-24 stsp };
472 6458efa5 2020-11-24 stsp
473 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
474 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
475 6458efa5 2020-11-24 stsp struct got_reference *ref;
476 6458efa5 2020-11-24 stsp int idx;
477 6458efa5 2020-11-24 stsp };
478 6458efa5 2020-11-24 stsp
479 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
480 6458efa5 2020-11-24 stsp
481 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
482 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
483 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
486 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 6458efa5 2020-11-24 stsp struct got_repository *repo;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
489 bddb1296 2019-11-08 stsp struct tog_colors colors;
490 7cbe629d 2018-08-04 stsp };
491 7cbe629d 2018-08-04 stsp
492 669b5ffa 2018-10-07 stsp /*
493 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
494 669b5ffa 2018-10-07 stsp *
495 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
496 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
497 669b5ffa 2018-10-07 stsp * there is enough screen estate.
498 669b5ffa 2018-10-07 stsp *
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
501 669b5ffa 2018-10-07 stsp *
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
504 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
505 669b5ffa 2018-10-07 stsp *
506 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
507 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
508 669b5ffa 2018-10-07 stsp */
509 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
510 669b5ffa 2018-10-07 stsp
511 cc3c9aac 2018-08-01 stsp struct tog_view {
512 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
513 26ed57b2 2018-05-19 stsp WINDOW *window;
514 26ed57b2 2018-05-19 stsp PANEL *panel;
515 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
517 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
518 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
519 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 94b80cfa 2022-08-01 mark int gline, hiline; /* navigate to and highlight this nG line */
521 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
522 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
523 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
524 9970f7fc 2020-12-03 stsp int dying;
525 669b5ffa 2018-10-07 stsp struct tog_view *parent;
526 669b5ffa 2018-10-07 stsp struct tog_view *child;
527 5dc9f4bc 2018-08-04 stsp
528 e78dc838 2020-12-04 stsp /*
529 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
530 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
531 e78dc838 2020-12-04 stsp * between parent and child.
532 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
533 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
534 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
535 e78dc838 2020-12-04 stsp * situations.
536 e78dc838 2020-12-04 stsp */
537 e78dc838 2020-12-04 stsp int focus_child;
538 e78dc838 2020-12-04 stsp
539 9b058f45 2022-06-30 mark enum tog_view_mode mode;
540 5dc9f4bc 2018-08-04 stsp /* type-specific state */
541 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
542 5dc9f4bc 2018-08-04 stsp union {
543 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
544 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
545 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
546 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
547 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
548 5dc9f4bc 2018-08-04 stsp } state;
549 e5a0f69f 2018-08-18 stsp
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
552 e78dc838 2020-12-04 stsp struct tog_view *, int);
553 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
554 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
556 60493ae3 2019-06-20 stsp
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
559 c0c4acc8 2021-01-24 stsp int search_started;
560 60493ae3 2019-06-20 stsp int searching;
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
563 60493ae3 2019-06-20 stsp int search_next_done;
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
566 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
567 1803e47f 2019-06-22 stsp regex_t regex;
568 41605754 2020-11-12 stsp regmatch_t regmatch;
569 cc3c9aac 2018-08-01 stsp };
570 cd0acaa7 2018-05-20 stsp
571 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
572 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
573 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_repository *);
575 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
577 e78dc838 2020-12-04 stsp struct tog_view *, int);
578 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp
583 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
585 78756c87 2020-11-24 stsp const char *, const char *, int);
586 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp
594 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
595 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
596 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 e5a0f69f 2018-08-18 stsp
604 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
605 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
606 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
608 e78dc838 2020-12-04 stsp struct tog_view *, int);
609 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp
613 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
614 6458efa5 2020-11-24 stsp struct got_repository *);
615 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
617 e78dc838 2020-12-04 stsp struct tog_view *, int);
618 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp
622 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
623 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
624 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
626 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp
628 25791caa 2018-10-24 stsp static void
629 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp {
631 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
632 83baff54 2019-08-12 stsp }
633 83baff54 2019-08-12 stsp
634 83baff54 2019-08-12 stsp static void
635 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp {
637 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
638 25791caa 2018-10-24 stsp }
639 26ed57b2 2018-05-19 stsp
640 61266923 2020-01-14 stsp static void
641 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp {
643 61266923 2020-01-14 stsp tog_sigcont_received = 1;
644 61266923 2020-01-14 stsp }
645 61266923 2020-01-14 stsp
646 2497f032 2022-05-31 stsp static void
647 2497f032 2022-05-31 stsp tog_sigint(int signo)
648 2497f032 2022-05-31 stsp {
649 2497f032 2022-05-31 stsp tog_sigint_received = 1;
650 2497f032 2022-05-31 stsp }
651 2497f032 2022-05-31 stsp
652 2497f032 2022-05-31 stsp static void
653 2497f032 2022-05-31 stsp tog_sigterm(int signo)
654 2497f032 2022-05-31 stsp {
655 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
656 2497f032 2022-05-31 stsp }
657 2497f032 2022-05-31 stsp
658 2497f032 2022-05-31 stsp static int
659 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
660 2497f032 2022-05-31 stsp {
661 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
662 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
663 2497f032 2022-05-31 stsp }
664 2497f032 2022-05-31 stsp
665 e5a0f69f 2018-08-18 stsp static const struct got_error *
666 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 ea5e7bb5 2018-08-01 stsp {
668 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
669 e5a0f69f 2018-08-18 stsp
670 669b5ffa 2018-10-07 stsp if (view->child) {
671 5629093a 2022-07-11 stsp child_err = view_close(view->child);
672 669b5ffa 2018-10-07 stsp view->child = NULL;
673 669b5ffa 2018-10-07 stsp }
674 e5a0f69f 2018-08-18 stsp if (view->close)
675 e5a0f69f 2018-08-18 stsp err = view->close(view);
676 ea5e7bb5 2018-08-01 stsp if (view->panel)
677 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
678 ea5e7bb5 2018-08-01 stsp if (view->window)
679 ea5e7bb5 2018-08-01 stsp delwin(view->window);
680 ea5e7bb5 2018-08-01 stsp free(view);
681 5629093a 2022-07-11 stsp return err ? err : child_err;
682 ea5e7bb5 2018-08-01 stsp }
683 ea5e7bb5 2018-08-01 stsp
684 ea5e7bb5 2018-08-01 stsp static struct tog_view *
685 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
686 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ea5e7bb5 2018-08-01 stsp {
688 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp
690 ea5e7bb5 2018-08-01 stsp if (view == NULL)
691 ea5e7bb5 2018-08-01 stsp return NULL;
692 ea5e7bb5 2018-08-01 stsp
693 d6b05b5b 2018-08-04 stsp view->type = type;
694 f7d12f7e 2018-08-01 stsp view->lines = LINES;
695 f7d12f7e 2018-08-01 stsp view->cols = COLS;
696 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
697 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
698 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
699 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
700 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
701 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
702 96a765a8 2018-08-04 stsp view_close(view);
703 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp }
705 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
706 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
707 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
708 96a765a8 2018-08-04 stsp view_close(view);
709 ea5e7bb5 2018-08-01 stsp return NULL;
710 ea5e7bb5 2018-08-01 stsp }
711 ea5e7bb5 2018-08-01 stsp
712 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
713 ea5e7bb5 2018-08-01 stsp return view;
714 cdf1ee82 2018-08-01 stsp }
715 cdf1ee82 2018-08-01 stsp
716 0cf4efb1 2018-09-29 stsp static int
717 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 0cf4efb1 2018-09-29 stsp {
719 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 0cf4efb1 2018-09-29 stsp return 0;
721 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
722 5c60c32a 2018-10-18 stsp }
723 5c60c32a 2018-10-18 stsp
724 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
725 9b058f45 2022-06-30 mark static int
726 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
727 9b058f45 2022-06-30 mark {
728 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
729 9b058f45 2022-06-30 mark }
730 9b058f45 2022-06-30 mark
731 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp
733 5c60c32a 2018-10-18 stsp static const struct got_error *
734 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp {
736 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 5c60c32a 2018-10-18 stsp
738 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
740 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
741 3c1dfe12 2022-07-08 mark else
742 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
743 9b058f45 2022-06-30 mark view->begin_x = 0;
744 571ccd73 2022-07-19 mark } else if (!view->resized) {
745 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
746 3c1dfe12 2022-07-08 mark view->cols > 119)
747 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
748 3c1dfe12 2022-07-08 mark else
749 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
750 9b058f45 2022-06-30 mark view->begin_y = 0;
751 9b058f45 2022-06-30 mark }
752 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
753 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
754 5c60c32a 2018-10-18 stsp view->lines = LINES;
755 5c60c32a 2018-10-18 stsp view->cols = COLS;
756 5c60c32a 2018-10-18 stsp err = view_resize(view);
757 5c60c32a 2018-10-18 stsp if (err)
758 5c60c32a 2018-10-18 stsp return err;
759 5c60c32a 2018-10-18 stsp
760 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
762 9b058f45 2022-06-30 mark
763 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
765 5c60c32a 2018-10-18 stsp
766 5c60c32a 2018-10-18 stsp return NULL;
767 5c60c32a 2018-10-18 stsp }
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp static const struct got_error *
770 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
771 5c60c32a 2018-10-18 stsp {
772 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp view->begin_x = 0;
775 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
776 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
777 5c60c32a 2018-10-18 stsp view->ncols = COLS;
778 5c60c32a 2018-10-18 stsp view->lines = LINES;
779 5c60c32a 2018-10-18 stsp view->cols = COLS;
780 5c60c32a 2018-10-18 stsp err = view_resize(view);
781 5c60c32a 2018-10-18 stsp if (err)
782 5c60c32a 2018-10-18 stsp return err;
783 5c60c32a 2018-10-18 stsp
784 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
786 5c60c32a 2018-10-18 stsp
787 5c60c32a 2018-10-18 stsp return NULL;
788 0cf4efb1 2018-09-29 stsp }
789 0cf4efb1 2018-09-29 stsp
790 5c60c32a 2018-10-18 stsp static int
791 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
792 5c60c32a 2018-10-18 stsp {
793 5c60c32a 2018-10-18 stsp return view->parent == NULL;
794 5c60c32a 2018-10-18 stsp }
795 5c60c32a 2018-10-18 stsp
796 6131ff18 2022-06-20 mark static int
797 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
798 6131ff18 2022-06-20 mark {
799 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
800 24b9cfdc 2022-06-30 stsp }
801 24b9cfdc 2022-06-30 stsp
802 24b9cfdc 2022-06-30 stsp static int
803 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
804 24b9cfdc 2022-06-30 stsp {
805 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
806 6131ff18 2022-06-20 mark }
807 6131ff18 2022-06-20 mark
808 49b24ee5 2022-07-03 mark static int
809 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
810 49b24ee5 2022-07-03 mark {
811 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
813 49b24ee5 2022-07-03 mark }
814 49b24ee5 2022-07-03 mark
815 9b058f45 2022-06-30 mark static void
816 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
817 9b058f45 2022-06-30 mark {
818 9b058f45 2022-06-30 mark PANEL *panel;
819 9b058f45 2022-06-30 mark const struct tog_view *view_above;
820 6131ff18 2022-06-20 mark
821 9b058f45 2022-06-30 mark if (view->parent)
822 9b058f45 2022-06-30 mark return view_border(view->parent);
823 9b058f45 2022-06-30 mark
824 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
825 9b058f45 2022-06-30 mark if (panel == NULL)
826 9b058f45 2022-06-30 mark return;
827 9b058f45 2022-06-30 mark
828 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
829 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
831 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
832 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
833 9b058f45 2022-06-30 mark else
834 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
836 9b058f45 2022-06-30 mark }
837 9b058f45 2022-06-30 mark
838 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
840 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
841 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
842 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
843 9b058f45 2022-06-30 mark
844 4d8c2215 2018-08-19 stsp static const struct got_error *
845 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
846 f7d12f7e 2018-08-01 stsp {
847 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
848 9b058f45 2022-06-30 mark int dif, nlines, ncols;
849 f7d12f7e 2018-08-01 stsp
850 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
851 9b058f45 2022-06-30 mark
852 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
853 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
854 0cf4efb1 2018-09-29 stsp else
855 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
856 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
857 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
858 0cf4efb1 2018-09-29 stsp else
859 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
860 6d0fee91 2018-08-01 stsp
861 4dd27a72 2022-06-29 stsp if (view->child) {
862 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
863 9b058f45 2022-06-30 mark
864 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
865 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
866 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
868 0dbbbe90 2022-06-17 op ncols = COLS;
869 0dbbbe90 2022-06-17 op
870 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
871 5c60c32a 2018-10-18 stsp if (view->child->focussed)
872 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
873 5c60c32a 2018-10-18 stsp else
874 5c60c32a 2018-10-18 stsp show_panel(view->panel);
875 5c60c32a 2018-10-18 stsp } else {
876 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
877 0dbbbe90 2022-06-17 op
878 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
879 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
880 9b058f45 2022-06-30 mark }
881 9b058f45 2022-06-30 mark /*
882 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
883 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
884 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
885 9b058f45 2022-06-30 mark */
886 9b058f45 2022-06-30 mark if (hs) {
887 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
888 9b058f45 2022-06-30 mark if (err)
889 9b058f45 2022-06-30 mark return err;
890 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
891 9b058f45 2022-06-30 mark err = offset_selection_down(view);
892 9b058f45 2022-06-30 mark if (err)
893 9b058f45 2022-06-30 mark return err;
894 9b058f45 2022-06-30 mark }
895 9b058f45 2022-06-30 mark view_border(view);
896 9b058f45 2022-06-30 mark update_panels();
897 9b058f45 2022-06-30 mark doupdate();
898 9b058f45 2022-06-30 mark show_panel(view->child->panel);
899 9b058f45 2022-06-30 mark nlines = view->nlines;
900 9b058f45 2022-06-30 mark }
901 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
902 0dbbbe90 2022-06-17 op ncols = COLS;
903 669b5ffa 2018-10-07 stsp
904 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
905 571ccd73 2022-07-19 mark err = view->resize(view, dif);
906 571ccd73 2022-07-19 mark if (err)
907 571ccd73 2022-07-19 mark return err;
908 571ccd73 2022-07-19 mark }
909 571ccd73 2022-07-19 mark
910 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
911 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
912 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
913 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
914 0dbbbe90 2022-06-17 op wclear(view->window);
915 0dbbbe90 2022-06-17 op
916 0dbbbe90 2022-06-17 op view->nlines = nlines;
917 0dbbbe90 2022-06-17 op view->ncols = ncols;
918 0dbbbe90 2022-06-17 op view->lines = LINES;
919 0dbbbe90 2022-06-17 op view->cols = COLS;
920 0dbbbe90 2022-06-17 op
921 5c60c32a 2018-10-18 stsp return NULL;
922 571ccd73 2022-07-19 mark }
923 571ccd73 2022-07-19 mark
924 571ccd73 2022-07-19 mark static const struct got_error *
925 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
926 571ccd73 2022-07-19 mark {
927 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
928 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
929 6fe51fee 2022-07-22 mark int n = 0;
930 571ccd73 2022-07-19 mark
931 6fe51fee 2022-07-22 mark if (s->selected_entry)
932 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
933 6fe51fee 2022-07-22 mark
934 571ccd73 2022-07-19 mark /*
935 571ccd73 2022-07-19 mark * Request commits to account for the increased
936 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
937 571ccd73 2022-07-19 mark */
938 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
939 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
940 571ccd73 2022-07-19 mark err = request_log_commits(view);
941 571ccd73 2022-07-19 mark }
942 571ccd73 2022-07-19 mark
943 571ccd73 2022-07-19 mark return err;
944 d9a7ab53 2022-07-11 mark }
945 d9a7ab53 2022-07-11 mark
946 d9a7ab53 2022-07-11 mark static void
947 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
948 d9a7ab53 2022-07-11 mark {
949 d9a7ab53 2022-07-11 mark if (n == 0)
950 d9a7ab53 2022-07-11 mark return;
951 d9a7ab53 2022-07-11 mark
952 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
953 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
954 d9a7ab53 2022-07-11 mark view->parent->offset += n;
955 d9a7ab53 2022-07-11 mark else
956 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
957 d9a7ab53 2022-07-11 mark } else if (view->offset) {
958 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
959 d9a7ab53 2022-07-11 mark view->offset -= n;
960 d9a7ab53 2022-07-11 mark else
961 d9a7ab53 2022-07-11 mark view->offset = 0;
962 d9a7ab53 2022-07-11 mark }
963 3c1dfe12 2022-07-08 mark }
964 3c1dfe12 2022-07-08 mark
965 3c1dfe12 2022-07-08 mark static const struct got_error *
966 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
967 3c1dfe12 2022-07-08 mark {
968 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
969 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
970 3c1dfe12 2022-07-08 mark
971 3c1dfe12 2022-07-08 mark if (view->parent)
972 3c1dfe12 2022-07-08 mark v = view->parent;
973 3c1dfe12 2022-07-08 mark else
974 3c1dfe12 2022-07-08 mark v = view;
975 3c1dfe12 2022-07-08 mark
976 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
977 3c1dfe12 2022-07-08 mark return NULL;
978 3c1dfe12 2022-07-08 mark
979 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
980 3c1dfe12 2022-07-08 mark
981 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
982 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
983 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
984 3c1dfe12 2022-07-08 mark if (view->parent)
985 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
986 3c1dfe12 2022-07-08 mark else
987 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
988 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
989 3c1dfe12 2022-07-08 mark view->count = 0;
990 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
991 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
992 3c1dfe12 2022-07-08 mark view->count = 0;
993 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
994 3c1dfe12 2022-07-08 mark }
995 3c1dfe12 2022-07-08 mark v->ncols = COLS;
996 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
997 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
998 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
999 3c1dfe12 2022-07-08 mark if (err)
1000 3c1dfe12 2022-07-08 mark return err;
1001 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1002 3c1dfe12 2022-07-08 mark } else {
1003 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1004 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1005 3c1dfe12 2022-07-08 mark if (view->parent)
1006 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1007 3c1dfe12 2022-07-08 mark else
1008 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1009 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1010 3c1dfe12 2022-07-08 mark view->count = 0;
1011 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1012 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1013 3c1dfe12 2022-07-08 mark view->count = 0;
1014 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1015 3c1dfe12 2022-07-08 mark }
1016 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1017 3c1dfe12 2022-07-08 mark }
1018 3c1dfe12 2022-07-08 mark
1019 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1020 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1021 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1022 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1023 3c1dfe12 2022-07-08 mark
1024 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1025 3c1dfe12 2022-07-08 mark if (err)
1026 3c1dfe12 2022-07-08 mark return err;
1027 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1028 3c1dfe12 2022-07-08 mark if (err)
1029 3c1dfe12 2022-07-08 mark return err;
1030 3c1dfe12 2022-07-08 mark
1031 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1032 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1033 3c1dfe12 2022-07-08 mark if (err)
1034 3c1dfe12 2022-07-08 mark return err;
1035 3c1dfe12 2022-07-08 mark }
1036 3c1dfe12 2022-07-08 mark
1037 6fe51fee 2022-07-22 mark if (v->resize)
1038 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1039 6fe51fee 2022-07-22 mark else if (v->child->resize)
1040 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1041 3c1dfe12 2022-07-08 mark
1042 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1043 3c1dfe12 2022-07-08 mark
1044 3c1dfe12 2022-07-08 mark return err;
1045 3c1dfe12 2022-07-08 mark }
1046 3c1dfe12 2022-07-08 mark
1047 3c1dfe12 2022-07-08 mark static void
1048 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1049 3c1dfe12 2022-07-08 mark {
1050 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1051 3c1dfe12 2022-07-08 mark
1052 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1053 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1054 669b5ffa 2018-10-07 stsp }
1055 669b5ffa 2018-10-07 stsp
1056 669b5ffa 2018-10-07 stsp static const struct got_error *
1057 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1058 669b5ffa 2018-10-07 stsp {
1059 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1062 669b5ffa 2018-10-07 stsp return NULL;
1063 669b5ffa 2018-10-07 stsp
1064 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1065 669b5ffa 2018-10-07 stsp view->child = NULL;
1066 669b5ffa 2018-10-07 stsp return err;
1067 669b5ffa 2018-10-07 stsp }
1068 669b5ffa 2018-10-07 stsp
1069 0dbbbe90 2022-06-17 op static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1071 669b5ffa 2018-10-07 stsp {
1072 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1073 3c1dfe12 2022-07-08 mark
1074 669b5ffa 2018-10-07 stsp view->child = child;
1075 669b5ffa 2018-10-07 stsp child->parent = view;
1076 0dbbbe90 2022-06-17 op
1077 3c1dfe12 2022-07-08 mark err = view_resize(view);
1078 3c1dfe12 2022-07-08 mark if (err)
1079 3c1dfe12 2022-07-08 mark return err;
1080 3c1dfe12 2022-07-08 mark
1081 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1082 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1083 3c1dfe12 2022-07-08 mark
1084 3c1dfe12 2022-07-08 mark return err;
1085 bfddd0d9 2018-09-29 stsp }
1086 136e2bd4 2022-07-23 mark
1087 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1088 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1089 136e2bd4 2022-07-23 mark
1090 136e2bd4 2022-07-23 mark static const struct got_error *
1091 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1092 136e2bd4 2022-07-23 mark enum tog_view_type request)
1093 136e2bd4 2022-07-23 mark {
1094 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1095 136e2bd4 2022-07-23 mark const struct got_error *err;
1096 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1097 136e2bd4 2022-07-23 mark
1098 136e2bd4 2022-07-23 mark *requested = NULL;
1099 136e2bd4 2022-07-23 mark
1100 136e2bd4 2022-07-23 mark if (view_is_parent_view(view))
1101 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1102 bfddd0d9 2018-09-29 stsp
1103 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1104 136e2bd4 2022-07-23 mark if (err)
1105 136e2bd4 2022-07-23 mark return err;
1106 136e2bd4 2022-07-23 mark
1107 136e2bd4 2022-07-23 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1108 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1109 136e2bd4 2022-07-23 mark if (err)
1110 136e2bd4 2022-07-23 mark return err;
1111 136e2bd4 2022-07-23 mark }
1112 136e2bd4 2022-07-23 mark
1113 136e2bd4 2022-07-23 mark view->focussed = 0;
1114 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1115 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1116 136e2bd4 2022-07-23 mark new_view->nlines = view->lines - y;
1117 136e2bd4 2022-07-23 mark
1118 136e2bd4 2022-07-23 mark if (view_is_parent_view(view)) {
1119 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1120 136e2bd4 2022-07-23 mark err = view_close_child(view);
1121 136e2bd4 2022-07-23 mark if (err)
1122 136e2bd4 2022-07-23 mark return err;
1123 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1124 136e2bd4 2022-07-23 mark if (err)
1125 136e2bd4 2022-07-23 mark return err;
1126 136e2bd4 2022-07-23 mark view->focus_child = 1;
1127 136e2bd4 2022-07-23 mark } else
1128 136e2bd4 2022-07-23 mark *requested = new_view;
1129 136e2bd4 2022-07-23 mark
1130 136e2bd4 2022-07-23 mark return NULL;
1131 136e2bd4 2022-07-23 mark }
1132 136e2bd4 2022-07-23 mark
1133 34bc9ec9 2019-02-22 stsp static void
1134 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1135 25791caa 2018-10-24 stsp {
1136 25791caa 2018-10-24 stsp int cols, lines;
1137 25791caa 2018-10-24 stsp struct winsize size;
1138 25791caa 2018-10-24 stsp
1139 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1140 25791caa 2018-10-24 stsp cols = 80; /* Default */
1141 25791caa 2018-10-24 stsp lines = 24;
1142 25791caa 2018-10-24 stsp } else {
1143 25791caa 2018-10-24 stsp cols = size.ws_col;
1144 25791caa 2018-10-24 stsp lines = size.ws_row;
1145 25791caa 2018-10-24 stsp }
1146 25791caa 2018-10-24 stsp resize_term(lines, cols);
1147 2b49a8ae 2019-06-22 stsp }
1148 2b49a8ae 2019-06-22 stsp
1149 2b49a8ae 2019-06-22 stsp static const struct got_error *
1150 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1151 2b49a8ae 2019-06-22 stsp {
1152 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1153 9b058f45 2022-06-30 mark struct tog_view *v = view;
1154 2b49a8ae 2019-06-22 stsp char pattern[1024];
1155 2b49a8ae 2019-06-22 stsp int ret;
1156 c0c4acc8 2021-01-24 stsp
1157 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1158 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1159 c0c4acc8 2021-01-24 stsp view->searching = 0;
1160 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1161 c0c4acc8 2021-01-24 stsp }
1162 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1163 2b49a8ae 2019-06-22 stsp
1164 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1165 2b49a8ae 2019-06-22 stsp return NULL;
1166 2b49a8ae 2019-06-22 stsp
1167 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1168 9b058f45 2022-06-30 mark v = view->child;
1169 2b49a8ae 2019-06-22 stsp
1170 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1171 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1172 9b058f45 2022-06-30 mark
1173 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1174 2b49a8ae 2019-06-22 stsp nocbreak();
1175 2b49a8ae 2019-06-22 stsp echo();
1176 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1177 9b058f45 2022-06-30 mark wrefresh(v->window);
1178 2b49a8ae 2019-06-22 stsp cbreak();
1179 2b49a8ae 2019-06-22 stsp noecho();
1180 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1181 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1182 2b49a8ae 2019-06-22 stsp return NULL;
1183 2b49a8ae 2019-06-22 stsp
1184 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1185 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1186 7c32bd05 2019-06-22 stsp if (err) {
1187 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1188 7c32bd05 2019-06-22 stsp return err;
1189 7c32bd05 2019-06-22 stsp }
1190 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1191 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1192 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1193 2b49a8ae 2019-06-22 stsp view->search_next(view);
1194 2b49a8ae 2019-06-22 stsp }
1195 2b49a8ae 2019-06-22 stsp
1196 2b49a8ae 2019-06-22 stsp return NULL;
1197 d2366e29 2022-07-07 mark }
1198 d2366e29 2022-07-07 mark
1199 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1200 d2366e29 2022-07-07 mark static const struct got_error *
1201 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1202 d2366e29 2022-07-07 mark {
1203 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1204 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1205 d2366e29 2022-07-07 mark
1206 d2366e29 2022-07-07 mark if (view->parent)
1207 d2366e29 2022-07-07 mark v = view->parent;
1208 d2366e29 2022-07-07 mark else
1209 d2366e29 2022-07-07 mark v = view;
1210 d2366e29 2022-07-07 mark
1211 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1212 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1213 7532ccda 2022-07-11 mark else
1214 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1215 d2366e29 2022-07-07 mark
1216 7532ccda 2022-07-11 mark if (!v->child)
1217 7532ccda 2022-07-11 mark return NULL;
1218 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1219 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1220 7532ccda 2022-07-11 mark
1221 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1222 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1223 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1224 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1225 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1226 d2366e29 2022-07-07 mark
1227 7532ccda 2022-07-11 mark
1228 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1229 d2366e29 2022-07-07 mark v->ncols = COLS;
1230 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1231 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1232 d2366e29 2022-07-07 mark
1233 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1234 d2366e29 2022-07-07 mark if (err)
1235 d2366e29 2022-07-07 mark return err;
1236 d2366e29 2022-07-07 mark }
1237 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1238 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1239 d2366e29 2022-07-07 mark v->focus_child = 1;
1240 d2366e29 2022-07-07 mark
1241 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1242 d2366e29 2022-07-07 mark if (err)
1243 d2366e29 2022-07-07 mark return err;
1244 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1245 d2366e29 2022-07-07 mark if (err)
1246 d2366e29 2022-07-07 mark return err;
1247 d2366e29 2022-07-07 mark
1248 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1249 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1250 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1251 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1252 279d2047 2022-07-29 stsp if (err)
1253 279d2047 2022-07-29 stsp return err;
1254 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1255 279d2047 2022-07-29 stsp if (err)
1256 279d2047 2022-07-29 stsp return err;
1257 7532ccda 2022-07-11 mark } else {
1258 7532ccda 2022-07-11 mark offset_selection_up(v);
1259 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1260 d2366e29 2022-07-07 mark }
1261 dff91825 2022-07-22 mark if (v->resize)
1262 dff91825 2022-07-22 mark err = v->resize(v, 0);
1263 dff91825 2022-07-22 mark else if (v->child->resize)
1264 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1265 d2366e29 2022-07-07 mark
1266 d2366e29 2022-07-07 mark return err;
1267 0cf4efb1 2018-09-29 stsp }
1268 6d0fee91 2018-08-01 stsp
1269 640cd7ff 2022-06-22 mark /*
1270 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1271 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1272 640cd7ff 2022-06-22 mark */
1273 640cd7ff 2022-06-22 mark static int
1274 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1275 640cd7ff 2022-06-22 mark {
1276 9b058f45 2022-06-30 mark struct tog_view *v = view;
1277 9b058f45 2022-06-30 mark int x, n = 0;
1278 640cd7ff 2022-06-22 mark
1279 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1280 9b058f45 2022-06-30 mark v = view->child;
1281 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1282 9b058f45 2022-06-30 mark v = view->parent;
1283 9b058f45 2022-06-30 mark
1284 640cd7ff 2022-06-22 mark view->count = 0;
1285 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1286 94b80cfa 2022-08-01 mark nodelay(view->window, FALSE);
1287 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1288 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1289 9b058f45 2022-06-30 mark waddch(v->window, ':');
1290 640cd7ff 2022-06-22 mark
1291 640cd7ff 2022-06-22 mark do {
1292 9b058f45 2022-06-30 mark x = getcurx(v->window);
1293 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1294 9b058f45 2022-06-30 mark waddch(v->window, c);
1295 9b058f45 2022-06-30 mark wrefresh(v->window);
1296 9b058f45 2022-06-30 mark }
1297 9b058f45 2022-06-30 mark
1298 640cd7ff 2022-06-22 mark /*
1299 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1300 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1301 640cd7ff 2022-06-22 mark */
1302 640cd7ff 2022-06-22 mark if (n >= 9999999)
1303 640cd7ff 2022-06-22 mark n = 9999999;
1304 640cd7ff 2022-06-22 mark else
1305 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1306 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1307 640cd7ff 2022-06-22 mark
1308 94b80cfa 2022-08-01 mark if (c == 'G' || c == 'g') { /* nG key map */
1309 94b80cfa 2022-08-01 mark view->gline = view->hiline = n;
1310 94b80cfa 2022-08-01 mark n = 0;
1311 94b80cfa 2022-08-01 mark c = 0;
1312 94b80cfa 2022-08-01 mark }
1313 94b80cfa 2022-08-01 mark
1314 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1315 640cd7ff 2022-06-22 mark view->count = n;
1316 640cd7ff 2022-06-22 mark
1317 640cd7ff 2022-06-22 mark return c;
1318 640cd7ff 2022-06-22 mark }
1319 640cd7ff 2022-06-22 mark
1320 0cf4efb1 2018-09-29 stsp static const struct got_error *
1321 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1322 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1323 e5a0f69f 2018-08-18 stsp {
1324 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1325 669b5ffa 2018-10-07 stsp struct tog_view *v;
1326 1a76625f 2018-10-22 stsp int ch, errcode;
1327 e5a0f69f 2018-08-18 stsp
1328 e5a0f69f 2018-08-18 stsp *new = NULL;
1329 8f4ed634 2020-03-26 stsp
1330 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1331 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1332 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1333 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1334 640cd7ff 2022-06-22 mark view->count = 0;
1335 640cd7ff 2022-06-22 mark }
1336 e5a0f69f 2018-08-18 stsp
1337 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1338 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1339 82954512 2020-02-03 stsp if (errcode)
1340 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1341 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1342 3da8ef85 2021-09-21 stsp sched_yield();
1343 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1344 82954512 2020-02-03 stsp if (errcode)
1345 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1346 82954512 2020-02-03 stsp "pthread_mutex_lock");
1347 60493ae3 2019-06-20 stsp view->search_next(view);
1348 60493ae3 2019-06-20 stsp return NULL;
1349 60493ae3 2019-06-20 stsp }
1350 60493ae3 2019-06-20 stsp
1351 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1352 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1353 1a76625f 2018-10-22 stsp if (errcode)
1354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1355 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1356 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1357 a6d37fac 2022-07-03 mark cbreak();
1358 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1359 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1360 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1361 a6d37fac 2022-07-03 mark view->count = 0;
1362 a6d37fac 2022-07-03 mark else
1363 a6d37fac 2022-07-03 mark ch = view->ch;
1364 a6d37fac 2022-07-03 mark } else {
1365 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1366 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1367 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1368 640cd7ff 2022-06-22 mark }
1369 94b80cfa 2022-08-01 mark if (view->hiline && ch != ERR && ch != 0)
1370 94b80cfa 2022-08-01 mark view->hiline = 0; /* key pressed, clear line highlight */
1371 94b80cfa 2022-08-01 mark nodelay(view->window, TRUE);
1372 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1373 1a76625f 2018-10-22 stsp if (errcode)
1374 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1375 25791caa 2018-10-24 stsp
1376 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1377 25791caa 2018-10-24 stsp tog_resizeterm();
1378 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1379 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1380 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1381 25791caa 2018-10-24 stsp err = view_resize(v);
1382 25791caa 2018-10-24 stsp if (err)
1383 25791caa 2018-10-24 stsp return err;
1384 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1385 25791caa 2018-10-24 stsp if (err)
1386 25791caa 2018-10-24 stsp return err;
1387 cdfcfb03 2020-12-06 stsp if (v->child) {
1388 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1389 cdfcfb03 2020-12-06 stsp if (err)
1390 cdfcfb03 2020-12-06 stsp return err;
1391 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1392 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1393 cdfcfb03 2020-12-06 stsp if (err)
1394 cdfcfb03 2020-12-06 stsp return err;
1395 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1396 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1397 3c1dfe12 2022-07-08 mark if (err)
1398 3c1dfe12 2022-07-08 mark return err;
1399 3c1dfe12 2022-07-08 mark }
1400 cdfcfb03 2020-12-06 stsp }
1401 25791caa 2018-10-24 stsp }
1402 25791caa 2018-10-24 stsp }
1403 25791caa 2018-10-24 stsp
1404 e5a0f69f 2018-08-18 stsp switch (ch) {
1405 1e37a5c2 2019-05-12 jcs case '\t':
1406 640cd7ff 2022-06-22 mark view->count = 0;
1407 1e37a5c2 2019-05-12 jcs if (view->child) {
1408 e78dc838 2020-12-04 stsp view->focussed = 0;
1409 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1410 e78dc838 2020-12-04 stsp view->focus_child = 1;
1411 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1412 e78dc838 2020-12-04 stsp view->focussed = 0;
1413 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1414 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1415 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1416 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1417 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1418 6fe51fee 2022-07-22 mark 0);
1419 9b058f45 2022-06-30 mark if (err)
1420 9b058f45 2022-06-30 mark return err;
1421 9b058f45 2022-06-30 mark }
1422 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1423 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1424 9b058f45 2022-06-30 mark if (err)
1425 9b058f45 2022-06-30 mark return err;
1426 9b058f45 2022-06-30 mark }
1427 1e37a5c2 2019-05-12 jcs }
1428 1e37a5c2 2019-05-12 jcs break;
1429 1e37a5c2 2019-05-12 jcs case 'q':
1430 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1431 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1432 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1433 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1434 9b058f45 2022-06-30 mark if (err)
1435 9b058f45 2022-06-30 mark break;
1436 9b058f45 2022-06-30 mark }
1437 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1438 9b058f45 2022-06-30 mark }
1439 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1440 9970f7fc 2020-12-03 stsp view->dying = 1;
1441 1e37a5c2 2019-05-12 jcs break;
1442 1e37a5c2 2019-05-12 jcs case 'Q':
1443 1e37a5c2 2019-05-12 jcs *done = 1;
1444 1e37a5c2 2019-05-12 jcs break;
1445 61417565 2022-06-20 mark case 'F':
1446 640cd7ff 2022-06-22 mark view->count = 0;
1447 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1448 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1449 1e37a5c2 2019-05-12 jcs break;
1450 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1451 e78dc838 2020-12-04 stsp view->focussed = 0;
1452 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1453 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1454 3c1dfe12 2022-07-08 mark } else {
1455 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1456 3c1dfe12 2022-07-08 mark if (!err)
1457 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1458 3c1dfe12 2022-07-08 mark }
1459 1e37a5c2 2019-05-12 jcs if (err)
1460 1e37a5c2 2019-05-12 jcs break;
1461 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1462 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1463 1e37a5c2 2019-05-12 jcs } else {
1464 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1465 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1466 e78dc838 2020-12-04 stsp view->focussed = 1;
1467 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1468 1e37a5c2 2019-05-12 jcs } else {
1469 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1470 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1471 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1472 3c1dfe12 2022-07-08 mark if (!err)
1473 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1474 669b5ffa 2018-10-07 stsp }
1475 1e37a5c2 2019-05-12 jcs if (err)
1476 1e37a5c2 2019-05-12 jcs break;
1477 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1478 1e37a5c2 2019-05-12 jcs }
1479 9b058f45 2022-06-30 mark if (err)
1480 9b058f45 2022-06-30 mark break;
1481 6fe51fee 2022-07-22 mark if (view->resize) {
1482 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1483 9b058f45 2022-06-30 mark if (err)
1484 9b058f45 2022-06-30 mark break;
1485 9b058f45 2022-06-30 mark }
1486 9b058f45 2022-06-30 mark if (view->parent)
1487 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1488 9b058f45 2022-06-30 mark if (!err)
1489 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1490 1e37a5c2 2019-05-12 jcs break;
1491 d2366e29 2022-07-07 mark case 'S':
1492 3c1dfe12 2022-07-08 mark view->count = 0;
1493 d2366e29 2022-07-07 mark err = switch_split(view);
1494 3c1dfe12 2022-07-08 mark break;
1495 3c1dfe12 2022-07-08 mark case '-':
1496 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1497 d2366e29 2022-07-07 mark break;
1498 3c1dfe12 2022-07-08 mark case '+':
1499 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1500 3c1dfe12 2022-07-08 mark break;
1501 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1502 60493ae3 2019-06-20 stsp break;
1503 60493ae3 2019-06-20 stsp case '/':
1504 640cd7ff 2022-06-22 mark view->count = 0;
1505 60493ae3 2019-06-20 stsp if (view->search_start)
1506 2b49a8ae 2019-06-22 stsp view_search_start(view);
1507 60493ae3 2019-06-20 stsp else
1508 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1509 1e37a5c2 2019-05-12 jcs break;
1510 b1bf1435 2019-06-21 stsp case 'N':
1511 60493ae3 2019-06-20 stsp case 'n':
1512 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1513 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1514 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1515 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1516 60493ae3 2019-06-20 stsp view->search_next(view);
1517 60493ae3 2019-06-20 stsp } else
1518 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1519 917d79a7 2022-07-01 stsp break;
1520 917d79a7 2022-07-01 stsp case 'A':
1521 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1522 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1523 917d79a7 2022-07-01 stsp else
1524 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1525 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1526 917d79a7 2022-07-01 stsp if (v->reset) {
1527 917d79a7 2022-07-01 stsp err = v->reset(v);
1528 917d79a7 2022-07-01 stsp if (err)
1529 917d79a7 2022-07-01 stsp return err;
1530 917d79a7 2022-07-01 stsp }
1531 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1532 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1533 917d79a7 2022-07-01 stsp if (err)
1534 917d79a7 2022-07-01 stsp return err;
1535 917d79a7 2022-07-01 stsp }
1536 917d79a7 2022-07-01 stsp }
1537 60493ae3 2019-06-20 stsp break;
1538 1e37a5c2 2019-05-12 jcs default:
1539 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1540 1e37a5c2 2019-05-12 jcs break;
1541 e5a0f69f 2018-08-18 stsp }
1542 e5a0f69f 2018-08-18 stsp
1543 e5a0f69f 2018-08-18 stsp return err;
1544 e5a0f69f 2018-08-18 stsp }
1545 e5a0f69f 2018-08-18 stsp
1546 336075a4 2022-06-25 op static int
1547 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1548 a3404814 2018-09-02 stsp {
1549 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1550 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1551 669b5ffa 2018-10-07 stsp return 0;
1552 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1553 669b5ffa 2018-10-07 stsp return 0;
1554 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1555 a3404814 2018-09-02 stsp return 0;
1556 a3404814 2018-09-02 stsp
1557 669b5ffa 2018-10-07 stsp return view->focussed;
1558 a3404814 2018-09-02 stsp }
1559 a3404814 2018-09-02 stsp
1560 bcbd79e2 2018-08-19 stsp static const struct got_error *
1561 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1562 e5a0f69f 2018-08-18 stsp {
1563 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1564 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1565 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1566 d2366e29 2022-07-07 mark char *mode;
1567 fd823528 2018-10-22 stsp int fast_refresh = 10;
1568 1a76625f 2018-10-22 stsp int done = 0, errcode;
1569 e5a0f69f 2018-08-18 stsp
1570 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1571 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1572 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1573 d2366e29 2022-07-07 mark else
1574 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1575 d2366e29 2022-07-07 mark
1576 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1577 1a76625f 2018-10-22 stsp if (errcode)
1578 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1579 1a76625f 2018-10-22 stsp
1580 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1581 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1582 e5a0f69f 2018-08-18 stsp
1583 1004088d 2018-09-29 stsp view->focussed = 1;
1584 878940b7 2018-09-29 stsp err = view->show(view);
1585 0cf4efb1 2018-09-29 stsp if (err)
1586 0cf4efb1 2018-09-29 stsp return err;
1587 0cf4efb1 2018-09-29 stsp update_panels();
1588 0cf4efb1 2018-09-29 stsp doupdate();
1589 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1590 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1591 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1592 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1593 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1594 fd823528 2018-10-22 stsp
1595 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1596 e5a0f69f 2018-08-18 stsp if (err)
1597 e5a0f69f 2018-08-18 stsp break;
1598 9970f7fc 2020-12-03 stsp if (view->dying) {
1599 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1600 669b5ffa 2018-10-07 stsp
1601 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1602 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1603 9970f7fc 2020-12-03 stsp entry);
1604 e78dc838 2020-12-04 stsp else if (view->parent)
1605 669b5ffa 2018-10-07 stsp prev = view->parent;
1606 669b5ffa 2018-10-07 stsp
1607 e78dc838 2020-12-04 stsp if (view->parent) {
1608 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1609 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1610 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1611 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1612 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1613 0dbbbe90 2022-06-17 op if (err)
1614 0dbbbe90 2022-06-17 op break;
1615 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1616 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1617 e78dc838 2020-12-04 stsp } else
1618 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1619 669b5ffa 2018-10-07 stsp
1620 9970f7fc 2020-12-03 stsp err = view_close(view);
1621 fb59748f 2020-12-05 stsp if (err)
1622 e5a0f69f 2018-08-18 stsp goto done;
1623 669b5ffa 2018-10-07 stsp
1624 e78dc838 2020-12-04 stsp view = NULL;
1625 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1626 e78dc838 2020-12-04 stsp if (v->focussed)
1627 e78dc838 2020-12-04 stsp break;
1628 0cf4efb1 2018-09-29 stsp }
1629 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1630 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1631 e78dc838 2020-12-04 stsp if (prev)
1632 e78dc838 2020-12-04 stsp view = prev;
1633 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1634 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1635 e78dc838 2020-12-04 stsp tog_view_list_head);
1636 e78dc838 2020-12-04 stsp }
1637 e78dc838 2020-12-04 stsp if (view) {
1638 e78dc838 2020-12-04 stsp if (view->focus_child) {
1639 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1640 e78dc838 2020-12-04 stsp view = view->child;
1641 e78dc838 2020-12-04 stsp } else
1642 e78dc838 2020-12-04 stsp view->focussed = 1;
1643 e78dc838 2020-12-04 stsp }
1644 e78dc838 2020-12-04 stsp }
1645 e5a0f69f 2018-08-18 stsp }
1646 bcbd79e2 2018-08-19 stsp if (new_view) {
1647 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1648 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1649 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1650 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1651 86c66b02 2018-10-18 stsp continue;
1652 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1653 86c66b02 2018-10-18 stsp err = view_close(v);
1654 86c66b02 2018-10-18 stsp if (err)
1655 86c66b02 2018-10-18 stsp goto done;
1656 86c66b02 2018-10-18 stsp break;
1657 86c66b02 2018-10-18 stsp }
1658 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1659 fed7eaa8 2018-10-24 stsp view = new_view;
1660 0ae84acc 2022-06-15 tracey }
1661 669b5ffa 2018-10-07 stsp if (view) {
1662 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1663 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1664 e78dc838 2020-12-04 stsp view = view->child;
1665 e78dc838 2020-12-04 stsp } else {
1666 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1667 e78dc838 2020-12-04 stsp view = view->parent;
1668 1a76625f 2018-10-22 stsp }
1669 e78dc838 2020-12-04 stsp show_panel(view->panel);
1670 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1671 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1672 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1673 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1674 669b5ffa 2018-10-07 stsp if (err)
1675 1a76625f 2018-10-22 stsp goto done;
1676 669b5ffa 2018-10-07 stsp }
1677 669b5ffa 2018-10-07 stsp err = view->show(view);
1678 0cf4efb1 2018-09-29 stsp if (err)
1679 1a76625f 2018-10-22 stsp goto done;
1680 669b5ffa 2018-10-07 stsp if (view->child) {
1681 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1682 669b5ffa 2018-10-07 stsp if (err)
1683 1a76625f 2018-10-22 stsp goto done;
1684 669b5ffa 2018-10-07 stsp }
1685 1a76625f 2018-10-22 stsp update_panels();
1686 1a76625f 2018-10-22 stsp doupdate();
1687 0cf4efb1 2018-09-29 stsp }
1688 e5a0f69f 2018-08-18 stsp }
1689 e5a0f69f 2018-08-18 stsp done:
1690 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1691 5629093a 2022-07-11 stsp const struct got_error *close_err;
1692 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1693 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1694 5629093a 2022-07-11 stsp close_err = view_close(view);
1695 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1696 5629093a 2022-07-11 stsp err = close_err;
1697 e5a0f69f 2018-08-18 stsp }
1698 1a76625f 2018-10-22 stsp
1699 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1700 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1701 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1702 1a76625f 2018-10-22 stsp
1703 e5a0f69f 2018-08-18 stsp return err;
1704 ea5e7bb5 2018-08-01 stsp }
1705 ea5e7bb5 2018-08-01 stsp
1706 4ed7e80c 2018-05-20 stsp __dead static void
1707 9f7d7167 2018-04-29 stsp usage_log(void)
1708 9f7d7167 2018-04-29 stsp {
1709 80ddbec8 2018-04-29 stsp endwin();
1710 c70c5802 2018-08-01 stsp fprintf(stderr,
1711 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1712 9f7d7167 2018-04-29 stsp getprogname());
1713 9f7d7167 2018-04-29 stsp exit(1);
1714 80ddbec8 2018-04-29 stsp }
1715 80ddbec8 2018-04-29 stsp
1716 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1717 80ddbec8 2018-04-29 stsp static const struct got_error *
1718 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1719 963b370f 2018-05-20 stsp {
1720 00dfcb92 2018-06-11 stsp char *vis = NULL;
1721 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1722 963b370f 2018-05-20 stsp
1723 963b370f 2018-05-20 stsp *ws = NULL;
1724 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1725 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1726 00dfcb92 2018-06-11 stsp int vislen;
1727 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1728 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1729 00dfcb92 2018-06-11 stsp
1730 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1731 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1732 00dfcb92 2018-06-11 stsp if (err)
1733 00dfcb92 2018-06-11 stsp return err;
1734 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1735 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1736 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1737 a7f50699 2018-06-11 stsp goto done;
1738 a7f50699 2018-06-11 stsp }
1739 00dfcb92 2018-06-11 stsp }
1740 963b370f 2018-05-20 stsp
1741 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1742 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1743 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1744 a7f50699 2018-06-11 stsp goto done;
1745 a7f50699 2018-06-11 stsp }
1746 963b370f 2018-05-20 stsp
1747 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1748 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1749 a7f50699 2018-06-11 stsp done:
1750 00dfcb92 2018-06-11 stsp free(vis);
1751 963b370f 2018-05-20 stsp if (err) {
1752 963b370f 2018-05-20 stsp free(*ws);
1753 963b370f 2018-05-20 stsp *ws = NULL;
1754 963b370f 2018-05-20 stsp *wlen = 0;
1755 963b370f 2018-05-20 stsp }
1756 963b370f 2018-05-20 stsp return err;
1757 145b6838 2022-06-16 stsp }
1758 145b6838 2022-06-16 stsp
1759 145b6838 2022-06-16 stsp static const struct got_error *
1760 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1761 145b6838 2022-06-16 stsp {
1762 145b6838 2022-06-16 stsp char *dst;
1763 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1764 145b6838 2022-06-16 stsp
1765 145b6838 2022-06-16 stsp *ptr = NULL;
1766 145b6838 2022-06-16 stsp n = len = strlen(src);
1767 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1768 145b6838 2022-06-16 stsp if (dst == NULL)
1769 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1770 145b6838 2022-06-16 stsp
1771 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1772 145b6838 2022-06-16 stsp const char c = src[idx];
1773 145b6838 2022-06-16 stsp
1774 145b6838 2022-06-16 stsp if (c == '\t') {
1775 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1776 367ddf28 2022-06-16 mark char *p;
1777 367ddf28 2022-06-16 mark
1778 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1779 6e1c41ad 2022-06-16 mark if (p == NULL) {
1780 6e1c41ad 2022-06-16 mark free(dst);
1781 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1782 6e1c41ad 2022-06-16 mark
1783 6e1c41ad 2022-06-16 mark }
1784 6e1c41ad 2022-06-16 mark dst = p;
1785 145b6838 2022-06-16 stsp n += nb;
1786 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1787 145b6838 2022-06-16 stsp sz += nb;
1788 145b6838 2022-06-16 stsp } else
1789 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1790 145b6838 2022-06-16 stsp ++idx;
1791 145b6838 2022-06-16 stsp }
1792 145b6838 2022-06-16 stsp
1793 145b6838 2022-06-16 stsp dst[sz] = '\0';
1794 145b6838 2022-06-16 stsp *ptr = dst;
1795 145b6838 2022-06-16 stsp return NULL;
1796 963b370f 2018-05-20 stsp }
1797 963b370f 2018-05-20 stsp
1798 4e4a9ac8 2022-06-17 op /*
1799 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1800 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1801 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1802 4e4a9ac8 2022-06-17 op * *rcol.
1803 ccda2f4d 2022-06-16 stsp */
1804 4e4a9ac8 2022-06-17 op static int
1805 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1806 4e4a9ac8 2022-06-17 op {
1807 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1808 ccda2f4d 2022-06-16 stsp
1809 4e4a9ac8 2022-06-17 op if (n == 0) {
1810 4e4a9ac8 2022-06-17 op *rcol = cols;
1811 4e4a9ac8 2022-06-17 op return off;
1812 4e4a9ac8 2022-06-17 op }
1813 ccda2f4d 2022-06-16 stsp
1814 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1815 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1816 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1817 4e4a9ac8 2022-06-17 op else
1818 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1819 ccda2f4d 2022-06-16 stsp
1820 4e4a9ac8 2022-06-17 op if (width == -1) {
1821 4e4a9ac8 2022-06-17 op width = 1;
1822 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1823 ccda2f4d 2022-06-16 stsp }
1824 ccda2f4d 2022-06-16 stsp
1825 4e4a9ac8 2022-06-17 op if (cols + width > n)
1826 4e4a9ac8 2022-06-17 op break;
1827 4e4a9ac8 2022-06-17 op cols += width;
1828 ccda2f4d 2022-06-16 stsp }
1829 ccda2f4d 2022-06-16 stsp
1830 4e4a9ac8 2022-06-17 op *rcol = cols;
1831 4e4a9ac8 2022-06-17 op return i;
1832 ccda2f4d 2022-06-16 stsp }
1833 ccda2f4d 2022-06-16 stsp
1834 ccda2f4d 2022-06-16 stsp /*
1835 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1836 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1837 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1838 ccda2f4d 2022-06-16 stsp */
1839 ccda2f4d 2022-06-16 stsp static const struct got_error *
1840 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1841 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1842 ccda2f4d 2022-06-16 stsp {
1843 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1844 4e4a9ac8 2022-06-17 op int cols;
1845 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1846 145b6838 2022-06-16 stsp char *exstr = NULL;
1847 963b370f 2018-05-20 stsp size_t wlen;
1848 4e4a9ac8 2022-06-17 op int i, scrollx;
1849 963b370f 2018-05-20 stsp
1850 963b370f 2018-05-20 stsp *wlinep = NULL;
1851 b700b5d6 2018-07-10 stsp *widthp = 0;
1852 963b370f 2018-05-20 stsp
1853 145b6838 2022-06-16 stsp if (expand) {
1854 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1855 145b6838 2022-06-16 stsp if (err)
1856 145b6838 2022-06-16 stsp return err;
1857 145b6838 2022-06-16 stsp }
1858 145b6838 2022-06-16 stsp
1859 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1860 145b6838 2022-06-16 stsp free(exstr);
1861 963b370f 2018-05-20 stsp if (err)
1862 963b370f 2018-05-20 stsp return err;
1863 963b370f 2018-05-20 stsp
1864 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1865 ccda2f4d 2022-06-16 stsp
1866 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1867 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1868 3f670bfb 2020-12-10 stsp wlen--;
1869 3f670bfb 2020-12-10 stsp }
1870 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1871 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1872 3f670bfb 2020-12-10 stsp wlen--;
1873 3f670bfb 2020-12-10 stsp }
1874 3f670bfb 2020-12-10 stsp
1875 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1876 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1877 27a741e5 2019-09-11 stsp
1878 b700b5d6 2018-07-10 stsp if (widthp)
1879 b700b5d6 2018-07-10 stsp *widthp = cols;
1880 ccda2f4d 2022-06-16 stsp if (scrollxp)
1881 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1882 963b370f 2018-05-20 stsp if (err)
1883 963b370f 2018-05-20 stsp free(wline);
1884 963b370f 2018-05-20 stsp else
1885 963b370f 2018-05-20 stsp *wlinep = wline;
1886 963b370f 2018-05-20 stsp return err;
1887 963b370f 2018-05-20 stsp }
1888 963b370f 2018-05-20 stsp
1889 8b473291 2019-02-21 stsp static const struct got_error*
1890 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1891 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1892 8b473291 2019-02-21 stsp {
1893 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1894 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1895 8b473291 2019-02-21 stsp char *s;
1896 8b473291 2019-02-21 stsp const char *name;
1897 8b473291 2019-02-21 stsp
1898 8b473291 2019-02-21 stsp *refs_str = NULL;
1899 8b473291 2019-02-21 stsp
1900 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1901 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1902 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1903 52b5abe1 2019-08-13 stsp int cmp;
1904 52b5abe1 2019-08-13 stsp
1905 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1906 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1907 8b473291 2019-02-21 stsp continue;
1908 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1909 8b473291 2019-02-21 stsp name += 5;
1910 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1911 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1912 7143d404 2019-03-12 stsp continue;
1913 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1914 8b473291 2019-02-21 stsp name += 6;
1915 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1916 8b473291 2019-02-21 stsp name += 8;
1917 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1918 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1919 79cc719f 2020-04-24 stsp continue;
1920 79cc719f 2020-04-24 stsp }
1921 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1922 48cae60d 2020-09-22 stsp if (err)
1923 48cae60d 2020-09-22 stsp break;
1924 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1925 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1926 5d844a1e 2019-08-13 stsp if (err) {
1927 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1928 48cae60d 2020-09-22 stsp free(ref_id);
1929 5d844a1e 2019-08-13 stsp break;
1930 48cae60d 2020-09-22 stsp }
1931 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1932 5d844a1e 2019-08-13 stsp err = NULL;
1933 5d844a1e 2019-08-13 stsp tag = NULL;
1934 5d844a1e 2019-08-13 stsp }
1935 52b5abe1 2019-08-13 stsp }
1936 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1937 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1938 48cae60d 2020-09-22 stsp free(ref_id);
1939 52b5abe1 2019-08-13 stsp if (tag)
1940 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1941 52b5abe1 2019-08-13 stsp if (cmp != 0)
1942 52b5abe1 2019-08-13 stsp continue;
1943 8b473291 2019-02-21 stsp s = *refs_str;
1944 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1945 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1946 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1947 8b473291 2019-02-21 stsp free(s);
1948 8b473291 2019-02-21 stsp *refs_str = NULL;
1949 8b473291 2019-02-21 stsp break;
1950 8b473291 2019-02-21 stsp }
1951 8b473291 2019-02-21 stsp free(s);
1952 8b473291 2019-02-21 stsp }
1953 8b473291 2019-02-21 stsp
1954 8b473291 2019-02-21 stsp return err;
1955 8b473291 2019-02-21 stsp }
1956 8b473291 2019-02-21 stsp
1957 963b370f 2018-05-20 stsp static const struct got_error *
1958 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1959 27a741e5 2019-09-11 stsp int col_tab_align)
1960 5813d178 2019-03-09 stsp {
1961 e6b8b890 2020-12-29 naddy char *smallerthan;
1962 5813d178 2019-03-09 stsp
1963 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1964 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1965 5813d178 2019-03-09 stsp author = smallerthan + 1;
1966 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1967 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1968 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1969 5813d178 2019-03-09 stsp }
1970 5813d178 2019-03-09 stsp
1971 5813d178 2019-03-09 stsp static const struct got_error *
1972 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1973 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1974 8fdc79fe 2020-12-01 naddy int author_display_cols)
1975 80ddbec8 2018-04-29 stsp {
1976 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1977 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1978 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1979 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1980 5813d178 2019-03-09 stsp char *author = NULL;
1981 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1982 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1983 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1984 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1985 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1986 ccb26ccd 2018-11-05 stsp struct tm tm;
1987 45d799e2 2018-12-23 stsp time_t committer_time;
1988 11b20872 2019-11-08 stsp struct tog_color *tc;
1989 80ddbec8 2018-04-29 stsp
1990 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1991 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1992 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1993 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1994 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1995 b39d25c7 2018-07-10 stsp
1996 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1997 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1998 b39d25c7 2018-07-10 stsp else
1999 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2000 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2001 11b20872 2019-11-08 stsp if (tc)
2002 11b20872 2019-11-08 stsp wattr_on(view->window,
2003 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2004 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2005 11b20872 2019-11-08 stsp if (tc)
2006 11b20872 2019-11-08 stsp wattr_off(view->window,
2007 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2008 27a741e5 2019-09-11 stsp col = limit;
2009 b39d25c7 2018-07-10 stsp if (col > avail)
2010 b39d25c7 2018-07-10 stsp goto done;
2011 6570a66d 2019-11-08 stsp
2012 6570a66d 2019-11-08 stsp if (avail >= 120) {
2013 6570a66d 2019-11-08 stsp char *id_str;
2014 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2015 6570a66d 2019-11-08 stsp if (err)
2016 6570a66d 2019-11-08 stsp goto done;
2017 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2018 11b20872 2019-11-08 stsp if (tc)
2019 11b20872 2019-11-08 stsp wattr_on(view->window,
2020 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2021 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2022 11b20872 2019-11-08 stsp if (tc)
2023 11b20872 2019-11-08 stsp wattr_off(view->window,
2024 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2025 6570a66d 2019-11-08 stsp free(id_str);
2026 6570a66d 2019-11-08 stsp col += 9;
2027 6570a66d 2019-11-08 stsp if (col > avail)
2028 6570a66d 2019-11-08 stsp goto done;
2029 6570a66d 2019-11-08 stsp }
2030 b39d25c7 2018-07-10 stsp
2031 10aab77f 2022-07-19 op if (s->use_committer)
2032 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2033 10aab77f 2022-07-19 op else
2034 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2035 5813d178 2019-03-09 stsp if (author == NULL) {
2036 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2037 80ddbec8 2018-04-29 stsp goto done;
2038 80ddbec8 2018-04-29 stsp }
2039 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2040 bb737323 2018-05-20 stsp if (err)
2041 bb737323 2018-05-20 stsp goto done;
2042 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2043 11b20872 2019-11-08 stsp if (tc)
2044 11b20872 2019-11-08 stsp wattr_on(view->window,
2045 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2046 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2047 11b20872 2019-11-08 stsp if (tc)
2048 11b20872 2019-11-08 stsp wattr_off(view->window,
2049 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2050 bb737323 2018-05-20 stsp col += author_width;
2051 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2052 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2053 bb737323 2018-05-20 stsp col++;
2054 bb737323 2018-05-20 stsp author_width++;
2055 bb737323 2018-05-20 stsp }
2056 9c2eaf34 2018-05-20 stsp if (col > avail)
2057 9c2eaf34 2018-05-20 stsp goto done;
2058 80ddbec8 2018-04-29 stsp
2059 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2060 5943eee2 2019-08-13 stsp if (err)
2061 6d9fbc00 2018-04-29 stsp goto done;
2062 bb737323 2018-05-20 stsp logmsg = logmsg0;
2063 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2064 bb737323 2018-05-20 stsp logmsg++;
2065 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2066 bb737323 2018-05-20 stsp if (newline)
2067 bb737323 2018-05-20 stsp *newline = '\0';
2068 ccda2f4d 2022-06-16 stsp limit = avail - col;
2069 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2070 4d1f6af3 2022-06-17 op limit--; /* for the border */
2071 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2072 ccda2f4d 2022-06-16 stsp limit, col, 1);
2073 29688b02 2022-06-16 stsp if (err)
2074 29688b02 2022-06-16 stsp goto done;
2075 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2076 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2077 27a741e5 2019-09-11 stsp while (col < avail) {
2078 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2079 bb737323 2018-05-20 stsp col++;
2080 881b2d3e 2018-04-30 stsp }
2081 80ddbec8 2018-04-29 stsp done:
2082 80ddbec8 2018-04-29 stsp free(logmsg0);
2083 bb737323 2018-05-20 stsp free(wlogmsg);
2084 5813d178 2019-03-09 stsp free(author);
2085 bb737323 2018-05-20 stsp free(wauthor);
2086 80ddbec8 2018-04-29 stsp free(line);
2087 80ddbec8 2018-04-29 stsp return err;
2088 80ddbec8 2018-04-29 stsp }
2089 26ed57b2 2018-05-19 stsp
2090 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2091 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2092 899d86c2 2018-05-10 stsp struct got_object_id *id)
2093 80ddbec8 2018-04-29 stsp {
2094 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2095 e15c42de 2022-09-05 op struct got_object_id *dup;
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 e15c42de 2022-09-05 op dup = got_object_id_dup(id);
2102 e15c42de 2022-09-05 op if (dup == NULL) {
2103 e15c42de 2022-09-05 op free(entry);
2104 e15c42de 2022-09-05 op return NULL;
2105 e15c42de 2022-09-05 op }
2106 e15c42de 2022-09-05 op
2107 e15c42de 2022-09-05 op entry->id = dup;
2108 99db9666 2018-05-07 stsp entry->commit = commit;
2109 899d86c2 2018-05-10 stsp return entry;
2110 99db9666 2018-05-07 stsp }
2111 80ddbec8 2018-04-29 stsp
2112 99db9666 2018-05-07 stsp static void
2113 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2114 99db9666 2018-05-07 stsp {
2115 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2116 99db9666 2018-05-07 stsp
2117 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2118 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2119 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2120 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2121 e15c42de 2022-09-05 op free(entry->id);
2122 99db9666 2018-05-07 stsp free(entry);
2123 99db9666 2018-05-07 stsp }
2124 99db9666 2018-05-07 stsp
2125 99db9666 2018-05-07 stsp static void
2126 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2127 99db9666 2018-05-07 stsp {
2128 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2129 99db9666 2018-05-07 stsp pop_commit(commits);
2130 c4972b91 2018-05-07 stsp }
2131 c4972b91 2018-05-07 stsp
2132 c4972b91 2018-05-07 stsp static const struct got_error *
2133 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2134 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2135 13add988 2019-10-15 stsp {
2136 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2137 13add988 2019-10-15 stsp regmatch_t regmatch;
2138 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2139 13add988 2019-10-15 stsp
2140 13add988 2019-10-15 stsp *have_match = 0;
2141 13add988 2019-10-15 stsp
2142 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2143 13add988 2019-10-15 stsp if (err)
2144 13add988 2019-10-15 stsp return err;
2145 13add988 2019-10-15 stsp
2146 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2147 13add988 2019-10-15 stsp if (err)
2148 13add988 2019-10-15 stsp goto done;
2149 13add988 2019-10-15 stsp
2150 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2151 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2152 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2153 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2154 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2155 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2156 13add988 2019-10-15 stsp *have_match = 1;
2157 13add988 2019-10-15 stsp done:
2158 13add988 2019-10-15 stsp free(id_str);
2159 13add988 2019-10-15 stsp free(logmsg);
2160 13add988 2019-10-15 stsp return err;
2161 13add988 2019-10-15 stsp }
2162 13add988 2019-10-15 stsp
2163 13add988 2019-10-15 stsp static const struct got_error *
2164 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2165 c4972b91 2018-05-07 stsp {
2166 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2167 9ba79e04 2018-06-11 stsp
2168 1a76625f 2018-10-22 stsp /*
2169 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2170 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2171 1a76625f 2018-10-22 stsp * while updating the display.
2172 1a76625f 2018-10-22 stsp */
2173 4e0d2870 2020-12-07 naddy do {
2174 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2175 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2176 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2177 1a76625f 2018-10-22 stsp int errcode;
2178 899d86c2 2018-05-10 stsp
2179 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2180 4e0d2870 2020-12-07 naddy NULL, NULL);
2181 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2182 ecb28ae0 2018-07-16 stsp break;
2183 899d86c2 2018-05-10 stsp
2184 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2185 9ba79e04 2018-06-11 stsp if (err)
2186 9ba79e04 2018-06-11 stsp break;
2187 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2188 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2189 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2190 9ba79e04 2018-06-11 stsp break;
2191 9ba79e04 2018-06-11 stsp }
2192 93e45b7c 2018-09-24 stsp
2193 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2194 1a76625f 2018-10-22 stsp if (errcode) {
2195 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2196 13add988 2019-10-15 stsp "pthread_mutex_lock");
2197 1a76625f 2018-10-22 stsp break;
2198 1a76625f 2018-10-22 stsp }
2199 1a76625f 2018-10-22 stsp
2200 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2201 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2202 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2203 1a76625f 2018-10-22 stsp
2204 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2205 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2206 7c1452c1 2020-03-26 stsp int have_match;
2207 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2208 7c1452c1 2020-03-26 stsp if (err)
2209 7c1452c1 2020-03-26 stsp break;
2210 7c1452c1 2020-03-26 stsp if (have_match)
2211 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2212 13add988 2019-10-15 stsp }
2213 13add988 2019-10-15 stsp
2214 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2215 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2216 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2217 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2218 7c1452c1 2020-03-26 stsp if (err)
2219 13add988 2019-10-15 stsp break;
2220 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2221 899d86c2 2018-05-10 stsp
2222 9ba79e04 2018-06-11 stsp return err;
2223 0553a4e3 2018-04-30 stsp }
2224 0553a4e3 2018-04-30 stsp
2225 2b779855 2020-12-05 naddy static void
2226 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2227 2b779855 2020-12-05 naddy {
2228 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2229 2b779855 2020-12-05 naddy int ncommits = 0;
2230 2b779855 2020-12-05 naddy
2231 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2232 2b779855 2020-12-05 naddy while (entry) {
2233 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2234 2b779855 2020-12-05 naddy s->selected_entry = entry;
2235 2b779855 2020-12-05 naddy break;
2236 2b779855 2020-12-05 naddy }
2237 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2238 2b779855 2020-12-05 naddy ncommits++;
2239 2b779855 2020-12-05 naddy }
2240 2b779855 2020-12-05 naddy }
2241 2b779855 2020-12-05 naddy
2242 0553a4e3 2018-04-30 stsp static const struct got_error *
2243 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2244 0553a4e3 2018-04-30 stsp {
2245 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2246 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2247 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2248 cbb0c8d7 2022-08-12 mark int limit = view->nlines;
2249 60493ae3 2019-06-20 stsp int width;
2250 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2251 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2252 8b473291 2019-02-21 stsp char *refs_str = NULL;
2253 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2254 11b20872 2019-11-08 stsp struct tog_color *tc;
2255 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2256 cbb0c8d7 2022-08-12 mark
2257 cbb0c8d7 2022-08-12 mark if (view_is_hsplit_top(view))
2258 cbb0c8d7 2022-08-12 mark --limit; /* account for border */
2259 0553a4e3 2018-04-30 stsp
2260 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2261 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2262 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2263 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2264 1a76625f 2018-10-22 stsp if (err)
2265 ecb28ae0 2018-07-16 stsp return err;
2266 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2267 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2268 d2075bf3 2020-12-25 stsp if (refs) {
2269 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2270 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2271 d2075bf3 2020-12-25 stsp if (err)
2272 d2075bf3 2020-12-25 stsp goto done;
2273 d2075bf3 2020-12-25 stsp }
2274 867c6645 2018-07-10 stsp }
2275 359bfafd 2019-02-22 stsp
2276 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2277 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2278 1a76625f 2018-10-22 stsp
2279 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2280 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2281 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2282 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2283 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2284 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2285 8f4ed634 2020-03-26 stsp goto done;
2286 8f4ed634 2020-03-26 stsp }
2287 8f4ed634 2020-03-26 stsp } else {
2288 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2289 f9686aa5 2020-03-27 stsp
2290 f9686aa5 2020-03-27 stsp if (view->searching) {
2291 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2292 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2293 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2294 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2295 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2296 f9686aa5 2020-03-27 stsp search_str = "searching...";
2297 f9686aa5 2020-03-27 stsp }
2298 f9686aa5 2020-03-27 stsp
2299 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2300 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2301 f9686aa5 2020-03-27 stsp search_str ? search_str :
2302 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2303 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2304 8f4ed634 2020-03-26 stsp goto done;
2305 8f4ed634 2020-03-26 stsp }
2306 8b473291 2019-02-21 stsp }
2307 1a76625f 2018-10-22 stsp
2308 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2309 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2310 87411fa9 2022-06-16 stsp "........................................",
2311 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2312 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2313 1a76625f 2018-10-22 stsp header = NULL;
2314 1a76625f 2018-10-22 stsp goto done;
2315 1a76625f 2018-10-22 stsp }
2316 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2317 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2318 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2319 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2320 1a76625f 2018-10-22 stsp header = NULL;
2321 1a76625f 2018-10-22 stsp goto done;
2322 ecb28ae0 2018-07-16 stsp }
2323 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2324 1a76625f 2018-10-22 stsp if (err)
2325 1a76625f 2018-10-22 stsp goto done;
2326 867c6645 2018-07-10 stsp
2327 2814baeb 2018-08-01 stsp werase(view->window);
2328 867c6645 2018-07-10 stsp
2329 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2330 a3404814 2018-09-02 stsp wstandout(view->window);
2331 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2332 11b20872 2019-11-08 stsp if (tc)
2333 11b20872 2019-11-08 stsp wattr_on(view->window,
2334 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2335 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2336 11b20872 2019-11-08 stsp if (tc)
2337 11b20872 2019-11-08 stsp wattr_off(view->window,
2338 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2339 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2340 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2341 1a76625f 2018-10-22 stsp width++;
2342 1a76625f 2018-10-22 stsp }
2343 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2344 a3404814 2018-09-02 stsp wstandend(view->window);
2345 ecb28ae0 2018-07-16 stsp free(wline);
2346 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2347 1a76625f 2018-10-22 stsp goto done;
2348 0553a4e3 2018-04-30 stsp
2349 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2350 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2351 5813d178 2019-03-09 stsp ncommits = 0;
2352 145b6838 2022-06-16 stsp view->maxx = 0;
2353 5813d178 2019-03-09 stsp while (entry) {
2354 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2355 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2356 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2357 5813d178 2019-03-09 stsp int width;
2358 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2359 5813d178 2019-03-09 stsp break;
2360 10aab77f 2022-07-19 op if (s->use_committer)
2361 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2362 10aab77f 2022-07-19 op else
2363 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2364 5813d178 2019-03-09 stsp if (author == NULL) {
2365 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2366 5813d178 2019-03-09 stsp goto done;
2367 5813d178 2019-03-09 stsp }
2368 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2369 27a741e5 2019-09-11 stsp date_display_cols);
2370 5813d178 2019-03-09 stsp if (author_cols < width)
2371 5813d178 2019-03-09 stsp author_cols = width;
2372 5813d178 2019-03-09 stsp free(wauthor);
2373 5813d178 2019-03-09 stsp free(author);
2374 a310d9c3 2022-07-21 florian if (err)
2375 a310d9c3 2022-07-21 florian goto done;
2376 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2377 145b6838 2022-06-16 stsp if (err)
2378 145b6838 2022-06-16 stsp goto done;
2379 145b6838 2022-06-16 stsp msg = msg0;
2380 145b6838 2022-06-16 stsp while (*msg == '\n')
2381 145b6838 2022-06-16 stsp ++msg;
2382 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2383 29688b02 2022-06-16 stsp *eol = '\0';
2384 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2385 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2386 29688b02 2022-06-16 stsp if (err)
2387 29688b02 2022-06-16 stsp goto done;
2388 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2389 145b6838 2022-06-16 stsp free(msg0);
2390 29688b02 2022-06-16 stsp free(wmsg);
2391 7ca04879 2019-10-19 stsp ncommits++;
2392 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2393 5813d178 2019-03-09 stsp }
2394 5813d178 2019-03-09 stsp
2395 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2396 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2397 867c6645 2018-07-10 stsp ncommits = 0;
2398 899d86c2 2018-05-10 stsp while (entry) {
2399 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2400 899d86c2 2018-05-10 stsp break;
2401 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2402 2814baeb 2018-08-01 stsp wstandout(view->window);
2403 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2404 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2405 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2406 2814baeb 2018-08-01 stsp wstandend(view->window);
2407 0553a4e3 2018-04-30 stsp if (err)
2408 60493ae3 2019-06-20 stsp goto done;
2409 0553a4e3 2018-04-30 stsp ncommits++;
2410 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2411 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2412 80ddbec8 2018-04-29 stsp }
2413 80ddbec8 2018-04-29 stsp
2414 9b058f45 2022-06-30 mark view_border(view);
2415 1a76625f 2018-10-22 stsp done:
2416 1a76625f 2018-10-22 stsp free(id_str);
2417 8b473291 2019-02-21 stsp free(refs_str);
2418 1a76625f 2018-10-22 stsp free(ncommits_str);
2419 1a76625f 2018-10-22 stsp free(header);
2420 80ddbec8 2018-04-29 stsp return err;
2421 9f7d7167 2018-04-29 stsp }
2422 07b55e75 2018-05-10 stsp
2423 07b55e75 2018-05-10 stsp static void
2424 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2425 07b55e75 2018-05-10 stsp {
2426 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2427 07b55e75 2018-05-10 stsp int nscrolled = 0;
2428 07b55e75 2018-05-10 stsp
2429 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2430 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2431 07b55e75 2018-05-10 stsp return;
2432 9f7d7167 2018-04-29 stsp
2433 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2434 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2435 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2436 07b55e75 2018-05-10 stsp if (entry) {
2437 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2438 07b55e75 2018-05-10 stsp nscrolled++;
2439 07b55e75 2018-05-10 stsp }
2440 07b55e75 2018-05-10 stsp }
2441 aa075928 2018-05-10 stsp }
2442 aa075928 2018-05-10 stsp
2443 aa075928 2018-05-10 stsp static const struct got_error *
2444 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2445 aa075928 2018-05-10 stsp {
2446 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2447 5e224a3e 2019-02-22 stsp int errcode;
2448 8a42fca8 2019-02-22 stsp
2449 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2450 aa075928 2018-05-10 stsp
2451 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2452 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2453 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2454 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2455 7aafa0d1 2019-02-22 stsp if (errcode)
2456 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2457 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2458 7c1452c1 2020-03-26 stsp
2459 7c1452c1 2020-03-26 stsp /*
2460 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2461 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2462 7c1452c1 2020-03-26 stsp */
2463 7c1452c1 2020-03-26 stsp if (!wait)
2464 7c1452c1 2020-03-26 stsp break;
2465 7c1452c1 2020-03-26 stsp
2466 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2467 ffe38506 2020-12-01 naddy show_log_view(view);
2468 7c1452c1 2020-03-26 stsp update_panels();
2469 7c1452c1 2020-03-26 stsp doupdate();
2470 7c1452c1 2020-03-26 stsp
2471 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2472 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2473 82954512 2020-02-03 stsp if (errcode)
2474 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2475 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2476 82954512 2020-02-03 stsp
2477 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2478 ffe38506 2020-12-01 naddy show_log_view(view);
2479 7c1452c1 2020-03-26 stsp update_panels();
2480 7c1452c1 2020-03-26 stsp doupdate();
2481 5e224a3e 2019-02-22 stsp }
2482 5e224a3e 2019-02-22 stsp
2483 5e224a3e 2019-02-22 stsp return NULL;
2484 5e224a3e 2019-02-22 stsp }
2485 5e224a3e 2019-02-22 stsp
2486 5e224a3e 2019-02-22 stsp static const struct got_error *
2487 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2488 9b058f45 2022-06-30 mark {
2489 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2490 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2491 2525dccb 2022-07-13 mark
2492 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2493 2525dccb 2022-07-13 mark return NULL;
2494 9b058f45 2022-06-30 mark
2495 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2496 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2497 9b058f45 2022-06-30 mark view->nscrolled = 0;
2498 9b058f45 2022-06-30 mark
2499 9b058f45 2022-06-30 mark return err;
2500 9b058f45 2022-06-30 mark }
2501 9b058f45 2022-06-30 mark
2502 9b058f45 2022-06-30 mark static const struct got_error *
2503 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2504 5e224a3e 2019-02-22 stsp {
2505 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2506 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2507 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2508 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2509 5e224a3e 2019-02-22 stsp
2510 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2511 5e224a3e 2019-02-22 stsp return NULL;
2512 5e224a3e 2019-02-22 stsp
2513 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2514 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2515 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2516 08ebd0a9 2019-02-22 stsp /*
2517 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2518 08ebd0a9 2019-02-22 stsp */
2519 94b80cfa 2022-08-01 mark s->thread_args.commits_needed +=
2520 94b80cfa 2022-08-01 mark ncommits_needed - s->commits.ncommits;
2521 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2522 5e224a3e 2019-02-22 stsp if (err)
2523 5e224a3e 2019-02-22 stsp return err;
2524 7aafa0d1 2019-02-22 stsp }
2525 b295e71b 2019-02-22 stsp
2526 7aafa0d1 2019-02-22 stsp do {
2527 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2528 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2529 88048b54 2019-02-21 stsp break;
2530 88048b54 2019-02-21 stsp
2531 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2532 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2533 aa075928 2018-05-10 stsp
2534 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2535 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2536 dd0a52c1 2018-05-20 stsp break;
2537 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2538 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2539 aa075928 2018-05-10 stsp
2540 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2541 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2542 9b058f45 2022-06-30 mark else
2543 9b058f45 2022-06-30 mark view->nscrolled = 0;
2544 9b058f45 2022-06-30 mark
2545 dd0a52c1 2018-05-20 stsp return err;
2546 07b55e75 2018-05-10 stsp }
2547 4a7f7875 2018-05-10 stsp
2548 cd0acaa7 2018-05-20 stsp static const struct got_error *
2549 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2550 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2551 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2552 cd0acaa7 2018-05-20 stsp {
2553 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2554 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2555 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2556 cd0acaa7 2018-05-20 stsp
2557 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2558 15a94983 2018-12-23 stsp if (diff_view == NULL)
2559 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2560 ea5e7bb5 2018-08-01 stsp
2561 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2562 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2563 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2564 e5a0f69f 2018-08-18 stsp if (err == NULL)
2565 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2566 cd0acaa7 2018-05-20 stsp return err;
2567 4a7f7875 2018-05-10 stsp }
2568 4a7f7875 2018-05-10 stsp
2569 80ddbec8 2018-04-29 stsp static const struct got_error *
2570 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2571 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2572 9343a5fb 2018-06-23 stsp {
2573 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2574 941e9f74 2019-05-21 stsp
2575 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2576 941e9f74 2019-05-21 stsp if (parent == NULL)
2577 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2578 941e9f74 2019-05-21 stsp
2579 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2580 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2581 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2582 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2583 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2584 941e9f74 2019-05-21 stsp s->tree = subtree;
2585 941e9f74 2019-05-21 stsp s->selected = 0;
2586 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2587 941e9f74 2019-05-21 stsp return NULL;
2588 941e9f74 2019-05-21 stsp }
2589 941e9f74 2019-05-21 stsp
2590 941e9f74 2019-05-21 stsp static const struct got_error *
2591 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2592 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2593 941e9f74 2019-05-21 stsp {
2594 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2595 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2596 941e9f74 2019-05-21 stsp const char *p;
2597 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2598 9343a5fb 2018-06-23 stsp
2599 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2600 941e9f74 2019-05-21 stsp p = path;
2601 941e9f74 2019-05-21 stsp while (*p) {
2602 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2603 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2604 56e0773d 2019-11-28 stsp char *te_name;
2605 33cbf02b 2020-01-12 stsp
2606 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2607 33cbf02b 2020-01-12 stsp p++;
2608 941e9f74 2019-05-21 stsp
2609 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2610 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2611 941e9f74 2019-05-21 stsp if (slash == NULL)
2612 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2613 33cbf02b 2020-01-12 stsp else
2614 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2615 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2616 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2617 56e0773d 2019-11-28 stsp break;
2618 941e9f74 2019-05-21 stsp }
2619 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2620 56e0773d 2019-11-28 stsp if (te == NULL) {
2621 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2622 56e0773d 2019-11-28 stsp free(te_name);
2623 941e9f74 2019-05-21 stsp break;
2624 941e9f74 2019-05-21 stsp }
2625 56e0773d 2019-11-28 stsp free(te_name);
2626 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2627 941e9f74 2019-05-21 stsp
2628 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2629 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2630 b03c880f 2019-05-21 stsp
2631 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2632 941e9f74 2019-05-21 stsp if (slash)
2633 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2634 941e9f74 2019-05-21 stsp else
2635 941e9f74 2019-05-21 stsp subpath = strdup(path);
2636 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2637 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2638 941e9f74 2019-05-21 stsp break;
2639 941e9f74 2019-05-21 stsp }
2640 941e9f74 2019-05-21 stsp
2641 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2642 941e9f74 2019-05-21 stsp subpath);
2643 941e9f74 2019-05-21 stsp if (err)
2644 941e9f74 2019-05-21 stsp break;
2645 941e9f74 2019-05-21 stsp
2646 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2647 941e9f74 2019-05-21 stsp free(tree_id);
2648 941e9f74 2019-05-21 stsp if (err)
2649 941e9f74 2019-05-21 stsp break;
2650 941e9f74 2019-05-21 stsp
2651 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2652 941e9f74 2019-05-21 stsp if (err) {
2653 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2654 941e9f74 2019-05-21 stsp break;
2655 941e9f74 2019-05-21 stsp }
2656 941e9f74 2019-05-21 stsp if (slash == NULL)
2657 941e9f74 2019-05-21 stsp break;
2658 941e9f74 2019-05-21 stsp free(subpath);
2659 941e9f74 2019-05-21 stsp subpath = NULL;
2660 941e9f74 2019-05-21 stsp p = slash;
2661 941e9f74 2019-05-21 stsp }
2662 941e9f74 2019-05-21 stsp
2663 941e9f74 2019-05-21 stsp free(subpath);
2664 1a76625f 2018-10-22 stsp return err;
2665 61266923 2020-01-14 stsp }
2666 61266923 2020-01-14 stsp
2667 61266923 2020-01-14 stsp static const struct got_error *
2668 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2669 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2670 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2671 55cccc34 2020-02-20 stsp {
2672 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2673 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2674 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2675 55cccc34 2020-02-20 stsp
2676 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2677 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2678 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2679 55cccc34 2020-02-20 stsp
2680 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2681 bc573f3b 2021-07-10 stsp if (err)
2682 55cccc34 2020-02-20 stsp return err;
2683 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2684 55cccc34 2020-02-20 stsp
2685 55cccc34 2020-02-20 stsp *new_view = tree_view;
2686 55cccc34 2020-02-20 stsp
2687 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2688 55cccc34 2020-02-20 stsp return NULL;
2689 55cccc34 2020-02-20 stsp
2690 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2691 55cccc34 2020-02-20 stsp }
2692 55cccc34 2020-02-20 stsp
2693 55cccc34 2020-02-20 stsp static const struct got_error *
2694 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2695 61266923 2020-01-14 stsp {
2696 61266923 2020-01-14 stsp sigset_t sigset;
2697 61266923 2020-01-14 stsp int errcode;
2698 61266923 2020-01-14 stsp
2699 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2700 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2701 61266923 2020-01-14 stsp
2702 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2703 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2704 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2705 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2706 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2707 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2708 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2709 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2710 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2711 61266923 2020-01-14 stsp
2712 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2713 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2714 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2715 61266923 2020-01-14 stsp
2716 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2717 61266923 2020-01-14 stsp if (errcode)
2718 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2719 61266923 2020-01-14 stsp
2720 61266923 2020-01-14 stsp return NULL;
2721 1a76625f 2018-10-22 stsp }
2722 1a76625f 2018-10-22 stsp
2723 1a76625f 2018-10-22 stsp static void *
2724 1a76625f 2018-10-22 stsp log_thread(void *arg)
2725 1a76625f 2018-10-22 stsp {
2726 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2727 1a76625f 2018-10-22 stsp int errcode = 0;
2728 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2729 1a76625f 2018-10-22 stsp int done = 0;
2730 61266923 2020-01-14 stsp
2731 5629093a 2022-07-11 stsp /*
2732 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2733 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2734 5629093a 2022-07-11 stsp */
2735 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2736 5629093a 2022-07-11 stsp if (errcode) {
2737 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2738 61266923 2020-01-14 stsp return (void *)err;
2739 5629093a 2022-07-11 stsp }
2740 1a76625f 2018-10-22 stsp
2741 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2742 5629093a 2022-07-11 stsp if (err) {
2743 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2744 5629093a 2022-07-11 stsp goto done;
2745 5629093a 2022-07-11 stsp }
2746 5629093a 2022-07-11 stsp
2747 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2748 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2749 5629093a 2022-07-11 stsp if (errcode) {
2750 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2751 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2752 5629093a 2022-07-11 stsp goto done;
2753 5629093a 2022-07-11 stsp }
2754 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2755 1a76625f 2018-10-22 stsp if (err) {
2756 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2757 5629093a 2022-07-11 stsp goto done;
2758 1a76625f 2018-10-22 stsp err = NULL;
2759 1a76625f 2018-10-22 stsp done = 1;
2760 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2761 1a76625f 2018-10-22 stsp a->commits_needed--;
2762 1a76625f 2018-10-22 stsp
2763 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2764 3abe8080 2019-04-10 stsp if (errcode) {
2765 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2766 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2767 5629093a 2022-07-11 stsp goto done;
2768 3abe8080 2019-04-10 stsp } else if (*a->quit)
2769 1a76625f 2018-10-22 stsp done = 1;
2770 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2771 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2772 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2773 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2774 1a76625f 2018-10-22 stsp }
2775 1a76625f 2018-10-22 stsp
2776 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2777 7c1452c1 2020-03-26 stsp if (errcode) {
2778 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2779 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2780 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2781 5629093a 2022-07-11 stsp goto done;
2782 7c1452c1 2020-03-26 stsp }
2783 7c1452c1 2020-03-26 stsp
2784 1a76625f 2018-10-22 stsp if (done)
2785 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2786 7c1452c1 2020-03-26 stsp else {
2787 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2788 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2789 7c1452c1 2020-03-26 stsp &tog_mutex);
2790 5629093a 2022-07-11 stsp if (errcode) {
2791 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2792 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2793 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2794 5629093a 2022-07-11 stsp goto done;
2795 5629093a 2022-07-11 stsp }
2796 21355643 2020-12-06 stsp if (*a->quit)
2797 21355643 2020-12-06 stsp done = 1;
2798 7c1452c1 2020-03-26 stsp }
2799 1a76625f 2018-10-22 stsp }
2800 1a76625f 2018-10-22 stsp }
2801 3abe8080 2019-04-10 stsp a->log_complete = 1;
2802 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2803 5629093a 2022-07-11 stsp if (errcode)
2804 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2805 5629093a 2022-07-11 stsp done:
2806 5629093a 2022-07-11 stsp if (err) {
2807 5629093a 2022-07-11 stsp tog_thread_error = 1;
2808 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2809 5629093a 2022-07-11 stsp }
2810 1a76625f 2018-10-22 stsp return (void *)err;
2811 1a76625f 2018-10-22 stsp }
2812 1a76625f 2018-10-22 stsp
2813 1a76625f 2018-10-22 stsp static const struct got_error *
2814 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2815 1a76625f 2018-10-22 stsp {
2816 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2817 1a76625f 2018-10-22 stsp int errcode;
2818 1a76625f 2018-10-22 stsp
2819 1a76625f 2018-10-22 stsp if (s->thread) {
2820 1a76625f 2018-10-22 stsp s->quit = 1;
2821 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2822 1a76625f 2018-10-22 stsp if (errcode)
2823 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2824 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2825 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2826 1a76625f 2018-10-22 stsp if (errcode)
2827 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2828 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2829 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2830 1a76625f 2018-10-22 stsp if (errcode)
2831 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2832 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2833 1a76625f 2018-10-22 stsp if (errcode)
2834 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2835 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2836 1a76625f 2018-10-22 stsp s->thread = NULL;
2837 1a76625f 2018-10-22 stsp }
2838 1a76625f 2018-10-22 stsp
2839 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2840 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2841 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2842 74467cc8 2022-06-15 stsp }
2843 74467cc8 2022-06-15 stsp
2844 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2845 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2846 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2847 74467cc8 2022-06-15 stsp if (err == NULL)
2848 74467cc8 2022-06-15 stsp err = pack_err;
2849 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2850 1a76625f 2018-10-22 stsp }
2851 1a76625f 2018-10-22 stsp
2852 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2853 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2854 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2855 1a76625f 2018-10-22 stsp }
2856 1a76625f 2018-10-22 stsp
2857 5629093a 2022-07-11 stsp return err ? err : thread_err;
2858 9343a5fb 2018-06-23 stsp }
2859 9343a5fb 2018-06-23 stsp
2860 9343a5fb 2018-06-23 stsp static const struct got_error *
2861 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2862 1a76625f 2018-10-22 stsp {
2863 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2864 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2865 276b94a1 2020-11-13 naddy int errcode;
2866 1a76625f 2018-10-22 stsp
2867 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2868 276b94a1 2020-11-13 naddy
2869 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2870 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2871 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2872 276b94a1 2020-11-13 naddy
2873 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2874 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2875 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2876 276b94a1 2020-11-13 naddy
2877 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2878 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2879 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2880 1a76625f 2018-10-22 stsp free(s->start_id);
2881 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2882 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2883 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2884 1a76625f 2018-10-22 stsp return err;
2885 1a76625f 2018-10-22 stsp }
2886 1a76625f 2018-10-22 stsp
2887 1a76625f 2018-10-22 stsp static const struct got_error *
2888 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2889 60493ae3 2019-06-20 stsp {
2890 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2891 60493ae3 2019-06-20 stsp
2892 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2893 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2894 60493ae3 2019-06-20 stsp return NULL;
2895 60493ae3 2019-06-20 stsp }
2896 60493ae3 2019-06-20 stsp
2897 60493ae3 2019-06-20 stsp static const struct got_error *
2898 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2899 60493ae3 2019-06-20 stsp {
2900 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2901 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2902 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2903 60493ae3 2019-06-20 stsp
2904 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2905 f9686aa5 2020-03-27 stsp show_log_view(view);
2906 f9686aa5 2020-03-27 stsp update_panels();
2907 f9686aa5 2020-03-27 stsp doupdate();
2908 f9686aa5 2020-03-27 stsp
2909 96e2b566 2019-07-08 stsp if (s->search_entry) {
2910 13add988 2019-10-15 stsp int errcode, ch;
2911 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2912 13add988 2019-10-15 stsp if (errcode)
2913 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2914 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2915 13add988 2019-10-15 stsp ch = wgetch(view->window);
2916 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2917 13add988 2019-10-15 stsp if (errcode)
2918 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2919 13add988 2019-10-15 stsp "pthread_mutex_lock");
2920 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2921 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2922 678cbce5 2019-07-28 stsp return NULL;
2923 678cbce5 2019-07-28 stsp }
2924 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2925 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2926 96e2b566 2019-07-08 stsp else
2927 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2928 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2929 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2930 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2931 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2932 364ac6fd 2022-06-18 stsp
2933 364ac6fd 2022-06-18 stsp /*
2934 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2935 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2936 f704b35c 2022-06-18 stsp * might have changed.
2937 364ac6fd 2022-06-18 stsp */
2938 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2939 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2940 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2941 364ac6fd 2022-06-18 stsp else
2942 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2943 4bfe9f0a 2022-06-18 stsp } else {
2944 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2945 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2946 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2947 364ac6fd 2022-06-18 stsp else
2948 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2949 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2950 4bfe9f0a 2022-06-18 stsp }
2951 20be8d96 2019-06-21 stsp } else {
2952 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2953 20be8d96 2019-06-21 stsp }
2954 60493ae3 2019-06-20 stsp
2955 60493ae3 2019-06-20 stsp while (1) {
2956 13add988 2019-10-15 stsp int have_match = 0;
2957 13add988 2019-10-15 stsp
2958 60493ae3 2019-06-20 stsp if (entry == NULL) {
2959 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2960 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2961 f9967bca 2020-03-27 stsp view->search_next_done =
2962 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2963 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2964 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2965 f801134a 2019-06-25 stsp return NULL;
2966 60493ae3 2019-06-20 stsp }
2967 96e2b566 2019-07-08 stsp /*
2968 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2969 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2970 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2971 96e2b566 2019-07-08 stsp */
2972 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2973 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2974 60493ae3 2019-06-20 stsp }
2975 60493ae3 2019-06-20 stsp
2976 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2977 13add988 2019-10-15 stsp &view->regex);
2978 5943eee2 2019-08-13 stsp if (err)
2979 13add988 2019-10-15 stsp break;
2980 13add988 2019-10-15 stsp if (have_match) {
2981 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2982 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2983 60493ae3 2019-06-20 stsp break;
2984 60493ae3 2019-06-20 stsp }
2985 13add988 2019-10-15 stsp
2986 96e2b566 2019-07-08 stsp s->search_entry = entry;
2987 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2988 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2989 b1bf1435 2019-06-21 stsp else
2990 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2991 60493ae3 2019-06-20 stsp }
2992 60493ae3 2019-06-20 stsp
2993 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2994 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2995 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2996 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2997 60493ae3 2019-06-20 stsp if (err)
2998 60493ae3 2019-06-20 stsp return err;
2999 ead14cbe 2019-06-21 stsp cur++;
3000 ead14cbe 2019-06-21 stsp }
3001 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3002 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3003 60493ae3 2019-06-20 stsp if (err)
3004 60493ae3 2019-06-20 stsp return err;
3005 ead14cbe 2019-06-21 stsp cur--;
3006 60493ae3 2019-06-20 stsp }
3007 60493ae3 2019-06-20 stsp }
3008 60493ae3 2019-06-20 stsp
3009 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3010 96e2b566 2019-07-08 stsp
3011 60493ae3 2019-06-20 stsp return NULL;
3012 60493ae3 2019-06-20 stsp }
3013 60493ae3 2019-06-20 stsp
3014 60493ae3 2019-06-20 stsp static const struct got_error *
3015 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3016 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3017 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3018 80ddbec8 2018-04-29 stsp {
3019 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3020 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3021 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3022 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3023 1a76625f 2018-10-22 stsp int errcode;
3024 80ddbec8 2018-04-29 stsp
3025 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3026 f135c941 2020-02-20 stsp free(s->in_repo_path);
3027 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3028 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3029 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3030 f135c941 2020-02-20 stsp }
3031 ecb28ae0 2018-07-16 stsp
3032 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3033 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3034 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3035 78756c87 2020-11-24 stsp
3036 fb2756b9 2018-08-04 stsp s->repo = repo;
3037 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3038 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3039 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3040 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3041 9cd7cbd1 2020-12-07 stsp goto done;
3042 9cd7cbd1 2020-12-07 stsp }
3043 9cd7cbd1 2020-12-07 stsp }
3044 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3045 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3046 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3047 5036bf37 2018-09-24 stsp goto done;
3048 5036bf37 2018-09-24 stsp }
3049 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3050 e5a0f69f 2018-08-18 stsp
3051 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3052 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3053 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3054 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3055 11b20872 2019-11-08 stsp if (err)
3056 11b20872 2019-11-08 stsp goto done;
3057 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3058 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3059 11b20872 2019-11-08 stsp if (err) {
3060 11b20872 2019-11-08 stsp free_colors(&s->colors);
3061 11b20872 2019-11-08 stsp goto done;
3062 11b20872 2019-11-08 stsp }
3063 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3064 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3065 11b20872 2019-11-08 stsp if (err) {
3066 11b20872 2019-11-08 stsp free_colors(&s->colors);
3067 11b20872 2019-11-08 stsp goto done;
3068 11b20872 2019-11-08 stsp }
3069 11b20872 2019-11-08 stsp }
3070 11b20872 2019-11-08 stsp
3071 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3072 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3073 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3074 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3075 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3076 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3077 1a76625f 2018-10-22 stsp
3078 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3079 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3080 74467cc8 2022-06-15 stsp if (err)
3081 74467cc8 2022-06-15 stsp goto done;
3082 74467cc8 2022-06-15 stsp }
3083 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3084 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3085 0ae84acc 2022-06-15 tracey if (err)
3086 0ae84acc 2022-06-15 tracey goto done;
3087 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3088 b672a97a 2020-01-27 stsp !s->log_branches);
3089 1a76625f 2018-10-22 stsp if (err)
3090 1a76625f 2018-10-22 stsp goto done;
3091 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3092 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3093 c5b78334 2020-01-12 stsp if (err)
3094 c5b78334 2020-01-12 stsp goto done;
3095 1a76625f 2018-10-22 stsp
3096 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3097 1a76625f 2018-10-22 stsp if (errcode) {
3098 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3099 1a76625f 2018-10-22 stsp goto done;
3100 1a76625f 2018-10-22 stsp }
3101 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3102 7c1452c1 2020-03-26 stsp if (errcode) {
3103 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3104 7c1452c1 2020-03-26 stsp goto done;
3105 7c1452c1 2020-03-26 stsp }
3106 1a76625f 2018-10-22 stsp
3107 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3108 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3109 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3110 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3111 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3112 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3113 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3114 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3115 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3116 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3117 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3118 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3119 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3120 ba4f502b 2018-08-04 stsp done:
3121 1a76625f 2018-10-22 stsp if (err)
3122 1a76625f 2018-10-22 stsp close_log_view(view);
3123 ba4f502b 2018-08-04 stsp return err;
3124 ba4f502b 2018-08-04 stsp }
3125 ba4f502b 2018-08-04 stsp
3126 e5a0f69f 2018-08-18 stsp static const struct got_error *
3127 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3128 ba4f502b 2018-08-04 stsp {
3129 f2f6d207 2020-11-24 stsp const struct got_error *err;
3130 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3131 ba4f502b 2018-08-04 stsp
3132 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3133 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3134 2b380cc8 2018-10-24 stsp &s->thread_args);
3135 2b380cc8 2018-10-24 stsp if (errcode)
3136 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3137 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3138 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3139 f2f6d207 2020-11-24 stsp if (err)
3140 f2f6d207 2020-11-24 stsp return err;
3141 f2f6d207 2020-11-24 stsp }
3142 2b380cc8 2018-10-24 stsp }
3143 2b380cc8 2018-10-24 stsp
3144 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3145 b880cc75 2022-06-30 mark }
3146 b880cc75 2022-06-30 mark
3147 b880cc75 2022-06-30 mark static void
3148 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3149 b880cc75 2022-06-30 mark {
3150 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3151 b880cc75 2022-06-30 mark
3152 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3153 b880cc75 2022-06-30 mark view->count = 0;
3154 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3155 b880cc75 2022-06-30 mark return;
3156 b880cc75 2022-06-30 mark
3157 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3158 b880cc75 2022-06-30 mark || home)
3159 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3160 b880cc75 2022-06-30 mark
3161 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3162 b880cc75 2022-06-30 mark --s->selected;
3163 b880cc75 2022-06-30 mark else
3164 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3165 b880cc75 2022-06-30 mark
3166 b880cc75 2022-06-30 mark select_commit(s);
3167 b880cc75 2022-06-30 mark return;
3168 b880cc75 2022-06-30 mark }
3169 b880cc75 2022-06-30 mark
3170 b880cc75 2022-06-30 mark static const struct got_error *
3171 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3172 b880cc75 2022-06-30 mark {
3173 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3174 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3175 631e7531 2022-08-12 mark int eos = view->nlines - 2;
3176 b880cc75 2022-06-30 mark
3177 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3178 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3179 b880cc75 2022-06-30 mark return NULL;
3180 b880cc75 2022-06-30 mark
3181 631e7531 2022-08-12 mark if (view_is_hsplit_top(view))
3182 631e7531 2022-08-12 mark --eos; /* border consumes the last line */
3183 b880cc75 2022-06-30 mark
3184 631e7531 2022-08-12 mark if (!page) {
3185 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3186 b880cc75 2022-06-30 mark ++s->selected;
3187 b880cc75 2022-06-30 mark else
3188 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3189 11edf34c 2022-08-12 mark } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3190 631e7531 2022-08-12 mark struct commit_queue_entry *entry;
3191 631e7531 2022-08-12 mark int n;
3192 631e7531 2022-08-12 mark
3193 631e7531 2022-08-12 mark s->selected = 0;
3194 631e7531 2022-08-12 mark entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3195 631e7531 2022-08-12 mark s->last_displayed_entry = entry;
3196 631e7531 2022-08-12 mark for (n = 0; n <= eos; n++) {
3197 631e7531 2022-08-12 mark if (entry == NULL)
3198 631e7531 2022-08-12 mark break;
3199 631e7531 2022-08-12 mark s->first_displayed_entry = entry;
3200 631e7531 2022-08-12 mark entry = TAILQ_PREV(entry, commit_queue_head, entry);
3201 631e7531 2022-08-12 mark }
3202 631e7531 2022-08-12 mark if (n > 0)
3203 631e7531 2022-08-12 mark s->selected = n - 1;
3204 b880cc75 2022-06-30 mark } else {
3205 cbb0c8d7 2022-08-12 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1 &&
3206 cbb0c8d7 2022-08-12 mark s->thread_args.log_complete)
3207 cbb0c8d7 2022-08-12 mark s->selected += MIN(page,
3208 cbb0c8d7 2022-08-12 mark s->commits.ncommits - s->selected_entry->idx - 1);
3209 cbb0c8d7 2022-08-12 mark else
3210 cbb0c8d7 2022-08-12 mark err = log_scroll_down(view, page);
3211 b880cc75 2022-06-30 mark }
3212 b880cc75 2022-06-30 mark if (err)
3213 b880cc75 2022-06-30 mark return err;
3214 b880cc75 2022-06-30 mark
3215 9b058f45 2022-06-30 mark /*
3216 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3217 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3218 9b058f45 2022-06-30 mark */
3219 374f69dd 2022-08-13 stsp if (s->first_displayed_entry && s->last_displayed_entry) {
3220 374f69dd 2022-08-13 stsp s->selected = MIN(s->selected,
3221 374f69dd 2022-08-13 stsp s->last_displayed_entry->idx -
3222 374f69dd 2022-08-13 stsp s->first_displayed_entry->idx);
3223 374f69dd 2022-08-13 stsp }
3224 9b058f45 2022-06-30 mark
3225 b880cc75 2022-06-30 mark select_commit(s);
3226 b880cc75 2022-06-30 mark
3227 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3228 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3229 b880cc75 2022-06-30 mark view->count = 0;
3230 b880cc75 2022-06-30 mark
3231 b880cc75 2022-06-30 mark return NULL;
3232 e5a0f69f 2018-08-18 stsp }
3233 04cc582a 2018-08-01 stsp
3234 9b058f45 2022-06-30 mark static void
3235 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3236 9b058f45 2022-06-30 mark {
3237 76364b2d 2022-07-02 mark *x = 0;
3238 76364b2d 2022-07-02 mark *y = 0;
3239 76364b2d 2022-07-02 mark
3240 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3241 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3242 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3243 7532ccda 2022-07-11 mark else if (view->resized_y)
3244 7532ccda 2022-07-11 mark *y = view->resized_y;
3245 3c1dfe12 2022-07-08 mark else
3246 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3247 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3248 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3249 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3250 7532ccda 2022-07-11 mark else if (view->resized_x)
3251 7532ccda 2022-07-11 mark *x = view->resized_x;
3252 3c1dfe12 2022-07-08 mark else
3253 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3254 3c1dfe12 2022-07-08 mark }
3255 9b058f45 2022-06-30 mark }
3256 9b058f45 2022-06-30 mark
3257 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3258 e5a0f69f 2018-08-18 stsp static const struct got_error *
3259 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3260 9b058f45 2022-06-30 mark {
3261 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3262 9b058f45 2022-06-30 mark
3263 9b058f45 2022-06-30 mark view->nlines = y;
3264 d2366e29 2022-07-07 mark view->ncols = COLS;
3265 9b058f45 2022-06-30 mark err = view_resize(view);
3266 9b058f45 2022-06-30 mark if (err)
3267 9b058f45 2022-06-30 mark return err;
3268 9b058f45 2022-06-30 mark
3269 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3270 9b058f45 2022-06-30 mark
3271 9b058f45 2022-06-30 mark return err;
3272 94b80cfa 2022-08-01 mark }
3273 94b80cfa 2022-08-01 mark
3274 94b80cfa 2022-08-01 mark static const struct got_error *
3275 94b80cfa 2022-08-01 mark log_goto_line(struct tog_view *view, int nlines)
3276 94b80cfa 2022-08-01 mark {
3277 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
3278 94b80cfa 2022-08-01 mark struct tog_log_view_state *s = &view->state.log;
3279 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
3280 374f69dd 2022-08-13 stsp
3281 374f69dd 2022-08-13 stsp if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3282 374f69dd 2022-08-13 stsp return NULL;
3283 94b80cfa 2022-08-01 mark
3284 94b80cfa 2022-08-01 mark g = view->gline;
3285 94b80cfa 2022-08-01 mark view->gline = 0;
3286 94b80cfa 2022-08-01 mark
3287 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
3288 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
3289 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
3290 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
3291 94b80cfa 2022-08-01 mark select_commit(s);
3292 94b80cfa 2022-08-01 mark return NULL;
3293 94b80cfa 2022-08-01 mark }
3294 94b80cfa 2022-08-01 mark
3295 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
3296 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view, g - idx - 1);
3297 94b80cfa 2022-08-01 mark if (!err && g > s->selected_entry->idx + 1)
3298 94b80cfa 2022-08-01 mark err = log_move_cursor_down(view,
3299 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1);
3300 94b80cfa 2022-08-01 mark if (err)
3301 94b80cfa 2022-08-01 mark return err;
3302 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
3303 94b80cfa 2022-08-01 mark log_move_cursor_up(view, idx - g + 1, 0);
3304 94b80cfa 2022-08-01 mark
3305 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
3306 94b80cfa 2022-08-01 mark s->selected = g - 1;
3307 94b80cfa 2022-08-01 mark
3308 94b80cfa 2022-08-01 mark select_commit(s);
3309 94b80cfa 2022-08-01 mark return NULL;
3310 94b80cfa 2022-08-01 mark
3311 9b058f45 2022-06-30 mark }
3312 9b058f45 2022-06-30 mark
3313 9b058f45 2022-06-30 mark static const struct got_error *
3314 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3315 e5a0f69f 2018-08-18 stsp {
3316 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3317 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3318 631e7531 2022-08-12 mark int eos, nscroll;
3319 80ddbec8 2018-04-29 stsp
3320 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3321 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3322 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3323 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3324 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3325 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3326 fb280deb 2021-08-30 stsp }
3327 11edf34c 2022-08-12 mark if (err)
3328 11edf34c 2022-08-12 mark return err;
3329 528dedf3 2021-08-30 stsp }
3330 0dca135e 2022-06-30 mark
3331 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3332 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3333 0dca135e 2022-06-30 mark --eos; /* border */
3334 94b80cfa 2022-08-01 mark
3335 94b80cfa 2022-08-01 mark if (view->gline)
3336 94b80cfa 2022-08-01 mark return log_goto_line(view, eos);
3337 0dca135e 2022-06-30 mark
3338 528dedf3 2021-08-30 stsp switch (ch) {
3339 1e37a5c2 2019-05-12 jcs case 'q':
3340 1e37a5c2 2019-05-12 jcs s->quit = 1;
3341 145b6838 2022-06-16 stsp break;
3342 145b6838 2022-06-16 stsp case '0':
3343 145b6838 2022-06-16 stsp view->x = 0;
3344 145b6838 2022-06-16 stsp break;
3345 145b6838 2022-06-16 stsp case '$':
3346 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3347 640cd7ff 2022-06-22 mark view->count = 0;
3348 145b6838 2022-06-16 stsp break;
3349 145b6838 2022-06-16 stsp case KEY_RIGHT:
3350 145b6838 2022-06-16 stsp case 'l':
3351 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3352 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3353 640cd7ff 2022-06-22 mark else
3354 640cd7ff 2022-06-22 mark view->count = 0;
3355 1e37a5c2 2019-05-12 jcs break;
3356 145b6838 2022-06-16 stsp case KEY_LEFT:
3357 145b6838 2022-06-16 stsp case 'h':
3358 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3359 640cd7ff 2022-06-22 mark if (view->x <= 0)
3360 640cd7ff 2022-06-22 mark view->count = 0;
3361 145b6838 2022-06-16 stsp break;
3362 1e37a5c2 2019-05-12 jcs case 'k':
3363 1e37a5c2 2019-05-12 jcs case KEY_UP:
3364 1e37a5c2 2019-05-12 jcs case '<':
3365 1e37a5c2 2019-05-12 jcs case ',':
3366 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3367 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3368 912a3f79 2021-08-30 j break;
3369 912a3f79 2021-08-30 j case 'g':
3370 912a3f79 2021-08-30 j case KEY_HOME:
3371 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3372 640cd7ff 2022-06-22 mark view->count = 0;
3373 1e37a5c2 2019-05-12 jcs break;
3374 83cc4199 2022-06-13 stsp case CTRL('u'):
3375 33c3719a 2022-06-15 stsp case 'u':
3376 83cc4199 2022-06-13 stsp nscroll /= 2;
3377 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3378 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3379 a4292ac5 2019-05-12 jcs case CTRL('b'):
3380 61417565 2022-06-20 mark case 'b':
3381 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3382 1e37a5c2 2019-05-12 jcs break;
3383 1e37a5c2 2019-05-12 jcs case 'j':
3384 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3385 1e37a5c2 2019-05-12 jcs case '>':
3386 1e37a5c2 2019-05-12 jcs case '.':
3387 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3388 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3389 912a3f79 2021-08-30 j break;
3390 10aab77f 2022-07-19 op case '@':
3391 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3392 10aab77f 2022-07-19 op break;
3393 912a3f79 2021-08-30 j case 'G':
3394 912a3f79 2021-08-30 j case KEY_END: {
3395 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3396 912a3f79 2021-08-30 j * traverse them all. */
3397 640cd7ff 2022-06-22 mark view->count = 0;
3398 631e7531 2022-08-12 mark s->thread_args.load_all = 1;
3399 631e7531 2022-08-12 mark if (!s->thread_args.log_complete)
3400 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3401 631e7531 2022-08-12 mark err = log_move_cursor_down(view, s->commits.ncommits);
3402 631e7531 2022-08-12 mark s->thread_args.load_all = 0;
3403 1e37a5c2 2019-05-12 jcs break;
3404 912a3f79 2021-08-30 j }
3405 80b7e8da 2022-06-11 stsp case CTRL('d'):
3406 33c3719a 2022-06-15 stsp case 'd':
3407 83cc4199 2022-06-13 stsp nscroll /= 2;
3408 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3409 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3410 61417565 2022-06-20 mark case CTRL('f'):
3411 48bb96f0 2022-06-20 naddy case 'f':
3412 b880cc75 2022-06-30 mark case ' ':
3413 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3414 1e37a5c2 2019-05-12 jcs break;
3415 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3416 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3417 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3418 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3419 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3420 2b779855 2020-12-05 naddy select_commit(s);
3421 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3422 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3423 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3424 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3425 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3426 0bf7f153 2020-12-02 naddy }
3427 1e37a5c2 2019-05-12 jcs break;
3428 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3429 49b24ee5 2022-07-03 mark case '\r':
3430 640cd7ff 2022-06-22 mark view->count = 0;
3431 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3432 e5a0f69f 2018-08-18 stsp break;
3433 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3434 1e37a5c2 2019-05-12 jcs break;
3435 5e98fb33 2022-07-22 mark case 'T':
3436 640cd7ff 2022-06-22 mark view->count = 0;
3437 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3438 5036bf37 2018-09-24 stsp break;
3439 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3440 1e37a5c2 2019-05-12 jcs break;
3441 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3442 21355643 2020-12-06 stsp case CTRL('l'):
3443 21355643 2020-12-06 stsp case 'B':
3444 640cd7ff 2022-06-22 mark view->count = 0;
3445 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3446 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3447 1e37a5c2 2019-05-12 jcs break;
3448 21355643 2020-12-06 stsp err = stop_log_thread(s);
3449 74cfe85e 2020-10-20 stsp if (err)
3450 74cfe85e 2020-10-20 stsp return err;
3451 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3452 21355643 2020-12-06 stsp char *parent_path;
3453 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3454 21355643 2020-12-06 stsp if (err)
3455 21355643 2020-12-06 stsp return err;
3456 21355643 2020-12-06 stsp free(s->in_repo_path);
3457 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3458 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3459 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3460 21355643 2020-12-06 stsp struct got_object_id *start_id;
3461 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3462 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3463 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3464 21355643 2020-12-06 stsp if (err)
3465 21355643 2020-12-06 stsp return err;
3466 21355643 2020-12-06 stsp free(s->start_id);
3467 21355643 2020-12-06 stsp s->start_id = start_id;
3468 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3469 21355643 2020-12-06 stsp } else /* 'B' */
3470 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3471 21355643 2020-12-06 stsp
3472 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3473 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3474 b0dd8d27 2022-06-16 stsp if (err)
3475 b0dd8d27 2022-06-16 stsp return err;
3476 b0dd8d27 2022-06-16 stsp }
3477 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3478 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3479 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3480 74cfe85e 2020-10-20 stsp if (err)
3481 21355643 2020-12-06 stsp return err;
3482 51a10b52 2020-12-26 stsp tog_free_refs();
3483 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3484 ca51c541 2020-12-07 stsp if (err)
3485 ca51c541 2020-12-07 stsp return err;
3486 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3487 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3488 d01904d4 2019-06-25 stsp if (err)
3489 d01904d4 2019-06-25 stsp return err;
3490 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3491 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3492 b672a97a 2020-01-27 stsp if (err)
3493 b672a97a 2020-01-27 stsp return err;
3494 21355643 2020-12-06 stsp free_commits(&s->commits);
3495 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3496 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3497 21355643 2020-12-06 stsp s->selected_entry = NULL;
3498 21355643 2020-12-06 stsp s->selected = 0;
3499 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3500 21355643 2020-12-06 stsp s->quit = 0;
3501 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3502 dfee752e 2022-06-18 op s->matched_entry = NULL;
3503 dfee752e 2022-06-18 op s->search_entry = NULL;
3504 01a7bcaf 2022-07-22 mark view->offset = 0;
3505 d01904d4 2019-06-25 stsp break;
3506 5e98fb33 2022-07-22 mark case 'R':
3507 640cd7ff 2022-06-22 mark view->count = 0;
3508 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3509 6458efa5 2020-11-24 stsp break;
3510 1e37a5c2 2019-05-12 jcs default:
3511 640cd7ff 2022-06-22 mark view->count = 0;
3512 1e37a5c2 2019-05-12 jcs break;
3513 899d86c2 2018-05-10 stsp }
3514 e5a0f69f 2018-08-18 stsp
3515 80ddbec8 2018-04-29 stsp return err;
3516 80ddbec8 2018-04-29 stsp }
3517 80ddbec8 2018-04-29 stsp
3518 4ed7e80c 2018-05-20 stsp static const struct got_error *
3519 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3520 c2db6724 2019-01-04 stsp {
3521 c2db6724 2019-01-04 stsp const struct got_error *error;
3522 c2db6724 2019-01-04 stsp
3523 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3524 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3525 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3526 37c06ea4 2019-07-15 stsp #endif
3527 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3528 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3529 c2db6724 2019-01-04 stsp
3530 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3531 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3532 c2db6724 2019-01-04 stsp
3533 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3534 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3535 c2db6724 2019-01-04 stsp
3536 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3537 c2db6724 2019-01-04 stsp if (error != NULL)
3538 c2db6724 2019-01-04 stsp return error;
3539 c2db6724 2019-01-04 stsp
3540 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3541 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3542 c2db6724 2019-01-04 stsp
3543 c2db6724 2019-01-04 stsp return NULL;
3544 c2db6724 2019-01-04 stsp }
3545 c2db6724 2019-01-04 stsp
3546 a915003a 2019-02-05 stsp static void
3547 a915003a 2019-02-05 stsp init_curses(void)
3548 a915003a 2019-02-05 stsp {
3549 2497f032 2022-05-31 stsp /*
3550 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3551 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3552 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3553 2497f032 2022-05-31 stsp */
3554 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3555 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3556 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3557 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3558 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3559 2497f032 2022-05-31 stsp
3560 a915003a 2019-02-05 stsp initscr();
3561 a915003a 2019-02-05 stsp cbreak();
3562 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3563 a915003a 2019-02-05 stsp noecho();
3564 a915003a 2019-02-05 stsp nonl();
3565 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3566 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3567 a915003a 2019-02-05 stsp curs_set(0);
3568 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3569 6d17833f 2019-11-08 stsp start_color();
3570 6d17833f 2019-11-08 stsp use_default_colors();
3571 6d17833f 2019-11-08 stsp }
3572 a915003a 2019-02-05 stsp }
3573 a915003a 2019-02-05 stsp
3574 c2db6724 2019-01-04 stsp static const struct got_error *
3575 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3576 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3577 f135c941 2020-02-20 stsp {
3578 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3579 f135c941 2020-02-20 stsp
3580 f135c941 2020-02-20 stsp if (argc == 0) {
3581 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3582 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3583 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3584 f135c941 2020-02-20 stsp return NULL;
3585 f135c941 2020-02-20 stsp }
3586 f135c941 2020-02-20 stsp
3587 f135c941 2020-02-20 stsp if (worktree) {
3588 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3589 bfd61697 2020-11-14 stsp char *p;
3590 f135c941 2020-02-20 stsp
3591 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3592 f135c941 2020-02-20 stsp if (err)
3593 f135c941 2020-02-20 stsp return err;
3594 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3595 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3596 bfd61697 2020-11-14 stsp p) == -1) {
3597 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3598 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3599 f135c941 2020-02-20 stsp }
3600 f135c941 2020-02-20 stsp free(p);
3601 f135c941 2020-02-20 stsp } else
3602 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3603 f135c941 2020-02-20 stsp
3604 f135c941 2020-02-20 stsp return err;
3605 f135c941 2020-02-20 stsp }
3606 f135c941 2020-02-20 stsp
3607 f135c941 2020-02-20 stsp static const struct got_error *
3608 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3609 9f7d7167 2018-04-29 stsp {
3610 80ddbec8 2018-04-29 stsp const struct got_error *error;
3611 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3612 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3613 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3614 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3615 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3616 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3617 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3618 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3619 04cc582a 2018-08-01 stsp struct tog_view *view;
3620 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3621 80ddbec8 2018-04-29 stsp
3622 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3623 80ddbec8 2018-04-29 stsp switch (ch) {
3624 b672a97a 2020-01-27 stsp case 'b':
3625 b672a97a 2020-01-27 stsp log_branches = 1;
3626 b672a97a 2020-01-27 stsp break;
3627 80ddbec8 2018-04-29 stsp case 'c':
3628 80ddbec8 2018-04-29 stsp start_commit = optarg;
3629 80ddbec8 2018-04-29 stsp break;
3630 ecb28ae0 2018-07-16 stsp case 'r':
3631 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3632 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3633 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3634 9ba1d308 2019-10-21 stsp optarg);
3635 ecb28ae0 2018-07-16 stsp break;
3636 80ddbec8 2018-04-29 stsp default:
3637 17020d27 2019-03-07 stsp usage_log();
3638 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3639 80ddbec8 2018-04-29 stsp }
3640 80ddbec8 2018-04-29 stsp }
3641 80ddbec8 2018-04-29 stsp
3642 80ddbec8 2018-04-29 stsp argc -= optind;
3643 80ddbec8 2018-04-29 stsp argv += optind;
3644 80ddbec8 2018-04-29 stsp
3645 f135c941 2020-02-20 stsp if (argc > 1)
3646 f135c941 2020-02-20 stsp usage_log();
3647 963f97a1 2019-03-18 stsp
3648 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3649 0ae84acc 2022-06-15 tracey if (error != NULL)
3650 0ae84acc 2022-06-15 tracey goto done;
3651 0ae84acc 2022-06-15 tracey
3652 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3653 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3654 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3655 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3656 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3657 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3658 c156c7a4 2020-12-18 stsp goto done;
3659 a1fbf39a 2019-08-11 stsp if (worktree)
3660 6962eb72 2020-02-20 stsp repo_path =
3661 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3662 a1fbf39a 2019-08-11 stsp else
3663 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3664 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3665 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3666 c156c7a4 2020-12-18 stsp goto done;
3667 c156c7a4 2020-12-18 stsp }
3668 ecb28ae0 2018-07-16 stsp }
3669 ecb28ae0 2018-07-16 stsp
3670 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3671 80ddbec8 2018-04-29 stsp if (error != NULL)
3672 ecb28ae0 2018-07-16 stsp goto done;
3673 80ddbec8 2018-04-29 stsp
3674 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3675 f135c941 2020-02-20 stsp repo, worktree);
3676 f135c941 2020-02-20 stsp if (error)
3677 f135c941 2020-02-20 stsp goto done;
3678 f135c941 2020-02-20 stsp
3679 f135c941 2020-02-20 stsp init_curses();
3680 f135c941 2020-02-20 stsp
3681 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3682 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3683 c02c541e 2019-03-29 stsp if (error)
3684 c02c541e 2019-03-29 stsp goto done;
3685 c02c541e 2019-03-29 stsp
3686 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3687 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3688 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3689 87670572 2020-12-26 naddy if (error)
3690 87670572 2020-12-26 naddy goto done;
3691 87670572 2020-12-26 naddy }
3692 51a10b52 2020-12-26 stsp
3693 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3694 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3695 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3696 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 d8f38dc4 2020-12-05 stsp if (error)
3698 d8f38dc4 2020-12-05 stsp goto done;
3699 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3700 d8f38dc4 2020-12-05 stsp } else {
3701 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3702 d8f38dc4 2020-12-05 stsp if (error == NULL)
3703 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3704 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3705 d8f38dc4 2020-12-05 stsp goto done;
3706 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3707 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3708 d8f38dc4 2020-12-05 stsp if (error)
3709 d8f38dc4 2020-12-05 stsp goto done;
3710 d8f38dc4 2020-12-05 stsp }
3711 8b473291 2019-02-21 stsp
3712 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3713 04cc582a 2018-08-01 stsp if (view == NULL) {
3714 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3715 04cc582a 2018-08-01 stsp goto done;
3716 04cc582a 2018-08-01 stsp }
3717 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3718 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3719 ba4f502b 2018-08-04 stsp if (error)
3720 ba4f502b 2018-08-04 stsp goto done;
3721 2fc00ff4 2019-08-31 stsp if (worktree) {
3722 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3723 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3724 2fc00ff4 2019-08-31 stsp worktree = NULL;
3725 2fc00ff4 2019-08-31 stsp }
3726 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3727 ecb28ae0 2018-07-16 stsp done:
3728 f135c941 2020-02-20 stsp free(in_repo_path);
3729 ecb28ae0 2018-07-16 stsp free(repo_path);
3730 ecb28ae0 2018-07-16 stsp free(cwd);
3731 899d86c2 2018-05-10 stsp free(start_id);
3732 d8f38dc4 2020-12-05 stsp free(label);
3733 d8f38dc4 2020-12-05 stsp if (ref)
3734 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3735 1d0f4054 2021-06-17 stsp if (repo) {
3736 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3737 1d0f4054 2021-06-17 stsp if (error == NULL)
3738 1d0f4054 2021-06-17 stsp error = close_err;
3739 1d0f4054 2021-06-17 stsp }
3740 ec142235 2019-03-07 stsp if (worktree)
3741 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3742 0ae84acc 2022-06-15 tracey if (pack_fds) {
3743 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3744 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3745 0ae84acc 2022-06-15 tracey if (error == NULL)
3746 0ae84acc 2022-06-15 tracey error = pack_err;
3747 0ae84acc 2022-06-15 tracey }
3748 51a10b52 2020-12-26 stsp tog_free_refs();
3749 80ddbec8 2018-04-29 stsp return error;
3750 9f7d7167 2018-04-29 stsp }
3751 9f7d7167 2018-04-29 stsp
3752 4ed7e80c 2018-05-20 stsp __dead static void
3753 9f7d7167 2018-04-29 stsp usage_diff(void)
3754 9f7d7167 2018-04-29 stsp {
3755 80ddbec8 2018-04-29 stsp endwin();
3756 827a167b 2022-08-16 stsp fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3757 827a167b 2022-08-16 stsp "object1 object2\n", getprogname());
3758 9f7d7167 2018-04-29 stsp exit(1);
3759 b304db33 2018-05-20 stsp }
3760 b304db33 2018-05-20 stsp
3761 6d17833f 2019-11-08 stsp static int
3762 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3763 41605754 2020-11-12 stsp regmatch_t *regmatch)
3764 6d17833f 2019-11-08 stsp {
3765 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3766 6d17833f 2019-11-08 stsp }
3767 6d17833f 2019-11-08 stsp
3768 336075a4 2022-06-25 op static struct tog_color *
3769 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3770 6d17833f 2019-11-08 stsp {
3771 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3772 6d17833f 2019-11-08 stsp
3773 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3774 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3775 6d17833f 2019-11-08 stsp return tc;
3776 6d17833f 2019-11-08 stsp }
3777 6d17833f 2019-11-08 stsp
3778 6d17833f 2019-11-08 stsp return NULL;
3779 6d17833f 2019-11-08 stsp }
3780 6d17833f 2019-11-08 stsp
3781 4ed7e80c 2018-05-20 stsp static const struct got_error *
3782 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3783 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3784 41605754 2020-11-12 stsp {
3785 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3786 44a87665 2022-06-16 stsp char *exstr = NULL;
3787 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3788 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3789 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3790 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3791 41605754 2020-11-12 stsp
3792 41605754 2020-11-12 stsp *wtotal = 0;
3793 1853e0f4 2022-06-16 stsp
3794 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3795 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3796 41605754 2020-11-12 stsp
3797 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3798 44a87665 2022-06-16 stsp if (err)
3799 44a87665 2022-06-16 stsp return err;
3800 44a87665 2022-06-16 stsp
3801 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3802 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3803 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3804 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3805 44a87665 2022-06-16 stsp goto done;
3806 44a87665 2022-06-16 stsp }
3807 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3808 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3809 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3810 1853e0f4 2022-06-16 stsp goto done;
3811 1853e0f4 2022-06-16 stsp }
3812 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3813 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3814 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3815 1853e0f4 2022-06-16 stsp goto done;
3816 1853e0f4 2022-06-16 stsp }
3817 145b6838 2022-06-16 stsp
3818 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3819 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3820 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3821 1853e0f4 2022-06-16 stsp if (err)
3822 1853e0f4 2022-06-16 stsp goto done;
3823 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3824 145b6838 2022-06-16 stsp if (n) {
3825 1853e0f4 2022-06-16 stsp free(wline);
3826 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3827 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3828 1853e0f4 2022-06-16 stsp if (err)
3829 1853e0f4 2022-06-16 stsp goto done;
3830 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3831 1853e0f4 2022-06-16 stsp wlimit -= width;
3832 1853e0f4 2022-06-16 stsp *wtotal += width;
3833 41605754 2020-11-12 stsp }
3834 41605754 2020-11-12 stsp
3835 41605754 2020-11-12 stsp if (wlimit > 0) {
3836 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3837 1853e0f4 2022-06-16 stsp size_t wlen;
3838 1853e0f4 2022-06-16 stsp
3839 1853e0f4 2022-06-16 stsp free(wline);
3840 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3841 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3842 1853e0f4 2022-06-16 stsp if (err)
3843 1853e0f4 2022-06-16 stsp goto done;
3844 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3845 1853e0f4 2022-06-16 stsp while (i < wlen) {
3846 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3847 1853e0f4 2022-06-16 stsp if (width == -1) {
3848 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3849 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3850 1853e0f4 2022-06-16 stsp goto done;
3851 1853e0f4 2022-06-16 stsp }
3852 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3853 1853e0f4 2022-06-16 stsp break;
3854 61417565 2022-06-20 mark w += width;
3855 1853e0f4 2022-06-16 stsp i++;
3856 41605754 2020-11-12 stsp }
3857 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3858 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3859 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3860 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3861 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3862 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3863 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3864 41605754 2020-11-12 stsp }
3865 41605754 2020-11-12 stsp }
3866 41605754 2020-11-12 stsp
3867 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3868 1853e0f4 2022-06-16 stsp free(wline);
3869 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3870 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3871 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3872 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3873 1853e0f4 2022-06-16 stsp if (err)
3874 1853e0f4 2022-06-16 stsp goto done;
3875 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3876 1853e0f4 2022-06-16 stsp } else {
3877 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3878 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3879 1853e0f4 2022-06-16 stsp if (err)
3880 1853e0f4 2022-06-16 stsp goto done;
3881 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3882 1853e0f4 2022-06-16 stsp }
3883 1853e0f4 2022-06-16 stsp *wtotal += width2;
3884 41605754 2020-11-12 stsp }
3885 1853e0f4 2022-06-16 stsp done:
3886 145b6838 2022-06-16 stsp free(wline);
3887 44a87665 2022-06-16 stsp free(exstr);
3888 1853e0f4 2022-06-16 stsp free(seg0);
3889 1853e0f4 2022-06-16 stsp free(seg1);
3890 1853e0f4 2022-06-16 stsp free(seg2);
3891 1853e0f4 2022-06-16 stsp return err;
3892 41605754 2020-11-12 stsp }
3893 94b80cfa 2022-08-01 mark
3894 94b80cfa 2022-08-01 mark static int
3895 94b80cfa 2022-08-01 mark gotoline(struct tog_view *view, int *lineno, int *nprinted)
3896 94b80cfa 2022-08-01 mark {
3897 94b80cfa 2022-08-01 mark FILE *f = NULL;
3898 94b80cfa 2022-08-01 mark int *eof, *first, *selected;
3899 94b80cfa 2022-08-01 mark
3900 94b80cfa 2022-08-01 mark if (view->type == TOG_VIEW_DIFF) {
3901 94b80cfa 2022-08-01 mark struct tog_diff_view_state *s = &view->state.diff;
3902 41605754 2020-11-12 stsp
3903 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3904 94b80cfa 2022-08-01 mark selected = first;
3905 94b80cfa 2022-08-01 mark eof = &s->eof;
3906 94b80cfa 2022-08-01 mark f = s->f;
3907 94b80cfa 2022-08-01 mark } else if (view->type == TOG_VIEW_BLAME) {
3908 94b80cfa 2022-08-01 mark struct tog_blame_view_state *s = &view->state.blame;
3909 94b80cfa 2022-08-01 mark
3910 94b80cfa 2022-08-01 mark first = &s->first_displayed_line;
3911 94b80cfa 2022-08-01 mark selected = &s->selected_line;
3912 94b80cfa 2022-08-01 mark eof = &s->eof;
3913 94b80cfa 2022-08-01 mark f = s->blame.f;
3914 94b80cfa 2022-08-01 mark } else
3915 94b80cfa 2022-08-01 mark return 0;
3916 94b80cfa 2022-08-01 mark
3917 94b80cfa 2022-08-01 mark /* Center gline in the middle of the page like vi(1). */
3918 94b80cfa 2022-08-01 mark if (*lineno < view->gline - (view->nlines - 3) / 2)
3919 94b80cfa 2022-08-01 mark return 0;
3920 94b80cfa 2022-08-01 mark if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
3921 94b80cfa 2022-08-01 mark rewind(f);
3922 94b80cfa 2022-08-01 mark *eof = 0;
3923 94b80cfa 2022-08-01 mark *first = 1;
3924 94b80cfa 2022-08-01 mark *lineno = 0;
3925 94b80cfa 2022-08-01 mark *nprinted = 0;
3926 94b80cfa 2022-08-01 mark return 0;
3927 94b80cfa 2022-08-01 mark }
3928 94b80cfa 2022-08-01 mark
3929 94b80cfa 2022-08-01 mark *selected = view->gline <= (view->nlines - 3) / 2 ?
3930 94b80cfa 2022-08-01 mark view->gline : (view->nlines - 3) / 2 + 1;
3931 94b80cfa 2022-08-01 mark view->gline = 0;
3932 94b80cfa 2022-08-01 mark
3933 94b80cfa 2022-08-01 mark return 1;
3934 94b80cfa 2022-08-01 mark }
3935 94b80cfa 2022-08-01 mark
3936 41605754 2020-11-12 stsp static const struct got_error *
3937 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3938 26ed57b2 2018-05-19 stsp {
3939 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3940 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3941 61e69b96 2018-05-20 stsp const struct got_error *err;
3942 c7d5c43c 2022-08-04 mark int nprinted = 0;
3943 b304db33 2018-05-20 stsp char *line;
3944 826082fe 2020-12-10 stsp size_t linesize = 0;
3945 826082fe 2020-12-10 stsp ssize_t linelen;
3946 61e69b96 2018-05-20 stsp wchar_t *wline;
3947 e0b650dd 2018-05-20 stsp int width;
3948 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3949 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3950 fe621944 2020-11-10 stsp off_t line_offset;
3951 26ed57b2 2018-05-19 stsp
3952 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1;
3953 c7d5c43c 2022-08-04 mark line_offset = s->lines[s->first_displayed_line - 1].offset;
3954 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3955 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3956 fe621944 2020-11-10 stsp
3957 f7d12f7e 2018-08-01 stsp werase(view->window);
3958 a3404814 2018-09-02 stsp
3959 94b80cfa 2022-08-01 mark if (view->gline > s->nlines - 1)
3960 94b80cfa 2022-08-01 mark view->gline = s->nlines - 1;
3961 94b80cfa 2022-08-01 mark
3962 a3404814 2018-09-02 stsp if (header) {
3963 94b80cfa 2022-08-01 mark int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
3964 94b80cfa 2022-08-01 mark 1 : view->gline - (view->nlines - 3) / 2 :
3965 c7d5c43c 2022-08-04 mark s->lineno + s->selected_line;
3966 94b80cfa 2022-08-01 mark
3967 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
3968 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3969 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3970 ccda2f4d 2022-06-16 stsp 0, 0);
3971 135a2da0 2020-11-11 stsp free(line);
3972 135a2da0 2020-11-11 stsp if (err)
3973 a3404814 2018-09-02 stsp return err;
3974 a3404814 2018-09-02 stsp
3975 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3976 a3404814 2018-09-02 stsp wstandout(view->window);
3977 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3978 e54cc94a 2020-11-11 stsp free(wline);
3979 e54cc94a 2020-11-11 stsp wline = NULL;
3980 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3981 a3404814 2018-09-02 stsp wstandend(view->window);
3982 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3983 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3984 26ed57b2 2018-05-19 stsp
3985 a3404814 2018-09-02 stsp if (max_lines <= 1)
3986 a3404814 2018-09-02 stsp return NULL;
3987 a3404814 2018-09-02 stsp max_lines--;
3988 a3404814 2018-09-02 stsp }
3989 a3404814 2018-09-02 stsp
3990 89f1a395 2020-12-01 naddy s->eof = 0;
3991 145b6838 2022-06-16 stsp view->maxx = 0;
3992 826082fe 2020-12-10 stsp line = NULL;
3993 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3994 6a5ff2d4 2022-08-06 mark enum got_diff_line_type linetype;
3995 6a5ff2d4 2022-08-06 mark attr_t attr = 0;
3996 6a5ff2d4 2022-08-06 mark
3997 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3998 826082fe 2020-12-10 stsp if (linelen == -1) {
3999 826082fe 2020-12-10 stsp if (feof(s->f)) {
4000 826082fe 2020-12-10 stsp s->eof = 1;
4001 826082fe 2020-12-10 stsp break;
4002 826082fe 2020-12-10 stsp }
4003 826082fe 2020-12-10 stsp free(line);
4004 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4005 61e69b96 2018-05-20 stsp }
4006 145b6838 2022-06-16 stsp
4007 c7d5c43c 2022-08-04 mark if (++s->lineno < s->first_displayed_line)
4008 94b80cfa 2022-08-01 mark continue;
4009 c7d5c43c 2022-08-04 mark if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4010 94b80cfa 2022-08-01 mark continue;
4011 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline)
4012 94b80cfa 2022-08-01 mark attr = A_STANDOUT;
4013 94b80cfa 2022-08-01 mark
4014 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4015 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4016 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
4017 1853e0f4 2022-06-16 stsp if (err) {
4018 1853e0f4 2022-06-16 stsp free(line);
4019 1853e0f4 2022-06-16 stsp return err;
4020 1853e0f4 2022-06-16 stsp }
4021 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4022 1853e0f4 2022-06-16 stsp free(wline);
4023 1853e0f4 2022-06-16 stsp wline = NULL;
4024 1853e0f4 2022-06-16 stsp
4025 6a5ff2d4 2022-08-06 mark linetype = s->lines[s->lineno].type;
4026 6a5ff2d4 2022-08-06 mark if (linetype > GOT_DIFF_LINE_LOGMSG &&
4027 6a5ff2d4 2022-08-06 mark linetype < GOT_DIFF_LINE_CONTEXT)
4028 6a5ff2d4 2022-08-06 mark attr |= COLOR_PAIR(linetype);
4029 94b80cfa 2022-08-01 mark if (attr)
4030 94b80cfa 2022-08-01 mark wattron(view->window, attr);
4031 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4032 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4033 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4034 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4035 41605754 2020-11-12 stsp if (err) {
4036 41605754 2020-11-12 stsp free(line);
4037 41605754 2020-11-12 stsp return err;
4038 41605754 2020-11-12 stsp }
4039 41605754 2020-11-12 stsp } else {
4040 969c159c 2022-06-16 stsp int skip;
4041 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4042 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
4043 969c159c 2022-06-16 stsp if (err) {
4044 969c159c 2022-06-16 stsp free(line);
4045 969c159c 2022-06-16 stsp return err;
4046 969c159c 2022-06-16 stsp }
4047 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4048 969c159c 2022-06-16 stsp free(wline);
4049 41605754 2020-11-12 stsp wline = NULL;
4050 41605754 2020-11-12 stsp }
4051 c7d5c43c 2022-08-04 mark if (s->lineno == view->hiline) {
4052 94b80cfa 2022-08-01 mark /* highlight full gline length */
4053 94b80cfa 2022-08-01 mark while (width++ < view->ncols)
4054 94b80cfa 2022-08-01 mark waddch(view->window, ' ');
4055 94b80cfa 2022-08-01 mark } else {
4056 94b80cfa 2022-08-01 mark if (width <= view->ncols - 1)
4057 94b80cfa 2022-08-01 mark waddch(view->window, '\n');
4058 94b80cfa 2022-08-01 mark }
4059 94b80cfa 2022-08-01 mark if (attr)
4060 94b80cfa 2022-08-01 mark wattroff(view->window, attr);
4061 94b80cfa 2022-08-01 mark if (++nprinted == 1)
4062 c7d5c43c 2022-08-04 mark s->first_displayed_line = s->lineno;
4063 826082fe 2020-12-10 stsp }
4064 826082fe 2020-12-10 stsp free(line);
4065 fe621944 2020-11-10 stsp if (nprinted >= 1)
4066 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4067 89f1a395 2020-12-01 naddy (nprinted - 1);
4068 fe621944 2020-11-10 stsp else
4069 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4070 26ed57b2 2018-05-19 stsp
4071 9b058f45 2022-06-30 mark view_border(view);
4072 c3e9aa98 2019-05-13 jcs
4073 89f1a395 2020-12-01 naddy if (s->eof) {
4074 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4075 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4076 c3e9aa98 2019-05-13 jcs nprinted++;
4077 c3e9aa98 2019-05-13 jcs }
4078 c3e9aa98 2019-05-13 jcs
4079 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4080 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
4081 c3e9aa98 2019-05-13 jcs if (err) {
4082 c3e9aa98 2019-05-13 jcs return err;
4083 c3e9aa98 2019-05-13 jcs }
4084 26ed57b2 2018-05-19 stsp
4085 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4086 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4087 e54cc94a 2020-11-11 stsp free(wline);
4088 e54cc94a 2020-11-11 stsp wline = NULL;
4089 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4090 c3e9aa98 2019-05-13 jcs }
4091 c3e9aa98 2019-05-13 jcs
4092 26ed57b2 2018-05-19 stsp return NULL;
4093 abd2672a 2018-12-23 stsp }
4094 abd2672a 2018-12-23 stsp
4095 abd2672a 2018-12-23 stsp static char *
4096 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4097 abd2672a 2018-12-23 stsp {
4098 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4099 09867e48 2019-08-13 stsp char *p, *s;
4100 09867e48 2019-08-13 stsp
4101 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4102 09867e48 2019-08-13 stsp if (tm == NULL)
4103 09867e48 2019-08-13 stsp return NULL;
4104 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4105 09867e48 2019-08-13 stsp if (s == NULL)
4106 09867e48 2019-08-13 stsp return NULL;
4107 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4108 abd2672a 2018-12-23 stsp if (p)
4109 abd2672a 2018-12-23 stsp *p = '\0';
4110 abd2672a 2018-12-23 stsp return s;
4111 9f7d7167 2018-04-29 stsp }
4112 9f7d7167 2018-04-29 stsp
4113 4ed7e80c 2018-05-20 stsp static const struct got_error *
4114 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4115 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4116 0208f208 2020-05-05 stsp {
4117 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4118 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4119 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4120 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4121 0208f208 2020-05-05 stsp
4122 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4123 0208f208 2020-05-05 stsp if (qid != NULL) {
4124 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4125 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4126 d7b5a0e8 2022-04-20 stsp &qid->id);
4127 0208f208 2020-05-05 stsp if (err)
4128 0208f208 2020-05-05 stsp return err;
4129 0208f208 2020-05-05 stsp
4130 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4131 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4132 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4133 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4134 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4135 aa8b5dd0 2021-08-01 stsp }
4136 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4137 0208f208 2020-05-05 stsp
4138 0208f208 2020-05-05 stsp }
4139 0208f208 2020-05-05 stsp
4140 0208f208 2020-05-05 stsp if (tree_id1) {
4141 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4142 0208f208 2020-05-05 stsp if (err)
4143 0208f208 2020-05-05 stsp goto done;
4144 0208f208 2020-05-05 stsp }
4145 0208f208 2020-05-05 stsp
4146 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4147 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4148 0208f208 2020-05-05 stsp if (err)
4149 0208f208 2020-05-05 stsp goto done;
4150 0208f208 2020-05-05 stsp
4151 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4152 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4153 0208f208 2020-05-05 stsp done:
4154 0208f208 2020-05-05 stsp if (tree1)
4155 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4156 0208f208 2020-05-05 stsp if (tree2)
4157 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4158 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4159 0208f208 2020-05-05 stsp return err;
4160 0208f208 2020-05-05 stsp }
4161 0208f208 2020-05-05 stsp
4162 0208f208 2020-05-05 stsp static const struct got_error *
4163 c7d5c43c 2022-08-04 mark add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4164 c7d5c43c 2022-08-04 mark off_t off, uint8_t type)
4165 abd2672a 2018-12-23 stsp {
4166 c7d5c43c 2022-08-04 mark struct got_diff_line *p;
4167 fe621944 2020-11-10 stsp
4168 c7d5c43c 2022-08-04 mark p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4169 fe621944 2020-11-10 stsp if (p == NULL)
4170 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4171 c7d5c43c 2022-08-04 mark *lines = p;
4172 c7d5c43c 2022-08-04 mark (*lines)[*nlines].offset = off;
4173 c7d5c43c 2022-08-04 mark (*lines)[*nlines].type = type;
4174 fe621944 2020-11-10 stsp (*nlines)++;
4175 c7d5c43c 2022-08-04 mark
4176 fe621944 2020-11-10 stsp return NULL;
4177 fe621944 2020-11-10 stsp }
4178 fe621944 2020-11-10 stsp
4179 fe621944 2020-11-10 stsp static const struct got_error *
4180 c7d5c43c 2022-08-04 mark write_commit_info(struct got_diff_line **lines, size_t *nlines,
4181 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4182 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4183 fe621944 2020-11-10 stsp {
4184 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4185 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4186 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4187 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4188 45d799e2 2018-12-23 stsp time_t committer_time;
4189 45d799e2 2018-12-23 stsp const char *author, *committer;
4190 8b473291 2019-02-21 stsp char *refs_str = NULL;
4191 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4192 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4193 fe621944 2020-11-10 stsp off_t outoff = 0;
4194 fe621944 2020-11-10 stsp int n;
4195 abd2672a 2018-12-23 stsp
4196 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4197 0208f208 2020-05-05 stsp
4198 8b473291 2019-02-21 stsp if (refs) {
4199 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4200 8b473291 2019-02-21 stsp if (err)
4201 8b473291 2019-02-21 stsp return err;
4202 8b473291 2019-02-21 stsp }
4203 8b473291 2019-02-21 stsp
4204 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4205 abd2672a 2018-12-23 stsp if (err)
4206 abd2672a 2018-12-23 stsp return err;
4207 abd2672a 2018-12-23 stsp
4208 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4209 15a94983 2018-12-23 stsp if (err) {
4210 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4211 15a94983 2018-12-23 stsp goto done;
4212 15a94983 2018-12-23 stsp }
4213 abd2672a 2018-12-23 stsp
4214 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4215 fe621944 2020-11-10 stsp if (err)
4216 fe621944 2020-11-10 stsp goto done;
4217 fe621944 2020-11-10 stsp
4218 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4219 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4220 fe621944 2020-11-10 stsp if (n < 0) {
4221 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4222 abd2672a 2018-12-23 stsp goto done;
4223 abd2672a 2018-12-23 stsp }
4224 fe621944 2020-11-10 stsp outoff += n;
4225 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4226 fe621944 2020-11-10 stsp if (err)
4227 fe621944 2020-11-10 stsp goto done;
4228 fe621944 2020-11-10 stsp
4229 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4230 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4231 fe621944 2020-11-10 stsp if (n < 0) {
4232 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4233 abd2672a 2018-12-23 stsp goto done;
4234 abd2672a 2018-12-23 stsp }
4235 fe621944 2020-11-10 stsp outoff += n;
4236 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4237 fe621944 2020-11-10 stsp if (err)
4238 fe621944 2020-11-10 stsp goto done;
4239 fe621944 2020-11-10 stsp
4240 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4241 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4242 fe621944 2020-11-10 stsp if (datestr) {
4243 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4244 fe621944 2020-11-10 stsp if (n < 0) {
4245 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4246 fe621944 2020-11-10 stsp goto done;
4247 fe621944 2020-11-10 stsp }
4248 fe621944 2020-11-10 stsp outoff += n;
4249 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4250 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_DATE);
4251 fe621944 2020-11-10 stsp if (err)
4252 fe621944 2020-11-10 stsp goto done;
4253 abd2672a 2018-12-23 stsp }
4254 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4255 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4256 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4257 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4258 fe621944 2020-11-10 stsp if (n < 0) {
4259 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4260 fe621944 2020-11-10 stsp goto done;
4261 fe621944 2020-11-10 stsp }
4262 fe621944 2020-11-10 stsp outoff += n;
4263 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4264 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_AUTHOR);
4265 fe621944 2020-11-10 stsp if (err)
4266 fe621944 2020-11-10 stsp goto done;
4267 abd2672a 2018-12-23 stsp }
4268 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4269 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4270 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4271 9f98ca05 2021-09-24 stsp int pn = 1;
4272 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4273 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4274 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4275 9f98ca05 2021-09-24 stsp if (err)
4276 9f98ca05 2021-09-24 stsp goto done;
4277 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4278 9f98ca05 2021-09-24 stsp if (n < 0) {
4279 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4280 9f98ca05 2021-09-24 stsp goto done;
4281 9f98ca05 2021-09-24 stsp }
4282 9f98ca05 2021-09-24 stsp outoff += n;
4283 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4284 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_META);
4285 9f98ca05 2021-09-24 stsp if (err)
4286 9f98ca05 2021-09-24 stsp goto done;
4287 9f98ca05 2021-09-24 stsp free(id_str);
4288 9f98ca05 2021-09-24 stsp id_str = NULL;
4289 9f98ca05 2021-09-24 stsp }
4290 9f98ca05 2021-09-24 stsp }
4291 9f98ca05 2021-09-24 stsp
4292 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4293 5943eee2 2019-08-13 stsp if (err)
4294 5943eee2 2019-08-13 stsp goto done;
4295 fe621944 2020-11-10 stsp s = logmsg;
4296 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4297 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4298 fe621944 2020-11-10 stsp if (n < 0) {
4299 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4300 fe621944 2020-11-10 stsp goto done;
4301 fe621944 2020-11-10 stsp }
4302 fe621944 2020-11-10 stsp outoff += n;
4303 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4304 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_LOGMSG);
4305 fe621944 2020-11-10 stsp if (err)
4306 fe621944 2020-11-10 stsp goto done;
4307 abd2672a 2018-12-23 stsp }
4308 fe621944 2020-11-10 stsp
4309 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4310 0208f208 2020-05-05 stsp if (err)
4311 0208f208 2020-05-05 stsp goto done;
4312 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4313 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4314 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4315 fe621944 2020-11-10 stsp if (n < 0) {
4316 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4317 fe621944 2020-11-10 stsp goto done;
4318 fe621944 2020-11-10 stsp }
4319 fe621944 2020-11-10 stsp outoff += n;
4320 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff,
4321 c7d5c43c 2022-08-04 mark GOT_DIFF_LINE_CHANGES);
4322 fe621944 2020-11-10 stsp if (err)
4323 fe621944 2020-11-10 stsp goto done;
4324 0208f208 2020-05-05 stsp free((char *)pe->path);
4325 0208f208 2020-05-05 stsp free(pe->data);
4326 0208f208 2020-05-05 stsp }
4327 fe621944 2020-11-10 stsp
4328 0208f208 2020-05-05 stsp fputc('\n', outfile);
4329 fe621944 2020-11-10 stsp outoff++;
4330 c7d5c43c 2022-08-04 mark err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4331 abd2672a 2018-12-23 stsp done:
4332 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4333 abd2672a 2018-12-23 stsp free(id_str);
4334 5943eee2 2019-08-13 stsp free(logmsg);
4335 8b473291 2019-02-21 stsp free(refs_str);
4336 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4337 7510f233 2020-08-09 stsp if (err) {
4338 c7d5c43c 2022-08-04 mark free(*lines);
4339 c7d5c43c 2022-08-04 mark *lines = NULL;
4340 ae6a6978 2020-08-09 stsp *nlines = 0;
4341 7510f233 2020-08-09 stsp }
4342 fe621944 2020-11-10 stsp return err;
4343 abd2672a 2018-12-23 stsp }
4344 abd2672a 2018-12-23 stsp
4345 abd2672a 2018-12-23 stsp static const struct got_error *
4346 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4347 26ed57b2 2018-05-19 stsp {
4348 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4349 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4350 15a94983 2018-12-23 stsp int obj_type;
4351 fe621944 2020-11-10 stsp
4352 c7d5c43c 2022-08-04 mark free(s->lines);
4353 c7d5c43c 2022-08-04 mark s->lines = malloc(sizeof(*s->lines));
4354 c7d5c43c 2022-08-04 mark if (s->lines == NULL)
4355 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4356 fe621944 2020-11-10 stsp s->nlines = 0;
4357 26ed57b2 2018-05-19 stsp
4358 511a516b 2018-05-19 stsp f = got_opentemp();
4359 48ae06ee 2018-10-18 stsp if (f == NULL) {
4360 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4361 48ae06ee 2018-10-18 stsp goto done;
4362 48ae06ee 2018-10-18 stsp }
4363 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4364 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4365 fb43ecf1 2019-02-11 stsp goto done;
4366 fb43ecf1 2019-02-11 stsp }
4367 48ae06ee 2018-10-18 stsp s->f = f;
4368 26ed57b2 2018-05-19 stsp
4369 15a94983 2018-12-23 stsp if (s->id1)
4370 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4371 15a94983 2018-12-23 stsp else
4372 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4373 15a94983 2018-12-23 stsp if (err)
4374 15a94983 2018-12-23 stsp goto done;
4375 15a94983 2018-12-23 stsp
4376 15a94983 2018-12-23 stsp switch (obj_type) {
4377 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4378 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4379 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4380 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4381 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4382 26ed57b2 2018-05-19 stsp break;
4383 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4384 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4385 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4386 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4387 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4388 26ed57b2 2018-05-19 stsp break;
4389 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4390 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4391 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4392 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4393 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4394 abd2672a 2018-12-23 stsp
4395 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4396 abd2672a 2018-12-23 stsp if (err)
4397 3ffacbe1 2020-02-02 tracey goto done;
4398 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4399 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4400 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4401 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines, &s->nlines, s->id2,
4402 c7d5c43c 2022-08-04 mark refs, s->repo, s->f);
4403 f44b1f58 2020-02-02 tracey if (err)
4404 f44b1f58 2020-02-02 tracey goto done;
4405 f44b1f58 2020-02-02 tracey } else {
4406 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4407 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4408 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4409 c7d5c43c 2022-08-04 mark err = write_commit_info(&s->lines,
4410 c7d5c43c 2022-08-04 mark &s->nlines, s->id2, refs, s->repo,
4411 c7d5c43c 2022-08-04 mark s->f);
4412 f44b1f58 2020-02-02 tracey if (err)
4413 f44b1f58 2020-02-02 tracey goto done;
4414 f5404e4e 2020-02-02 tracey break;
4415 15a087fe 2019-02-21 stsp }
4416 abd2672a 2018-12-23 stsp }
4417 abd2672a 2018-12-23 stsp }
4418 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4419 abd2672a 2018-12-23 stsp
4420 c7d5c43c 2022-08-04 mark err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4421 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4422 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4423 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4424 26ed57b2 2018-05-19 stsp break;
4425 abd2672a 2018-12-23 stsp }
4426 26ed57b2 2018-05-19 stsp default:
4427 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4428 48ae06ee 2018-10-18 stsp break;
4429 26ed57b2 2018-05-19 stsp }
4430 48ae06ee 2018-10-18 stsp done:
4431 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4432 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4433 48ae06ee 2018-10-18 stsp return err;
4434 48ae06ee 2018-10-18 stsp }
4435 26ed57b2 2018-05-19 stsp
4436 f5215bb9 2019-02-22 stsp static void
4437 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4438 f5215bb9 2019-02-22 stsp {
4439 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4440 f5215bb9 2019-02-22 stsp update_panels();
4441 f5215bb9 2019-02-22 stsp doupdate();
4442 f44b1f58 2020-02-02 tracey }
4443 f44b1f58 2020-02-02 tracey
4444 f44b1f58 2020-02-02 tracey static const struct got_error *
4445 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4446 f44b1f58 2020-02-02 tracey {
4447 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4448 f44b1f58 2020-02-02 tracey
4449 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4450 f44b1f58 2020-02-02 tracey return NULL;
4451 f44b1f58 2020-02-02 tracey }
4452 f44b1f58 2020-02-02 tracey
4453 f44b1f58 2020-02-02 tracey static const struct got_error *
4454 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4455 f44b1f58 2020-02-02 tracey {
4456 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4457 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4458 f44b1f58 2020-02-02 tracey int lineno;
4459 cb713507 2022-06-16 stsp char *line = NULL;
4460 826082fe 2020-12-10 stsp size_t linesize = 0;
4461 826082fe 2020-12-10 stsp ssize_t linelen;
4462 f44b1f58 2020-02-02 tracey
4463 f44b1f58 2020-02-02 tracey if (!view->searching) {
4464 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4465 f44b1f58 2020-02-02 tracey return NULL;
4466 f44b1f58 2020-02-02 tracey }
4467 f44b1f58 2020-02-02 tracey
4468 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4469 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4470 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4471 f44b1f58 2020-02-02 tracey else
4472 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4473 487cd7d2 2021-12-17 stsp } else
4474 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4475 f44b1f58 2020-02-02 tracey
4476 f44b1f58 2020-02-02 tracey while (1) {
4477 f44b1f58 2020-02-02 tracey off_t offset;
4478 f44b1f58 2020-02-02 tracey
4479 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4480 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4481 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4482 f44b1f58 2020-02-02 tracey break;
4483 f44b1f58 2020-02-02 tracey }
4484 f44b1f58 2020-02-02 tracey
4485 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4486 f44b1f58 2020-02-02 tracey lineno = 1;
4487 f44b1f58 2020-02-02 tracey else
4488 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4489 f44b1f58 2020-02-02 tracey }
4490 f44b1f58 2020-02-02 tracey
4491 c7d5c43c 2022-08-04 mark offset = s->lines[lineno - 1].offset;
4492 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4493 f44b1f58 2020-02-02 tracey free(line);
4494 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4495 f44b1f58 2020-02-02 tracey }
4496 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4497 cb713507 2022-06-16 stsp if (linelen != -1) {
4498 cb713507 2022-06-16 stsp char *exstr;
4499 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4500 cb713507 2022-06-16 stsp if (err)
4501 cb713507 2022-06-16 stsp break;
4502 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4503 cb713507 2022-06-16 stsp &view->regmatch)) {
4504 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4505 cb713507 2022-06-16 stsp s->matched_line = lineno;
4506 cb713507 2022-06-16 stsp free(exstr);
4507 cb713507 2022-06-16 stsp break;
4508 cb713507 2022-06-16 stsp }
4509 cb713507 2022-06-16 stsp free(exstr);
4510 f44b1f58 2020-02-02 tracey }
4511 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4512 f44b1f58 2020-02-02 tracey lineno++;
4513 f44b1f58 2020-02-02 tracey else
4514 f44b1f58 2020-02-02 tracey lineno--;
4515 f44b1f58 2020-02-02 tracey }
4516 826082fe 2020-12-10 stsp free(line);
4517 f44b1f58 2020-02-02 tracey
4518 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4519 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4520 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4521 f44b1f58 2020-02-02 tracey }
4522 f44b1f58 2020-02-02 tracey
4523 145b6838 2022-06-16 stsp return err;
4524 f5215bb9 2019-02-22 stsp }
4525 f5215bb9 2019-02-22 stsp
4526 48ae06ee 2018-10-18 stsp static const struct got_error *
4527 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4528 b72706c3 2022-06-01 stsp {
4529 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4530 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4531 b72706c3 2022-06-01 stsp
4532 b72706c3 2022-06-01 stsp free(s->id1);
4533 b72706c3 2022-06-01 stsp s->id1 = NULL;
4534 b72706c3 2022-06-01 stsp free(s->id2);
4535 b72706c3 2022-06-01 stsp s->id2 = NULL;
4536 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4537 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4538 b72706c3 2022-06-01 stsp s->f = NULL;
4539 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4540 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4541 b72706c3 2022-06-01 stsp s->f1 = NULL;
4542 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4543 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4544 b72706c3 2022-06-01 stsp s->f2 = NULL;
4545 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4546 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4547 f9d37699 2022-06-28 stsp s->fd1 = -1;
4548 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4549 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4550 f9d37699 2022-06-28 stsp s->fd2 = -1;
4551 c7d5c43c 2022-08-04 mark free(s->lines);
4552 c7d5c43c 2022-08-04 mark s->lines = NULL;
4553 b72706c3 2022-06-01 stsp s->nlines = 0;
4554 b72706c3 2022-06-01 stsp return err;
4555 b72706c3 2022-06-01 stsp }
4556 b72706c3 2022-06-01 stsp
4557 b72706c3 2022-06-01 stsp static const struct got_error *
4558 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4559 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4560 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4561 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4562 48ae06ee 2018-10-18 stsp {
4563 48ae06ee 2018-10-18 stsp const struct got_error *err;
4564 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4565 5dc9f4bc 2018-08-04 stsp
4566 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4567 f9d37699 2022-06-28 stsp s->fd1 = -1;
4568 f9d37699 2022-06-28 stsp s->fd2 = -1;
4569 b72706c3 2022-06-01 stsp
4570 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4571 15a94983 2018-12-23 stsp int type1, type2;
4572 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4573 15a94983 2018-12-23 stsp if (err)
4574 15a94983 2018-12-23 stsp return err;
4575 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4576 15a94983 2018-12-23 stsp if (err)
4577 15a94983 2018-12-23 stsp return err;
4578 15a94983 2018-12-23 stsp
4579 15a94983 2018-12-23 stsp if (type1 != type2)
4580 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4581 15a94983 2018-12-23 stsp }
4582 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4583 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4584 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4585 f44b1f58 2020-02-02 tracey s->repo = repo;
4586 f44b1f58 2020-02-02 tracey s->id1 = id1;
4587 f44b1f58 2020-02-02 tracey s->id2 = id2;
4588 3dbaef42 2020-11-24 stsp s->label1 = label1;
4589 3dbaef42 2020-11-24 stsp s->label2 = label2;
4590 48ae06ee 2018-10-18 stsp
4591 15a94983 2018-12-23 stsp if (id1) {
4592 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4593 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4594 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4595 48ae06ee 2018-10-18 stsp } else
4596 5465d566 2020-02-01 tracey s->id1 = NULL;
4597 48ae06ee 2018-10-18 stsp
4598 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4599 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4600 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4601 b72706c3 2022-06-01 stsp goto done;
4602 48ae06ee 2018-10-18 stsp }
4603 b72706c3 2022-06-01 stsp
4604 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4605 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4606 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4607 a00719e9 2022-06-17 stsp goto done;
4608 a00719e9 2022-06-17 stsp }
4609 a00719e9 2022-06-17 stsp
4610 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4611 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4612 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4613 b72706c3 2022-06-01 stsp goto done;
4614 b72706c3 2022-06-01 stsp }
4615 b72706c3 2022-06-01 stsp
4616 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4617 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4618 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4619 f9d37699 2022-06-28 stsp goto done;
4620 f9d37699 2022-06-28 stsp }
4621 f9d37699 2022-06-28 stsp
4622 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4623 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4624 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4625 f9d37699 2022-06-28 stsp goto done;
4626 f9d37699 2022-06-28 stsp }
4627 f9d37699 2022-06-28 stsp
4628 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4629 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4630 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4631 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4632 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4633 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4634 5465d566 2020-02-01 tracey s->repo = repo;
4635 6d17833f 2019-11-08 stsp
4636 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4637 6a5ff2d4 2022-08-06 mark int rc;
4638 6d17833f 2019-11-08 stsp
4639 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_MINUS,
4640 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4641 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4642 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_PLUS,
4643 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4644 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4645 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_HUNK,
4646 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4647 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4648 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_META,
4649 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4650 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4651 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_CHANGES,
4652 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4653 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4654 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4655 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4656 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4657 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4658 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DIFF_META"), -1);
4659 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4660 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4661 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_AUTHOR"), -1);
4662 6a5ff2d4 2022-08-06 mark if (rc != ERR)
4663 6a5ff2d4 2022-08-06 mark rc = init_pair(GOT_DIFF_LINE_DATE,
4664 6a5ff2d4 2022-08-06 mark get_color_value("TOG_COLOR_DATE"), -1);
4665 6a5ff2d4 2022-08-06 mark if (rc == ERR) {
4666 6a5ff2d4 2022-08-06 mark err = got_error(GOT_ERR_RANGE);
4667 b72706c3 2022-06-01 stsp goto done;
4668 6a5ff2d4 2022-08-06 mark }
4669 6d17833f 2019-11-08 stsp }
4670 5dc9f4bc 2018-08-04 stsp
4671 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4672 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4673 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4674 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4675 f5215bb9 2019-02-22 stsp
4676 5465d566 2020-02-01 tracey err = create_diff(s);
4677 48ae06ee 2018-10-18 stsp
4678 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4679 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4680 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4681 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4682 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4683 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4684 b72706c3 2022-06-01 stsp done:
4685 b72706c3 2022-06-01 stsp if (err)
4686 b72706c3 2022-06-01 stsp close_diff_view(view);
4687 e5a0f69f 2018-08-18 stsp return err;
4688 5dc9f4bc 2018-08-04 stsp }
4689 5dc9f4bc 2018-08-04 stsp
4690 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4691 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4692 5dc9f4bc 2018-08-04 stsp {
4693 a3404814 2018-09-02 stsp const struct got_error *err;
4694 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4695 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4696 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4697 a3404814 2018-09-02 stsp
4698 a3404814 2018-09-02 stsp if (s->id1) {
4699 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4700 a3404814 2018-09-02 stsp if (err)
4701 a3404814 2018-09-02 stsp return err;
4702 4924afe1 2022-08-31 mark label1 = s->label1 ? s->label1 : id_str1;
4703 3dbaef42 2020-11-24 stsp } else
4704 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4705 3dbaef42 2020-11-24 stsp
4706 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4707 a3404814 2018-09-02 stsp if (err)
4708 a3404814 2018-09-02 stsp return err;
4709 4924afe1 2022-08-31 mark label2 = s->label2 ? s->label2 : id_str2;
4710 26ed57b2 2018-05-19 stsp
4711 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4712 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4713 a3404814 2018-09-02 stsp free(id_str1);
4714 a3404814 2018-09-02 stsp free(id_str2);
4715 a3404814 2018-09-02 stsp return err;
4716 a3404814 2018-09-02 stsp }
4717 a3404814 2018-09-02 stsp free(id_str1);
4718 a3404814 2018-09-02 stsp free(id_str2);
4719 a3404814 2018-09-02 stsp
4720 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4721 267bb3b8 2021-08-01 stsp free(header);
4722 267bb3b8 2021-08-01 stsp return err;
4723 15a087fe 2019-02-21 stsp }
4724 15a087fe 2019-02-21 stsp
4725 15a087fe 2019-02-21 stsp static const struct got_error *
4726 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4727 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4728 15a087fe 2019-02-21 stsp {
4729 d7a04538 2019-02-21 stsp const struct got_error *err;
4730 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4731 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4732 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4733 15a087fe 2019-02-21 stsp
4734 15a087fe 2019-02-21 stsp free(s->id2);
4735 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4736 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4737 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4738 15a087fe 2019-02-21 stsp
4739 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4740 d7a04538 2019-02-21 stsp if (err)
4741 d7a04538 2019-02-21 stsp return err;
4742 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4743 15a087fe 2019-02-21 stsp free(s->id1);
4744 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4745 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4746 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4747 15a087fe 2019-02-21 stsp return NULL;
4748 0cf4efb1 2018-09-29 stsp }
4749 0cf4efb1 2018-09-29 stsp
4750 0cf4efb1 2018-09-29 stsp static const struct got_error *
4751 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4752 917d79a7 2022-07-01 stsp {
4753 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4754 917d79a7 2022-07-01 stsp
4755 917d79a7 2022-07-01 stsp view->count = 0;
4756 917d79a7 2022-07-01 stsp wclear(view->window);
4757 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4758 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4759 917d79a7 2022-07-01 stsp s->matched_line = 0;
4760 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4761 917d79a7 2022-07-01 stsp return create_diff(s);
4762 c7d5c43c 2022-08-04 mark }
4763 c7d5c43c 2022-08-04 mark
4764 c7d5c43c 2022-08-04 mark static void
4765 c7d5c43c 2022-08-04 mark diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4766 c7d5c43c 2022-08-04 mark {
4767 c7d5c43c 2022-08-04 mark int start, i;
4768 c7d5c43c 2022-08-04 mark
4769 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line - 1;
4770 c7d5c43c 2022-08-04 mark
4771 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4772 c7d5c43c 2022-08-04 mark if (i == 0)
4773 c7d5c43c 2022-08-04 mark i = s->nlines - 1;
4774 c7d5c43c 2022-08-04 mark if (--i == start)
4775 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4776 c7d5c43c 2022-08-04 mark }
4777 c7d5c43c 2022-08-04 mark
4778 c7d5c43c 2022-08-04 mark s->selected_line = 1;
4779 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
4780 917d79a7 2022-07-01 stsp }
4781 917d79a7 2022-07-01 stsp
4782 c7d5c43c 2022-08-04 mark static void
4783 c7d5c43c 2022-08-04 mark diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4784 c7d5c43c 2022-08-04 mark {
4785 c7d5c43c 2022-08-04 mark int start, i;
4786 c7d5c43c 2022-08-04 mark
4787 c7d5c43c 2022-08-04 mark i = start = s->first_displayed_line + 1;
4788 c7d5c43c 2022-08-04 mark
4789 c7d5c43c 2022-08-04 mark while (s->lines[i].type != type) {
4790 c7d5c43c 2022-08-04 mark if (i == s->nlines - 1)
4791 c7d5c43c 2022-08-04 mark i = 0;
4792 c7d5c43c 2022-08-04 mark if (++i == start)
4793 c7d5c43c 2022-08-04 mark return; /* do nothing, requested type not in file */
4794 c7d5c43c 2022-08-04 mark }
4795 c7d5c43c 2022-08-04 mark
4796 c7d5c43c 2022-08-04 mark s->selected_line = 1;
4797 c7d5c43c 2022-08-04 mark s->first_displayed_line = i;
4798 c7d5c43c 2022-08-04 mark }
4799 c7d5c43c 2022-08-04 mark
4800 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4801 c0f61fa4 2022-07-11 mark int, int, int);
4802 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4803 c0f61fa4 2022-07-11 mark int, int);
4804 c0f61fa4 2022-07-11 mark
4805 917d79a7 2022-07-01 stsp static const struct got_error *
4806 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4807 e5a0f69f 2018-08-18 stsp {
4808 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4809 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4810 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4811 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4812 826082fe 2020-12-10 stsp char *line = NULL;
4813 826082fe 2020-12-10 stsp size_t linesize = 0;
4814 826082fe 2020-12-10 stsp ssize_t linelen;
4815 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4816 e5a0f69f 2018-08-18 stsp
4817 c7d5c43c 2022-08-04 mark s->lineno = s->first_displayed_line - 1 + s->selected_line;
4818 c7d5c43c 2022-08-04 mark
4819 e5a0f69f 2018-08-18 stsp switch (ch) {
4820 145b6838 2022-06-16 stsp case '0':
4821 145b6838 2022-06-16 stsp view->x = 0;
4822 145b6838 2022-06-16 stsp break;
4823 145b6838 2022-06-16 stsp case '$':
4824 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4825 640cd7ff 2022-06-22 mark view->count = 0;
4826 145b6838 2022-06-16 stsp break;
4827 145b6838 2022-06-16 stsp case KEY_RIGHT:
4828 145b6838 2022-06-16 stsp case 'l':
4829 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4830 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4831 640cd7ff 2022-06-22 mark else
4832 640cd7ff 2022-06-22 mark view->count = 0;
4833 145b6838 2022-06-16 stsp break;
4834 145b6838 2022-06-16 stsp case KEY_LEFT:
4835 145b6838 2022-06-16 stsp case 'h':
4836 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4837 640cd7ff 2022-06-22 mark if (view->x <= 0)
4838 640cd7ff 2022-06-22 mark view->count = 0;
4839 145b6838 2022-06-16 stsp break;
4840 64453f7e 2020-11-21 stsp case 'a':
4841 3dbaef42 2020-11-24 stsp case 'w':
4842 3dbaef42 2020-11-24 stsp if (ch == 'a')
4843 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4844 3dbaef42 2020-11-24 stsp if (ch == 'w')
4845 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4846 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4847 912a3f79 2021-08-30 j break;
4848 912a3f79 2021-08-30 j case 'g':
4849 912a3f79 2021-08-30 j case KEY_HOME:
4850 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4851 640cd7ff 2022-06-22 mark view->count = 0;
4852 64453f7e 2020-11-21 stsp break;
4853 912a3f79 2021-08-30 j case 'G':
4854 912a3f79 2021-08-30 j case KEY_END:
4855 640cd7ff 2022-06-22 mark view->count = 0;
4856 912a3f79 2021-08-30 j if (s->eof)
4857 912a3f79 2021-08-30 j break;
4858 912a3f79 2021-08-30 j
4859 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4860 912a3f79 2021-08-30 j s->eof = 1;
4861 912a3f79 2021-08-30 j break;
4862 1e37a5c2 2019-05-12 jcs case 'k':
4863 1e37a5c2 2019-05-12 jcs case KEY_UP:
4864 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4865 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4866 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4867 640cd7ff 2022-06-22 mark else
4868 640cd7ff 2022-06-22 mark view->count = 0;
4869 1e37a5c2 2019-05-12 jcs break;
4870 83cc4199 2022-06-13 stsp case CTRL('u'):
4871 33c3719a 2022-06-15 stsp case 'u':
4872 83cc4199 2022-06-13 stsp nscroll /= 2;
4873 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4874 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4875 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4876 61417565 2022-06-20 mark case 'b':
4877 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4878 640cd7ff 2022-06-22 mark view->count = 0;
4879 26ed57b2 2018-05-19 stsp break;
4880 640cd7ff 2022-06-22 mark }
4881 1e37a5c2 2019-05-12 jcs i = 0;
4882 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4883 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4884 1e37a5c2 2019-05-12 jcs break;
4885 1e37a5c2 2019-05-12 jcs case 'j':
4886 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4887 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4888 1e37a5c2 2019-05-12 jcs if (!s->eof)
4889 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4890 640cd7ff 2022-06-22 mark else
4891 640cd7ff 2022-06-22 mark view->count = 0;
4892 1e37a5c2 2019-05-12 jcs break;
4893 83cc4199 2022-06-13 stsp case CTRL('d'):
4894 33c3719a 2022-06-15 stsp case 'd':
4895 83cc4199 2022-06-13 stsp nscroll /= 2;
4896 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4897 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4898 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4899 61417565 2022-06-20 mark case 'f':
4900 1e37a5c2 2019-05-12 jcs case ' ':
4901 640cd7ff 2022-06-22 mark if (s->eof) {
4902 640cd7ff 2022-06-22 mark view->count = 0;
4903 1e37a5c2 2019-05-12 jcs break;
4904 640cd7ff 2022-06-22 mark }
4905 1e37a5c2 2019-05-12 jcs i = 0;
4906 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4907 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4908 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4909 826082fe 2020-12-10 stsp if (linelen == -1) {
4910 826082fe 2020-12-10 stsp if (feof(s->f)) {
4911 826082fe 2020-12-10 stsp s->eof = 1;
4912 826082fe 2020-12-10 stsp } else
4913 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4914 34bc9ec9 2019-02-22 stsp break;
4915 826082fe 2020-12-10 stsp }
4916 1e37a5c2 2019-05-12 jcs }
4917 826082fe 2020-12-10 stsp free(line);
4918 1e37a5c2 2019-05-12 jcs break;
4919 c7d5c43c 2022-08-04 mark case '(':
4920 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
4921 c7d5c43c 2022-08-04 mark break;
4922 c7d5c43c 2022-08-04 mark case ')':
4923 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
4924 c7d5c43c 2022-08-04 mark break;
4925 c7d5c43c 2022-08-04 mark case '{':
4926 c7d5c43c 2022-08-04 mark diff_prev_index(s, GOT_DIFF_LINE_HUNK);
4927 c7d5c43c 2022-08-04 mark break;
4928 c7d5c43c 2022-08-04 mark case '}':
4929 c7d5c43c 2022-08-04 mark diff_next_index(s, GOT_DIFF_LINE_HUNK);
4930 c7d5c43c 2022-08-04 mark break;
4931 1e37a5c2 2019-05-12 jcs case '[':
4932 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4933 1e37a5c2 2019-05-12 jcs s->diff_context--;
4934 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4935 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4936 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4937 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4938 27829c9e 2020-11-21 stsp s->nlines) {
4939 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4940 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4941 27829c9e 2020-11-21 stsp }
4942 640cd7ff 2022-06-22 mark } else
4943 640cd7ff 2022-06-22 mark view->count = 0;
4944 1e37a5c2 2019-05-12 jcs break;
4945 1e37a5c2 2019-05-12 jcs case ']':
4946 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4947 1e37a5c2 2019-05-12 jcs s->diff_context++;
4948 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4949 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4950 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4951 640cd7ff 2022-06-22 mark } else
4952 640cd7ff 2022-06-22 mark view->count = 0;
4953 1e37a5c2 2019-05-12 jcs break;
4954 1e37a5c2 2019-05-12 jcs case '<':
4955 1e37a5c2 2019-05-12 jcs case ',':
4956 2b3e6702 2022-07-20 mark case 'K':
4957 c0f61fa4 2022-07-11 mark up = 1;
4958 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4959 c0f61fa4 2022-07-11 mark case '>':
4960 c0f61fa4 2022-07-11 mark case '.':
4961 2b3e6702 2022-07-20 mark case 'J':
4962 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4963 640cd7ff 2022-06-22 mark view->count = 0;
4964 48ae06ee 2018-10-18 stsp break;
4965 640cd7ff 2022-06-22 mark }
4966 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4967 6524637e 2019-02-21 stsp
4968 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4969 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4970 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4971 15a087fe 2019-02-21 stsp
4972 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4973 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4974 c0f61fa4 2022-07-11 mark if (err)
4975 c0f61fa4 2022-07-11 mark break;
4976 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4977 15a087fe 2019-02-21 stsp
4978 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4979 c0f61fa4 2022-07-11 mark break;
4980 15a087fe 2019-02-21 stsp
4981 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4982 c0f61fa4 2022-07-11 mark if (err)
4983 c0f61fa4 2022-07-11 mark break;
4984 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4985 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4986 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4987 5e224a3e 2019-02-22 stsp
4988 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4989 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4990 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4991 c0f61fa4 2022-07-11 mark
4992 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4993 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4994 c0f61fa4 2022-07-11 mark if (err)
4995 c0f61fa4 2022-07-11 mark break;
4996 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4997 5a8b5076 2020-12-05 stsp
4998 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4999 c0f61fa4 2022-07-11 mark break;
5000 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
5001 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
5002 c0f61fa4 2022-07-11 mark bs->selected_line);
5003 c0f61fa4 2022-07-11 mark if (id == NULL)
5004 c0f61fa4 2022-07-11 mark break;
5005 15a087fe 2019-02-21 stsp
5006 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
5007 c0f61fa4 2022-07-11 mark break;
5008 15a087fe 2019-02-21 stsp
5009 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5010 c0f61fa4 2022-07-11 mark if (err)
5011 c0f61fa4 2022-07-11 mark break;
5012 c0f61fa4 2022-07-11 mark }
5013 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5014 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5015 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5016 145b6838 2022-06-16 stsp view->x = 0;
5017 1e37a5c2 2019-05-12 jcs
5018 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5019 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5020 1e37a5c2 2019-05-12 jcs break;
5021 1e37a5c2 2019-05-12 jcs default:
5022 640cd7ff 2022-06-22 mark view->count = 0;
5023 1e37a5c2 2019-05-12 jcs break;
5024 26ed57b2 2018-05-19 stsp }
5025 e5a0f69f 2018-08-18 stsp
5026 bcbd79e2 2018-08-19 stsp return err;
5027 26ed57b2 2018-05-19 stsp }
5028 26ed57b2 2018-05-19 stsp
5029 4ed7e80c 2018-05-20 stsp static const struct got_error *
5030 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5031 9f7d7167 2018-04-29 stsp {
5032 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5033 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5034 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5035 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5036 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5037 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5038 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5039 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5040 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5041 3dbaef42 2020-11-24 stsp const char *errstr;
5042 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5043 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5044 70ac5f84 2019-03-28 stsp
5045 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5046 26ed57b2 2018-05-19 stsp switch (ch) {
5047 64453f7e 2020-11-21 stsp case 'a':
5048 64453f7e 2020-11-21 stsp force_text_diff = 1;
5049 3dbaef42 2020-11-24 stsp break;
5050 3dbaef42 2020-11-24 stsp case 'C':
5051 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5052 3dbaef42 2020-11-24 stsp &errstr);
5053 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5054 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
5055 5a20d08d 2022-02-09 op errstr, errstr);
5056 64453f7e 2020-11-21 stsp break;
5057 09b5bff8 2020-02-23 naddy case 'r':
5058 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5059 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5060 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5061 09b5bff8 2020-02-23 naddy optarg);
5062 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5063 09b5bff8 2020-02-23 naddy break;
5064 3dbaef42 2020-11-24 stsp case 'w':
5065 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5066 3dbaef42 2020-11-24 stsp break;
5067 26ed57b2 2018-05-19 stsp default:
5068 17020d27 2019-03-07 stsp usage_diff();
5069 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5070 26ed57b2 2018-05-19 stsp }
5071 26ed57b2 2018-05-19 stsp }
5072 26ed57b2 2018-05-19 stsp
5073 26ed57b2 2018-05-19 stsp argc -= optind;
5074 26ed57b2 2018-05-19 stsp argv += optind;
5075 26ed57b2 2018-05-19 stsp
5076 26ed57b2 2018-05-19 stsp if (argc == 0) {
5077 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5078 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5079 15a94983 2018-12-23 stsp id_str1 = argv[0];
5080 15a94983 2018-12-23 stsp id_str2 = argv[1];
5081 26ed57b2 2018-05-19 stsp } else
5082 26ed57b2 2018-05-19 stsp usage_diff();
5083 eb6600df 2019-01-04 stsp
5084 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5085 0ae84acc 2022-06-15 tracey if (error)
5086 0ae84acc 2022-06-15 tracey goto done;
5087 0ae84acc 2022-06-15 tracey
5088 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5089 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5090 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5091 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5092 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5093 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5094 c156c7a4 2020-12-18 stsp goto done;
5095 a273ac94 2020-02-23 naddy if (worktree)
5096 a273ac94 2020-02-23 naddy repo_path =
5097 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5098 a273ac94 2020-02-23 naddy else
5099 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5100 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5101 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5102 c156c7a4 2020-12-18 stsp goto done;
5103 c156c7a4 2020-12-18 stsp }
5104 a273ac94 2020-02-23 naddy }
5105 a273ac94 2020-02-23 naddy
5106 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5107 eb6600df 2019-01-04 stsp if (error)
5108 eb6600df 2019-01-04 stsp goto done;
5109 26ed57b2 2018-05-19 stsp
5110 a273ac94 2020-02-23 naddy init_curses();
5111 a273ac94 2020-02-23 naddy
5112 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5113 51a10b52 2020-12-26 stsp if (error)
5114 51a10b52 2020-12-26 stsp goto done;
5115 51a10b52 2020-12-26 stsp
5116 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5117 26ed57b2 2018-05-19 stsp if (error)
5118 26ed57b2 2018-05-19 stsp goto done;
5119 26ed57b2 2018-05-19 stsp
5120 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5121 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5122 26ed57b2 2018-05-19 stsp if (error)
5123 26ed57b2 2018-05-19 stsp goto done;
5124 26ed57b2 2018-05-19 stsp
5125 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5126 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5127 26ed57b2 2018-05-19 stsp if (error)
5128 26ed57b2 2018-05-19 stsp goto done;
5129 26ed57b2 2018-05-19 stsp
5130 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5131 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5132 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5133 ea5e7bb5 2018-08-01 stsp goto done;
5134 ea5e7bb5 2018-08-01 stsp }
5135 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5136 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5137 5dc9f4bc 2018-08-04 stsp if (error)
5138 5dc9f4bc 2018-08-04 stsp goto done;
5139 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5140 26ed57b2 2018-05-19 stsp done:
5141 3dbaef42 2020-11-24 stsp free(label1);
5142 3dbaef42 2020-11-24 stsp free(label2);
5143 c02c541e 2019-03-29 stsp free(repo_path);
5144 a273ac94 2020-02-23 naddy free(cwd);
5145 1d0f4054 2021-06-17 stsp if (repo) {
5146 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5147 1d0f4054 2021-06-17 stsp if (error == NULL)
5148 1d0f4054 2021-06-17 stsp error = close_err;
5149 1d0f4054 2021-06-17 stsp }
5150 a273ac94 2020-02-23 naddy if (worktree)
5151 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5152 0ae84acc 2022-06-15 tracey if (pack_fds) {
5153 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5154 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5155 0ae84acc 2022-06-15 tracey if (error == NULL)
5156 0ae84acc 2022-06-15 tracey error = pack_err;
5157 0ae84acc 2022-06-15 tracey }
5158 51a10b52 2020-12-26 stsp tog_free_refs();
5159 26ed57b2 2018-05-19 stsp return error;
5160 9f7d7167 2018-04-29 stsp }
5161 9f7d7167 2018-04-29 stsp
5162 4ed7e80c 2018-05-20 stsp __dead static void
5163 9f7d7167 2018-04-29 stsp usage_blame(void)
5164 9f7d7167 2018-04-29 stsp {
5165 80ddbec8 2018-04-29 stsp endwin();
5166 87411fa9 2022-06-16 stsp fprintf(stderr,
5167 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5168 9f7d7167 2018-04-29 stsp getprogname());
5169 9f7d7167 2018-04-29 stsp exit(1);
5170 9f7d7167 2018-04-29 stsp }
5171 84451b3e 2018-07-10 stsp
5172 84451b3e 2018-07-10 stsp struct tog_blame_line {
5173 84451b3e 2018-07-10 stsp int annotated;
5174 84451b3e 2018-07-10 stsp struct got_object_id *id;
5175 84451b3e 2018-07-10 stsp };
5176 9f7d7167 2018-04-29 stsp
5177 4ed7e80c 2018-05-20 stsp static const struct got_error *
5178 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5179 84451b3e 2018-07-10 stsp {
5180 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5181 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5182 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5183 84451b3e 2018-07-10 stsp const struct got_error *err;
5184 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5185 826082fe 2020-12-10 stsp char *line = NULL;
5186 826082fe 2020-12-10 stsp size_t linesize = 0;
5187 826082fe 2020-12-10 stsp ssize_t linelen;
5188 84451b3e 2018-07-10 stsp wchar_t *wline;
5189 27a741e5 2019-09-11 stsp int width;
5190 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5191 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5192 ab089a2a 2018-07-12 stsp char *id_str;
5193 11b20872 2019-11-08 stsp struct tog_color *tc;
5194 ab089a2a 2018-07-12 stsp
5195 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5196 ab089a2a 2018-07-12 stsp if (err)
5197 ab089a2a 2018-07-12 stsp return err;
5198 84451b3e 2018-07-10 stsp
5199 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5200 f7d12f7e 2018-08-01 stsp werase(view->window);
5201 84451b3e 2018-07-10 stsp
5202 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5203 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5204 ab089a2a 2018-07-12 stsp free(id_str);
5205 ab089a2a 2018-07-12 stsp return err;
5206 ab089a2a 2018-07-12 stsp }
5207 ab089a2a 2018-07-12 stsp
5208 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5209 ab089a2a 2018-07-12 stsp free(line);
5210 2550e4c3 2018-07-13 stsp line = NULL;
5211 1cae65b4 2019-09-22 stsp if (err)
5212 1cae65b4 2019-09-22 stsp return err;
5213 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5214 a3404814 2018-09-02 stsp wstandout(view->window);
5215 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5216 11b20872 2019-11-08 stsp if (tc)
5217 11b20872 2019-11-08 stsp wattr_on(view->window,
5218 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5219 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5220 11b20872 2019-11-08 stsp if (tc)
5221 11b20872 2019-11-08 stsp wattr_off(view->window,
5222 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5223 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5224 a3404814 2018-09-02 stsp wstandend(view->window);
5225 2550e4c3 2018-07-13 stsp free(wline);
5226 2550e4c3 2018-07-13 stsp wline = NULL;
5227 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5228 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5229 ab089a2a 2018-07-12 stsp
5230 94b80cfa 2022-08-01 mark if (view->gline > blame->nlines)
5231 94b80cfa 2022-08-01 mark view->gline = blame->nlines;
5232 94b80cfa 2022-08-01 mark
5233 94b80cfa 2022-08-01 mark if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5234 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5235 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5236 ab089a2a 2018-07-12 stsp free(id_str);
5237 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5238 ab089a2a 2018-07-12 stsp }
5239 ab089a2a 2018-07-12 stsp free(id_str);
5240 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5241 3f60a8ef 2018-07-10 stsp free(line);
5242 2550e4c3 2018-07-13 stsp line = NULL;
5243 3f60a8ef 2018-07-10 stsp if (err)
5244 3f60a8ef 2018-07-10 stsp return err;
5245 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5246 2550e4c3 2018-07-13 stsp free(wline);
5247 2550e4c3 2018-07-13 stsp wline = NULL;
5248 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5249 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5250 3f60a8ef 2018-07-10 stsp
5251 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5252 145b6838 2022-06-16 stsp view->maxx = 0;
5253 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5254 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5255 826082fe 2020-12-10 stsp if (linelen == -1) {
5256 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5257 826082fe 2020-12-10 stsp s->eof = 1;
5258 826082fe 2020-12-10 stsp break;
5259 826082fe 2020-12-10 stsp }
5260 84451b3e 2018-07-10 stsp free(line);
5261 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5262 84451b3e 2018-07-10 stsp }
5263 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5264 94b80cfa 2022-08-01 mark continue;
5265 94b80cfa 2022-08-01 mark if (view->gline && !gotoline(view, &lineno, &nprinted))
5266 826082fe 2020-12-10 stsp continue;
5267 1853e0f4 2022-06-16 stsp
5268 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5269 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5270 1853e0f4 2022-06-16 stsp if (err) {
5271 1853e0f4 2022-06-16 stsp free(line);
5272 1853e0f4 2022-06-16 stsp return err;
5273 1853e0f4 2022-06-16 stsp }
5274 1853e0f4 2022-06-16 stsp free(wline);
5275 1853e0f4 2022-06-16 stsp wline = NULL;
5276 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5277 84451b3e 2018-07-10 stsp
5278 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5279 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5280 b700b5d6 2018-07-10 stsp
5281 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5282 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5283 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5284 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5285 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5286 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5287 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5288 8d0fe45a 2019-08-12 stsp char *id_str;
5289 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5290 87411fa9 2022-06-16 stsp blame_line->id);
5291 8d0fe45a 2019-08-12 stsp if (err) {
5292 8d0fe45a 2019-08-12 stsp free(line);
5293 8d0fe45a 2019-08-12 stsp return err;
5294 8d0fe45a 2019-08-12 stsp }
5295 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5296 11b20872 2019-11-08 stsp if (tc)
5297 11b20872 2019-11-08 stsp wattr_on(view->window,
5298 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5299 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5300 11b20872 2019-11-08 stsp if (tc)
5301 11b20872 2019-11-08 stsp wattr_off(view->window,
5302 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5303 8d0fe45a 2019-08-12 stsp free(id_str);
5304 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5305 8d0fe45a 2019-08-12 stsp } else {
5306 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5307 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5308 84451b3e 2018-07-10 stsp }
5309 ee41ec32 2018-07-10 stsp } else {
5310 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5311 ee41ec32 2018-07-10 stsp prev_id = NULL;
5312 ee41ec32 2018-07-10 stsp }
5313 84451b3e 2018-07-10 stsp
5314 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5315 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5316 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5317 27a741e5 2019-09-11 stsp
5318 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5319 41605754 2020-11-12 stsp width = 9;
5320 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5321 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5322 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5323 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5324 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5325 41605754 2020-11-12 stsp if (err) {
5326 41605754 2020-11-12 stsp free(line);
5327 41605754 2020-11-12 stsp return err;
5328 41605754 2020-11-12 stsp }
5329 41605754 2020-11-12 stsp width += 9;
5330 41605754 2020-11-12 stsp } else {
5331 4cb9d869 2022-06-16 stsp int skip;
5332 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5333 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5334 4cb9d869 2022-06-16 stsp if (err) {
5335 4cb9d869 2022-06-16 stsp free(line);
5336 4cb9d869 2022-06-16 stsp return err;
5337 145b6838 2022-06-16 stsp }
5338 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5339 a75b3e2d 2022-06-16 stsp width += 9;
5340 41605754 2020-11-12 stsp free(wline);
5341 41605754 2020-11-12 stsp wline = NULL;
5342 41605754 2020-11-12 stsp }
5343 41605754 2020-11-12 stsp
5344 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5345 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5346 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5347 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5348 84451b3e 2018-07-10 stsp }
5349 826082fe 2020-12-10 stsp free(line);
5350 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5351 84451b3e 2018-07-10 stsp
5352 9b058f45 2022-06-30 mark view_border(view);
5353 84451b3e 2018-07-10 stsp
5354 84451b3e 2018-07-10 stsp return NULL;
5355 84451b3e 2018-07-10 stsp }
5356 84451b3e 2018-07-10 stsp
5357 84451b3e 2018-07-10 stsp static const struct got_error *
5358 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5359 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5360 84451b3e 2018-07-10 stsp {
5361 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5362 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5363 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5364 1a76625f 2018-10-22 stsp int errcode;
5365 84451b3e 2018-07-10 stsp
5366 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5367 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5368 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5369 84451b3e 2018-07-10 stsp
5370 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5371 1a76625f 2018-10-22 stsp if (errcode)
5372 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5373 84451b3e 2018-07-10 stsp
5374 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5375 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5376 d68a0a7d 2018-07-10 stsp goto done;
5377 d68a0a7d 2018-07-10 stsp }
5378 d68a0a7d 2018-07-10 stsp
5379 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5380 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5381 d68a0a7d 2018-07-10 stsp
5382 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5383 d68a0a7d 2018-07-10 stsp if (line->annotated)
5384 d68a0a7d 2018-07-10 stsp goto done;
5385 d68a0a7d 2018-07-10 stsp
5386 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5387 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5388 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5389 84451b3e 2018-07-10 stsp goto done;
5390 84451b3e 2018-07-10 stsp }
5391 84451b3e 2018-07-10 stsp line->annotated = 1;
5392 84451b3e 2018-07-10 stsp done:
5393 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5394 1a76625f 2018-10-22 stsp if (errcode)
5395 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5396 84451b3e 2018-07-10 stsp return err;
5397 84451b3e 2018-07-10 stsp }
5398 84451b3e 2018-07-10 stsp
5399 84451b3e 2018-07-10 stsp static void *
5400 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5401 84451b3e 2018-07-10 stsp {
5402 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5403 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5404 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5405 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5406 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5407 1b484788 2022-06-28 tracey
5408 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5409 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5410 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5411 e6e73e55 2022-06-30 tracey
5412 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5413 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5414 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5415 e6e73e55 2022-06-30 tracey goto done;
5416 e6e73e55 2022-06-30 tracey }
5417 18430de3 2018-07-10 stsp
5418 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5419 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5420 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5421 e6e73e55 2022-06-30 tracey goto done;
5422 e6e73e55 2022-06-30 tracey }
5423 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5424 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5425 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5426 e6e73e55 2022-06-30 tracey goto done;
5427 e6e73e55 2022-06-30 tracey }
5428 e6e73e55 2022-06-30 tracey
5429 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5430 61266923 2020-01-14 stsp if (err)
5431 e6e73e55 2022-06-30 tracey goto done;
5432 61266923 2020-01-14 stsp
5433 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5434 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5435 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5436 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5437 fc06ba56 2019-08-22 stsp err = NULL;
5438 18430de3 2018-07-10 stsp
5439 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5440 e6e73e55 2022-06-30 tracey if (errcode) {
5441 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5442 e6e73e55 2022-06-30 tracey goto done;
5443 e6e73e55 2022-06-30 tracey }
5444 18430de3 2018-07-10 stsp
5445 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5446 1d0f4054 2021-06-17 stsp if (err == NULL)
5447 1d0f4054 2021-06-17 stsp err = close_err;
5448 c9beca56 2018-07-22 stsp ta->repo = NULL;
5449 c9beca56 2018-07-22 stsp *ta->complete = 1;
5450 18430de3 2018-07-10 stsp
5451 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5452 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5453 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5454 18430de3 2018-07-10 stsp
5455 e6e73e55 2022-06-30 tracey done:
5456 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5457 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5458 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5459 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5460 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5461 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5462 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5463 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5464 1b484788 2022-06-28 tracey
5465 18430de3 2018-07-10 stsp return (void *)err;
5466 84451b3e 2018-07-10 stsp }
5467 84451b3e 2018-07-10 stsp
5468 245d91c1 2018-07-12 stsp static struct got_object_id *
5469 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5470 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5471 245d91c1 2018-07-12 stsp {
5472 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5473 8d0fe45a 2019-08-12 stsp
5474 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5475 8d0fe45a 2019-08-12 stsp return NULL;
5476 b880a918 2018-07-10 stsp
5477 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5478 c0f61fa4 2022-07-11 mark if (!line->annotated)
5479 c0f61fa4 2022-07-11 mark return NULL;
5480 c0f61fa4 2022-07-11 mark
5481 c0f61fa4 2022-07-11 mark return line->id;
5482 c0f61fa4 2022-07-11 mark }
5483 c0f61fa4 2022-07-11 mark
5484 c0f61fa4 2022-07-11 mark static struct got_object_id *
5485 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5486 c0f61fa4 2022-07-11 mark int lineno)
5487 c0f61fa4 2022-07-11 mark {
5488 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5489 c0f61fa4 2022-07-11 mark
5490 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5491 c0f61fa4 2022-07-11 mark return NULL;
5492 c0f61fa4 2022-07-11 mark
5493 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5494 245d91c1 2018-07-12 stsp if (!line->annotated)
5495 245d91c1 2018-07-12 stsp return NULL;
5496 245d91c1 2018-07-12 stsp
5497 245d91c1 2018-07-12 stsp return line->id;
5498 b880a918 2018-07-10 stsp }
5499 245d91c1 2018-07-12 stsp
5500 b880a918 2018-07-10 stsp static const struct got_error *
5501 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5502 a70480e0 2018-06-23 stsp {
5503 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5504 245d91c1 2018-07-12 stsp int i;
5505 245d91c1 2018-07-12 stsp
5506 245d91c1 2018-07-12 stsp if (blame->thread) {
5507 1a76625f 2018-10-22 stsp int errcode;
5508 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5509 1a76625f 2018-10-22 stsp if (errcode)
5510 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5511 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5512 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5513 1a76625f 2018-10-22 stsp if (errcode)
5514 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5515 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5516 1a76625f 2018-10-22 stsp if (errcode)
5517 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5518 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5519 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5520 245d91c1 2018-07-12 stsp err = NULL;
5521 245d91c1 2018-07-12 stsp blame->thread = NULL;
5522 245d91c1 2018-07-12 stsp }
5523 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5524 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5525 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5526 1d0f4054 2021-06-17 stsp if (err == NULL)
5527 1d0f4054 2021-06-17 stsp err = close_err;
5528 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5529 245d91c1 2018-07-12 stsp }
5530 245d91c1 2018-07-12 stsp if (blame->f) {
5531 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5532 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5533 245d91c1 2018-07-12 stsp blame->f = NULL;
5534 245d91c1 2018-07-12 stsp }
5535 57670559 2018-12-23 stsp if (blame->lines) {
5536 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5537 57670559 2018-12-23 stsp free(blame->lines[i].id);
5538 57670559 2018-12-23 stsp free(blame->lines);
5539 57670559 2018-12-23 stsp blame->lines = NULL;
5540 57670559 2018-12-23 stsp }
5541 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5542 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5543 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5544 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5545 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5546 0ae84acc 2022-06-15 tracey if (err == NULL)
5547 0ae84acc 2022-06-15 tracey err = pack_err;
5548 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5549 0ae84acc 2022-06-15 tracey }
5550 245d91c1 2018-07-12 stsp return err;
5551 245d91c1 2018-07-12 stsp }
5552 245d91c1 2018-07-12 stsp
5553 245d91c1 2018-07-12 stsp static const struct got_error *
5554 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5555 fc06ba56 2019-08-22 stsp {
5556 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5557 fc06ba56 2019-08-22 stsp int *done = arg;
5558 fc06ba56 2019-08-22 stsp int errcode;
5559 fc06ba56 2019-08-22 stsp
5560 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5561 fc06ba56 2019-08-22 stsp if (errcode)
5562 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5563 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5564 fc06ba56 2019-08-22 stsp
5565 fc06ba56 2019-08-22 stsp if (*done)
5566 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5567 fc06ba56 2019-08-22 stsp
5568 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5569 fc06ba56 2019-08-22 stsp if (errcode)
5570 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5571 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5572 fc06ba56 2019-08-22 stsp
5573 fc06ba56 2019-08-22 stsp return err;
5574 fc06ba56 2019-08-22 stsp }
5575 fc06ba56 2019-08-22 stsp
5576 fc06ba56 2019-08-22 stsp static const struct got_error *
5577 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5578 245d91c1 2018-07-12 stsp {
5579 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5580 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5581 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5582 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5583 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5584 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5585 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5586 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5587 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5588 a70480e0 2018-06-23 stsp
5589 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5590 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5591 27d434c2 2018-09-15 stsp if (err)
5592 15a94983 2018-12-23 stsp return err;
5593 eb81bc23 2022-06-28 tracey
5594 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5595 eb81bc23 2022-06-28 tracey if (fd == -1) {
5596 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5597 eb81bc23 2022-06-28 tracey goto done;
5598 eb81bc23 2022-06-28 tracey }
5599 a44927cc 2022-04-07 stsp
5600 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5601 a44927cc 2022-04-07 stsp if (err)
5602 a44927cc 2022-04-07 stsp goto done;
5603 27d434c2 2018-09-15 stsp
5604 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5605 84451b3e 2018-07-10 stsp if (err)
5606 84451b3e 2018-07-10 stsp goto done;
5607 27d434c2 2018-09-15 stsp
5608 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5609 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5610 84451b3e 2018-07-10 stsp goto done;
5611 84451b3e 2018-07-10 stsp }
5612 a70480e0 2018-06-23 stsp
5613 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5614 a70480e0 2018-06-23 stsp if (err)
5615 a70480e0 2018-06-23 stsp goto done;
5616 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5617 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5618 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5619 84451b3e 2018-07-10 stsp goto done;
5620 84451b3e 2018-07-10 stsp }
5621 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5622 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5623 1fddf795 2021-01-20 stsp if (err)
5624 1fddf795 2021-01-20 stsp goto done;
5625 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5626 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5627 84451b3e 2018-07-10 stsp goto done;
5628 1fddf795 2021-01-20 stsp }
5629 b02560ec 2019-08-19 stsp
5630 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5631 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5632 b02560ec 2019-08-19 stsp blame->nlines--;
5633 a70480e0 2018-06-23 stsp
5634 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5635 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5636 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5637 84451b3e 2018-07-10 stsp goto done;
5638 84451b3e 2018-07-10 stsp }
5639 a70480e0 2018-06-23 stsp
5640 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5641 bd24772e 2018-07-11 stsp if (err)
5642 bd24772e 2018-07-11 stsp goto done;
5643 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5644 0ae84acc 2022-06-15 tracey pack_fds);
5645 0ae84acc 2022-06-15 tracey if (err)
5646 0ae84acc 2022-06-15 tracey goto done;
5647 bd24772e 2018-07-11 stsp
5648 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5649 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5650 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5651 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5652 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5653 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5654 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5655 245d91c1 2018-07-12 stsp goto done;
5656 245d91c1 2018-07-12 stsp }
5657 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5658 245d91c1 2018-07-12 stsp
5659 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5660 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5661 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5662 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5663 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5664 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5665 a5388363 2020-12-01 naddy s->blame_complete = 0;
5666 f5a09613 2020-12-13 naddy
5667 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5668 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5669 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5670 f5a09613 2020-12-13 naddy s->selected_line = 1;
5671 f5a09613 2020-12-13 naddy }
5672 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5673 245d91c1 2018-07-12 stsp
5674 245d91c1 2018-07-12 stsp done:
5675 a44927cc 2022-04-07 stsp if (commit)
5676 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5677 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5678 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5679 245d91c1 2018-07-12 stsp if (blob)
5680 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5681 27d434c2 2018-09-15 stsp free(obj_id);
5682 245d91c1 2018-07-12 stsp if (err)
5683 245d91c1 2018-07-12 stsp stop_blame(blame);
5684 245d91c1 2018-07-12 stsp return err;
5685 245d91c1 2018-07-12 stsp }
5686 245d91c1 2018-07-12 stsp
5687 245d91c1 2018-07-12 stsp static const struct got_error *
5688 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5689 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5690 245d91c1 2018-07-12 stsp {
5691 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5692 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5693 dbc6a6b6 2018-07-12 stsp
5694 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5695 245d91c1 2018-07-12 stsp
5696 c4843652 2019-08-12 stsp s->path = strdup(path);
5697 c4843652 2019-08-12 stsp if (s->path == NULL)
5698 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5699 c4843652 2019-08-12 stsp
5700 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5701 c4843652 2019-08-12 stsp if (err) {
5702 c4843652 2019-08-12 stsp free(s->path);
5703 7cbe629d 2018-08-04 stsp return err;
5704 c4843652 2019-08-12 stsp }
5705 245d91c1 2018-07-12 stsp
5706 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5707 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5708 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5709 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5710 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5711 fb2756b9 2018-08-04 stsp s->repo = repo;
5712 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5713 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5714 7cbe629d 2018-08-04 stsp
5715 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5716 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5717 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5718 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5719 11b20872 2019-11-08 stsp if (err)
5720 11b20872 2019-11-08 stsp return err;
5721 11b20872 2019-11-08 stsp }
5722 11b20872 2019-11-08 stsp
5723 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5724 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5725 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5726 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5727 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5728 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5729 e5a0f69f 2018-08-18 stsp
5730 a5388363 2020-12-01 naddy return run_blame(view);
5731 7cbe629d 2018-08-04 stsp }
5732 7cbe629d 2018-08-04 stsp
5733 e5a0f69f 2018-08-18 stsp static const struct got_error *
5734 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5735 7cbe629d 2018-08-04 stsp {
5736 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5737 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5738 7cbe629d 2018-08-04 stsp
5739 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5740 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5741 e5a0f69f 2018-08-18 stsp
5742 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5743 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5744 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5745 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5746 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5747 7cbe629d 2018-08-04 stsp }
5748 e5a0f69f 2018-08-18 stsp
5749 e5a0f69f 2018-08-18 stsp free(s->path);
5750 11b20872 2019-11-08 stsp free_colors(&s->colors);
5751 e5a0f69f 2018-08-18 stsp return err;
5752 7cbe629d 2018-08-04 stsp }
5753 7cbe629d 2018-08-04 stsp
5754 7cbe629d 2018-08-04 stsp static const struct got_error *
5755 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5756 6c4c42e0 2019-06-24 stsp {
5757 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5758 6c4c42e0 2019-06-24 stsp
5759 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5760 6c4c42e0 2019-06-24 stsp return NULL;
5761 6c4c42e0 2019-06-24 stsp }
5762 6c4c42e0 2019-06-24 stsp
5763 6c4c42e0 2019-06-24 stsp static const struct got_error *
5764 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5765 6c4c42e0 2019-06-24 stsp {
5766 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5767 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5768 6c4c42e0 2019-06-24 stsp int lineno;
5769 cb713507 2022-06-16 stsp char *line = NULL;
5770 826082fe 2020-12-10 stsp size_t linesize = 0;
5771 826082fe 2020-12-10 stsp ssize_t linelen;
5772 6c4c42e0 2019-06-24 stsp
5773 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5774 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5775 6c4c42e0 2019-06-24 stsp return NULL;
5776 6c4c42e0 2019-06-24 stsp }
5777 6c4c42e0 2019-06-24 stsp
5778 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5779 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5780 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5781 6c4c42e0 2019-06-24 stsp else
5782 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5783 487cd7d2 2021-12-17 stsp } else
5784 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5785 6c4c42e0 2019-06-24 stsp
5786 6c4c42e0 2019-06-24 stsp while (1) {
5787 6c4c42e0 2019-06-24 stsp off_t offset;
5788 6c4c42e0 2019-06-24 stsp
5789 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5790 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5791 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5792 6c4c42e0 2019-06-24 stsp break;
5793 6c4c42e0 2019-06-24 stsp }
5794 2246482e 2019-06-25 stsp
5795 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5796 6c4c42e0 2019-06-24 stsp lineno = 1;
5797 6c4c42e0 2019-06-24 stsp else
5798 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5799 6c4c42e0 2019-06-24 stsp }
5800 6c4c42e0 2019-06-24 stsp
5801 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5802 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5803 6c4c42e0 2019-06-24 stsp free(line);
5804 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5805 6c4c42e0 2019-06-24 stsp }
5806 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5807 cb713507 2022-06-16 stsp if (linelen != -1) {
5808 cb713507 2022-06-16 stsp char *exstr;
5809 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5810 cb713507 2022-06-16 stsp if (err)
5811 cb713507 2022-06-16 stsp break;
5812 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5813 cb713507 2022-06-16 stsp &view->regmatch)) {
5814 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5815 cb713507 2022-06-16 stsp s->matched_line = lineno;
5816 cb713507 2022-06-16 stsp free(exstr);
5817 cb713507 2022-06-16 stsp break;
5818 cb713507 2022-06-16 stsp }
5819 cb713507 2022-06-16 stsp free(exstr);
5820 6c4c42e0 2019-06-24 stsp }
5821 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5822 6c4c42e0 2019-06-24 stsp lineno++;
5823 6c4c42e0 2019-06-24 stsp else
5824 6c4c42e0 2019-06-24 stsp lineno--;
5825 6c4c42e0 2019-06-24 stsp }
5826 826082fe 2020-12-10 stsp free(line);
5827 6c4c42e0 2019-06-24 stsp
5828 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5829 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5830 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5831 6c4c42e0 2019-06-24 stsp }
5832 6c4c42e0 2019-06-24 stsp
5833 145b6838 2022-06-16 stsp return err;
5834 6c4c42e0 2019-06-24 stsp }
5835 6c4c42e0 2019-06-24 stsp
5836 6c4c42e0 2019-06-24 stsp static const struct got_error *
5837 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5838 7cbe629d 2018-08-04 stsp {
5839 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5840 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5841 2b380cc8 2018-10-24 stsp int errcode;
5842 2b380cc8 2018-10-24 stsp
5843 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5844 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5845 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5846 2b380cc8 2018-10-24 stsp if (errcode)
5847 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5848 51fe7530 2019-08-19 stsp
5849 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5850 2b380cc8 2018-10-24 stsp }
5851 e5a0f69f 2018-08-18 stsp
5852 51fe7530 2019-08-19 stsp if (s->blame_complete)
5853 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5854 51fe7530 2019-08-19 stsp
5855 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5856 e5a0f69f 2018-08-18 stsp
5857 9b058f45 2022-06-30 mark view_border(view);
5858 e5a0f69f 2018-08-18 stsp return err;
5859 e5a0f69f 2018-08-18 stsp }
5860 e5a0f69f 2018-08-18 stsp
5861 e5a0f69f 2018-08-18 stsp static const struct got_error *
5862 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5863 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5864 05f04cdf 2022-07-20 mark {
5865 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5866 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5867 05f04cdf 2022-07-20 mark
5868 05f04cdf 2022-07-20 mark *new_view = NULL;
5869 05f04cdf 2022-07-20 mark
5870 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5871 05f04cdf 2022-07-20 mark if (log_view == NULL)
5872 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5873 05f04cdf 2022-07-20 mark
5874 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5875 05f04cdf 2022-07-20 mark if (err)
5876 05f04cdf 2022-07-20 mark view_close(log_view);
5877 05f04cdf 2022-07-20 mark else
5878 05f04cdf 2022-07-20 mark *new_view = log_view;
5879 05f04cdf 2022-07-20 mark
5880 05f04cdf 2022-07-20 mark return err;
5881 05f04cdf 2022-07-20 mark }
5882 05f04cdf 2022-07-20 mark
5883 05f04cdf 2022-07-20 mark static const struct got_error *
5884 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5885 e5a0f69f 2018-08-18 stsp {
5886 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5887 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
5888 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5889 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5890 9b058f45 2022-06-30 mark
5891 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5892 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5893 9b058f45 2022-06-30 mark --eos; /* border */
5894 7cbe629d 2018-08-04 stsp
5895 e5a0f69f 2018-08-18 stsp switch (ch) {
5896 145b6838 2022-06-16 stsp case '0':
5897 145b6838 2022-06-16 stsp view->x = 0;
5898 145b6838 2022-06-16 stsp break;
5899 145b6838 2022-06-16 stsp case '$':
5900 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5901 640cd7ff 2022-06-22 mark view->count = 0;
5902 145b6838 2022-06-16 stsp break;
5903 145b6838 2022-06-16 stsp case KEY_RIGHT:
5904 145b6838 2022-06-16 stsp case 'l':
5905 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5906 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5907 640cd7ff 2022-06-22 mark else
5908 640cd7ff 2022-06-22 mark view->count = 0;
5909 145b6838 2022-06-16 stsp break;
5910 145b6838 2022-06-16 stsp case KEY_LEFT:
5911 145b6838 2022-06-16 stsp case 'h':
5912 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5913 640cd7ff 2022-06-22 mark if (view->x <= 0)
5914 640cd7ff 2022-06-22 mark view->count = 0;
5915 145b6838 2022-06-16 stsp break;
5916 1e37a5c2 2019-05-12 jcs case 'q':
5917 1e37a5c2 2019-05-12 jcs s->done = 1;
5918 4deef56f 2021-09-02 naddy break;
5919 4deef56f 2021-09-02 naddy case 'g':
5920 4deef56f 2021-09-02 naddy case KEY_HOME:
5921 4deef56f 2021-09-02 naddy s->selected_line = 1;
5922 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5923 640cd7ff 2022-06-22 mark view->count = 0;
5924 4deef56f 2021-09-02 naddy break;
5925 4deef56f 2021-09-02 naddy case 'G':
5926 4deef56f 2021-09-02 naddy case KEY_END:
5927 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5928 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5929 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5930 4deef56f 2021-09-02 naddy } else {
5931 9b058f45 2022-06-30 mark s->selected_line = eos;
5932 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5933 4deef56f 2021-09-02 naddy }
5934 640cd7ff 2022-06-22 mark view->count = 0;
5935 1e37a5c2 2019-05-12 jcs break;
5936 1e37a5c2 2019-05-12 jcs case 'k':
5937 1e37a5c2 2019-05-12 jcs case KEY_UP:
5938 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5939 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5940 1e37a5c2 2019-05-12 jcs s->selected_line--;
5941 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5942 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5943 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5944 640cd7ff 2022-06-22 mark else
5945 640cd7ff 2022-06-22 mark view->count = 0;
5946 1e37a5c2 2019-05-12 jcs break;
5947 83cc4199 2022-06-13 stsp case CTRL('u'):
5948 33c3719a 2022-06-15 stsp case 'u':
5949 83cc4199 2022-06-13 stsp nscroll /= 2;
5950 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5951 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5952 ea025d1d 2020-02-22 naddy case CTRL('b'):
5953 61417565 2022-06-20 mark case 'b':
5954 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5955 640cd7ff 2022-06-22 mark if (view->count > 1)
5956 640cd7ff 2022-06-22 mark nscroll += nscroll;
5957 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5958 640cd7ff 2022-06-22 mark view->count = 0;
5959 e5a0f69f 2018-08-18 stsp break;
5960 1e37a5c2 2019-05-12 jcs }
5961 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5962 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5963 1e37a5c2 2019-05-12 jcs else
5964 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5965 1e37a5c2 2019-05-12 jcs break;
5966 1e37a5c2 2019-05-12 jcs case 'j':
5967 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5968 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5969 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5970 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5971 1e37a5c2 2019-05-12 jcs s->selected_line++;
5972 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5973 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5974 640cd7ff 2022-06-22 mark else
5975 640cd7ff 2022-06-22 mark view->count = 0;
5976 1e37a5c2 2019-05-12 jcs break;
5977 61417565 2022-06-20 mark case 'c':
5978 1e37a5c2 2019-05-12 jcs case 'p': {
5979 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5980 640cd7ff 2022-06-22 mark
5981 640cd7ff 2022-06-22 mark view->count = 0;
5982 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5983 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5984 1e37a5c2 2019-05-12 jcs if (id == NULL)
5985 e5a0f69f 2018-08-18 stsp break;
5986 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5987 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5988 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5989 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5990 1e37a5c2 2019-05-12 jcs int obj_type;
5991 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5992 1e37a5c2 2019-05-12 jcs s->repo, id);
5993 e5a0f69f 2018-08-18 stsp if (err)
5994 e5a0f69f 2018-08-18 stsp break;
5995 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5996 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5997 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5998 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5999 e5a0f69f 2018-08-18 stsp break;
6000 e5a0f69f 2018-08-18 stsp }
6001 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6002 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
6003 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
6004 a44927cc 2022-04-07 stsp if (err)
6005 a44927cc 2022-04-07 stsp break;
6006 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6007 a44927cc 2022-04-07 stsp pcommit, s->path);
6008 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
6009 e5a0f69f 2018-08-18 stsp if (err) {
6010 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6011 1e37a5c2 2019-05-12 jcs err = NULL;
6012 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6013 e5a0f69f 2018-08-18 stsp break;
6014 e5a0f69f 2018-08-18 stsp }
6015 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6016 1e37a5c2 2019-05-12 jcs blob_id);
6017 1e37a5c2 2019-05-12 jcs free(blob_id);
6018 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6019 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6020 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6021 e5a0f69f 2018-08-18 stsp break;
6022 1e37a5c2 2019-05-12 jcs }
6023 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6024 d7b5a0e8 2022-04-20 stsp &pid->id);
6025 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6026 1e37a5c2 2019-05-12 jcs } else {
6027 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6028 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
6029 1e37a5c2 2019-05-12 jcs break;
6030 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6031 1e37a5c2 2019-05-12 jcs id);
6032 1e37a5c2 2019-05-12 jcs }
6033 1e37a5c2 2019-05-12 jcs if (err)
6034 e5a0f69f 2018-08-18 stsp break;
6035 1e37a5c2 2019-05-12 jcs s->done = 1;
6036 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6037 1e37a5c2 2019-05-12 jcs s->done = 0;
6038 1e37a5c2 2019-05-12 jcs if (thread_err)
6039 1e37a5c2 2019-05-12 jcs break;
6040 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6041 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6042 a5388363 2020-12-01 naddy err = run_blame(view);
6043 1e37a5c2 2019-05-12 jcs if (err)
6044 1e37a5c2 2019-05-12 jcs break;
6045 1e37a5c2 2019-05-12 jcs break;
6046 1e37a5c2 2019-05-12 jcs }
6047 61417565 2022-06-20 mark case 'C': {
6048 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6049 640cd7ff 2022-06-22 mark
6050 640cd7ff 2022-06-22 mark view->count = 0;
6051 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6052 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
6053 1e37a5c2 2019-05-12 jcs break;
6054 1e37a5c2 2019-05-12 jcs s->done = 1;
6055 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6056 1e37a5c2 2019-05-12 jcs s->done = 0;
6057 1e37a5c2 2019-05-12 jcs if (thread_err)
6058 1e37a5c2 2019-05-12 jcs break;
6059 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6060 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6061 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6062 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6063 a5388363 2020-12-01 naddy err = run_blame(view);
6064 1e37a5c2 2019-05-12 jcs if (err)
6065 1e37a5c2 2019-05-12 jcs break;
6066 1e37a5c2 2019-05-12 jcs break;
6067 1e37a5c2 2019-05-12 jcs }
6068 136e2bd4 2022-07-23 mark case 'L':
6069 05f04cdf 2022-07-20 mark view->count = 0;
6070 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
6071 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
6072 136e2bd4 2022-07-23 mark if (s->id_to_log)
6073 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6074 05f04cdf 2022-07-20 mark break;
6075 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6076 1e37a5c2 2019-05-12 jcs case '\r': {
6077 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6078 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6079 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6080 640cd7ff 2022-06-22 mark
6081 640cd7ff 2022-06-22 mark view->count = 0;
6082 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6083 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6084 1e37a5c2 2019-05-12 jcs if (id == NULL)
6085 1e37a5c2 2019-05-12 jcs break;
6086 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6087 1e37a5c2 2019-05-12 jcs if (err)
6088 1e37a5c2 2019-05-12 jcs break;
6089 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6090 c0f61fa4 2022-07-11 mark if (*new_view) {
6091 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
6092 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
6093 c0f61fa4 2022-07-11 mark if (err)
6094 c0f61fa4 2022-07-11 mark break;
6095 c0f61fa4 2022-07-11 mark diff_view = *new_view;
6096 c0f61fa4 2022-07-11 mark } else {
6097 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
6098 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
6099 9b058f45 2022-06-30 mark
6100 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
6101 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
6102 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
6103 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
6104 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
6105 c0f61fa4 2022-07-11 mark break;
6106 c0f61fa4 2022-07-11 mark }
6107 15a94983 2018-12-23 stsp }
6108 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6109 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
6110 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6111 1e37a5c2 2019-05-12 jcs if (err) {
6112 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6113 1e37a5c2 2019-05-12 jcs break;
6114 1e37a5c2 2019-05-12 jcs }
6115 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
6116 c0f61fa4 2022-07-11 mark s->selected_line;
6117 c0f61fa4 2022-07-11 mark if (*new_view)
6118 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
6119 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
6120 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
6121 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6122 9b058f45 2022-06-30 mark if (err)
6123 9b058f45 2022-06-30 mark break;
6124 9b058f45 2022-06-30 mark }
6125 9b058f45 2022-06-30 mark
6126 e78dc838 2020-12-04 stsp view->focussed = 0;
6127 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6128 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
6129 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
6130 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6131 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
6132 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6133 1e37a5c2 2019-05-12 jcs if (err)
6134 34bc9ec9 2019-02-22 stsp break;
6135 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
6136 0dbbbe90 2022-06-17 op if (err)
6137 0dbbbe90 2022-06-17 op break;
6138 e78dc838 2020-12-04 stsp view->focus_child = 1;
6139 1e37a5c2 2019-05-12 jcs } else
6140 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6141 1e37a5c2 2019-05-12 jcs if (err)
6142 e5a0f69f 2018-08-18 stsp break;
6143 1e37a5c2 2019-05-12 jcs break;
6144 1e37a5c2 2019-05-12 jcs }
6145 83cc4199 2022-06-13 stsp case CTRL('d'):
6146 33c3719a 2022-06-15 stsp case 'd':
6147 83cc4199 2022-06-13 stsp nscroll /= 2;
6148 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6149 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6150 ea025d1d 2020-02-22 naddy case CTRL('f'):
6151 61417565 2022-06-20 mark case 'f':
6152 1e37a5c2 2019-05-12 jcs case ' ':
6153 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6154 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6155 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6156 640cd7ff 2022-06-22 mark view->count = 0;
6157 e5a0f69f 2018-08-18 stsp break;
6158 1e37a5c2 2019-05-12 jcs }
6159 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6160 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6161 83cc4199 2022-06-13 stsp s->selected_line +=
6162 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
6163 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
6164 1e37a5c2 2019-05-12 jcs }
6165 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
6166 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
6167 1e37a5c2 2019-05-12 jcs else
6168 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6169 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
6170 1e37a5c2 2019-05-12 jcs break;
6171 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6172 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6173 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6174 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6175 1e37a5c2 2019-05-12 jcs }
6176 1e37a5c2 2019-05-12 jcs break;
6177 1e37a5c2 2019-05-12 jcs default:
6178 640cd7ff 2022-06-22 mark view->count = 0;
6179 1e37a5c2 2019-05-12 jcs break;
6180 a70480e0 2018-06-23 stsp }
6181 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6182 917d79a7 2022-07-01 stsp }
6183 917d79a7 2022-07-01 stsp
6184 917d79a7 2022-07-01 stsp static const struct got_error *
6185 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6186 917d79a7 2022-07-01 stsp {
6187 917d79a7 2022-07-01 stsp const struct got_error *err;
6188 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6189 917d79a7 2022-07-01 stsp
6190 917d79a7 2022-07-01 stsp view->count = 0;
6191 917d79a7 2022-07-01 stsp s->done = 1;
6192 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6193 917d79a7 2022-07-01 stsp s->done = 0;
6194 917d79a7 2022-07-01 stsp if (err)
6195 917d79a7 2022-07-01 stsp return err;
6196 917d79a7 2022-07-01 stsp return run_blame(view);
6197 a70480e0 2018-06-23 stsp }
6198 a70480e0 2018-06-23 stsp
6199 a70480e0 2018-06-23 stsp static const struct got_error *
6200 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6201 9f7d7167 2018-04-29 stsp {
6202 a70480e0 2018-06-23 stsp const struct got_error *error;
6203 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6204 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6205 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6206 0587e10c 2020-07-23 stsp char *link_target = NULL;
6207 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6208 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6209 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6210 a70480e0 2018-06-23 stsp int ch;
6211 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6212 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6213 a70480e0 2018-06-23 stsp
6214 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6215 a70480e0 2018-06-23 stsp switch (ch) {
6216 a70480e0 2018-06-23 stsp case 'c':
6217 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6218 a70480e0 2018-06-23 stsp break;
6219 69069811 2018-08-02 stsp case 'r':
6220 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6221 69069811 2018-08-02 stsp if (repo_path == NULL)
6222 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6223 9ba1d308 2019-10-21 stsp optarg);
6224 69069811 2018-08-02 stsp break;
6225 a70480e0 2018-06-23 stsp default:
6226 17020d27 2019-03-07 stsp usage_blame();
6227 a70480e0 2018-06-23 stsp /* NOTREACHED */
6228 a70480e0 2018-06-23 stsp }
6229 a70480e0 2018-06-23 stsp }
6230 a70480e0 2018-06-23 stsp
6231 a70480e0 2018-06-23 stsp argc -= optind;
6232 a70480e0 2018-06-23 stsp argv += optind;
6233 a70480e0 2018-06-23 stsp
6234 f135c941 2020-02-20 stsp if (argc != 1)
6235 a70480e0 2018-06-23 stsp usage_blame();
6236 6962eb72 2020-02-20 stsp
6237 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6238 0ae84acc 2022-06-15 tracey if (error != NULL)
6239 0ae84acc 2022-06-15 tracey goto done;
6240 0ae84acc 2022-06-15 tracey
6241 69069811 2018-08-02 stsp if (repo_path == NULL) {
6242 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6243 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6244 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6245 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6246 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6247 c156c7a4 2020-12-18 stsp goto done;
6248 f135c941 2020-02-20 stsp if (worktree)
6249 eb41ed75 2019-02-05 stsp repo_path =
6250 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6251 f135c941 2020-02-20 stsp else
6252 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6253 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6254 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6255 c156c7a4 2020-12-18 stsp goto done;
6256 c156c7a4 2020-12-18 stsp }
6257 f135c941 2020-02-20 stsp }
6258 a915003a 2019-02-05 stsp
6259 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6260 c02c541e 2019-03-29 stsp if (error != NULL)
6261 8e94dd5b 2019-01-04 stsp goto done;
6262 69069811 2018-08-02 stsp
6263 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6264 f135c941 2020-02-20 stsp worktree);
6265 c02c541e 2019-03-29 stsp if (error)
6266 92205607 2019-01-04 stsp goto done;
6267 69069811 2018-08-02 stsp
6268 f135c941 2020-02-20 stsp init_curses();
6269 f135c941 2020-02-20 stsp
6270 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6271 51a10b52 2020-12-26 stsp if (error)
6272 51a10b52 2020-12-26 stsp goto done;
6273 51a10b52 2020-12-26 stsp
6274 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6275 eb41ed75 2019-02-05 stsp if (error)
6276 69069811 2018-08-02 stsp goto done;
6277 a70480e0 2018-06-23 stsp
6278 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6279 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6280 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6281 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6282 a70480e0 2018-06-23 stsp if (error != NULL)
6283 66b4983c 2018-06-23 stsp goto done;
6284 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6285 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6286 a70480e0 2018-06-23 stsp } else {
6287 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6288 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6289 a70480e0 2018-06-23 stsp }
6290 a19e88aa 2018-06-23 stsp if (error != NULL)
6291 8b473291 2019-02-21 stsp goto done;
6292 8b473291 2019-02-21 stsp
6293 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6294 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6295 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6296 e1cd8fed 2018-08-01 stsp goto done;
6297 e1cd8fed 2018-08-01 stsp }
6298 0587e10c 2020-07-23 stsp
6299 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6300 a44927cc 2022-04-07 stsp if (error)
6301 a44927cc 2022-04-07 stsp goto done;
6302 a44927cc 2022-04-07 stsp
6303 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6304 a44927cc 2022-04-07 stsp commit, repo);
6305 7cbe629d 2018-08-04 stsp if (error)
6306 7cbe629d 2018-08-04 stsp goto done;
6307 0587e10c 2020-07-23 stsp
6308 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6309 78756c87 2020-11-24 stsp commit_id, repo);
6310 0587e10c 2020-07-23 stsp if (error)
6311 0587e10c 2020-07-23 stsp goto done;
6312 12314ad4 2019-08-31 stsp if (worktree) {
6313 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6314 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6315 12314ad4 2019-08-31 stsp worktree = NULL;
6316 12314ad4 2019-08-31 stsp }
6317 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6318 a70480e0 2018-06-23 stsp done:
6319 69069811 2018-08-02 stsp free(repo_path);
6320 f135c941 2020-02-20 stsp free(in_repo_path);
6321 0587e10c 2020-07-23 stsp free(link_target);
6322 69069811 2018-08-02 stsp free(cwd);
6323 a70480e0 2018-06-23 stsp free(commit_id);
6324 a44927cc 2022-04-07 stsp if (commit)
6325 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6326 eb41ed75 2019-02-05 stsp if (worktree)
6327 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6328 1d0f4054 2021-06-17 stsp if (repo) {
6329 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6330 1d0f4054 2021-06-17 stsp if (error == NULL)
6331 1d0f4054 2021-06-17 stsp error = close_err;
6332 1d0f4054 2021-06-17 stsp }
6333 0ae84acc 2022-06-15 tracey if (pack_fds) {
6334 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6335 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6336 0ae84acc 2022-06-15 tracey if (error == NULL)
6337 0ae84acc 2022-06-15 tracey error = pack_err;
6338 0ae84acc 2022-06-15 tracey }
6339 51a10b52 2020-12-26 stsp tog_free_refs();
6340 a70480e0 2018-06-23 stsp return error;
6341 ffd1d5e5 2018-06-23 stsp }
6342 ffd1d5e5 2018-06-23 stsp
6343 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6344 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6345 ffd1d5e5 2018-06-23 stsp {
6346 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6347 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6348 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6349 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6350 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6351 94b80cfa 2022-08-01 mark int width, n, nentries, i = 1;
6352 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6353 ffd1d5e5 2018-06-23 stsp
6354 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6355 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6356 9b058f45 2022-06-30 mark --limit; /* border */
6357 ffd1d5e5 2018-06-23 stsp
6358 f7d12f7e 2018-08-01 stsp werase(view->window);
6359 ffd1d5e5 2018-06-23 stsp
6360 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6361 ffd1d5e5 2018-06-23 stsp return NULL;
6362 ffd1d5e5 2018-06-23 stsp
6363 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6364 ccda2f4d 2022-06-16 stsp 0, 0);
6365 ffd1d5e5 2018-06-23 stsp if (err)
6366 ffd1d5e5 2018-06-23 stsp return err;
6367 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6368 a3404814 2018-09-02 stsp wstandout(view->window);
6369 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6370 11b20872 2019-11-08 stsp if (tc)
6371 11b20872 2019-11-08 stsp wattr_on(view->window,
6372 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6373 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6374 11b20872 2019-11-08 stsp if (tc)
6375 11b20872 2019-11-08 stsp wattr_off(view->window,
6376 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6377 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6378 a3404814 2018-09-02 stsp wstandend(view->window);
6379 2550e4c3 2018-07-13 stsp free(wline);
6380 2550e4c3 2018-07-13 stsp wline = NULL;
6381 94b80cfa 2022-08-01 mark
6382 df68a56b 2022-08-12 mark i += s->selected;
6383 df68a56b 2022-08-12 mark if (s->first_displayed_entry) {
6384 df68a56b 2022-08-12 mark i += got_tree_entry_get_index(s->first_displayed_entry);
6385 df68a56b 2022-08-12 mark if (s->tree != s->root)
6386 df68a56b 2022-08-12 mark ++i; /* account for ".." entry */
6387 94b80cfa 2022-08-01 mark }
6388 94b80cfa 2022-08-01 mark nentries = got_object_tree_get_nentries(s->tree);
6389 94b80cfa 2022-08-01 mark wprintw(view->window, " [%d/%d]", i,
6390 94b80cfa 2022-08-01 mark nentries + (s->tree == s->root ? 0 : 1)); /* ".." in !root tree */
6391 94b80cfa 2022-08-01 mark
6392 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6393 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6394 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6395 ffd1d5e5 2018-06-23 stsp return NULL;
6396 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6397 ccda2f4d 2022-06-16 stsp 0, 0);
6398 ce52c690 2018-06-23 stsp if (err)
6399 ce52c690 2018-06-23 stsp return err;
6400 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6401 2550e4c3 2018-07-13 stsp free(wline);
6402 2550e4c3 2018-07-13 stsp wline = NULL;
6403 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6404 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6405 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6406 ffd1d5e5 2018-06-23 stsp return NULL;
6407 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6408 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6409 a1eca9bb 2018-06-23 stsp return NULL;
6410 ffd1d5e5 2018-06-23 stsp
6411 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6412 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6413 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6414 0cf4efb1 2018-09-29 stsp if (view->focussed)
6415 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6416 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6417 ffd1d5e5 2018-06-23 stsp }
6418 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6419 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6420 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6421 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6422 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6423 ffd1d5e5 2018-06-23 stsp return NULL;
6424 ffd1d5e5 2018-06-23 stsp n = 1;
6425 ffd1d5e5 2018-06-23 stsp } else {
6426 ffd1d5e5 2018-06-23 stsp n = 0;
6427 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6428 ffd1d5e5 2018-06-23 stsp }
6429 ffd1d5e5 2018-06-23 stsp
6430 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6431 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6432 848d6979 2019-08-12 stsp const char *modestr = "";
6433 56e0773d 2019-11-28 stsp mode_t mode;
6434 1d13200f 2018-07-12 stsp
6435 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6436 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6437 56e0773d 2019-11-28 stsp
6438 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6439 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6440 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6441 1d13200f 2018-07-12 stsp if (err)
6442 638f9024 2019-05-13 stsp return got_error_from_errno(
6443 230a42bd 2019-05-11 jcs "got_object_id_str");
6444 1d13200f 2018-07-12 stsp }
6445 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6446 63c5ca5d 2019-08-24 stsp modestr = "$";
6447 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6448 0d6c6ee3 2020-05-20 stsp int i;
6449 0d6c6ee3 2020-05-20 stsp
6450 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6451 d86d3b18 2020-12-01 naddy te, s->repo);
6452 0d6c6ee3 2020-05-20 stsp if (err) {
6453 0d6c6ee3 2020-05-20 stsp free(id_str);
6454 0d6c6ee3 2020-05-20 stsp return err;
6455 0d6c6ee3 2020-05-20 stsp }
6456 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6457 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6458 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6459 0d6c6ee3 2020-05-20 stsp }
6460 848d6979 2019-08-12 stsp modestr = "@";
6461 0d6c6ee3 2020-05-20 stsp }
6462 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6463 848d6979 2019-08-12 stsp modestr = "/";
6464 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6465 848d6979 2019-08-12 stsp modestr = "*";
6466 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6467 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6468 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6469 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6470 1d13200f 2018-07-12 stsp free(id_str);
6471 0d6c6ee3 2020-05-20 stsp free(link_target);
6472 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6473 1d13200f 2018-07-12 stsp }
6474 1d13200f 2018-07-12 stsp free(id_str);
6475 0d6c6ee3 2020-05-20 stsp free(link_target);
6476 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6477 ccda2f4d 2022-06-16 stsp 0, 0);
6478 ffd1d5e5 2018-06-23 stsp if (err) {
6479 ffd1d5e5 2018-06-23 stsp free(line);
6480 ffd1d5e5 2018-06-23 stsp break;
6481 ffd1d5e5 2018-06-23 stsp }
6482 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6483 0cf4efb1 2018-09-29 stsp if (view->focussed)
6484 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6485 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6486 ffd1d5e5 2018-06-23 stsp }
6487 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6488 f26dddb7 2019-11-08 stsp if (tc)
6489 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6490 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6491 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6492 f26dddb7 2019-11-08 stsp if (tc)
6493 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6494 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6495 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6496 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6497 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6498 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6499 ffd1d5e5 2018-06-23 stsp free(line);
6500 2550e4c3 2018-07-13 stsp free(wline);
6501 2550e4c3 2018-07-13 stsp wline = NULL;
6502 ffd1d5e5 2018-06-23 stsp n++;
6503 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6504 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6505 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6506 ffd1d5e5 2018-06-23 stsp break;
6507 ffd1d5e5 2018-06-23 stsp }
6508 ffd1d5e5 2018-06-23 stsp
6509 ffd1d5e5 2018-06-23 stsp return err;
6510 ffd1d5e5 2018-06-23 stsp }
6511 ffd1d5e5 2018-06-23 stsp
6512 ffd1d5e5 2018-06-23 stsp static void
6513 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6514 ffd1d5e5 2018-06-23 stsp {
6515 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6516 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6517 fa86c4bf 2020-11-29 stsp int i = 0;
6518 ffd1d5e5 2018-06-23 stsp
6519 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6520 ffd1d5e5 2018-06-23 stsp return;
6521 ffd1d5e5 2018-06-23 stsp
6522 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6523 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6524 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6525 fa86c4bf 2020-11-29 stsp if (!isroot)
6526 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6527 fa86c4bf 2020-11-29 stsp break;
6528 fa86c4bf 2020-11-29 stsp }
6529 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6530 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6531 ffd1d5e5 2018-06-23 stsp }
6532 ffd1d5e5 2018-06-23 stsp }
6533 ffd1d5e5 2018-06-23 stsp
6534 9b058f45 2022-06-30 mark static const struct got_error *
6535 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6536 ffd1d5e5 2018-06-23 stsp {
6537 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6538 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6539 ffd1d5e5 2018-06-23 stsp int n = 0;
6540 ffd1d5e5 2018-06-23 stsp
6541 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6542 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6543 694d3271 2020-12-01 naddy s->first_displayed_entry);
6544 694d3271 2020-12-01 naddy else
6545 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6546 56e0773d 2019-11-28 stsp
6547 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6548 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6549 94b80cfa 2022-08-01 mark if (last) {
6550 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
6551 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6552 94b80cfa 2022-08-01 mark }
6553 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6554 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6555 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6556 768394f3 2019-01-24 stsp }
6557 ffd1d5e5 2018-06-23 stsp }
6558 9b058f45 2022-06-30 mark
6559 9b058f45 2022-06-30 mark return NULL;
6560 ffd1d5e5 2018-06-23 stsp }
6561 ffd1d5e5 2018-06-23 stsp
6562 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6563 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6564 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6565 ffd1d5e5 2018-06-23 stsp {
6566 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6567 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6568 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6569 ffd1d5e5 2018-06-23 stsp
6570 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6571 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6572 56e0773d 2019-11-28 stsp + 1 /* slash */;
6573 ce52c690 2018-06-23 stsp if (te)
6574 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6575 ce52c690 2018-06-23 stsp
6576 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6577 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6578 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6579 ffd1d5e5 2018-06-23 stsp
6580 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6581 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6582 d9765a41 2018-06-23 stsp while (pt) {
6583 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6584 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6585 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6586 cb2ebc8a 2018-06-23 stsp goto done;
6587 cb2ebc8a 2018-06-23 stsp }
6588 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6589 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6590 cb2ebc8a 2018-06-23 stsp goto done;
6591 cb2ebc8a 2018-06-23 stsp }
6592 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6593 ffd1d5e5 2018-06-23 stsp }
6594 ce52c690 2018-06-23 stsp if (te) {
6595 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6596 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6597 ce52c690 2018-06-23 stsp goto done;
6598 ce52c690 2018-06-23 stsp }
6599 cb2ebc8a 2018-06-23 stsp }
6600 ce52c690 2018-06-23 stsp done:
6601 ce52c690 2018-06-23 stsp if (err) {
6602 ce52c690 2018-06-23 stsp free(*path);
6603 ce52c690 2018-06-23 stsp *path = NULL;
6604 ce52c690 2018-06-23 stsp }
6605 ce52c690 2018-06-23 stsp return err;
6606 ce52c690 2018-06-23 stsp }
6607 ce52c690 2018-06-23 stsp
6608 ce52c690 2018-06-23 stsp static const struct got_error *
6609 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6610 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6611 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6612 ce52c690 2018-06-23 stsp {
6613 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6614 ce52c690 2018-06-23 stsp char *path;
6615 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6616 a0de39f3 2019-08-09 stsp
6617 a0de39f3 2019-08-09 stsp *new_view = NULL;
6618 69efd4c4 2018-07-18 stsp
6619 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6620 ce52c690 2018-06-23 stsp if (err)
6621 ce52c690 2018-06-23 stsp return err;
6622 ffd1d5e5 2018-06-23 stsp
6623 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6624 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6625 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6626 83ce39e3 2019-08-12 stsp goto done;
6627 83ce39e3 2019-08-12 stsp }
6628 cdf1ee82 2018-08-01 stsp
6629 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6630 e5a0f69f 2018-08-18 stsp if (err) {
6631 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6632 fc06ba56 2019-08-22 stsp err = NULL;
6633 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6634 e5a0f69f 2018-08-18 stsp } else
6635 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6636 83ce39e3 2019-08-12 stsp done:
6637 83ce39e3 2019-08-12 stsp free(path);
6638 69efd4c4 2018-07-18 stsp return err;
6639 69efd4c4 2018-07-18 stsp }
6640 69efd4c4 2018-07-18 stsp
6641 69efd4c4 2018-07-18 stsp static const struct got_error *
6642 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6643 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6644 69efd4c4 2018-07-18 stsp {
6645 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6646 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6647 69efd4c4 2018-07-18 stsp char *path;
6648 a0de39f3 2019-08-09 stsp
6649 a0de39f3 2019-08-09 stsp *new_view = NULL;
6650 69efd4c4 2018-07-18 stsp
6651 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6652 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6653 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6654 e5a0f69f 2018-08-18 stsp
6655 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6656 69efd4c4 2018-07-18 stsp if (err)
6657 69efd4c4 2018-07-18 stsp return err;
6658 69efd4c4 2018-07-18 stsp
6659 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6660 4e97c21c 2020-12-06 stsp path, 0);
6661 ba4f502b 2018-08-04 stsp if (err)
6662 e5a0f69f 2018-08-18 stsp view_close(log_view);
6663 e5a0f69f 2018-08-18 stsp else
6664 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6665 cb2ebc8a 2018-06-23 stsp free(path);
6666 cb2ebc8a 2018-06-23 stsp return err;
6667 ffd1d5e5 2018-06-23 stsp }
6668 ffd1d5e5 2018-06-23 stsp
6669 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6670 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6671 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6672 ffd1d5e5 2018-06-23 stsp {
6673 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6674 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6675 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6676 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6677 ffd1d5e5 2018-06-23 stsp
6678 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6679 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6680 bc573f3b 2021-07-10 stsp
6681 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6682 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6683 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6684 ffd1d5e5 2018-06-23 stsp
6685 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6686 bc573f3b 2021-07-10 stsp if (err)
6687 bc573f3b 2021-07-10 stsp goto done;
6688 bc573f3b 2021-07-10 stsp
6689 bc573f3b 2021-07-10 stsp /*
6690 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6691 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6692 bc573f3b 2021-07-10 stsp * closed on demand.
6693 bc573f3b 2021-07-10 stsp */
6694 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6695 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6696 bc573f3b 2021-07-10 stsp if (err)
6697 bc573f3b 2021-07-10 stsp goto done;
6698 bc573f3b 2021-07-10 stsp s->tree = s->root;
6699 bc573f3b 2021-07-10 stsp
6700 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6701 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6702 ffd1d5e5 2018-06-23 stsp goto done;
6703 ffd1d5e5 2018-06-23 stsp
6704 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6705 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6706 ffd1d5e5 2018-06-23 stsp goto done;
6707 ffd1d5e5 2018-06-23 stsp }
6708 ffd1d5e5 2018-06-23 stsp
6709 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6710 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6711 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6712 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6713 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6714 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6715 9cd7cbd1 2020-12-07 stsp goto done;
6716 9cd7cbd1 2020-12-07 stsp }
6717 9cd7cbd1 2020-12-07 stsp }
6718 fb2756b9 2018-08-04 stsp s->repo = repo;
6719 c0b01bdb 2019-11-08 stsp
6720 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6721 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6722 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6723 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6724 c0b01bdb 2019-11-08 stsp if (err)
6725 c0b01bdb 2019-11-08 stsp goto done;
6726 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6727 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6728 bc573f3b 2021-07-10 stsp if (err)
6729 c0b01bdb 2019-11-08 stsp goto done;
6730 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6731 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6732 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6733 bc573f3b 2021-07-10 stsp if (err)
6734 c0b01bdb 2019-11-08 stsp goto done;
6735 e5a0f69f 2018-08-18 stsp
6736 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6737 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6738 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6739 bc573f3b 2021-07-10 stsp if (err)
6740 11b20872 2019-11-08 stsp goto done;
6741 11b20872 2019-11-08 stsp
6742 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6743 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6744 bc573f3b 2021-07-10 stsp if (err)
6745 c0b01bdb 2019-11-08 stsp goto done;
6746 c0b01bdb 2019-11-08 stsp }
6747 c0b01bdb 2019-11-08 stsp
6748 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6749 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6750 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6751 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6752 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6753 ad80ab7b 2018-08-04 stsp done:
6754 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6755 bc573f3b 2021-07-10 stsp if (commit)
6756 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6757 bc573f3b 2021-07-10 stsp if (err)
6758 bc573f3b 2021-07-10 stsp close_tree_view(view);
6759 ad80ab7b 2018-08-04 stsp return err;
6760 ad80ab7b 2018-08-04 stsp }
6761 ad80ab7b 2018-08-04 stsp
6762 e5a0f69f 2018-08-18 stsp static const struct got_error *
6763 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6764 ad80ab7b 2018-08-04 stsp {
6765 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6766 ad80ab7b 2018-08-04 stsp
6767 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6768 fb2756b9 2018-08-04 stsp free(s->tree_label);
6769 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6770 6484ec90 2018-09-29 stsp free(s->commit_id);
6771 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6772 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6773 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6774 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6775 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6776 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6777 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6778 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6779 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6780 ad80ab7b 2018-08-04 stsp free(parent);
6781 ad80ab7b 2018-08-04 stsp
6782 ad80ab7b 2018-08-04 stsp }
6783 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6784 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6785 bc573f3b 2021-07-10 stsp if (s->root)
6786 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6787 7c32bd05 2019-06-22 stsp return NULL;
6788 7c32bd05 2019-06-22 stsp }
6789 7c32bd05 2019-06-22 stsp
6790 7c32bd05 2019-06-22 stsp static const struct got_error *
6791 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6792 7c32bd05 2019-06-22 stsp {
6793 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6794 7c32bd05 2019-06-22 stsp
6795 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6796 7c32bd05 2019-06-22 stsp return NULL;
6797 7c32bd05 2019-06-22 stsp }
6798 7c32bd05 2019-06-22 stsp
6799 7c32bd05 2019-06-22 stsp static int
6800 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6801 7c32bd05 2019-06-22 stsp {
6802 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6803 7c32bd05 2019-06-22 stsp
6804 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6805 56e0773d 2019-11-28 stsp 0) == 0;
6806 7c32bd05 2019-06-22 stsp }
6807 7c32bd05 2019-06-22 stsp
6808 7c32bd05 2019-06-22 stsp static const struct got_error *
6809 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6810 7c32bd05 2019-06-22 stsp {
6811 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6812 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6813 7c32bd05 2019-06-22 stsp
6814 7c32bd05 2019-06-22 stsp if (!view->searching) {
6815 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6816 7c32bd05 2019-06-22 stsp return NULL;
6817 7c32bd05 2019-06-22 stsp }
6818 7c32bd05 2019-06-22 stsp
6819 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6820 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6821 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6822 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6823 56e0773d 2019-11-28 stsp s->selected_entry);
6824 7c32bd05 2019-06-22 stsp else
6825 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6826 56e0773d 2019-11-28 stsp } else {
6827 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6828 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6829 56e0773d 2019-11-28 stsp else
6830 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6831 56e0773d 2019-11-28 stsp s->selected_entry);
6832 7c32bd05 2019-06-22 stsp }
6833 7c32bd05 2019-06-22 stsp } else {
6834 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6835 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6836 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6837 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6838 56e0773d 2019-11-28 stsp else
6839 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6840 7c32bd05 2019-06-22 stsp }
6841 7c32bd05 2019-06-22 stsp
6842 7c32bd05 2019-06-22 stsp while (1) {
6843 56e0773d 2019-11-28 stsp if (te == NULL) {
6844 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6845 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6846 ac66afb8 2019-06-24 stsp return NULL;
6847 ac66afb8 2019-06-24 stsp }
6848 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6849 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6850 56e0773d 2019-11-28 stsp else
6851 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6852 7c32bd05 2019-06-22 stsp }
6853 7c32bd05 2019-06-22 stsp
6854 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6855 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6856 56e0773d 2019-11-28 stsp s->matched_entry = te;
6857 7c32bd05 2019-06-22 stsp break;
6858 7c32bd05 2019-06-22 stsp }
6859 7c32bd05 2019-06-22 stsp
6860 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6861 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6862 56e0773d 2019-11-28 stsp else
6863 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6864 7c32bd05 2019-06-22 stsp }
6865 e5a0f69f 2018-08-18 stsp
6866 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6867 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6868 7c32bd05 2019-06-22 stsp s->selected = 0;
6869 7c32bd05 2019-06-22 stsp }
6870 7c32bd05 2019-06-22 stsp
6871 e5a0f69f 2018-08-18 stsp return NULL;
6872 ad80ab7b 2018-08-04 stsp }
6873 ad80ab7b 2018-08-04 stsp
6874 ad80ab7b 2018-08-04 stsp static const struct got_error *
6875 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6876 ad80ab7b 2018-08-04 stsp {
6877 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6878 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6879 e5a0f69f 2018-08-18 stsp char *parent_path;
6880 ad80ab7b 2018-08-04 stsp
6881 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6882 e5a0f69f 2018-08-18 stsp if (err)
6883 e5a0f69f 2018-08-18 stsp return err;
6884 ffd1d5e5 2018-06-23 stsp
6885 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6886 e5a0f69f 2018-08-18 stsp free(parent_path);
6887 669b5ffa 2018-10-07 stsp
6888 9b058f45 2022-06-30 mark view_border(view);
6889 e5a0f69f 2018-08-18 stsp return err;
6890 94b80cfa 2022-08-01 mark }
6891 94b80cfa 2022-08-01 mark
6892 94b80cfa 2022-08-01 mark static const struct got_error *
6893 94b80cfa 2022-08-01 mark tree_goto_line(struct tog_view *view, int nlines)
6894 94b80cfa 2022-08-01 mark {
6895 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
6896 94b80cfa 2022-08-01 mark struct tog_tree_view_state *s = &view->state.tree;
6897 94b80cfa 2022-08-01 mark struct got_tree_entry **fte, **lte, **ste;
6898 94b80cfa 2022-08-01 mark int g, last, first = 1, i = 1;
6899 94b80cfa 2022-08-01 mark int root = s->tree == s->root;
6900 94b80cfa 2022-08-01 mark int off = root ? 1 : 2;
6901 94b80cfa 2022-08-01 mark
6902 94b80cfa 2022-08-01 mark g = view->gline;
6903 94b80cfa 2022-08-01 mark view->gline = 0;
6904 94b80cfa 2022-08-01 mark
6905 94b80cfa 2022-08-01 mark if (g == 0)
6906 94b80cfa 2022-08-01 mark g = 1;
6907 94b80cfa 2022-08-01 mark else if (g > got_object_tree_get_nentries(s->tree))
6908 94b80cfa 2022-08-01 mark g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
6909 94b80cfa 2022-08-01 mark
6910 94b80cfa 2022-08-01 mark fte = &s->first_displayed_entry;
6911 94b80cfa 2022-08-01 mark lte = &s->last_displayed_entry;
6912 94b80cfa 2022-08-01 mark ste = &s->selected_entry;
6913 94b80cfa 2022-08-01 mark
6914 94b80cfa 2022-08-01 mark if (*fte != NULL) {
6915 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6916 94b80cfa 2022-08-01 mark first += off; /* account for ".." */
6917 94b80cfa 2022-08-01 mark }
6918 94b80cfa 2022-08-01 mark last = got_tree_entry_get_index(*lte);
6919 94b80cfa 2022-08-01 mark last += off;
6920 94b80cfa 2022-08-01 mark
6921 94b80cfa 2022-08-01 mark if (g >= first && g <= last && g - first < nlines) {
6922 94b80cfa 2022-08-01 mark s->selected = g - first;
6923 94b80cfa 2022-08-01 mark return NULL; /* gline is on the current page */
6924 94b80cfa 2022-08-01 mark }
6925 94b80cfa 2022-08-01 mark
6926 94b80cfa 2022-08-01 mark if (*ste != NULL) {
6927 94b80cfa 2022-08-01 mark i = got_tree_entry_get_index(*ste);
6928 94b80cfa 2022-08-01 mark i += off;
6929 94b80cfa 2022-08-01 mark }
6930 94b80cfa 2022-08-01 mark
6931 94b80cfa 2022-08-01 mark if (i < g) {
6932 94b80cfa 2022-08-01 mark err = tree_scroll_down(view, g - i);
6933 94b80cfa 2022-08-01 mark if (err)
6934 94b80cfa 2022-08-01 mark return err;
6935 94b80cfa 2022-08-01 mark if (got_tree_entry_get_index(*lte) >=
6936 94b80cfa 2022-08-01 mark got_object_tree_get_nentries(s->tree) - 1 &&
6937 94b80cfa 2022-08-01 mark first + s->selected < g &&
6938 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1) {
6939 94b80cfa 2022-08-01 mark first = got_tree_entry_get_index(*fte);
6940 94b80cfa 2022-08-01 mark first += off;
6941 94b80cfa 2022-08-01 mark s->selected = g - first;
6942 94b80cfa 2022-08-01 mark }
6943 94b80cfa 2022-08-01 mark } else if (i > g)
6944 94b80cfa 2022-08-01 mark tree_scroll_up(s, i - g);
6945 94b80cfa 2022-08-01 mark
6946 94b80cfa 2022-08-01 mark if (g < nlines &&
6947 94b80cfa 2022-08-01 mark (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
6948 94b80cfa 2022-08-01 mark s->selected = g - 1;
6949 94b80cfa 2022-08-01 mark
6950 94b80cfa 2022-08-01 mark return NULL;
6951 e5a0f69f 2018-08-18 stsp }
6952 ce52c690 2018-06-23 stsp
6953 e5a0f69f 2018-08-18 stsp static const struct got_error *
6954 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6955 e5a0f69f 2018-08-18 stsp {
6956 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6957 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6958 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6959 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
6960 ffd1d5e5 2018-06-23 stsp
6961 94b80cfa 2022-08-01 mark if (view->gline)
6962 94b80cfa 2022-08-01 mark return tree_goto_line(view, nscroll);
6963 94b80cfa 2022-08-01 mark
6964 e5a0f69f 2018-08-18 stsp switch (ch) {
6965 1e37a5c2 2019-05-12 jcs case 'i':
6966 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6967 640cd7ff 2022-06-22 mark view->count = 0;
6968 1e37a5c2 2019-05-12 jcs break;
6969 5e98fb33 2022-07-22 mark case 'L':
6970 640cd7ff 2022-06-22 mark view->count = 0;
6971 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6972 ffd1d5e5 2018-06-23 stsp break;
6973 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6974 152c1c93 2020-11-29 stsp break;
6975 5e98fb33 2022-07-22 mark case 'R':
6976 640cd7ff 2022-06-22 mark view->count = 0;
6977 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
6978 e4526bf5 2021-09-03 naddy break;
6979 e4526bf5 2021-09-03 naddy case 'g':
6980 e4526bf5 2021-09-03 naddy case KEY_HOME:
6981 e4526bf5 2021-09-03 naddy s->selected = 0;
6982 640cd7ff 2022-06-22 mark view->count = 0;
6983 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6984 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6985 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6986 e4526bf5 2021-09-03 naddy else
6987 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6988 1e37a5c2 2019-05-12 jcs break;
6989 e4526bf5 2021-09-03 naddy case 'G':
6990 9b058f45 2022-06-30 mark case KEY_END: {
6991 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6992 9b058f45 2022-06-30 mark
6993 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6994 9b058f45 2022-06-30 mark --eos; /* border */
6995 e4526bf5 2021-09-03 naddy s->selected = 0;
6996 640cd7ff 2022-06-22 mark view->count = 0;
6997 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6998 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6999 e4526bf5 2021-09-03 naddy if (te == NULL) {
7000 ad055527 2022-07-16 mark if (s->tree != s->root) {
7001 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7002 e4526bf5 2021-09-03 naddy n++;
7003 e4526bf5 2021-09-03 naddy }
7004 e4526bf5 2021-09-03 naddy break;
7005 e4526bf5 2021-09-03 naddy }
7006 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7007 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7008 e4526bf5 2021-09-03 naddy }
7009 e4526bf5 2021-09-03 naddy if (n > 0)
7010 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7011 e4526bf5 2021-09-03 naddy break;
7012 9b058f45 2022-06-30 mark }
7013 1e37a5c2 2019-05-12 jcs case 'k':
7014 1e37a5c2 2019-05-12 jcs case KEY_UP:
7015 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7016 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7017 1e37a5c2 2019-05-12 jcs s->selected--;
7018 fa86c4bf 2020-11-29 stsp break;
7019 1e37a5c2 2019-05-12 jcs }
7020 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7021 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7022 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7023 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7024 640cd7ff 2022-06-22 mark view->count = 0;
7025 1e37a5c2 2019-05-12 jcs break;
7026 83cc4199 2022-06-13 stsp case CTRL('u'):
7027 33c3719a 2022-06-15 stsp case 'u':
7028 83cc4199 2022-06-13 stsp nscroll /= 2;
7029 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7030 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7031 ea025d1d 2020-02-22 naddy case CTRL('b'):
7032 61417565 2022-06-20 mark case 'b':
7033 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7034 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7035 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7036 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7037 fa86c4bf 2020-11-29 stsp } else {
7038 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7039 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
7040 fa86c4bf 2020-11-29 stsp }
7041 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
7042 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
7043 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
7044 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
7045 640cd7ff 2022-06-22 mark view->count = 0;
7046 1e37a5c2 2019-05-12 jcs break;
7047 1e37a5c2 2019-05-12 jcs case 'j':
7048 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7049 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7050 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7051 1e37a5c2 2019-05-12 jcs s->selected++;
7052 1e37a5c2 2019-05-12 jcs break;
7053 1e37a5c2 2019-05-12 jcs }
7054 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7055 640cd7ff 2022-06-22 mark == NULL) {
7056 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7057 640cd7ff 2022-06-22 mark view->count = 0;
7058 1e37a5c2 2019-05-12 jcs break;
7059 640cd7ff 2022-06-22 mark }
7060 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
7061 1e37a5c2 2019-05-12 jcs break;
7062 83cc4199 2022-06-13 stsp case CTRL('d'):
7063 33c3719a 2022-06-15 stsp case 'd':
7064 83cc4199 2022-06-13 stsp nscroll /= 2;
7065 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7066 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7067 ea025d1d 2020-02-22 naddy case CTRL('f'):
7068 61417565 2022-06-20 mark case 'f':
7069 48bb96f0 2022-06-20 naddy case ' ':
7070 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7071 1e37a5c2 2019-05-12 jcs == NULL) {
7072 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7073 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7074 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7075 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7076 640cd7ff 2022-06-22 mark else
7077 640cd7ff 2022-06-22 mark view->count = 0;
7078 1e37a5c2 2019-05-12 jcs break;
7079 1e37a5c2 2019-05-12 jcs }
7080 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
7081 1e37a5c2 2019-05-12 jcs break;
7082 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7083 1e37a5c2 2019-05-12 jcs case '\r':
7084 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7085 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7086 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7087 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7088 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
7089 640cd7ff 2022-06-22 mark view->count = 0;
7090 1e37a5c2 2019-05-12 jcs break;
7091 640cd7ff 2022-06-22 mark }
7092 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7093 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7094 1e37a5c2 2019-05-12 jcs entry);
7095 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7096 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7097 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7098 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7099 1e37a5c2 2019-05-12 jcs s->selected_entry =
7100 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7101 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7102 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
7103 9b058f45 2022-06-30 mark err = offset_selection_down(view);
7104 9b058f45 2022-06-30 mark if (err)
7105 9b058f45 2022-06-30 mark break;
7106 9b058f45 2022-06-30 mark }
7107 1e37a5c2 2019-05-12 jcs free(parent);
7108 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7109 56e0773d 2019-11-28 stsp s->selected_entry))) {
7110 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7111 640cd7ff 2022-06-22 mark view->count = 0;
7112 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7113 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7114 1e37a5c2 2019-05-12 jcs if (err)
7115 1e37a5c2 2019-05-12 jcs break;
7116 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7117 941e9f74 2019-05-21 stsp if (err) {
7118 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7119 1e37a5c2 2019-05-12 jcs break;
7120 1e37a5c2 2019-05-12 jcs }
7121 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7122 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7123 1e37a5c2 2019-05-12 jcs break;
7124 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7125 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7126 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7127 640cd7ff 2022-06-22 mark view->count = 0;
7128 1e37a5c2 2019-05-12 jcs break;
7129 1e37a5c2 2019-05-12 jcs default:
7130 640cd7ff 2022-06-22 mark view->count = 0;
7131 1e37a5c2 2019-05-12 jcs break;
7132 ffd1d5e5 2018-06-23 stsp }
7133 e5a0f69f 2018-08-18 stsp
7134 ffd1d5e5 2018-06-23 stsp return err;
7135 9f7d7167 2018-04-29 stsp }
7136 9f7d7167 2018-04-29 stsp
7137 ffd1d5e5 2018-06-23 stsp __dead static void
7138 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7139 ffd1d5e5 2018-06-23 stsp {
7140 ffd1d5e5 2018-06-23 stsp endwin();
7141 87411fa9 2022-06-16 stsp fprintf(stderr,
7142 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7143 ffd1d5e5 2018-06-23 stsp getprogname());
7144 ffd1d5e5 2018-06-23 stsp exit(1);
7145 ffd1d5e5 2018-06-23 stsp }
7146 ffd1d5e5 2018-06-23 stsp
7147 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7148 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7149 ffd1d5e5 2018-06-23 stsp {
7150 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7151 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7152 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7153 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7154 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7155 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7156 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7157 4e97c21c 2020-12-06 stsp char *label = NULL;
7158 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7159 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7160 ffd1d5e5 2018-06-23 stsp int ch;
7161 5221c383 2018-08-01 stsp struct tog_view *view;
7162 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7163 70ac5f84 2019-03-28 stsp
7164 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7165 ffd1d5e5 2018-06-23 stsp switch (ch) {
7166 ffd1d5e5 2018-06-23 stsp case 'c':
7167 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7168 74283ab8 2020-02-07 stsp break;
7169 74283ab8 2020-02-07 stsp case 'r':
7170 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7171 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7172 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7173 74283ab8 2020-02-07 stsp optarg);
7174 ffd1d5e5 2018-06-23 stsp break;
7175 ffd1d5e5 2018-06-23 stsp default:
7176 e99e2d15 2020-11-24 naddy usage_tree();
7177 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7178 ffd1d5e5 2018-06-23 stsp }
7179 ffd1d5e5 2018-06-23 stsp }
7180 ffd1d5e5 2018-06-23 stsp
7181 ffd1d5e5 2018-06-23 stsp argc -= optind;
7182 ffd1d5e5 2018-06-23 stsp argv += optind;
7183 ffd1d5e5 2018-06-23 stsp
7184 55cccc34 2020-02-20 stsp if (argc > 1)
7185 e99e2d15 2020-11-24 naddy usage_tree();
7186 0ae84acc 2022-06-15 tracey
7187 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7188 0ae84acc 2022-06-15 tracey if (error != NULL)
7189 0ae84acc 2022-06-15 tracey goto done;
7190 74283ab8 2020-02-07 stsp
7191 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7192 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7193 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7194 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7195 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7196 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7197 c156c7a4 2020-12-18 stsp goto done;
7198 55cccc34 2020-02-20 stsp if (worktree)
7199 52185f70 2019-02-05 stsp repo_path =
7200 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7201 55cccc34 2020-02-20 stsp else
7202 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7203 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7204 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7205 c156c7a4 2020-12-18 stsp goto done;
7206 c156c7a4 2020-12-18 stsp }
7207 55cccc34 2020-02-20 stsp }
7208 a915003a 2019-02-05 stsp
7209 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7210 c02c541e 2019-03-29 stsp if (error != NULL)
7211 52185f70 2019-02-05 stsp goto done;
7212 d188b9a6 2019-01-04 stsp
7213 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7214 55cccc34 2020-02-20 stsp repo, worktree);
7215 55cccc34 2020-02-20 stsp if (error)
7216 55cccc34 2020-02-20 stsp goto done;
7217 55cccc34 2020-02-20 stsp
7218 55cccc34 2020-02-20 stsp init_curses();
7219 55cccc34 2020-02-20 stsp
7220 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7221 c02c541e 2019-03-29 stsp if (error)
7222 52185f70 2019-02-05 stsp goto done;
7223 ffd1d5e5 2018-06-23 stsp
7224 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7225 51a10b52 2020-12-26 stsp if (error)
7226 51a10b52 2020-12-26 stsp goto done;
7227 51a10b52 2020-12-26 stsp
7228 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7229 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7230 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7231 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7232 4e97c21c 2020-12-06 stsp if (error)
7233 4e97c21c 2020-12-06 stsp goto done;
7234 4e97c21c 2020-12-06 stsp head_ref_name = label;
7235 4e97c21c 2020-12-06 stsp } else {
7236 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7237 4e97c21c 2020-12-06 stsp if (error == NULL)
7238 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7239 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7240 4e97c21c 2020-12-06 stsp goto done;
7241 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7242 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7243 4e97c21c 2020-12-06 stsp if (error)
7244 4e97c21c 2020-12-06 stsp goto done;
7245 4e97c21c 2020-12-06 stsp }
7246 ffd1d5e5 2018-06-23 stsp
7247 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7248 a44927cc 2022-04-07 stsp if (error)
7249 a44927cc 2022-04-07 stsp goto done;
7250 a44927cc 2022-04-07 stsp
7251 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7252 5221c383 2018-08-01 stsp if (view == NULL) {
7253 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7254 5221c383 2018-08-01 stsp goto done;
7255 5221c383 2018-08-01 stsp }
7256 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7257 ad80ab7b 2018-08-04 stsp if (error)
7258 ad80ab7b 2018-08-04 stsp goto done;
7259 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7260 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7261 d91faf3b 2020-12-01 naddy in_repo_path);
7262 55cccc34 2020-02-20 stsp if (error)
7263 55cccc34 2020-02-20 stsp goto done;
7264 55cccc34 2020-02-20 stsp }
7265 55cccc34 2020-02-20 stsp
7266 55cccc34 2020-02-20 stsp if (worktree) {
7267 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7268 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7269 55cccc34 2020-02-20 stsp worktree = NULL;
7270 55cccc34 2020-02-20 stsp }
7271 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7272 ffd1d5e5 2018-06-23 stsp done:
7273 52185f70 2019-02-05 stsp free(repo_path);
7274 e4a0e26d 2020-02-20 stsp free(cwd);
7275 ffd1d5e5 2018-06-23 stsp free(commit_id);
7276 4e97c21c 2020-12-06 stsp free(label);
7277 486cd271 2020-12-06 stsp if (ref)
7278 486cd271 2020-12-06 stsp got_ref_close(ref);
7279 1d0f4054 2021-06-17 stsp if (repo) {
7280 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7281 1d0f4054 2021-06-17 stsp if (error == NULL)
7282 1d0f4054 2021-06-17 stsp error = close_err;
7283 1d0f4054 2021-06-17 stsp }
7284 0ae84acc 2022-06-15 tracey if (pack_fds) {
7285 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7286 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7287 0ae84acc 2022-06-15 tracey if (error == NULL)
7288 0ae84acc 2022-06-15 tracey error = pack_err;
7289 0ae84acc 2022-06-15 tracey }
7290 51a10b52 2020-12-26 stsp tog_free_refs();
7291 ffd1d5e5 2018-06-23 stsp return error;
7292 6458efa5 2020-11-24 stsp }
7293 6458efa5 2020-11-24 stsp
7294 6458efa5 2020-11-24 stsp static const struct got_error *
7295 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7296 6458efa5 2020-11-24 stsp {
7297 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7298 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7299 6458efa5 2020-11-24 stsp
7300 6458efa5 2020-11-24 stsp s->nrefs = 0;
7301 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7302 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7303 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7304 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7305 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7306 6458efa5 2020-11-24 stsp continue;
7307 6458efa5 2020-11-24 stsp
7308 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7309 6458efa5 2020-11-24 stsp if (re == NULL)
7310 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7311 6458efa5 2020-11-24 stsp
7312 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7313 8924d611 2020-12-26 stsp if (re->ref == NULL)
7314 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7315 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7316 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7317 6458efa5 2020-11-24 stsp }
7318 6458efa5 2020-11-24 stsp
7319 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7320 6458efa5 2020-11-24 stsp return NULL;
7321 6458efa5 2020-11-24 stsp }
7322 6458efa5 2020-11-24 stsp
7323 336075a4 2022-06-25 op static void
7324 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7325 6458efa5 2020-11-24 stsp {
7326 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7327 6458efa5 2020-11-24 stsp
7328 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7329 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7330 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7331 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7332 6458efa5 2020-11-24 stsp free(re);
7333 6458efa5 2020-11-24 stsp }
7334 6458efa5 2020-11-24 stsp }
7335 6458efa5 2020-11-24 stsp
7336 6458efa5 2020-11-24 stsp static const struct got_error *
7337 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7338 6458efa5 2020-11-24 stsp {
7339 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7340 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7341 6458efa5 2020-11-24 stsp
7342 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7343 6458efa5 2020-11-24 stsp s->repo = repo;
7344 6458efa5 2020-11-24 stsp
7345 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7346 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7347 6458efa5 2020-11-24 stsp
7348 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7349 6458efa5 2020-11-24 stsp if (err)
7350 6458efa5 2020-11-24 stsp return err;
7351 34ba6917 2020-11-30 stsp
7352 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7353 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7354 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7355 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7356 6458efa5 2020-11-24 stsp if (err)
7357 6458efa5 2020-11-24 stsp goto done;
7358 6458efa5 2020-11-24 stsp
7359 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7360 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7361 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7362 6458efa5 2020-11-24 stsp if (err)
7363 6458efa5 2020-11-24 stsp goto done;
7364 6458efa5 2020-11-24 stsp
7365 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7366 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7367 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7368 cc488aa7 2022-01-23 stsp if (err)
7369 cc488aa7 2022-01-23 stsp goto done;
7370 cc488aa7 2022-01-23 stsp
7371 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7372 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7373 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7374 6458efa5 2020-11-24 stsp if (err)
7375 6458efa5 2020-11-24 stsp goto done;
7376 6458efa5 2020-11-24 stsp }
7377 6458efa5 2020-11-24 stsp
7378 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7379 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7380 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7381 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7382 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7383 6458efa5 2020-11-24 stsp done:
7384 6458efa5 2020-11-24 stsp if (err)
7385 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7386 6458efa5 2020-11-24 stsp return err;
7387 6458efa5 2020-11-24 stsp }
7388 6458efa5 2020-11-24 stsp
7389 6458efa5 2020-11-24 stsp static const struct got_error *
7390 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7391 6458efa5 2020-11-24 stsp {
7392 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7395 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7396 6458efa5 2020-11-24 stsp
7397 6458efa5 2020-11-24 stsp return NULL;
7398 9f7d7167 2018-04-29 stsp }
7399 ce5b7c56 2019-07-09 stsp
7400 6458efa5 2020-11-24 stsp static const struct got_error *
7401 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7402 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7403 6458efa5 2020-11-24 stsp {
7404 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7405 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7406 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7407 6458efa5 2020-11-24 stsp int obj_type;
7408 6458efa5 2020-11-24 stsp
7409 c42c9805 2020-11-24 stsp *commit_id = NULL;
7410 6458efa5 2020-11-24 stsp
7411 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7412 6458efa5 2020-11-24 stsp if (err)
7413 6458efa5 2020-11-24 stsp return err;
7414 6458efa5 2020-11-24 stsp
7415 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7416 6458efa5 2020-11-24 stsp if (err)
7417 6458efa5 2020-11-24 stsp goto done;
7418 6458efa5 2020-11-24 stsp
7419 6458efa5 2020-11-24 stsp switch (obj_type) {
7420 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7421 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7422 6458efa5 2020-11-24 stsp break;
7423 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7424 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7425 6458efa5 2020-11-24 stsp if (err)
7426 6458efa5 2020-11-24 stsp goto done;
7427 c42c9805 2020-11-24 stsp free(obj_id);
7428 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7429 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7430 c42c9805 2020-11-24 stsp if (err)
7431 6458efa5 2020-11-24 stsp goto done;
7432 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7433 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7434 c42c9805 2020-11-24 stsp goto done;
7435 c42c9805 2020-11-24 stsp }
7436 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7437 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7438 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7439 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7440 c42c9805 2020-11-24 stsp goto done;
7441 c42c9805 2020-11-24 stsp }
7442 6458efa5 2020-11-24 stsp break;
7443 6458efa5 2020-11-24 stsp default:
7444 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7445 c42c9805 2020-11-24 stsp break;
7446 6458efa5 2020-11-24 stsp }
7447 6458efa5 2020-11-24 stsp
7448 c42c9805 2020-11-24 stsp done:
7449 c42c9805 2020-11-24 stsp if (tag)
7450 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7451 c42c9805 2020-11-24 stsp if (err) {
7452 c42c9805 2020-11-24 stsp free(*commit_id);
7453 c42c9805 2020-11-24 stsp *commit_id = NULL;
7454 c42c9805 2020-11-24 stsp }
7455 c42c9805 2020-11-24 stsp return err;
7456 c42c9805 2020-11-24 stsp }
7457 c42c9805 2020-11-24 stsp
7458 c42c9805 2020-11-24 stsp static const struct got_error *
7459 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7460 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7461 c42c9805 2020-11-24 stsp {
7462 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7463 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7464 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7465 c42c9805 2020-11-24 stsp
7466 c42c9805 2020-11-24 stsp *new_view = NULL;
7467 c42c9805 2020-11-24 stsp
7468 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7469 c42c9805 2020-11-24 stsp if (err) {
7470 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7471 c42c9805 2020-11-24 stsp return err;
7472 c42c9805 2020-11-24 stsp else
7473 c42c9805 2020-11-24 stsp return NULL;
7474 c42c9805 2020-11-24 stsp }
7475 c42c9805 2020-11-24 stsp
7476 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7477 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7478 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7479 6458efa5 2020-11-24 stsp goto done;
7480 6458efa5 2020-11-24 stsp }
7481 6458efa5 2020-11-24 stsp
7482 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7483 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7484 6458efa5 2020-11-24 stsp done:
7485 6458efa5 2020-11-24 stsp if (err)
7486 6458efa5 2020-11-24 stsp view_close(log_view);
7487 6458efa5 2020-11-24 stsp else
7488 6458efa5 2020-11-24 stsp *new_view = log_view;
7489 c42c9805 2020-11-24 stsp free(commit_id);
7490 6458efa5 2020-11-24 stsp return err;
7491 6458efa5 2020-11-24 stsp }
7492 6458efa5 2020-11-24 stsp
7493 ce5b7c56 2019-07-09 stsp static void
7494 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7495 6458efa5 2020-11-24 stsp {
7496 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7497 34ba6917 2020-11-30 stsp int i = 0;
7498 6458efa5 2020-11-24 stsp
7499 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7500 6458efa5 2020-11-24 stsp return;
7501 6458efa5 2020-11-24 stsp
7502 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7503 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7504 34ba6917 2020-11-30 stsp if (re == NULL)
7505 34ba6917 2020-11-30 stsp break;
7506 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7507 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7508 6458efa5 2020-11-24 stsp }
7509 6458efa5 2020-11-24 stsp }
7510 6458efa5 2020-11-24 stsp
7511 9b058f45 2022-06-30 mark static const struct got_error *
7512 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7513 6458efa5 2020-11-24 stsp {
7514 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7515 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7516 6458efa5 2020-11-24 stsp int n = 0;
7517 6458efa5 2020-11-24 stsp
7518 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7519 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7520 6458efa5 2020-11-24 stsp else
7521 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7522 6458efa5 2020-11-24 stsp
7523 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7524 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7525 94b80cfa 2022-08-01 mark if (last) {
7526 94b80cfa 2022-08-01 mark s->last_displayed_entry = last;
7527 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7528 94b80cfa 2022-08-01 mark }
7529 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7530 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7531 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7532 6458efa5 2020-11-24 stsp }
7533 6458efa5 2020-11-24 stsp }
7534 9b058f45 2022-06-30 mark
7535 9b058f45 2022-06-30 mark return NULL;
7536 6458efa5 2020-11-24 stsp }
7537 6458efa5 2020-11-24 stsp
7538 6458efa5 2020-11-24 stsp static const struct got_error *
7539 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7540 6458efa5 2020-11-24 stsp {
7541 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7542 6458efa5 2020-11-24 stsp
7543 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7544 6458efa5 2020-11-24 stsp return NULL;
7545 6458efa5 2020-11-24 stsp }
7546 6458efa5 2020-11-24 stsp
7547 6458efa5 2020-11-24 stsp static int
7548 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7549 6458efa5 2020-11-24 stsp {
7550 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7551 6458efa5 2020-11-24 stsp
7552 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7553 6458efa5 2020-11-24 stsp 0) == 0;
7554 6458efa5 2020-11-24 stsp }
7555 6458efa5 2020-11-24 stsp
7556 6458efa5 2020-11-24 stsp static const struct got_error *
7557 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7558 6458efa5 2020-11-24 stsp {
7559 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7560 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7561 6458efa5 2020-11-24 stsp
7562 6458efa5 2020-11-24 stsp if (!view->searching) {
7563 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7564 6458efa5 2020-11-24 stsp return NULL;
7565 6458efa5 2020-11-24 stsp }
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7568 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7569 6458efa5 2020-11-24 stsp if (s->selected_entry)
7570 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7571 6458efa5 2020-11-24 stsp else
7572 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7573 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7574 6458efa5 2020-11-24 stsp } else {
7575 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7576 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7577 6458efa5 2020-11-24 stsp else
7578 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7579 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7580 6458efa5 2020-11-24 stsp }
7581 6458efa5 2020-11-24 stsp } else {
7582 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7583 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7584 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7585 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7586 6458efa5 2020-11-24 stsp else
7587 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7588 6458efa5 2020-11-24 stsp }
7589 6458efa5 2020-11-24 stsp
7590 6458efa5 2020-11-24 stsp while (1) {
7591 6458efa5 2020-11-24 stsp if (re == NULL) {
7592 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7593 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7594 6458efa5 2020-11-24 stsp return NULL;
7595 6458efa5 2020-11-24 stsp }
7596 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7597 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7598 6458efa5 2020-11-24 stsp else
7599 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7600 6458efa5 2020-11-24 stsp }
7601 6458efa5 2020-11-24 stsp
7602 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7603 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7604 6458efa5 2020-11-24 stsp s->matched_entry = re;
7605 6458efa5 2020-11-24 stsp break;
7606 6458efa5 2020-11-24 stsp }
7607 6458efa5 2020-11-24 stsp
7608 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7609 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7610 6458efa5 2020-11-24 stsp else
7611 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7612 6458efa5 2020-11-24 stsp }
7613 6458efa5 2020-11-24 stsp
7614 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7615 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7616 6458efa5 2020-11-24 stsp s->selected = 0;
7617 6458efa5 2020-11-24 stsp }
7618 6458efa5 2020-11-24 stsp
7619 6458efa5 2020-11-24 stsp return NULL;
7620 6458efa5 2020-11-24 stsp }
7621 6458efa5 2020-11-24 stsp
7622 6458efa5 2020-11-24 stsp static const struct got_error *
7623 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7624 6458efa5 2020-11-24 stsp {
7625 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7626 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7627 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7628 6458efa5 2020-11-24 stsp char *line = NULL;
7629 6458efa5 2020-11-24 stsp wchar_t *wline;
7630 6458efa5 2020-11-24 stsp struct tog_color *tc;
7631 6458efa5 2020-11-24 stsp int width, n;
7632 6458efa5 2020-11-24 stsp int limit = view->nlines;
7633 6458efa5 2020-11-24 stsp
7634 6458efa5 2020-11-24 stsp werase(view->window);
7635 6458efa5 2020-11-24 stsp
7636 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7637 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7638 9b058f45 2022-06-30 mark --limit; /* border */
7639 6458efa5 2020-11-24 stsp
7640 6458efa5 2020-11-24 stsp if (limit == 0)
7641 6458efa5 2020-11-24 stsp return NULL;
7642 6458efa5 2020-11-24 stsp
7643 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7644 6458efa5 2020-11-24 stsp
7645 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7646 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7647 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7648 6458efa5 2020-11-24 stsp
7649 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7650 6458efa5 2020-11-24 stsp if (err) {
7651 6458efa5 2020-11-24 stsp free(line);
7652 6458efa5 2020-11-24 stsp return err;
7653 6458efa5 2020-11-24 stsp }
7654 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7655 6458efa5 2020-11-24 stsp wstandout(view->window);
7656 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7657 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7658 6458efa5 2020-11-24 stsp wstandend(view->window);
7659 6458efa5 2020-11-24 stsp free(wline);
7660 6458efa5 2020-11-24 stsp wline = NULL;
7661 6458efa5 2020-11-24 stsp free(line);
7662 6458efa5 2020-11-24 stsp line = NULL;
7663 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7664 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7665 6458efa5 2020-11-24 stsp if (--limit <= 0)
7666 6458efa5 2020-11-24 stsp return NULL;
7667 6458efa5 2020-11-24 stsp
7668 6458efa5 2020-11-24 stsp n = 0;
7669 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7670 6458efa5 2020-11-24 stsp char *line = NULL;
7671 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7672 6458efa5 2020-11-24 stsp
7673 b4996bee 2022-06-16 stsp if (s->show_date) {
7674 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7675 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7676 b4996bee 2022-06-16 stsp struct got_object_id *id;
7677 b4996bee 2022-06-16 stsp struct tm tm;
7678 b4996bee 2022-06-16 stsp time_t t;
7679 b4996bee 2022-06-16 stsp
7680 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7681 b4996bee 2022-06-16 stsp if (err)
7682 b4996bee 2022-06-16 stsp return err;
7683 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7684 b4996bee 2022-06-16 stsp if (err) {
7685 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7686 b4996bee 2022-06-16 stsp free(id);
7687 b4996bee 2022-06-16 stsp return err;
7688 b4996bee 2022-06-16 stsp }
7689 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7690 b4996bee 2022-06-16 stsp id);
7691 b4996bee 2022-06-16 stsp if (err) {
7692 b4996bee 2022-06-16 stsp free(id);
7693 b4996bee 2022-06-16 stsp return err;
7694 b4996bee 2022-06-16 stsp }
7695 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7696 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7697 b4996bee 2022-06-16 stsp } else {
7698 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7699 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7700 b4996bee 2022-06-16 stsp }
7701 b4996bee 2022-06-16 stsp free(id);
7702 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7703 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7704 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7705 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7706 b4996bee 2022-06-16 stsp }
7707 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7708 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7709 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7710 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7711 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7712 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7713 6458efa5 2020-11-24 stsp struct got_object_id *id;
7714 6458efa5 2020-11-24 stsp char *id_str;
7715 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7716 6458efa5 2020-11-24 stsp if (err)
7717 6458efa5 2020-11-24 stsp return err;
7718 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7719 6458efa5 2020-11-24 stsp if (err) {
7720 6458efa5 2020-11-24 stsp free(id);
7721 6458efa5 2020-11-24 stsp return err;
7722 6458efa5 2020-11-24 stsp }
7723 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7724 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7725 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7726 6458efa5 2020-11-24 stsp free(id);
7727 6458efa5 2020-11-24 stsp free(id_str);
7728 6458efa5 2020-11-24 stsp return err;
7729 6458efa5 2020-11-24 stsp }
7730 6458efa5 2020-11-24 stsp free(id);
7731 6458efa5 2020-11-24 stsp free(id_str);
7732 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7733 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7734 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7735 6458efa5 2020-11-24 stsp
7736 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7737 ccda2f4d 2022-06-16 stsp 0, 0);
7738 6458efa5 2020-11-24 stsp if (err) {
7739 6458efa5 2020-11-24 stsp free(line);
7740 6458efa5 2020-11-24 stsp return err;
7741 6458efa5 2020-11-24 stsp }
7742 6458efa5 2020-11-24 stsp if (n == s->selected) {
7743 6458efa5 2020-11-24 stsp if (view->focussed)
7744 6458efa5 2020-11-24 stsp wstandout(view->window);
7745 6458efa5 2020-11-24 stsp s->selected_entry = re;
7746 6458efa5 2020-11-24 stsp }
7747 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7748 6458efa5 2020-11-24 stsp if (tc)
7749 6458efa5 2020-11-24 stsp wattr_on(view->window,
7750 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7751 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7752 6458efa5 2020-11-24 stsp if (tc)
7753 6458efa5 2020-11-24 stsp wattr_off(view->window,
7754 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7755 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7756 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7757 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7758 6458efa5 2020-11-24 stsp wstandend(view->window);
7759 6458efa5 2020-11-24 stsp free(line);
7760 6458efa5 2020-11-24 stsp free(wline);
7761 6458efa5 2020-11-24 stsp wline = NULL;
7762 6458efa5 2020-11-24 stsp n++;
7763 6458efa5 2020-11-24 stsp s->ndisplayed++;
7764 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7765 6458efa5 2020-11-24 stsp
7766 6458efa5 2020-11-24 stsp limit--;
7767 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7768 6458efa5 2020-11-24 stsp }
7769 6458efa5 2020-11-24 stsp
7770 9b058f45 2022-06-30 mark view_border(view);
7771 6458efa5 2020-11-24 stsp return err;
7772 6458efa5 2020-11-24 stsp }
7773 6458efa5 2020-11-24 stsp
7774 6458efa5 2020-11-24 stsp static const struct got_error *
7775 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7776 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7777 c42c9805 2020-11-24 stsp {
7778 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7779 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7780 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7781 c42c9805 2020-11-24 stsp
7782 c42c9805 2020-11-24 stsp *new_view = NULL;
7783 c42c9805 2020-11-24 stsp
7784 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7785 c42c9805 2020-11-24 stsp if (err) {
7786 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7787 c42c9805 2020-11-24 stsp return err;
7788 c42c9805 2020-11-24 stsp else
7789 c42c9805 2020-11-24 stsp return NULL;
7790 c42c9805 2020-11-24 stsp }
7791 c42c9805 2020-11-24 stsp
7792 c42c9805 2020-11-24 stsp
7793 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7794 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7795 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7796 c42c9805 2020-11-24 stsp goto done;
7797 c42c9805 2020-11-24 stsp }
7798 c42c9805 2020-11-24 stsp
7799 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7800 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7801 c42c9805 2020-11-24 stsp if (err)
7802 c42c9805 2020-11-24 stsp goto done;
7803 c42c9805 2020-11-24 stsp
7804 c42c9805 2020-11-24 stsp *new_view = tree_view;
7805 c42c9805 2020-11-24 stsp done:
7806 c42c9805 2020-11-24 stsp free(commit_id);
7807 c42c9805 2020-11-24 stsp return err;
7808 94b80cfa 2022-08-01 mark }
7809 94b80cfa 2022-08-01 mark
7810 94b80cfa 2022-08-01 mark static const struct got_error *
7811 94b80cfa 2022-08-01 mark ref_goto_line(struct tog_view *view, int nlines)
7812 94b80cfa 2022-08-01 mark {
7813 94b80cfa 2022-08-01 mark const struct got_error *err = NULL;
7814 94b80cfa 2022-08-01 mark struct tog_ref_view_state *s = &view->state.ref;
7815 94b80cfa 2022-08-01 mark int g, idx = s->selected_entry->idx;
7816 94b80cfa 2022-08-01 mark
7817 94b80cfa 2022-08-01 mark g = view->gline;
7818 94b80cfa 2022-08-01 mark view->gline = 0;
7819 94b80cfa 2022-08-01 mark
7820 94b80cfa 2022-08-01 mark if (g == 0)
7821 94b80cfa 2022-08-01 mark g = 1;
7822 94b80cfa 2022-08-01 mark else if (g > s->nrefs)
7823 94b80cfa 2022-08-01 mark g = s->nrefs;
7824 94b80cfa 2022-08-01 mark
7825 94b80cfa 2022-08-01 mark if (g >= s->first_displayed_entry->idx + 1 &&
7826 94b80cfa 2022-08-01 mark g <= s->last_displayed_entry->idx + 1 &&
7827 94b80cfa 2022-08-01 mark g - s->first_displayed_entry->idx - 1 < nlines) {
7828 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7829 94b80cfa 2022-08-01 mark return NULL;
7830 94b80cfa 2022-08-01 mark }
7831 94b80cfa 2022-08-01 mark
7832 94b80cfa 2022-08-01 mark if (idx + 1 < g) {
7833 94b80cfa 2022-08-01 mark err = ref_scroll_down(view, g - idx - 1);
7834 94b80cfa 2022-08-01 mark if (err)
7835 94b80cfa 2022-08-01 mark return err;
7836 94b80cfa 2022-08-01 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
7837 94b80cfa 2022-08-01 mark s->first_displayed_entry->idx + s->selected < g &&
7838 94b80cfa 2022-08-01 mark s->selected < s->ndisplayed - 1)
7839 94b80cfa 2022-08-01 mark s->selected = g - s->first_displayed_entry->idx - 1;
7840 94b80cfa 2022-08-01 mark } else if (idx + 1 > g)
7841 94b80cfa 2022-08-01 mark ref_scroll_up(s, idx - g + 1);
7842 94b80cfa 2022-08-01 mark
7843 94b80cfa 2022-08-01 mark if (g < nlines && s->first_displayed_entry->idx == 0)
7844 94b80cfa 2022-08-01 mark s->selected = g - 1;
7845 94b80cfa 2022-08-01 mark
7846 94b80cfa 2022-08-01 mark return NULL;
7847 94b80cfa 2022-08-01 mark
7848 c42c9805 2020-11-24 stsp }
7849 94b80cfa 2022-08-01 mark
7850 c42c9805 2020-11-24 stsp static const struct got_error *
7851 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7852 6458efa5 2020-11-24 stsp {
7853 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7854 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7855 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7856 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
7857 6458efa5 2020-11-24 stsp
7858 94b80cfa 2022-08-01 mark if (view->gline)
7859 94b80cfa 2022-08-01 mark return ref_goto_line(view, nscroll);
7860 94b80cfa 2022-08-01 mark
7861 6458efa5 2020-11-24 stsp switch (ch) {
7862 6458efa5 2020-11-24 stsp case 'i':
7863 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7864 640cd7ff 2022-06-22 mark view->count = 0;
7865 7f66531d 2021-11-16 stsp break;
7866 b4996bee 2022-06-16 stsp case 'm':
7867 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7868 640cd7ff 2022-06-22 mark view->count = 0;
7869 b4996bee 2022-06-16 stsp break;
7870 07a065fe 2021-11-20 stsp case 'o':
7871 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7872 640cd7ff 2022-06-22 mark view->count = 0;
7873 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7874 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7875 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7876 50617b77 2021-11-20 stsp if (err)
7877 50617b77 2021-11-20 stsp break;
7878 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7879 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7880 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7881 7f66531d 2021-11-16 stsp if (err)
7882 7f66531d 2021-11-16 stsp break;
7883 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7884 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7885 6458efa5 2020-11-24 stsp break;
7886 6458efa5 2020-11-24 stsp case KEY_ENTER:
7887 6458efa5 2020-11-24 stsp case '\r':
7888 640cd7ff 2022-06-22 mark view->count = 0;
7889 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7890 9b058f45 2022-06-30 mark break;
7891 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7892 6458efa5 2020-11-24 stsp break;
7893 5e98fb33 2022-07-22 mark case 'T':
7894 640cd7ff 2022-06-22 mark view->count = 0;
7895 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7896 c42c9805 2020-11-24 stsp break;
7897 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
7898 c42c9805 2020-11-24 stsp break;
7899 e4526bf5 2021-09-03 naddy case 'g':
7900 e4526bf5 2021-09-03 naddy case KEY_HOME:
7901 e4526bf5 2021-09-03 naddy s->selected = 0;
7902 640cd7ff 2022-06-22 mark view->count = 0;
7903 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7904 e4526bf5 2021-09-03 naddy break;
7905 e4526bf5 2021-09-03 naddy case 'G':
7906 9b058f45 2022-06-30 mark case KEY_END: {
7907 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7908 9b058f45 2022-06-30 mark
7909 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7910 9b058f45 2022-06-30 mark --eos; /* border */
7911 e4526bf5 2021-09-03 naddy s->selected = 0;
7912 640cd7ff 2022-06-22 mark view->count = 0;
7913 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7914 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7915 e4526bf5 2021-09-03 naddy if (re == NULL)
7916 e4526bf5 2021-09-03 naddy break;
7917 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7918 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7919 e4526bf5 2021-09-03 naddy }
7920 e4526bf5 2021-09-03 naddy if (n > 0)
7921 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7922 e4526bf5 2021-09-03 naddy break;
7923 9b058f45 2022-06-30 mark }
7924 6458efa5 2020-11-24 stsp case 'k':
7925 6458efa5 2020-11-24 stsp case KEY_UP:
7926 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7927 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7928 6458efa5 2020-11-24 stsp s->selected--;
7929 6458efa5 2020-11-24 stsp break;
7930 34ba6917 2020-11-30 stsp }
7931 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7932 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7933 640cd7ff 2022-06-22 mark view->count = 0;
7934 6458efa5 2020-11-24 stsp break;
7935 83cc4199 2022-06-13 stsp case CTRL('u'):
7936 33c3719a 2022-06-15 stsp case 'u':
7937 83cc4199 2022-06-13 stsp nscroll /= 2;
7938 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7939 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7940 6458efa5 2020-11-24 stsp case CTRL('b'):
7941 61417565 2022-06-20 mark case 'b':
7942 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7943 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7944 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7945 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7946 640cd7ff 2022-06-22 mark view->count = 0;
7947 6458efa5 2020-11-24 stsp break;
7948 6458efa5 2020-11-24 stsp case 'j':
7949 6458efa5 2020-11-24 stsp case KEY_DOWN:
7950 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7951 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7952 6458efa5 2020-11-24 stsp s->selected++;
7953 6458efa5 2020-11-24 stsp break;
7954 6458efa5 2020-11-24 stsp }
7955 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7956 6458efa5 2020-11-24 stsp /* can't scroll any further */
7957 640cd7ff 2022-06-22 mark view->count = 0;
7958 6458efa5 2020-11-24 stsp break;
7959 640cd7ff 2022-06-22 mark }
7960 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7961 6458efa5 2020-11-24 stsp break;
7962 83cc4199 2022-06-13 stsp case CTRL('d'):
7963 33c3719a 2022-06-15 stsp case 'd':
7964 83cc4199 2022-06-13 stsp nscroll /= 2;
7965 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7966 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7967 6458efa5 2020-11-24 stsp case CTRL('f'):
7968 61417565 2022-06-20 mark case 'f':
7969 48bb96f0 2022-06-20 naddy case ' ':
7970 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7971 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7972 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7973 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7974 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7975 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7976 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7977 640cd7ff 2022-06-22 mark view->count = 0;
7978 6458efa5 2020-11-24 stsp break;
7979 6458efa5 2020-11-24 stsp }
7980 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7981 6458efa5 2020-11-24 stsp break;
7982 6458efa5 2020-11-24 stsp case CTRL('l'):
7983 640cd7ff 2022-06-22 mark view->count = 0;
7984 8924d611 2020-12-26 stsp tog_free_refs();
7985 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7986 8924d611 2020-12-26 stsp if (err)
7987 8924d611 2020-12-26 stsp break;
7988 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7989 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7990 6458efa5 2020-11-24 stsp break;
7991 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7992 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7993 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7994 6458efa5 2020-11-24 stsp break;
7995 6458efa5 2020-11-24 stsp default:
7996 640cd7ff 2022-06-22 mark view->count = 0;
7997 6458efa5 2020-11-24 stsp break;
7998 6458efa5 2020-11-24 stsp }
7999 6458efa5 2020-11-24 stsp
8000 6458efa5 2020-11-24 stsp return err;
8001 6458efa5 2020-11-24 stsp }
8002 6458efa5 2020-11-24 stsp
8003 6458efa5 2020-11-24 stsp __dead static void
8004 6458efa5 2020-11-24 stsp usage_ref(void)
8005 6458efa5 2020-11-24 stsp {
8006 6458efa5 2020-11-24 stsp endwin();
8007 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8008 6458efa5 2020-11-24 stsp getprogname());
8009 6458efa5 2020-11-24 stsp exit(1);
8010 6458efa5 2020-11-24 stsp }
8011 6458efa5 2020-11-24 stsp
8012 6458efa5 2020-11-24 stsp static const struct got_error *
8013 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8014 6458efa5 2020-11-24 stsp {
8015 6458efa5 2020-11-24 stsp const struct got_error *error;
8016 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8017 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8018 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8019 6458efa5 2020-11-24 stsp int ch;
8020 6458efa5 2020-11-24 stsp struct tog_view *view;
8021 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8022 6458efa5 2020-11-24 stsp
8023 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8024 6458efa5 2020-11-24 stsp switch (ch) {
8025 6458efa5 2020-11-24 stsp case 'r':
8026 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8027 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8028 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8029 6458efa5 2020-11-24 stsp optarg);
8030 6458efa5 2020-11-24 stsp break;
8031 6458efa5 2020-11-24 stsp default:
8032 e99e2d15 2020-11-24 naddy usage_ref();
8033 6458efa5 2020-11-24 stsp /* NOTREACHED */
8034 6458efa5 2020-11-24 stsp }
8035 6458efa5 2020-11-24 stsp }
8036 6458efa5 2020-11-24 stsp
8037 6458efa5 2020-11-24 stsp argc -= optind;
8038 6458efa5 2020-11-24 stsp argv += optind;
8039 6458efa5 2020-11-24 stsp
8040 6458efa5 2020-11-24 stsp if (argc > 1)
8041 e99e2d15 2020-11-24 naddy usage_ref();
8042 0ae84acc 2022-06-15 tracey
8043 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8044 0ae84acc 2022-06-15 tracey if (error != NULL)
8045 0ae84acc 2022-06-15 tracey goto done;
8046 6458efa5 2020-11-24 stsp
8047 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8048 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8049 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8050 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8051 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8052 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8053 c156c7a4 2020-12-18 stsp goto done;
8054 6458efa5 2020-11-24 stsp if (worktree)
8055 6458efa5 2020-11-24 stsp repo_path =
8056 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8057 6458efa5 2020-11-24 stsp else
8058 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8059 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8060 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8061 c156c7a4 2020-12-18 stsp goto done;
8062 c156c7a4 2020-12-18 stsp }
8063 6458efa5 2020-11-24 stsp }
8064 6458efa5 2020-11-24 stsp
8065 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8066 6458efa5 2020-11-24 stsp if (error != NULL)
8067 6458efa5 2020-11-24 stsp goto done;
8068 6458efa5 2020-11-24 stsp
8069 6458efa5 2020-11-24 stsp init_curses();
8070 6458efa5 2020-11-24 stsp
8071 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8072 51a10b52 2020-12-26 stsp if (error)
8073 51a10b52 2020-12-26 stsp goto done;
8074 51a10b52 2020-12-26 stsp
8075 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8076 6458efa5 2020-11-24 stsp if (error)
8077 6458efa5 2020-11-24 stsp goto done;
8078 6458efa5 2020-11-24 stsp
8079 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8080 6458efa5 2020-11-24 stsp if (view == NULL) {
8081 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8082 6458efa5 2020-11-24 stsp goto done;
8083 6458efa5 2020-11-24 stsp }
8084 6458efa5 2020-11-24 stsp
8085 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8086 6458efa5 2020-11-24 stsp if (error)
8087 6458efa5 2020-11-24 stsp goto done;
8088 6458efa5 2020-11-24 stsp
8089 6458efa5 2020-11-24 stsp if (worktree) {
8090 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8091 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8092 6458efa5 2020-11-24 stsp worktree = NULL;
8093 6458efa5 2020-11-24 stsp }
8094 6458efa5 2020-11-24 stsp error = view_loop(view);
8095 6458efa5 2020-11-24 stsp done:
8096 6458efa5 2020-11-24 stsp free(repo_path);
8097 6458efa5 2020-11-24 stsp free(cwd);
8098 1d0f4054 2021-06-17 stsp if (repo) {
8099 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8100 1d0f4054 2021-06-17 stsp if (close_err)
8101 1d0f4054 2021-06-17 stsp error = close_err;
8102 1d0f4054 2021-06-17 stsp }
8103 0ae84acc 2022-06-15 tracey if (pack_fds) {
8104 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8105 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8106 0ae84acc 2022-06-15 tracey if (error == NULL)
8107 0ae84acc 2022-06-15 tracey error = pack_err;
8108 0ae84acc 2022-06-15 tracey }
8109 51a10b52 2020-12-26 stsp tog_free_refs();
8110 6458efa5 2020-11-24 stsp return error;
8111 6458efa5 2020-11-24 stsp }
8112 6458efa5 2020-11-24 stsp
8113 136e2bd4 2022-07-23 mark static const struct got_error *
8114 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8115 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
8116 136e2bd4 2022-07-23 mark {
8117 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
8118 136e2bd4 2022-07-23 mark
8119 136e2bd4 2022-07-23 mark *new_view = NULL;
8120 136e2bd4 2022-07-23 mark
8121 136e2bd4 2022-07-23 mark switch (request) {
8122 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
8123 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
8124 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
8125 136e2bd4 2022-07-23 mark
8126 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
8127 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
8128 136e2bd4 2022-07-23 mark view, s->repo);
8129 136e2bd4 2022-07-23 mark } else
8130 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8131 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8132 136e2bd4 2022-07-23 mark break;
8133 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
8134 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
8135 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
8136 136e2bd4 2022-07-23 mark
8137 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
8138 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
8139 136e2bd4 2022-07-23 mark s->repo);
8140 136e2bd4 2022-07-23 mark } else
8141 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8142 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8143 136e2bd4 2022-07-23 mark break;
8144 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
8145 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
8146 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
8147 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
8148 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8149 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
8150 136e2bd4 2022-07-23 mark &view->state.tree);
8151 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8152 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
8153 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8154 136e2bd4 2022-07-23 mark view->state.ref.repo);
8155 136e2bd4 2022-07-23 mark else
8156 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8157 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8158 136e2bd4 2022-07-23 mark break;
8159 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
8160 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8161 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
8162 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
8163 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
8164 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
8165 136e2bd4 2022-07-23 mark view->state.log.repo);
8166 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
8167 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
8168 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
8169 136e2bd4 2022-07-23 mark view->state.ref.repo);
8170 136e2bd4 2022-07-23 mark else
8171 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
8172 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8173 136e2bd4 2022-07-23 mark break;
8174 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
8175 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8176 136e2bd4 2022-07-23 mark if (*new_view == NULL)
8177 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
8178 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
8179 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
8180 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
8181 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
8182 136e2bd4 2022-07-23 mark else
8183 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
8184 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
8185 136e2bd4 2022-07-23 mark if (err)
8186 136e2bd4 2022-07-23 mark view_close(*new_view);
8187 136e2bd4 2022-07-23 mark break;
8188 136e2bd4 2022-07-23 mark default:
8189 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8190 136e2bd4 2022-07-23 mark }
8191 136e2bd4 2022-07-23 mark
8192 136e2bd4 2022-07-23 mark return err;
8193 136e2bd4 2022-07-23 mark }
8194 136e2bd4 2022-07-23 mark
8195 9b058f45 2022-06-30 mark /*
8196 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
8197 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
8198 9b058f45 2022-06-30 mark */
8199 6458efa5 2020-11-24 stsp static void
8200 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
8201 9b058f45 2022-06-30 mark {
8202 9b058f45 2022-06-30 mark switch (view->type) {
8203 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8204 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8205 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
8206 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
8207 9b058f45 2022-06-30 mark 1);
8208 9b058f45 2022-06-30 mark break;
8209 9b058f45 2022-06-30 mark }
8210 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
8211 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
8212 9b058f45 2022-06-30 mark else
8213 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
8214 9b058f45 2022-06-30 mark s->selected_line += view->offset;
8215 9b058f45 2022-06-30 mark break;
8216 9b058f45 2022-06-30 mark }
8217 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
8218 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
8219 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
8220 9b058f45 2022-06-30 mark break;
8221 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
8222 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
8223 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
8224 9b058f45 2022-06-30 mark break;
8225 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
8226 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
8227 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
8228 9b058f45 2022-06-30 mark break;
8229 9b058f45 2022-06-30 mark default:
8230 9b058f45 2022-06-30 mark break;
8231 9b058f45 2022-06-30 mark }
8232 9b058f45 2022-06-30 mark
8233 9b058f45 2022-06-30 mark view->offset = 0;
8234 9b058f45 2022-06-30 mark }
8235 9b058f45 2022-06-30 mark
8236 9b058f45 2022-06-30 mark /*
8237 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
8238 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
8239 9b058f45 2022-06-30 mark */
8240 9b058f45 2022-06-30 mark static const struct got_error *
8241 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
8242 9b058f45 2022-06-30 mark {
8243 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
8244 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
8245 9b058f45 2022-06-30 mark int *selected = NULL;
8246 9b058f45 2022-06-30 mark int header, offset;
8247 9b058f45 2022-06-30 mark
8248 9b058f45 2022-06-30 mark switch (view->type) {
8249 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
8250 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
8251 9b058f45 2022-06-30 mark header = 3;
8252 9b058f45 2022-06-30 mark scrolld = NULL;
8253 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
8254 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
8255 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
8256 9b058f45 2022-06-30 mark s->selected_line -= offset;
8257 9b058f45 2022-06-30 mark view->offset = offset;
8258 9b058f45 2022-06-30 mark }
8259 9b058f45 2022-06-30 mark break;
8260 9b058f45 2022-06-30 mark }
8261 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
8262 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
8263 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
8264 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
8265 9b058f45 2022-06-30 mark selected = &s->selected;
8266 9b058f45 2022-06-30 mark break;
8267 9b058f45 2022-06-30 mark }
8268 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
8269 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
8270 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
8271 9b058f45 2022-06-30 mark header = 3;
8272 9b058f45 2022-06-30 mark selected = &s->selected;
8273 9b058f45 2022-06-30 mark break;
8274 9b058f45 2022-06-30 mark }
8275 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
8276 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
8277 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
8278 9b058f45 2022-06-30 mark header = 5;
8279 9b058f45 2022-06-30 mark selected = &s->selected;
8280 9b058f45 2022-06-30 mark break;
8281 9b058f45 2022-06-30 mark }
8282 9b058f45 2022-06-30 mark default:
8283 9b058f45 2022-06-30 mark selected = NULL;
8284 9b058f45 2022-06-30 mark scrolld = NULL;
8285 9b058f45 2022-06-30 mark header = 0;
8286 9b058f45 2022-06-30 mark break;
8287 9b058f45 2022-06-30 mark }
8288 9b058f45 2022-06-30 mark
8289 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
8290 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
8291 9b058f45 2022-06-30 mark view->offset = offset;
8292 9b058f45 2022-06-30 mark if (scrolld && offset) {
8293 9b058f45 2022-06-30 mark err = scrolld(view, offset);
8294 9b058f45 2022-06-30 mark *selected -= offset;
8295 9b058f45 2022-06-30 mark }
8296 9b058f45 2022-06-30 mark }
8297 9b058f45 2022-06-30 mark
8298 9b058f45 2022-06-30 mark return err;
8299 9b058f45 2022-06-30 mark }
8300 9b058f45 2022-06-30 mark
8301 9b058f45 2022-06-30 mark static void
8302 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8303 ce5b7c56 2019-07-09 stsp {
8304 6059809a 2020-12-17 stsp size_t i;
8305 9f7d7167 2018-04-29 stsp
8306 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8307 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8308 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8309 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8310 ce5b7c56 2019-07-09 stsp }
8311 6879ba42 2020-10-01 naddy fputc('\n', fp);
8312 ce5b7c56 2019-07-09 stsp }
8313 ce5b7c56 2019-07-09 stsp
8314 4ed7e80c 2018-05-20 stsp __dead static void
8315 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8316 9f7d7167 2018-04-29 stsp {
8317 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8318 6879ba42 2020-10-01 naddy
8319 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8320 6879ba42 2020-10-01 naddy getprogname());
8321 6879ba42 2020-10-01 naddy if (hflag) {
8322 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8323 6879ba42 2020-10-01 naddy list_commands(fp);
8324 ee85c5e8 2020-02-29 stsp }
8325 6879ba42 2020-10-01 naddy exit(status);
8326 9f7d7167 2018-04-29 stsp }
8327 9f7d7167 2018-04-29 stsp
8328 c2301be8 2018-04-30 stsp static char **
8329 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8330 c2301be8 2018-04-30 stsp {
8331 ee85c5e8 2020-02-29 stsp va_list ap;
8332 c2301be8 2018-04-30 stsp char **argv;
8333 ee85c5e8 2020-02-29 stsp int i;
8334 c2301be8 2018-04-30 stsp
8335 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8336 ee85c5e8 2020-02-29 stsp
8337 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8338 c2301be8 2018-04-30 stsp if (argv == NULL)
8339 c2301be8 2018-04-30 stsp err(1, "calloc");
8340 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8341 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8342 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8343 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8344 c2301be8 2018-04-30 stsp }
8345 c2301be8 2018-04-30 stsp
8346 ee85c5e8 2020-02-29 stsp va_end(ap);
8347 c2301be8 2018-04-30 stsp return argv;
8348 ee85c5e8 2020-02-29 stsp }
8349 ee85c5e8 2020-02-29 stsp
8350 ee85c5e8 2020-02-29 stsp /*
8351 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8352 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8353 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8354 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8355 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8356 ee85c5e8 2020-02-29 stsp */
8357 ee85c5e8 2020-02-29 stsp static const struct got_error *
8358 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8359 ee85c5e8 2020-02-29 stsp {
8360 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8361 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8362 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8363 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8364 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8365 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8366 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8367 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8368 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8369 84de9106 2020-12-26 stsp
8370 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8371 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8372 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8373 ee85c5e8 2020-02-29 stsp
8374 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8375 0ae84acc 2022-06-15 tracey if (error != NULL)
8376 0ae84acc 2022-06-15 tracey goto done;
8377 0ae84acc 2022-06-15 tracey
8378 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8379 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8380 ee85c5e8 2020-02-29 stsp goto done;
8381 ee85c5e8 2020-02-29 stsp
8382 ee85c5e8 2020-02-29 stsp if (worktree)
8383 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8384 ee85c5e8 2020-02-29 stsp else
8385 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8386 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8387 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8388 ee85c5e8 2020-02-29 stsp goto done;
8389 ee85c5e8 2020-02-29 stsp }
8390 ee85c5e8 2020-02-29 stsp
8391 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8392 ee85c5e8 2020-02-29 stsp if (error != NULL)
8393 ee85c5e8 2020-02-29 stsp goto done;
8394 ee85c5e8 2020-02-29 stsp
8395 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8396 ee85c5e8 2020-02-29 stsp repo, worktree);
8397 ee85c5e8 2020-02-29 stsp if (error)
8398 ee85c5e8 2020-02-29 stsp goto done;
8399 ee85c5e8 2020-02-29 stsp
8400 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8401 84de9106 2020-12-26 stsp if (error)
8402 84de9106 2020-12-26 stsp goto done;
8403 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8404 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8405 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8406 ee85c5e8 2020-02-29 stsp if (error)
8407 ee85c5e8 2020-02-29 stsp goto done;
8408 ee85c5e8 2020-02-29 stsp
8409 ee85c5e8 2020-02-29 stsp if (worktree) {
8410 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8411 ee85c5e8 2020-02-29 stsp worktree = NULL;
8412 ee85c5e8 2020-02-29 stsp }
8413 ee85c5e8 2020-02-29 stsp
8414 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8415 a44927cc 2022-04-07 stsp if (error)
8416 a44927cc 2022-04-07 stsp goto done;
8417 a44927cc 2022-04-07 stsp
8418 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8419 ee85c5e8 2020-02-29 stsp if (error) {
8420 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8421 ee85c5e8 2020-02-29 stsp goto done;
8422 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8423 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8424 6879ba42 2020-10-01 naddy usage(1, 1);
8425 ee85c5e8 2020-02-29 stsp /* not reached */
8426 ee85c5e8 2020-02-29 stsp }
8427 ee85c5e8 2020-02-29 stsp
8428 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8429 ee85c5e8 2020-02-29 stsp if (error)
8430 ee85c5e8 2020-02-29 stsp goto done;
8431 ee85c5e8 2020-02-29 stsp
8432 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8433 ee85c5e8 2020-02-29 stsp argc = 4;
8434 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8435 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8436 ee85c5e8 2020-02-29 stsp done:
8437 1d0f4054 2021-06-17 stsp if (repo) {
8438 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8439 1d0f4054 2021-06-17 stsp if (error == NULL)
8440 1d0f4054 2021-06-17 stsp error = close_err;
8441 1d0f4054 2021-06-17 stsp }
8442 a44927cc 2022-04-07 stsp if (commit)
8443 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8444 ee85c5e8 2020-02-29 stsp if (worktree)
8445 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8446 0ae84acc 2022-06-15 tracey if (pack_fds) {
8447 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8448 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8449 0ae84acc 2022-06-15 tracey if (error == NULL)
8450 0ae84acc 2022-06-15 tracey error = pack_err;
8451 0ae84acc 2022-06-15 tracey }
8452 ee85c5e8 2020-02-29 stsp free(id);
8453 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8454 ee85c5e8 2020-02-29 stsp free(commit_id);
8455 ee85c5e8 2020-02-29 stsp free(cwd);
8456 ee85c5e8 2020-02-29 stsp free(repo_path);
8457 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8458 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8459 ee85c5e8 2020-02-29 stsp int i;
8460 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8461 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8462 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8463 ee85c5e8 2020-02-29 stsp }
8464 87670572 2020-12-26 naddy tog_free_refs();
8465 ee85c5e8 2020-02-29 stsp return error;
8466 c2301be8 2018-04-30 stsp }
8467 c2301be8 2018-04-30 stsp
8468 9f7d7167 2018-04-29 stsp int
8469 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8470 9f7d7167 2018-04-29 stsp {
8471 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8472 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8473 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8474 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8475 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8476 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8477 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8478 83cd27f8 2020-01-13 stsp };
8479 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8480 9f7d7167 2018-04-29 stsp
8481 3264c09c 2022-09-06 mark if (!isatty(STDIN_FILENO))
8482 3264c09c 2022-09-06 mark errx(1, "standard input is not a tty");
8483 3264c09c 2022-09-06 mark
8484 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8485 9f7d7167 2018-04-29 stsp
8486 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8487 9f7d7167 2018-04-29 stsp switch (ch) {
8488 9f7d7167 2018-04-29 stsp case 'h':
8489 9f7d7167 2018-04-29 stsp hflag = 1;
8490 9f7d7167 2018-04-29 stsp break;
8491 53ccebc2 2019-07-30 stsp case 'V':
8492 53ccebc2 2019-07-30 stsp Vflag = 1;
8493 53ccebc2 2019-07-30 stsp break;
8494 9f7d7167 2018-04-29 stsp default:
8495 6879ba42 2020-10-01 naddy usage(hflag, 1);
8496 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8497 9f7d7167 2018-04-29 stsp }
8498 9f7d7167 2018-04-29 stsp }
8499 9f7d7167 2018-04-29 stsp
8500 9f7d7167 2018-04-29 stsp argc -= optind;
8501 9f7d7167 2018-04-29 stsp argv += optind;
8502 9814e6a3 2020-09-27 naddy optind = 1;
8503 c2301be8 2018-04-30 stsp optreset = 1;
8504 9f7d7167 2018-04-29 stsp
8505 53ccebc2 2019-07-30 stsp if (Vflag) {
8506 53ccebc2 2019-07-30 stsp got_version_print_str();
8507 6879ba42 2020-10-01 naddy return 0;
8508 53ccebc2 2019-07-30 stsp }
8509 4010e238 2020-12-04 stsp
8510 4010e238 2020-12-04 stsp #ifndef PROFILE
8511 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8512 4010e238 2020-12-04 stsp NULL) == -1)
8513 4010e238 2020-12-04 stsp err(1, "pledge");
8514 4010e238 2020-12-04 stsp #endif
8515 53ccebc2 2019-07-30 stsp
8516 c2301be8 2018-04-30 stsp if (argc == 0) {
8517 f29d3e89 2018-06-23 stsp if (hflag)
8518 6879ba42 2020-10-01 naddy usage(hflag, 0);
8519 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8520 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8521 c2301be8 2018-04-30 stsp argc = 1;
8522 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8523 c2301be8 2018-04-30 stsp } else {
8524 6059809a 2020-12-17 stsp size_t i;
8525 9f7d7167 2018-04-29 stsp
8526 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8527 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8528 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8529 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8530 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8531 9f7d7167 2018-04-29 stsp break;
8532 9f7d7167 2018-04-29 stsp }
8533 9f7d7167 2018-04-29 stsp }
8534 ee85c5e8 2020-02-29 stsp }
8535 3642c4c6 2019-07-09 stsp
8536 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8537 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8538 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8539 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8540 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8541 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8542 917d79a7 2022-07-01 stsp }
8543 917d79a7 2022-07-01 stsp
8544 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8545 ee85c5e8 2020-02-29 stsp if (argc != 1)
8546 6879ba42 2020-10-01 naddy usage(0, 1);
8547 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8548 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8549 ee85c5e8 2020-02-29 stsp } else {
8550 ee85c5e8 2020-02-29 stsp if (hflag)
8551 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8552 ee85c5e8 2020-02-29 stsp else
8553 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8554 9f7d7167 2018-04-29 stsp }
8555 9f7d7167 2018-04-29 stsp
8556 9f7d7167 2018-04-29 stsp endwin();
8557 b46c1e04 2020-09-20 naddy putchar('\n');
8558 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8559 a2f4a359 2020-02-28 stsp int i;
8560 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8561 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8562 a2f4a359 2020-02-28 stsp free(cmd_argv);
8563 a2f4a359 2020-02-28 stsp }
8564 a2f4a359 2020-02-28 stsp
8565 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8566 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8567 9f7d7167 2018-04-29 stsp return 0;
8568 9f7d7167 2018-04-29 stsp }