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 cc488aa7 2022-01-23 stsp
140 cc488aa7 2022-01-23 stsp static const struct got_error *
141 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 cc488aa7 2022-01-23 stsp struct got_reference* re2)
143 cc488aa7 2022-01-23 stsp {
144 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
145 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
146 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
147 cc488aa7 2022-01-23 stsp
148 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
149 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
152 cc488aa7 2022-01-23 stsp *cmp = -1;
153 cc488aa7 2022-01-23 stsp return NULL;
154 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
155 cc488aa7 2022-01-23 stsp *cmp = 1;
156 cc488aa7 2022-01-23 stsp return NULL;
157 cc488aa7 2022-01-23 stsp }
158 cc488aa7 2022-01-23 stsp
159 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
160 cc488aa7 2022-01-23 stsp return NULL;
161 cc488aa7 2022-01-23 stsp }
162 51a10b52 2020-12-26 stsp
163 11b20872 2019-11-08 stsp static const struct got_error *
164 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
165 51a10b52 2020-12-26 stsp {
166 51a10b52 2020-12-26 stsp const struct got_error *err;
167 51a10b52 2020-12-26 stsp
168 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
170 7f66531d 2021-11-16 stsp repo);
171 51a10b52 2020-12-26 stsp if (err)
172 51a10b52 2020-12-26 stsp return err;
173 51a10b52 2020-12-26 stsp
174 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
175 51a10b52 2020-12-26 stsp repo);
176 51a10b52 2020-12-26 stsp }
177 51a10b52 2020-12-26 stsp
178 51a10b52 2020-12-26 stsp static void
179 51a10b52 2020-12-26 stsp tog_free_refs(void)
180 51a10b52 2020-12-26 stsp {
181 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
182 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
183 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
184 51a10b52 2020-12-26 stsp }
185 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
186 51a10b52 2020-12-26 stsp }
187 51a10b52 2020-12-26 stsp
188 51a10b52 2020-12-26 stsp static const struct got_error *
189 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
190 11b20872 2019-11-08 stsp int idx, short color)
191 11b20872 2019-11-08 stsp {
192 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
193 11b20872 2019-11-08 stsp struct tog_color *tc;
194 11b20872 2019-11-08 stsp int regerr = 0;
195 11b20872 2019-11-08 stsp
196 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
197 11b20872 2019-11-08 stsp return NULL;
198 11b20872 2019-11-08 stsp
199 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
200 11b20872 2019-11-08 stsp
201 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
202 11b20872 2019-11-08 stsp if (tc == NULL)
203 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
204 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
205 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 11b20872 2019-11-08 stsp if (regerr) {
207 11b20872 2019-11-08 stsp static char regerr_msg[512];
208 11b20872 2019-11-08 stsp static char err_msg[512];
209 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
210 11b20872 2019-11-08 stsp sizeof(regerr_msg));
211 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 11b20872 2019-11-08 stsp regerr_msg);
213 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
214 11b20872 2019-11-08 stsp free(tc);
215 11b20872 2019-11-08 stsp return err;
216 11b20872 2019-11-08 stsp }
217 11b20872 2019-11-08 stsp tc->colorpair = idx;
218 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
219 11b20872 2019-11-08 stsp return NULL;
220 11b20872 2019-11-08 stsp }
221 11b20872 2019-11-08 stsp
222 11b20872 2019-11-08 stsp static void
223 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
224 11b20872 2019-11-08 stsp {
225 11b20872 2019-11-08 stsp struct tog_color *tc;
226 11b20872 2019-11-08 stsp
227 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
228 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
229 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
230 11b20872 2019-11-08 stsp regfree(&tc->regex);
231 11b20872 2019-11-08 stsp free(tc);
232 11b20872 2019-11-08 stsp }
233 11b20872 2019-11-08 stsp }
234 11b20872 2019-11-08 stsp
235 336075a4 2022-06-25 op static struct tog_color *
236 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
237 11b20872 2019-11-08 stsp {
238 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
239 11b20872 2019-11-08 stsp
240 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
241 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
242 11b20872 2019-11-08 stsp return tc;
243 11b20872 2019-11-08 stsp }
244 11b20872 2019-11-08 stsp
245 11b20872 2019-11-08 stsp return NULL;
246 11b20872 2019-11-08 stsp }
247 11b20872 2019-11-08 stsp
248 11b20872 2019-11-08 stsp static int
249 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
250 11b20872 2019-11-08 stsp {
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 11b20872 2019-11-08 stsp return COLOR_CYAN;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 11b20872 2019-11-08 stsp return COLOR_YELLOW;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 91b8c405 2020-01-25 stsp return COLOR_CYAN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 11b20872 2019-11-08 stsp return COLOR_GREEN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_YELLOW;
273 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 6458efa5 2020-11-24 stsp return COLOR_GREEN;
275 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
277 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
279 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
280 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
281 11b20872 2019-11-08 stsp
282 11b20872 2019-11-08 stsp return -1;
283 11b20872 2019-11-08 stsp }
284 11b20872 2019-11-08 stsp
285 11b20872 2019-11-08 stsp static int
286 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
287 11b20872 2019-11-08 stsp {
288 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
289 11b20872 2019-11-08 stsp
290 11b20872 2019-11-08 stsp if (val == NULL)
291 11b20872 2019-11-08 stsp return default_color_value(envvar);
292 15a087fe 2019-02-21 stsp
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLACK;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
296 11b20872 2019-11-08 stsp return COLOR_RED;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
298 11b20872 2019-11-08 stsp return COLOR_GREEN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
300 11b20872 2019-11-08 stsp return COLOR_YELLOW;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLUE;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
304 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
306 11b20872 2019-11-08 stsp return COLOR_CYAN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
308 11b20872 2019-11-08 stsp return COLOR_WHITE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
310 11b20872 2019-11-08 stsp return -1;
311 11b20872 2019-11-08 stsp
312 11b20872 2019-11-08 stsp return default_color_value(envvar);
313 11b20872 2019-11-08 stsp }
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 15a087fe 2019-02-21 stsp int first_displayed_line;
322 15a087fe 2019-02-21 stsp int last_displayed_line;
323 15a087fe 2019-02-21 stsp int eof;
324 15a087fe 2019-02-21 stsp int diff_context;
325 3dbaef42 2020-11-24 stsp int ignore_whitespace;
326 64453f7e 2020-11-21 stsp int force_text_diff;
327 15a087fe 2019-02-21 stsp struct got_repository *repo;
328 bddb1296 2019-11-08 stsp struct tog_colors colors;
329 fe621944 2020-11-10 stsp size_t nlines;
330 f44b1f58 2020-02-02 tracey off_t *line_offsets;
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 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
335 fb872ab2 2019-02-21 stsp struct tog_view *log_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 1a76625f 2018-10-22 stsp
340 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
341 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
342 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
343 1a76625f 2018-10-22 stsp int commits_needed;
344 fb280deb 2021-08-30 stsp int load_all;
345 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
346 1a76625f 2018-10-22 stsp struct commit_queue *commits;
347 1a76625f 2018-10-22 stsp const char *in_repo_path;
348 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
349 1a76625f 2018-10-22 stsp struct got_repository *repo;
350 74467cc8 2022-06-15 stsp int *pack_fds;
351 1a76625f 2018-10-22 stsp int log_complete;
352 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
353 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
355 13add988 2019-10-15 stsp int *searching;
356 13add988 2019-10-15 stsp int *search_next_done;
357 13add988 2019-10-15 stsp regex_t *regex;
358 1a76625f 2018-10-22 stsp };
359 1a76625f 2018-10-22 stsp
360 1a76625f 2018-10-22 stsp struct tog_log_view_state {
361 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
365 b01e7d3b 2018-08-04 stsp int selected;
366 b01e7d3b 2018-08-04 stsp char *in_repo_path;
367 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
368 b672a97a 2020-01-27 stsp int log_branches;
369 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
370 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
371 1a76625f 2018-10-22 stsp sig_atomic_t quit;
372 1a76625f 2018-10-22 stsp pthread_t thread;
373 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
374 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
375 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
376 11b20872 2019-11-08 stsp struct tog_colors colors;
377 ba4f502b 2018-08-04 stsp };
378 11b20872 2019-11-08 stsp
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
387 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
388 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
389 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
393 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
394 ba4f502b 2018-08-04 stsp
395 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
396 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
397 e9424729 2018-08-04 stsp int nlines;
398 e9424729 2018-08-04 stsp
399 e9424729 2018-08-04 stsp struct tog_view *view;
400 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
401 e9424729 2018-08-04 stsp int *quit;
402 e9424729 2018-08-04 stsp };
403 e9424729 2018-08-04 stsp
404 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
405 e9424729 2018-08-04 stsp const char *path;
406 e9424729 2018-08-04 stsp struct got_repository *repo;
407 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
408 e9424729 2018-08-04 stsp int *complete;
409 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
410 fc06ba56 2019-08-22 stsp void *cancel_arg;
411 e9424729 2018-08-04 stsp };
412 e9424729 2018-08-04 stsp
413 e9424729 2018-08-04 stsp struct tog_blame {
414 e9424729 2018-08-04 stsp FILE *f;
415 be659d10 2020-11-18 stsp off_t filesize;
416 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
417 6fcac457 2018-11-19 stsp int nlines;
418 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
419 e9424729 2018-08-04 stsp pthread_t thread;
420 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
421 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
422 e9424729 2018-08-04 stsp const char *path;
423 0ae84acc 2022-06-15 tracey int *pack_fds;
424 e9424729 2018-08-04 stsp };
425 e9424729 2018-08-04 stsp
426 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
427 7cbe629d 2018-08-04 stsp int first_displayed_line;
428 7cbe629d 2018-08-04 stsp int last_displayed_line;
429 7cbe629d 2018-08-04 stsp int selected_line;
430 7cbe629d 2018-08-04 stsp int blame_complete;
431 e5a0f69f 2018-08-18 stsp int eof;
432 e5a0f69f 2018-08-18 stsp int done;
433 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
434 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
435 e5a0f69f 2018-08-18 stsp char *path;
436 7cbe629d 2018-08-04 stsp struct got_repository *repo;
437 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
438 e9424729 2018-08-04 stsp struct tog_blame blame;
439 6c4c42e0 2019-06-24 stsp int matched_line;
440 11b20872 2019-11-08 stsp struct tog_colors colors;
441 ad80ab7b 2018-08-04 stsp };
442 ad80ab7b 2018-08-04 stsp
443 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
444 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
445 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
446 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
448 ad80ab7b 2018-08-04 stsp int selected;
449 ad80ab7b 2018-08-04 stsp };
450 ad80ab7b 2018-08-04 stsp
451 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
452 ad80ab7b 2018-08-04 stsp
453 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
454 ad80ab7b 2018-08-04 stsp char *tree_label;
455 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
456 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
458 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
461 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
462 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
463 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
464 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
465 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
466 6458efa5 2020-11-24 stsp struct tog_colors colors;
467 6458efa5 2020-11-24 stsp };
468 6458efa5 2020-11-24 stsp
469 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
470 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
471 6458efa5 2020-11-24 stsp struct got_reference *ref;
472 6458efa5 2020-11-24 stsp int idx;
473 6458efa5 2020-11-24 stsp };
474 6458efa5 2020-11-24 stsp
475 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
476 6458efa5 2020-11-24 stsp
477 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
478 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
479 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
482 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
483 6458efa5 2020-11-24 stsp struct got_repository *repo;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
485 bddb1296 2019-11-08 stsp struct tog_colors colors;
486 7cbe629d 2018-08-04 stsp };
487 7cbe629d 2018-08-04 stsp
488 669b5ffa 2018-10-07 stsp /*
489 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
490 669b5ffa 2018-10-07 stsp *
491 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
492 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
493 669b5ffa 2018-10-07 stsp * there is enough screen estate.
494 669b5ffa 2018-10-07 stsp *
495 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
496 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
497 669b5ffa 2018-10-07 stsp *
498 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
499 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
500 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
501 669b5ffa 2018-10-07 stsp *
502 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
503 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
504 669b5ffa 2018-10-07 stsp */
505 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
506 669b5ffa 2018-10-07 stsp
507 cc3c9aac 2018-08-01 stsp struct tog_view {
508 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
509 26ed57b2 2018-05-19 stsp WINDOW *window;
510 26ed57b2 2018-05-19 stsp PANEL *panel;
511 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
512 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
513 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
514 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
515 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
516 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
517 9970f7fc 2020-12-03 stsp int dying;
518 669b5ffa 2018-10-07 stsp struct tog_view *parent;
519 669b5ffa 2018-10-07 stsp struct tog_view *child;
520 5dc9f4bc 2018-08-04 stsp
521 e78dc838 2020-12-04 stsp /*
522 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
523 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
524 e78dc838 2020-12-04 stsp * between parent and child.
525 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
526 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
527 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
528 e78dc838 2020-12-04 stsp * situations.
529 e78dc838 2020-12-04 stsp */
530 e78dc838 2020-12-04 stsp int focus_child;
531 e78dc838 2020-12-04 stsp
532 9b058f45 2022-06-30 mark enum tog_view_mode mode;
533 5dc9f4bc 2018-08-04 stsp /* type-specific state */
534 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
535 5dc9f4bc 2018-08-04 stsp union {
536 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
537 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
538 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
539 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
540 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
541 5dc9f4bc 2018-08-04 stsp } state;
542 e5a0f69f 2018-08-18 stsp
543 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
545 e78dc838 2020-12-04 stsp struct tog_view *, int);
546 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
547 60493ae3 2019-06-20 stsp
548 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
549 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
550 c0c4acc8 2021-01-24 stsp int search_started;
551 60493ae3 2019-06-20 stsp int searching;
552 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
553 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
554 60493ae3 2019-06-20 stsp int search_next_done;
555 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
556 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
557 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
558 1803e47f 2019-06-22 stsp regex_t regex;
559 41605754 2020-11-12 stsp regmatch_t regmatch;
560 cc3c9aac 2018-08-01 stsp };
561 cd0acaa7 2018-05-20 stsp
562 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
563 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
564 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
565 78756c87 2020-11-24 stsp struct got_repository *);
566 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
567 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
568 e78dc838 2020-12-04 stsp struct tog_view *, int);
569 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
570 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
571 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp
573 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
575 78756c87 2020-11-24 stsp const char *, const char *, int);
576 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
580 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
581 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp
583 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
585 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
586 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
587 e78dc838 2020-12-04 stsp struct tog_view *, int);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
589 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
590 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
591 e5a0f69f 2018-08-18 stsp
592 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
593 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
594 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
596 e78dc838 2020-12-04 stsp struct tog_view *, int);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
598 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
599 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp
601 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
602 6458efa5 2020-11-24 stsp struct got_repository *);
603 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
604 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
605 e78dc838 2020-12-04 stsp struct tog_view *, int);
606 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
607 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
609 25791caa 2018-10-24 stsp
610 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
611 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
612 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
613 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
614 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
615 25791caa 2018-10-24 stsp
616 25791caa 2018-10-24 stsp static void
617 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
618 25791caa 2018-10-24 stsp {
619 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
620 83baff54 2019-08-12 stsp }
621 83baff54 2019-08-12 stsp
622 83baff54 2019-08-12 stsp static void
623 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
624 83baff54 2019-08-12 stsp {
625 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
626 25791caa 2018-10-24 stsp }
627 26ed57b2 2018-05-19 stsp
628 61266923 2020-01-14 stsp static void
629 61266923 2020-01-14 stsp tog_sigcont(int signo)
630 61266923 2020-01-14 stsp {
631 61266923 2020-01-14 stsp tog_sigcont_received = 1;
632 61266923 2020-01-14 stsp }
633 61266923 2020-01-14 stsp
634 2497f032 2022-05-31 stsp static void
635 2497f032 2022-05-31 stsp tog_sigint(int signo)
636 2497f032 2022-05-31 stsp {
637 2497f032 2022-05-31 stsp tog_sigint_received = 1;
638 2497f032 2022-05-31 stsp }
639 2497f032 2022-05-31 stsp
640 2497f032 2022-05-31 stsp static void
641 2497f032 2022-05-31 stsp tog_sigterm(int signo)
642 2497f032 2022-05-31 stsp {
643 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
644 2497f032 2022-05-31 stsp }
645 2497f032 2022-05-31 stsp
646 2497f032 2022-05-31 stsp static int
647 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
648 2497f032 2022-05-31 stsp {
649 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
650 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
651 2497f032 2022-05-31 stsp }
652 2497f032 2022-05-31 stsp
653 2497f032 2022-05-31 stsp
654 e5a0f69f 2018-08-18 stsp static const struct got_error *
655 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
656 ea5e7bb5 2018-08-01 stsp {
657 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
658 e5a0f69f 2018-08-18 stsp
659 669b5ffa 2018-10-07 stsp if (view->child) {
660 669b5ffa 2018-10-07 stsp view_close(view->child);
661 669b5ffa 2018-10-07 stsp view->child = NULL;
662 669b5ffa 2018-10-07 stsp }
663 e5a0f69f 2018-08-18 stsp if (view->close)
664 e5a0f69f 2018-08-18 stsp err = view->close(view);
665 ea5e7bb5 2018-08-01 stsp if (view->panel)
666 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
667 ea5e7bb5 2018-08-01 stsp if (view->window)
668 ea5e7bb5 2018-08-01 stsp delwin(view->window);
669 ea5e7bb5 2018-08-01 stsp free(view);
670 e5a0f69f 2018-08-18 stsp return err;
671 ea5e7bb5 2018-08-01 stsp }
672 ea5e7bb5 2018-08-01 stsp
673 ea5e7bb5 2018-08-01 stsp static struct tog_view *
674 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
675 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
676 ea5e7bb5 2018-08-01 stsp {
677 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
678 ea5e7bb5 2018-08-01 stsp
679 ea5e7bb5 2018-08-01 stsp if (view == NULL)
680 ea5e7bb5 2018-08-01 stsp return NULL;
681 ea5e7bb5 2018-08-01 stsp
682 d6b05b5b 2018-08-04 stsp view->type = type;
683 f7d12f7e 2018-08-01 stsp view->lines = LINES;
684 f7d12f7e 2018-08-01 stsp view->cols = COLS;
685 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
686 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
687 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
688 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
689 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
690 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
691 96a765a8 2018-08-04 stsp view_close(view);
692 ea5e7bb5 2018-08-01 stsp return NULL;
693 ea5e7bb5 2018-08-01 stsp }
694 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
695 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
696 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
697 96a765a8 2018-08-04 stsp view_close(view);
698 ea5e7bb5 2018-08-01 stsp return NULL;
699 ea5e7bb5 2018-08-01 stsp }
700 ea5e7bb5 2018-08-01 stsp
701 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
702 ea5e7bb5 2018-08-01 stsp return view;
703 cdf1ee82 2018-08-01 stsp }
704 cdf1ee82 2018-08-01 stsp
705 0cf4efb1 2018-09-29 stsp static int
706 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
707 0cf4efb1 2018-09-29 stsp {
708 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
709 0cf4efb1 2018-09-29 stsp return 0;
710 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
711 5c60c32a 2018-10-18 stsp }
712 5c60c32a 2018-10-18 stsp
713 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
714 9b058f45 2022-06-30 mark static int
715 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
716 9b058f45 2022-06-30 mark {
717 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
718 9b058f45 2022-06-30 mark }
719 9b058f45 2022-06-30 mark
720 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
721 5c60c32a 2018-10-18 stsp
722 5c60c32a 2018-10-18 stsp static const struct got_error *
723 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
724 5c60c32a 2018-10-18 stsp {
725 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
726 5c60c32a 2018-10-18 stsp
727 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
728 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
729 9b058f45 2022-06-30 mark view->begin_x = 0;
730 9b058f45 2022-06-30 mark } else {
731 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
732 9b058f45 2022-06-30 mark view->begin_y = 0;
733 9b058f45 2022-06-30 mark }
734 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
735 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
736 5c60c32a 2018-10-18 stsp view->lines = LINES;
737 5c60c32a 2018-10-18 stsp view->cols = COLS;
738 5c60c32a 2018-10-18 stsp err = view_resize(view);
739 5c60c32a 2018-10-18 stsp if (err)
740 5c60c32a 2018-10-18 stsp return err;
741 5c60c32a 2018-10-18 stsp
742 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
743 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
744 9b058f45 2022-06-30 mark
745 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
746 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
747 5c60c32a 2018-10-18 stsp
748 5c60c32a 2018-10-18 stsp return NULL;
749 5c60c32a 2018-10-18 stsp }
750 5c60c32a 2018-10-18 stsp
751 5c60c32a 2018-10-18 stsp static const struct got_error *
752 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
753 5c60c32a 2018-10-18 stsp {
754 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
755 5c60c32a 2018-10-18 stsp
756 5c60c32a 2018-10-18 stsp view->begin_x = 0;
757 5c60c32a 2018-10-18 stsp view->begin_y = 0;
758 5c60c32a 2018-10-18 stsp view->nlines = LINES;
759 5c60c32a 2018-10-18 stsp view->ncols = COLS;
760 5c60c32a 2018-10-18 stsp view->lines = LINES;
761 5c60c32a 2018-10-18 stsp view->cols = COLS;
762 5c60c32a 2018-10-18 stsp err = view_resize(view);
763 5c60c32a 2018-10-18 stsp if (err)
764 5c60c32a 2018-10-18 stsp return err;
765 5c60c32a 2018-10-18 stsp
766 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
767 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp return NULL;
770 0cf4efb1 2018-09-29 stsp }
771 0cf4efb1 2018-09-29 stsp
772 5c60c32a 2018-10-18 stsp static int
773 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
774 5c60c32a 2018-10-18 stsp {
775 5c60c32a 2018-10-18 stsp return view->parent == NULL;
776 5c60c32a 2018-10-18 stsp }
777 5c60c32a 2018-10-18 stsp
778 6131ff18 2022-06-20 mark static int
779 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
780 6131ff18 2022-06-20 mark {
781 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
782 6131ff18 2022-06-20 mark }
783 6131ff18 2022-06-20 mark
784 9b058f45 2022-06-30 mark static void
785 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
786 9b058f45 2022-06-30 mark {
787 9b058f45 2022-06-30 mark PANEL *panel;
788 9b058f45 2022-06-30 mark const struct tog_view *view_above;
789 6131ff18 2022-06-20 mark
790 9b058f45 2022-06-30 mark if (view->parent)
791 9b058f45 2022-06-30 mark return view_border(view->parent);
792 9b058f45 2022-06-30 mark
793 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
794 9b058f45 2022-06-30 mark if (panel == NULL)
795 9b058f45 2022-06-30 mark return;
796 9b058f45 2022-06-30 mark
797 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
798 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
799 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
800 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
801 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
802 9b058f45 2022-06-30 mark else
803 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
804 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
805 9b058f45 2022-06-30 mark }
806 9b058f45 2022-06-30 mark
807 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
808 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
809 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
810 9b058f45 2022-06-30 mark
811 4d8c2215 2018-08-19 stsp static const struct got_error *
812 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
813 f7d12f7e 2018-08-01 stsp {
814 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
815 9b058f45 2022-06-30 mark int dif, nlines, ncols;
816 f7d12f7e 2018-08-01 stsp
817 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
818 9b058f45 2022-06-30 mark
819 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
820 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
821 0cf4efb1 2018-09-29 stsp else
822 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
823 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
824 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
825 0cf4efb1 2018-09-29 stsp else
826 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
827 6d0fee91 2018-08-01 stsp
828 4dd27a72 2022-06-29 stsp if (view->child) {
829 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
830 9b058f45 2022-06-30 mark
831 c71ed39a 2022-06-29 stsp if (view->child->focussed)
832 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
833 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
834 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
835 0dbbbe90 2022-06-17 op ncols = COLS;
836 0dbbbe90 2022-06-17 op
837 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
838 5c60c32a 2018-10-18 stsp if (view->child->focussed)
839 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
840 5c60c32a 2018-10-18 stsp else
841 5c60c32a 2018-10-18 stsp show_panel(view->panel);
842 5c60c32a 2018-10-18 stsp } else {
843 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
844 0dbbbe90 2022-06-17 op
845 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
846 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
847 5c60c32a 2018-10-18 stsp }
848 9b058f45 2022-06-30 mark /*
849 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
850 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
851 9b058f45 2022-06-30 mark */
852 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
853 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
854 9b058f45 2022-06-30 mark
855 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
856 9b058f45 2022-06-30 mark view->lines - ts->selected) {
857 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
858 9b058f45 2022-06-30 mark view->lines - ts->selected -
859 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
860 9b058f45 2022-06-30 mark err = request_log_commits(view);
861 9b058f45 2022-06-30 mark if (err)
862 9b058f45 2022-06-30 mark return err;
863 9b058f45 2022-06-30 mark }
864 9b058f45 2022-06-30 mark }
865 9b058f45 2022-06-30 mark
866 9b058f45 2022-06-30 mark /*
867 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
868 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
869 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
870 9b058f45 2022-06-30 mark */
871 9b058f45 2022-06-30 mark if (hs) {
872 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
873 9b058f45 2022-06-30 mark if (err)
874 9b058f45 2022-06-30 mark return err;
875 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
876 9b058f45 2022-06-30 mark err = offset_selection_down(view);
877 9b058f45 2022-06-30 mark if (err)
878 9b058f45 2022-06-30 mark return err;
879 9b058f45 2022-06-30 mark }
880 9b058f45 2022-06-30 mark view_border(view);
881 9b058f45 2022-06-30 mark update_panels();
882 9b058f45 2022-06-30 mark doupdate();
883 9b058f45 2022-06-30 mark show_panel(view->child->panel);
884 9b058f45 2022-06-30 mark nlines = view->nlines;
885 9b058f45 2022-06-30 mark }
886 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
887 0dbbbe90 2022-06-17 op ncols = COLS;
888 669b5ffa 2018-10-07 stsp
889 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
890 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
891 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
892 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
893 0dbbbe90 2022-06-17 op wclear(view->window);
894 0dbbbe90 2022-06-17 op
895 0dbbbe90 2022-06-17 op view->nlines = nlines;
896 0dbbbe90 2022-06-17 op view->ncols = ncols;
897 0dbbbe90 2022-06-17 op view->lines = LINES;
898 0dbbbe90 2022-06-17 op view->cols = COLS;
899 0dbbbe90 2022-06-17 op
900 5c60c32a 2018-10-18 stsp return NULL;
901 669b5ffa 2018-10-07 stsp }
902 669b5ffa 2018-10-07 stsp
903 669b5ffa 2018-10-07 stsp static const struct got_error *
904 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
905 669b5ffa 2018-10-07 stsp {
906 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
907 669b5ffa 2018-10-07 stsp
908 669b5ffa 2018-10-07 stsp if (view->child == NULL)
909 669b5ffa 2018-10-07 stsp return NULL;
910 669b5ffa 2018-10-07 stsp
911 669b5ffa 2018-10-07 stsp err = view_close(view->child);
912 669b5ffa 2018-10-07 stsp view->child = NULL;
913 669b5ffa 2018-10-07 stsp return err;
914 669b5ffa 2018-10-07 stsp }
915 669b5ffa 2018-10-07 stsp
916 0dbbbe90 2022-06-17 op static const struct got_error *
917 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
918 669b5ffa 2018-10-07 stsp {
919 669b5ffa 2018-10-07 stsp view->child = child;
920 669b5ffa 2018-10-07 stsp child->parent = view;
921 0dbbbe90 2022-06-17 op
922 0dbbbe90 2022-06-17 op return view_resize(view);
923 bfddd0d9 2018-09-29 stsp }
924 bfddd0d9 2018-09-29 stsp
925 34bc9ec9 2019-02-22 stsp static void
926 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
927 25791caa 2018-10-24 stsp {
928 25791caa 2018-10-24 stsp int cols, lines;
929 25791caa 2018-10-24 stsp struct winsize size;
930 25791caa 2018-10-24 stsp
931 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
932 25791caa 2018-10-24 stsp cols = 80; /* Default */
933 25791caa 2018-10-24 stsp lines = 24;
934 25791caa 2018-10-24 stsp } else {
935 25791caa 2018-10-24 stsp cols = size.ws_col;
936 25791caa 2018-10-24 stsp lines = size.ws_row;
937 25791caa 2018-10-24 stsp }
938 25791caa 2018-10-24 stsp resize_term(lines, cols);
939 2b49a8ae 2019-06-22 stsp }
940 2b49a8ae 2019-06-22 stsp
941 2b49a8ae 2019-06-22 stsp static const struct got_error *
942 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
943 2b49a8ae 2019-06-22 stsp {
944 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
945 9b058f45 2022-06-30 mark struct tog_view *v = view;
946 2b49a8ae 2019-06-22 stsp char pattern[1024];
947 2b49a8ae 2019-06-22 stsp int ret;
948 c0c4acc8 2021-01-24 stsp
949 c0c4acc8 2021-01-24 stsp if (view->search_started) {
950 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
951 c0c4acc8 2021-01-24 stsp view->searching = 0;
952 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
953 c0c4acc8 2021-01-24 stsp }
954 c0c4acc8 2021-01-24 stsp view->search_started = 0;
955 2b49a8ae 2019-06-22 stsp
956 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
957 2b49a8ae 2019-06-22 stsp return NULL;
958 2b49a8ae 2019-06-22 stsp
959 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
960 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
961 9b058f45 2022-06-30 mark v = view->child;
962 2b49a8ae 2019-06-22 stsp
963 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
964 9b058f45 2022-06-30 mark wclrtoeol(v->window);
965 9b058f45 2022-06-30 mark
966 2b49a8ae 2019-06-22 stsp nocbreak();
967 2b49a8ae 2019-06-22 stsp echo();
968 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
969 9b058f45 2022-06-30 mark wrefresh(v->window);
970 2b49a8ae 2019-06-22 stsp cbreak();
971 2b49a8ae 2019-06-22 stsp noecho();
972 2b49a8ae 2019-06-22 stsp if (ret == ERR)
973 2b49a8ae 2019-06-22 stsp return NULL;
974 2b49a8ae 2019-06-22 stsp
975 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
976 7c32bd05 2019-06-22 stsp err = view->search_start(view);
977 7c32bd05 2019-06-22 stsp if (err) {
978 7c32bd05 2019-06-22 stsp regfree(&view->regex);
979 7c32bd05 2019-06-22 stsp return err;
980 7c32bd05 2019-06-22 stsp }
981 c0c4acc8 2021-01-24 stsp view->search_started = 1;
982 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
983 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
984 2b49a8ae 2019-06-22 stsp view->search_next(view);
985 2b49a8ae 2019-06-22 stsp }
986 2b49a8ae 2019-06-22 stsp
987 2b49a8ae 2019-06-22 stsp return NULL;
988 0cf4efb1 2018-09-29 stsp }
989 6d0fee91 2018-08-01 stsp
990 640cd7ff 2022-06-22 mark /*
991 640cd7ff 2022-06-22 mark * Compute view->count from numeric user input. User has five-tenths of a
992 640cd7ff 2022-06-22 mark * second to follow each numeric keypress with another number to form count.
993 640cd7ff 2022-06-22 mark * Return first non-numeric input or ERR and assign total to view->count.
994 640cd7ff 2022-06-22 mark * XXX Should we add support for user-defined timeout?
995 640cd7ff 2022-06-22 mark */
996 640cd7ff 2022-06-22 mark static int
997 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
998 640cd7ff 2022-06-22 mark {
999 9b058f45 2022-06-30 mark struct tog_view *v = view;
1000 9b058f45 2022-06-30 mark int x, n = 0;
1001 640cd7ff 2022-06-22 mark
1002 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1003 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1004 9b058f45 2022-06-30 mark v = view->child;
1005 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1006 9b058f45 2022-06-30 mark v = view->parent;
1007 9b058f45 2022-06-30 mark
1008 640cd7ff 2022-06-22 mark view->count = 0;
1009 640cd7ff 2022-06-22 mark halfdelay(5); /* block for half a second */
1010 9b058f45 2022-06-30 mark wattron(v->window, A_BOLD);
1011 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1012 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1013 9b058f45 2022-06-30 mark waddch(v->window, ':');
1014 640cd7ff 2022-06-22 mark
1015 640cd7ff 2022-06-22 mark do {
1016 9b058f45 2022-06-30 mark x = getcurx(v->window);
1017 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1018 9b058f45 2022-06-30 mark waddch(v->window, c);
1019 9b058f45 2022-06-30 mark wrefresh(v->window);
1020 9b058f45 2022-06-30 mark }
1021 9b058f45 2022-06-30 mark
1022 640cd7ff 2022-06-22 mark /*
1023 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1024 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1025 640cd7ff 2022-06-22 mark */
1026 640cd7ff 2022-06-22 mark if (n >= 9999999)
1027 640cd7ff 2022-06-22 mark n = 9999999;
1028 640cd7ff 2022-06-22 mark else
1029 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1030 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1031 640cd7ff 2022-06-22 mark
1032 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1033 640cd7ff 2022-06-22 mark view->count = n;
1034 640cd7ff 2022-06-22 mark
1035 9b058f45 2022-06-30 mark wattroff(v->window, A_BOLD);
1036 640cd7ff 2022-06-22 mark cbreak(); /* return to blocking */
1037 640cd7ff 2022-06-22 mark return c;
1038 640cd7ff 2022-06-22 mark }
1039 640cd7ff 2022-06-22 mark
1040 0cf4efb1 2018-09-29 stsp static const struct got_error *
1041 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1042 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1043 e5a0f69f 2018-08-18 stsp {
1044 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1045 669b5ffa 2018-10-07 stsp struct tog_view *v;
1046 1a76625f 2018-10-22 stsp int ch, errcode;
1047 e5a0f69f 2018-08-18 stsp
1048 e5a0f69f 2018-08-18 stsp *new = NULL;
1049 8f4ed634 2020-03-26 stsp
1050 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1051 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1052 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1053 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1054 640cd7ff 2022-06-22 mark view->count = 0;
1055 640cd7ff 2022-06-22 mark }
1056 e5a0f69f 2018-08-18 stsp
1057 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1058 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1059 82954512 2020-02-03 stsp if (errcode)
1060 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1061 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1062 3da8ef85 2021-09-21 stsp sched_yield();
1063 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1064 82954512 2020-02-03 stsp if (errcode)
1065 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1066 82954512 2020-02-03 stsp "pthread_mutex_lock");
1067 60493ae3 2019-06-20 stsp view->search_next(view);
1068 60493ae3 2019-06-20 stsp return NULL;
1069 60493ae3 2019-06-20 stsp }
1070 60493ae3 2019-06-20 stsp
1071 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1072 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1073 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1074 1a76625f 2018-10-22 stsp if (errcode)
1075 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1076 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1077 640cd7ff 2022-06-22 mark ch = view->ch;
1078 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1079 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1080 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1081 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1082 640cd7ff 2022-06-22 mark }
1083 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1084 1a76625f 2018-10-22 stsp if (errcode)
1085 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1086 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1087 25791caa 2018-10-24 stsp
1088 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1089 25791caa 2018-10-24 stsp tog_resizeterm();
1090 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1091 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1092 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1093 25791caa 2018-10-24 stsp err = view_resize(v);
1094 25791caa 2018-10-24 stsp if (err)
1095 25791caa 2018-10-24 stsp return err;
1096 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1097 25791caa 2018-10-24 stsp if (err)
1098 25791caa 2018-10-24 stsp return err;
1099 cdfcfb03 2020-12-06 stsp if (v->child) {
1100 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1101 cdfcfb03 2020-12-06 stsp if (err)
1102 cdfcfb03 2020-12-06 stsp return err;
1103 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1104 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1105 cdfcfb03 2020-12-06 stsp if (err)
1106 cdfcfb03 2020-12-06 stsp return err;
1107 cdfcfb03 2020-12-06 stsp }
1108 25791caa 2018-10-24 stsp }
1109 25791caa 2018-10-24 stsp }
1110 25791caa 2018-10-24 stsp
1111 e5a0f69f 2018-08-18 stsp switch (ch) {
1112 1e37a5c2 2019-05-12 jcs case '\t':
1113 640cd7ff 2022-06-22 mark view->count = 0;
1114 1e37a5c2 2019-05-12 jcs if (view->child) {
1115 e78dc838 2020-12-04 stsp view->focussed = 0;
1116 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1117 e78dc838 2020-12-04 stsp view->focus_child = 1;
1118 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1119 e78dc838 2020-12-04 stsp view->focussed = 0;
1120 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1121 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1122 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1123 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1124 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1125 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1126 9b058f45 2022-06-30 mark if (err)
1127 9b058f45 2022-06-30 mark return err;
1128 9b058f45 2022-06-30 mark }
1129 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1130 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1131 9b058f45 2022-06-30 mark if (err)
1132 9b058f45 2022-06-30 mark return err;
1133 9b058f45 2022-06-30 mark }
1134 1e37a5c2 2019-05-12 jcs }
1135 1e37a5c2 2019-05-12 jcs break;
1136 1e37a5c2 2019-05-12 jcs case 'q':
1137 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1138 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1139 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1140 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1141 9b058f45 2022-06-30 mark if (err)
1142 9b058f45 2022-06-30 mark break;
1143 9b058f45 2022-06-30 mark }
1144 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1145 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1146 9b058f45 2022-06-30 mark }
1147 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1148 9970f7fc 2020-12-03 stsp view->dying = 1;
1149 1e37a5c2 2019-05-12 jcs break;
1150 1e37a5c2 2019-05-12 jcs case 'Q':
1151 1e37a5c2 2019-05-12 jcs *done = 1;
1152 1e37a5c2 2019-05-12 jcs break;
1153 61417565 2022-06-20 mark case 'F':
1154 640cd7ff 2022-06-22 mark view->count = 0;
1155 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1156 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1157 1e37a5c2 2019-05-12 jcs break;
1158 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1159 e78dc838 2020-12-04 stsp view->focussed = 0;
1160 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1161 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1162 1e37a5c2 2019-05-12 jcs } else
1163 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1164 1e37a5c2 2019-05-12 jcs if (err)
1165 1e37a5c2 2019-05-12 jcs break;
1166 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1167 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1168 1e37a5c2 2019-05-12 jcs } else {
1169 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1170 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1171 e78dc838 2020-12-04 stsp view->focussed = 1;
1172 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1173 1e37a5c2 2019-05-12 jcs } else {
1174 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1175 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1176 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1177 669b5ffa 2018-10-07 stsp }
1178 1e37a5c2 2019-05-12 jcs if (err)
1179 1e37a5c2 2019-05-12 jcs break;
1180 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1181 1e37a5c2 2019-05-12 jcs }
1182 9b058f45 2022-06-30 mark if (err)
1183 9b058f45 2022-06-30 mark break;
1184 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1185 9b058f45 2022-06-30 mark err = request_log_commits(view);
1186 9b058f45 2022-06-30 mark if (err)
1187 9b058f45 2022-06-30 mark break;
1188 9b058f45 2022-06-30 mark }
1189 9b058f45 2022-06-30 mark if (view->parent)
1190 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1191 9b058f45 2022-06-30 mark if (!err)
1192 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1193 1e37a5c2 2019-05-12 jcs break;
1194 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1195 60493ae3 2019-06-20 stsp break;
1196 60493ae3 2019-06-20 stsp case '/':
1197 640cd7ff 2022-06-22 mark view->count = 0;
1198 60493ae3 2019-06-20 stsp if (view->search_start)
1199 2b49a8ae 2019-06-22 stsp view_search_start(view);
1200 60493ae3 2019-06-20 stsp else
1201 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1202 1e37a5c2 2019-05-12 jcs break;
1203 b1bf1435 2019-06-21 stsp case 'N':
1204 60493ae3 2019-06-20 stsp case 'n':
1205 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1206 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1207 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1208 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1209 60493ae3 2019-06-20 stsp view->search_next(view);
1210 60493ae3 2019-06-20 stsp } else
1211 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1212 60493ae3 2019-06-20 stsp break;
1213 1e37a5c2 2019-05-12 jcs default:
1214 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1215 1e37a5c2 2019-05-12 jcs break;
1216 e5a0f69f 2018-08-18 stsp }
1217 e5a0f69f 2018-08-18 stsp
1218 e5a0f69f 2018-08-18 stsp return err;
1219 e5a0f69f 2018-08-18 stsp }
1220 e5a0f69f 2018-08-18 stsp
1221 336075a4 2022-06-25 op static int
1222 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1223 a3404814 2018-09-02 stsp {
1224 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1225 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1226 669b5ffa 2018-10-07 stsp return 0;
1227 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1228 669b5ffa 2018-10-07 stsp return 0;
1229 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1230 a3404814 2018-09-02 stsp return 0;
1231 a3404814 2018-09-02 stsp
1232 669b5ffa 2018-10-07 stsp return view->focussed;
1233 a3404814 2018-09-02 stsp }
1234 a3404814 2018-09-02 stsp
1235 bcbd79e2 2018-08-19 stsp static const struct got_error *
1236 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1237 e5a0f69f 2018-08-18 stsp {
1238 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1239 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1240 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1241 fd823528 2018-10-22 stsp int fast_refresh = 10;
1242 1a76625f 2018-10-22 stsp int done = 0, errcode;
1243 e5a0f69f 2018-08-18 stsp
1244 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1245 1a76625f 2018-10-22 stsp if (errcode)
1246 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1247 1a76625f 2018-10-22 stsp
1248 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1249 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1250 e5a0f69f 2018-08-18 stsp
1251 1004088d 2018-09-29 stsp view->focussed = 1;
1252 878940b7 2018-09-29 stsp err = view->show(view);
1253 0cf4efb1 2018-09-29 stsp if (err)
1254 0cf4efb1 2018-09-29 stsp return err;
1255 0cf4efb1 2018-09-29 stsp update_panels();
1256 0cf4efb1 2018-09-29 stsp doupdate();
1257 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1258 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1259 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1260 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1261 fd823528 2018-10-22 stsp
1262 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1263 e5a0f69f 2018-08-18 stsp if (err)
1264 e5a0f69f 2018-08-18 stsp break;
1265 9970f7fc 2020-12-03 stsp if (view->dying) {
1266 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1267 669b5ffa 2018-10-07 stsp
1268 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1269 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1270 9970f7fc 2020-12-03 stsp entry);
1271 e78dc838 2020-12-04 stsp else if (view->parent)
1272 669b5ffa 2018-10-07 stsp prev = view->parent;
1273 669b5ffa 2018-10-07 stsp
1274 e78dc838 2020-12-04 stsp if (view->parent) {
1275 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1276 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1277 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1278 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1279 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1280 0dbbbe90 2022-06-17 op if (err)
1281 0dbbbe90 2022-06-17 op break;
1282 e78dc838 2020-12-04 stsp } else
1283 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1284 669b5ffa 2018-10-07 stsp
1285 9970f7fc 2020-12-03 stsp err = view_close(view);
1286 fb59748f 2020-12-05 stsp if (err)
1287 e5a0f69f 2018-08-18 stsp goto done;
1288 669b5ffa 2018-10-07 stsp
1289 e78dc838 2020-12-04 stsp view = NULL;
1290 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1291 e78dc838 2020-12-04 stsp if (v->focussed)
1292 e78dc838 2020-12-04 stsp break;
1293 0cf4efb1 2018-09-29 stsp }
1294 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1295 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1296 e78dc838 2020-12-04 stsp if (prev)
1297 e78dc838 2020-12-04 stsp view = prev;
1298 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1299 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1300 e78dc838 2020-12-04 stsp tog_view_list_head);
1301 e78dc838 2020-12-04 stsp }
1302 e78dc838 2020-12-04 stsp if (view) {
1303 e78dc838 2020-12-04 stsp if (view->focus_child) {
1304 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1305 e78dc838 2020-12-04 stsp view = view->child;
1306 e78dc838 2020-12-04 stsp } else
1307 e78dc838 2020-12-04 stsp view->focussed = 1;
1308 e78dc838 2020-12-04 stsp }
1309 e78dc838 2020-12-04 stsp }
1310 e5a0f69f 2018-08-18 stsp }
1311 bcbd79e2 2018-08-19 stsp if (new_view) {
1312 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1313 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1314 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1315 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1316 86c66b02 2018-10-18 stsp continue;
1317 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1318 86c66b02 2018-10-18 stsp err = view_close(v);
1319 86c66b02 2018-10-18 stsp if (err)
1320 86c66b02 2018-10-18 stsp goto done;
1321 86c66b02 2018-10-18 stsp break;
1322 86c66b02 2018-10-18 stsp }
1323 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1324 fed7eaa8 2018-10-24 stsp view = new_view;
1325 0ae84acc 2022-06-15 tracey }
1326 669b5ffa 2018-10-07 stsp if (view) {
1327 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1328 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1329 e78dc838 2020-12-04 stsp view = view->child;
1330 e78dc838 2020-12-04 stsp } else {
1331 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1332 e78dc838 2020-12-04 stsp view = view->parent;
1333 1a76625f 2018-10-22 stsp }
1334 e78dc838 2020-12-04 stsp show_panel(view->panel);
1335 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1336 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1337 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1338 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1339 669b5ffa 2018-10-07 stsp if (err)
1340 1a76625f 2018-10-22 stsp goto done;
1341 669b5ffa 2018-10-07 stsp }
1342 669b5ffa 2018-10-07 stsp err = view->show(view);
1343 0cf4efb1 2018-09-29 stsp if (err)
1344 1a76625f 2018-10-22 stsp goto done;
1345 669b5ffa 2018-10-07 stsp if (view->child) {
1346 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1347 669b5ffa 2018-10-07 stsp if (err)
1348 1a76625f 2018-10-22 stsp goto done;
1349 669b5ffa 2018-10-07 stsp }
1350 1a76625f 2018-10-22 stsp update_panels();
1351 1a76625f 2018-10-22 stsp doupdate();
1352 0cf4efb1 2018-09-29 stsp }
1353 e5a0f69f 2018-08-18 stsp }
1354 e5a0f69f 2018-08-18 stsp done:
1355 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1356 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1357 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1358 e5a0f69f 2018-08-18 stsp view_close(view);
1359 e5a0f69f 2018-08-18 stsp }
1360 1a76625f 2018-10-22 stsp
1361 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1362 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1363 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1364 1a76625f 2018-10-22 stsp
1365 e5a0f69f 2018-08-18 stsp return err;
1366 ea5e7bb5 2018-08-01 stsp }
1367 ea5e7bb5 2018-08-01 stsp
1368 4ed7e80c 2018-05-20 stsp __dead static void
1369 9f7d7167 2018-04-29 stsp usage_log(void)
1370 9f7d7167 2018-04-29 stsp {
1371 80ddbec8 2018-04-29 stsp endwin();
1372 c70c5802 2018-08-01 stsp fprintf(stderr,
1373 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1374 9f7d7167 2018-04-29 stsp getprogname());
1375 9f7d7167 2018-04-29 stsp exit(1);
1376 80ddbec8 2018-04-29 stsp }
1377 80ddbec8 2018-04-29 stsp
1378 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1379 80ddbec8 2018-04-29 stsp static const struct got_error *
1380 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1381 963b370f 2018-05-20 stsp {
1382 00dfcb92 2018-06-11 stsp char *vis = NULL;
1383 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1384 963b370f 2018-05-20 stsp
1385 963b370f 2018-05-20 stsp *ws = NULL;
1386 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1387 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1388 00dfcb92 2018-06-11 stsp int vislen;
1389 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1390 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1391 00dfcb92 2018-06-11 stsp
1392 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1393 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1394 00dfcb92 2018-06-11 stsp if (err)
1395 00dfcb92 2018-06-11 stsp return err;
1396 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1397 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1398 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1399 a7f50699 2018-06-11 stsp goto done;
1400 a7f50699 2018-06-11 stsp }
1401 00dfcb92 2018-06-11 stsp }
1402 963b370f 2018-05-20 stsp
1403 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1404 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1405 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1406 a7f50699 2018-06-11 stsp goto done;
1407 a7f50699 2018-06-11 stsp }
1408 963b370f 2018-05-20 stsp
1409 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1410 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1411 a7f50699 2018-06-11 stsp done:
1412 00dfcb92 2018-06-11 stsp free(vis);
1413 963b370f 2018-05-20 stsp if (err) {
1414 963b370f 2018-05-20 stsp free(*ws);
1415 963b370f 2018-05-20 stsp *ws = NULL;
1416 963b370f 2018-05-20 stsp *wlen = 0;
1417 963b370f 2018-05-20 stsp }
1418 963b370f 2018-05-20 stsp return err;
1419 145b6838 2022-06-16 stsp }
1420 145b6838 2022-06-16 stsp
1421 145b6838 2022-06-16 stsp static const struct got_error *
1422 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1423 145b6838 2022-06-16 stsp {
1424 145b6838 2022-06-16 stsp char *dst;
1425 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1426 145b6838 2022-06-16 stsp
1427 145b6838 2022-06-16 stsp *ptr = NULL;
1428 145b6838 2022-06-16 stsp n = len = strlen(src);
1429 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1430 145b6838 2022-06-16 stsp if (dst == NULL)
1431 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1432 145b6838 2022-06-16 stsp
1433 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1434 145b6838 2022-06-16 stsp const char c = src[idx];
1435 145b6838 2022-06-16 stsp
1436 145b6838 2022-06-16 stsp if (c == '\t') {
1437 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1438 367ddf28 2022-06-16 mark char *p;
1439 367ddf28 2022-06-16 mark
1440 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1441 6e1c41ad 2022-06-16 mark if (p == NULL) {
1442 6e1c41ad 2022-06-16 mark free(dst);
1443 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1444 6e1c41ad 2022-06-16 mark
1445 6e1c41ad 2022-06-16 mark }
1446 6e1c41ad 2022-06-16 mark dst = p;
1447 145b6838 2022-06-16 stsp n += nb;
1448 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1449 145b6838 2022-06-16 stsp sz += nb;
1450 145b6838 2022-06-16 stsp } else
1451 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1452 145b6838 2022-06-16 stsp ++idx;
1453 145b6838 2022-06-16 stsp }
1454 145b6838 2022-06-16 stsp
1455 145b6838 2022-06-16 stsp dst[sz] = '\0';
1456 145b6838 2022-06-16 stsp *ptr = dst;
1457 145b6838 2022-06-16 stsp return NULL;
1458 963b370f 2018-05-20 stsp }
1459 963b370f 2018-05-20 stsp
1460 4e4a9ac8 2022-06-17 op /*
1461 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1462 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1463 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1464 4e4a9ac8 2022-06-17 op * *rcol.
1465 ccda2f4d 2022-06-16 stsp */
1466 4e4a9ac8 2022-06-17 op static int
1467 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1468 4e4a9ac8 2022-06-17 op {
1469 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1470 ccda2f4d 2022-06-16 stsp
1471 4e4a9ac8 2022-06-17 op if (n == 0) {
1472 4e4a9ac8 2022-06-17 op *rcol = cols;
1473 4e4a9ac8 2022-06-17 op return off;
1474 4e4a9ac8 2022-06-17 op }
1475 ccda2f4d 2022-06-16 stsp
1476 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1477 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1478 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1479 4e4a9ac8 2022-06-17 op else
1480 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1481 ccda2f4d 2022-06-16 stsp
1482 4e4a9ac8 2022-06-17 op if (width == -1) {
1483 4e4a9ac8 2022-06-17 op width = 1;
1484 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1485 ccda2f4d 2022-06-16 stsp }
1486 ccda2f4d 2022-06-16 stsp
1487 4e4a9ac8 2022-06-17 op if (cols + width > n)
1488 4e4a9ac8 2022-06-17 op break;
1489 4e4a9ac8 2022-06-17 op cols += width;
1490 ccda2f4d 2022-06-16 stsp }
1491 ccda2f4d 2022-06-16 stsp
1492 4e4a9ac8 2022-06-17 op *rcol = cols;
1493 4e4a9ac8 2022-06-17 op return i;
1494 ccda2f4d 2022-06-16 stsp }
1495 ccda2f4d 2022-06-16 stsp
1496 ccda2f4d 2022-06-16 stsp /*
1497 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1498 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1499 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1500 ccda2f4d 2022-06-16 stsp */
1501 ccda2f4d 2022-06-16 stsp static const struct got_error *
1502 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1503 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1504 ccda2f4d 2022-06-16 stsp {
1505 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1506 4e4a9ac8 2022-06-17 op int cols;
1507 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1508 145b6838 2022-06-16 stsp char *exstr = NULL;
1509 963b370f 2018-05-20 stsp size_t wlen;
1510 4e4a9ac8 2022-06-17 op int i, scrollx;
1511 963b370f 2018-05-20 stsp
1512 963b370f 2018-05-20 stsp *wlinep = NULL;
1513 b700b5d6 2018-07-10 stsp *widthp = 0;
1514 963b370f 2018-05-20 stsp
1515 145b6838 2022-06-16 stsp if (expand) {
1516 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1517 145b6838 2022-06-16 stsp if (err)
1518 145b6838 2022-06-16 stsp return err;
1519 145b6838 2022-06-16 stsp }
1520 145b6838 2022-06-16 stsp
1521 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1522 145b6838 2022-06-16 stsp free(exstr);
1523 963b370f 2018-05-20 stsp if (err)
1524 963b370f 2018-05-20 stsp return err;
1525 963b370f 2018-05-20 stsp
1526 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1527 ccda2f4d 2022-06-16 stsp
1528 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1529 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1530 3f670bfb 2020-12-10 stsp wlen--;
1531 3f670bfb 2020-12-10 stsp }
1532 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1533 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1534 3f670bfb 2020-12-10 stsp wlen--;
1535 3f670bfb 2020-12-10 stsp }
1536 3f670bfb 2020-12-10 stsp
1537 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1538 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1539 27a741e5 2019-09-11 stsp
1540 b700b5d6 2018-07-10 stsp if (widthp)
1541 b700b5d6 2018-07-10 stsp *widthp = cols;
1542 ccda2f4d 2022-06-16 stsp if (scrollxp)
1543 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1544 963b370f 2018-05-20 stsp if (err)
1545 963b370f 2018-05-20 stsp free(wline);
1546 963b370f 2018-05-20 stsp else
1547 963b370f 2018-05-20 stsp *wlinep = wline;
1548 963b370f 2018-05-20 stsp return err;
1549 963b370f 2018-05-20 stsp }
1550 963b370f 2018-05-20 stsp
1551 8b473291 2019-02-21 stsp static const struct got_error*
1552 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1553 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1554 8b473291 2019-02-21 stsp {
1555 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1556 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1557 8b473291 2019-02-21 stsp char *s;
1558 8b473291 2019-02-21 stsp const char *name;
1559 8b473291 2019-02-21 stsp
1560 8b473291 2019-02-21 stsp *refs_str = NULL;
1561 8b473291 2019-02-21 stsp
1562 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1563 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1564 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1565 52b5abe1 2019-08-13 stsp int cmp;
1566 52b5abe1 2019-08-13 stsp
1567 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1568 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1569 8b473291 2019-02-21 stsp continue;
1570 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1571 8b473291 2019-02-21 stsp name += 5;
1572 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1573 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1574 7143d404 2019-03-12 stsp continue;
1575 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1576 8b473291 2019-02-21 stsp name += 6;
1577 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1578 8b473291 2019-02-21 stsp name += 8;
1579 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1580 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1581 79cc719f 2020-04-24 stsp continue;
1582 79cc719f 2020-04-24 stsp }
1583 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1584 48cae60d 2020-09-22 stsp if (err)
1585 48cae60d 2020-09-22 stsp break;
1586 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1587 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1588 5d844a1e 2019-08-13 stsp if (err) {
1589 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1590 48cae60d 2020-09-22 stsp free(ref_id);
1591 5d844a1e 2019-08-13 stsp break;
1592 48cae60d 2020-09-22 stsp }
1593 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1594 5d844a1e 2019-08-13 stsp err = NULL;
1595 5d844a1e 2019-08-13 stsp tag = NULL;
1596 5d844a1e 2019-08-13 stsp }
1597 52b5abe1 2019-08-13 stsp }
1598 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1599 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1600 48cae60d 2020-09-22 stsp free(ref_id);
1601 52b5abe1 2019-08-13 stsp if (tag)
1602 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1603 52b5abe1 2019-08-13 stsp if (cmp != 0)
1604 52b5abe1 2019-08-13 stsp continue;
1605 8b473291 2019-02-21 stsp s = *refs_str;
1606 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1607 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1608 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1609 8b473291 2019-02-21 stsp free(s);
1610 8b473291 2019-02-21 stsp *refs_str = NULL;
1611 8b473291 2019-02-21 stsp break;
1612 8b473291 2019-02-21 stsp }
1613 8b473291 2019-02-21 stsp free(s);
1614 8b473291 2019-02-21 stsp }
1615 8b473291 2019-02-21 stsp
1616 8b473291 2019-02-21 stsp return err;
1617 8b473291 2019-02-21 stsp }
1618 8b473291 2019-02-21 stsp
1619 963b370f 2018-05-20 stsp static const struct got_error *
1620 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1621 27a741e5 2019-09-11 stsp int col_tab_align)
1622 5813d178 2019-03-09 stsp {
1623 e6b8b890 2020-12-29 naddy char *smallerthan;
1624 5813d178 2019-03-09 stsp
1625 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1626 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1627 5813d178 2019-03-09 stsp author = smallerthan + 1;
1628 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1629 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1630 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1631 5813d178 2019-03-09 stsp }
1632 5813d178 2019-03-09 stsp
1633 5813d178 2019-03-09 stsp static const struct got_error *
1634 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1635 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1636 8fdc79fe 2020-12-01 naddy int author_display_cols)
1637 80ddbec8 2018-04-29 stsp {
1638 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1639 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1640 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1641 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1642 5813d178 2019-03-09 stsp char *author = NULL;
1643 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1644 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1645 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1646 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1647 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1648 ccb26ccd 2018-11-05 stsp struct tm tm;
1649 45d799e2 2018-12-23 stsp time_t committer_time;
1650 11b20872 2019-11-08 stsp struct tog_color *tc;
1651 80ddbec8 2018-04-29 stsp
1652 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1653 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1654 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1655 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1656 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1657 b39d25c7 2018-07-10 stsp
1658 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1659 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1660 b39d25c7 2018-07-10 stsp else
1661 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1662 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1663 11b20872 2019-11-08 stsp if (tc)
1664 11b20872 2019-11-08 stsp wattr_on(view->window,
1665 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1666 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1667 11b20872 2019-11-08 stsp if (tc)
1668 11b20872 2019-11-08 stsp wattr_off(view->window,
1669 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1670 27a741e5 2019-09-11 stsp col = limit;
1671 b39d25c7 2018-07-10 stsp if (col > avail)
1672 b39d25c7 2018-07-10 stsp goto done;
1673 6570a66d 2019-11-08 stsp
1674 6570a66d 2019-11-08 stsp if (avail >= 120) {
1675 6570a66d 2019-11-08 stsp char *id_str;
1676 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1677 6570a66d 2019-11-08 stsp if (err)
1678 6570a66d 2019-11-08 stsp goto done;
1679 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1680 11b20872 2019-11-08 stsp if (tc)
1681 11b20872 2019-11-08 stsp wattr_on(view->window,
1682 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1683 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1684 11b20872 2019-11-08 stsp if (tc)
1685 11b20872 2019-11-08 stsp wattr_off(view->window,
1686 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1687 6570a66d 2019-11-08 stsp free(id_str);
1688 6570a66d 2019-11-08 stsp col += 9;
1689 6570a66d 2019-11-08 stsp if (col > avail)
1690 6570a66d 2019-11-08 stsp goto done;
1691 6570a66d 2019-11-08 stsp }
1692 b39d25c7 2018-07-10 stsp
1693 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1694 5813d178 2019-03-09 stsp if (author == NULL) {
1695 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1696 80ddbec8 2018-04-29 stsp goto done;
1697 80ddbec8 2018-04-29 stsp }
1698 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1699 bb737323 2018-05-20 stsp if (err)
1700 bb737323 2018-05-20 stsp goto done;
1701 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1702 11b20872 2019-11-08 stsp if (tc)
1703 11b20872 2019-11-08 stsp wattr_on(view->window,
1704 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1705 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1706 11b20872 2019-11-08 stsp if (tc)
1707 11b20872 2019-11-08 stsp wattr_off(view->window,
1708 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1709 bb737323 2018-05-20 stsp col += author_width;
1710 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1711 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1712 bb737323 2018-05-20 stsp col++;
1713 bb737323 2018-05-20 stsp author_width++;
1714 bb737323 2018-05-20 stsp }
1715 9c2eaf34 2018-05-20 stsp if (col > avail)
1716 9c2eaf34 2018-05-20 stsp goto done;
1717 80ddbec8 2018-04-29 stsp
1718 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1719 5943eee2 2019-08-13 stsp if (err)
1720 6d9fbc00 2018-04-29 stsp goto done;
1721 bb737323 2018-05-20 stsp logmsg = logmsg0;
1722 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1723 bb737323 2018-05-20 stsp logmsg++;
1724 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1725 bb737323 2018-05-20 stsp if (newline)
1726 bb737323 2018-05-20 stsp *newline = '\0';
1727 ccda2f4d 2022-06-16 stsp limit = avail - col;
1728 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1729 4d1f6af3 2022-06-17 op limit--; /* for the border */
1730 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1731 ccda2f4d 2022-06-16 stsp limit, col, 1);
1732 29688b02 2022-06-16 stsp if (err)
1733 29688b02 2022-06-16 stsp goto done;
1734 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1735 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1736 27a741e5 2019-09-11 stsp while (col < avail) {
1737 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1738 bb737323 2018-05-20 stsp col++;
1739 881b2d3e 2018-04-30 stsp }
1740 80ddbec8 2018-04-29 stsp done:
1741 80ddbec8 2018-04-29 stsp free(logmsg0);
1742 bb737323 2018-05-20 stsp free(wlogmsg);
1743 5813d178 2019-03-09 stsp free(author);
1744 bb737323 2018-05-20 stsp free(wauthor);
1745 80ddbec8 2018-04-29 stsp free(line);
1746 80ddbec8 2018-04-29 stsp return err;
1747 80ddbec8 2018-04-29 stsp }
1748 26ed57b2 2018-05-19 stsp
1749 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1750 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1751 899d86c2 2018-05-10 stsp struct got_object_id *id)
1752 80ddbec8 2018-04-29 stsp {
1753 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1754 80ddbec8 2018-04-29 stsp
1755 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1756 80ddbec8 2018-04-29 stsp if (entry == NULL)
1757 899d86c2 2018-05-10 stsp return NULL;
1758 99db9666 2018-05-07 stsp
1759 899d86c2 2018-05-10 stsp entry->id = id;
1760 99db9666 2018-05-07 stsp entry->commit = commit;
1761 899d86c2 2018-05-10 stsp return entry;
1762 99db9666 2018-05-07 stsp }
1763 80ddbec8 2018-04-29 stsp
1764 99db9666 2018-05-07 stsp static void
1765 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1766 99db9666 2018-05-07 stsp {
1767 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1768 99db9666 2018-05-07 stsp
1769 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1770 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1771 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1772 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1773 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1774 99db9666 2018-05-07 stsp free(entry);
1775 99db9666 2018-05-07 stsp }
1776 99db9666 2018-05-07 stsp
1777 99db9666 2018-05-07 stsp static void
1778 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1779 99db9666 2018-05-07 stsp {
1780 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1781 99db9666 2018-05-07 stsp pop_commit(commits);
1782 c4972b91 2018-05-07 stsp }
1783 c4972b91 2018-05-07 stsp
1784 c4972b91 2018-05-07 stsp static const struct got_error *
1785 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1786 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1787 13add988 2019-10-15 stsp {
1788 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1789 13add988 2019-10-15 stsp regmatch_t regmatch;
1790 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1791 13add988 2019-10-15 stsp
1792 13add988 2019-10-15 stsp *have_match = 0;
1793 13add988 2019-10-15 stsp
1794 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1795 13add988 2019-10-15 stsp if (err)
1796 13add988 2019-10-15 stsp return err;
1797 13add988 2019-10-15 stsp
1798 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1799 13add988 2019-10-15 stsp if (err)
1800 13add988 2019-10-15 stsp goto done;
1801 13add988 2019-10-15 stsp
1802 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1803 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1804 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1805 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1806 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1807 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1808 13add988 2019-10-15 stsp *have_match = 1;
1809 13add988 2019-10-15 stsp done:
1810 13add988 2019-10-15 stsp free(id_str);
1811 13add988 2019-10-15 stsp free(logmsg);
1812 13add988 2019-10-15 stsp return err;
1813 13add988 2019-10-15 stsp }
1814 13add988 2019-10-15 stsp
1815 13add988 2019-10-15 stsp static const struct got_error *
1816 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1817 c4972b91 2018-05-07 stsp {
1818 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1819 9ba79e04 2018-06-11 stsp
1820 1a76625f 2018-10-22 stsp /*
1821 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1822 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1823 1a76625f 2018-10-22 stsp * while updating the display.
1824 1a76625f 2018-10-22 stsp */
1825 4e0d2870 2020-12-07 naddy do {
1826 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1827 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1828 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1829 1a76625f 2018-10-22 stsp int errcode;
1830 899d86c2 2018-05-10 stsp
1831 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1832 4e0d2870 2020-12-07 naddy NULL, NULL);
1833 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1834 ecb28ae0 2018-07-16 stsp break;
1835 899d86c2 2018-05-10 stsp
1836 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1837 9ba79e04 2018-06-11 stsp if (err)
1838 9ba79e04 2018-06-11 stsp break;
1839 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1840 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1841 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1842 9ba79e04 2018-06-11 stsp break;
1843 9ba79e04 2018-06-11 stsp }
1844 93e45b7c 2018-09-24 stsp
1845 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1846 1a76625f 2018-10-22 stsp if (errcode) {
1847 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1848 13add988 2019-10-15 stsp "pthread_mutex_lock");
1849 1a76625f 2018-10-22 stsp break;
1850 1a76625f 2018-10-22 stsp }
1851 1a76625f 2018-10-22 stsp
1852 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1853 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1854 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1855 1a76625f 2018-10-22 stsp
1856 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1857 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1858 7c1452c1 2020-03-26 stsp int have_match;
1859 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1860 7c1452c1 2020-03-26 stsp if (err)
1861 7c1452c1 2020-03-26 stsp break;
1862 7c1452c1 2020-03-26 stsp if (have_match)
1863 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1864 13add988 2019-10-15 stsp }
1865 13add988 2019-10-15 stsp
1866 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1867 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1868 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1869 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1870 7c1452c1 2020-03-26 stsp if (err)
1871 13add988 2019-10-15 stsp break;
1872 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1873 899d86c2 2018-05-10 stsp
1874 9ba79e04 2018-06-11 stsp return err;
1875 0553a4e3 2018-04-30 stsp }
1876 0553a4e3 2018-04-30 stsp
1877 2b779855 2020-12-05 naddy static void
1878 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1879 2b779855 2020-12-05 naddy {
1880 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1881 2b779855 2020-12-05 naddy int ncommits = 0;
1882 2b779855 2020-12-05 naddy
1883 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1884 2b779855 2020-12-05 naddy while (entry) {
1885 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1886 2b779855 2020-12-05 naddy s->selected_entry = entry;
1887 2b779855 2020-12-05 naddy break;
1888 2b779855 2020-12-05 naddy }
1889 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1890 2b779855 2020-12-05 naddy ncommits++;
1891 2b779855 2020-12-05 naddy }
1892 2b779855 2020-12-05 naddy }
1893 2b779855 2020-12-05 naddy
1894 0553a4e3 2018-04-30 stsp static const struct got_error *
1895 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1896 0553a4e3 2018-04-30 stsp {
1897 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1898 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1899 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1900 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1901 60493ae3 2019-06-20 stsp int width;
1902 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1903 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1904 8b473291 2019-02-21 stsp char *refs_str = NULL;
1905 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1906 11b20872 2019-11-08 stsp struct tog_color *tc;
1907 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1908 0553a4e3 2018-04-30 stsp
1909 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1910 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1911 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1912 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1913 1a76625f 2018-10-22 stsp if (err)
1914 ecb28ae0 2018-07-16 stsp return err;
1915 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1916 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1917 d2075bf3 2020-12-25 stsp if (refs) {
1918 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1919 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1920 d2075bf3 2020-12-25 stsp if (err)
1921 d2075bf3 2020-12-25 stsp goto done;
1922 d2075bf3 2020-12-25 stsp }
1923 867c6645 2018-07-10 stsp }
1924 359bfafd 2019-02-22 stsp
1925 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1926 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1927 1a76625f 2018-10-22 stsp
1928 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1929 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1930 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1931 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1932 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1933 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1934 8f4ed634 2020-03-26 stsp goto done;
1935 8f4ed634 2020-03-26 stsp }
1936 8f4ed634 2020-03-26 stsp } else {
1937 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1938 f9686aa5 2020-03-27 stsp
1939 f9686aa5 2020-03-27 stsp if (view->searching) {
1940 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1941 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1942 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1943 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1944 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1945 f9686aa5 2020-03-27 stsp search_str = "searching...";
1946 f9686aa5 2020-03-27 stsp }
1947 f9686aa5 2020-03-27 stsp
1948 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1949 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1950 f9686aa5 2020-03-27 stsp search_str ? search_str :
1951 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1952 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1953 8f4ed634 2020-03-26 stsp goto done;
1954 8f4ed634 2020-03-26 stsp }
1955 8b473291 2019-02-21 stsp }
1956 1a76625f 2018-10-22 stsp
1957 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1958 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1959 87411fa9 2022-06-16 stsp "........................................",
1960 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1961 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1962 1a76625f 2018-10-22 stsp header = NULL;
1963 1a76625f 2018-10-22 stsp goto done;
1964 1a76625f 2018-10-22 stsp }
1965 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1966 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1967 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1968 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1969 1a76625f 2018-10-22 stsp header = NULL;
1970 1a76625f 2018-10-22 stsp goto done;
1971 ecb28ae0 2018-07-16 stsp }
1972 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1973 1a76625f 2018-10-22 stsp if (err)
1974 1a76625f 2018-10-22 stsp goto done;
1975 867c6645 2018-07-10 stsp
1976 2814baeb 2018-08-01 stsp werase(view->window);
1977 867c6645 2018-07-10 stsp
1978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1979 a3404814 2018-09-02 stsp wstandout(view->window);
1980 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1981 11b20872 2019-11-08 stsp if (tc)
1982 11b20872 2019-11-08 stsp wattr_on(view->window,
1983 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1984 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1985 11b20872 2019-11-08 stsp if (tc)
1986 11b20872 2019-11-08 stsp wattr_off(view->window,
1987 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1988 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1989 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1990 1a76625f 2018-10-22 stsp width++;
1991 1a76625f 2018-10-22 stsp }
1992 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1993 a3404814 2018-09-02 stsp wstandend(view->window);
1994 ecb28ae0 2018-07-16 stsp free(wline);
1995 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1996 1a76625f 2018-10-22 stsp goto done;
1997 0553a4e3 2018-04-30 stsp
1998 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
1999 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2000 5813d178 2019-03-09 stsp ncommits = 0;
2001 145b6838 2022-06-16 stsp view->maxx = 0;
2002 5813d178 2019-03-09 stsp while (entry) {
2003 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2004 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2005 5813d178 2019-03-09 stsp int width;
2006 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2007 5813d178 2019-03-09 stsp break;
2008 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2009 5813d178 2019-03-09 stsp if (author == NULL) {
2010 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2011 5813d178 2019-03-09 stsp goto done;
2012 5813d178 2019-03-09 stsp }
2013 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2014 27a741e5 2019-09-11 stsp date_display_cols);
2015 5813d178 2019-03-09 stsp if (author_cols < width)
2016 5813d178 2019-03-09 stsp author_cols = width;
2017 5813d178 2019-03-09 stsp free(wauthor);
2018 5813d178 2019-03-09 stsp free(author);
2019 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2020 145b6838 2022-06-16 stsp if (err)
2021 145b6838 2022-06-16 stsp goto done;
2022 145b6838 2022-06-16 stsp msg = msg0;
2023 145b6838 2022-06-16 stsp while (*msg == '\n')
2024 145b6838 2022-06-16 stsp ++msg;
2025 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2026 29688b02 2022-06-16 stsp *eol = '\0';
2027 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2028 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2029 29688b02 2022-06-16 stsp if (err)
2030 29688b02 2022-06-16 stsp goto done;
2031 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2032 145b6838 2022-06-16 stsp free(msg0);
2033 29688b02 2022-06-16 stsp free(wmsg);
2034 7ca04879 2019-10-19 stsp ncommits++;
2035 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2036 5813d178 2019-03-09 stsp }
2037 5813d178 2019-03-09 stsp
2038 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2039 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2040 867c6645 2018-07-10 stsp ncommits = 0;
2041 899d86c2 2018-05-10 stsp while (entry) {
2042 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2043 899d86c2 2018-05-10 stsp break;
2044 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2045 2814baeb 2018-08-01 stsp wstandout(view->window);
2046 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2047 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2048 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2049 2814baeb 2018-08-01 stsp wstandend(view->window);
2050 0553a4e3 2018-04-30 stsp if (err)
2051 60493ae3 2019-06-20 stsp goto done;
2052 0553a4e3 2018-04-30 stsp ncommits++;
2053 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2054 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2055 80ddbec8 2018-04-29 stsp }
2056 80ddbec8 2018-04-29 stsp
2057 9b058f45 2022-06-30 mark view_border(view);
2058 1a76625f 2018-10-22 stsp done:
2059 1a76625f 2018-10-22 stsp free(id_str);
2060 8b473291 2019-02-21 stsp free(refs_str);
2061 1a76625f 2018-10-22 stsp free(ncommits_str);
2062 1a76625f 2018-10-22 stsp free(header);
2063 80ddbec8 2018-04-29 stsp return err;
2064 9f7d7167 2018-04-29 stsp }
2065 07b55e75 2018-05-10 stsp
2066 07b55e75 2018-05-10 stsp static void
2067 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2068 07b55e75 2018-05-10 stsp {
2069 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2070 07b55e75 2018-05-10 stsp int nscrolled = 0;
2071 07b55e75 2018-05-10 stsp
2072 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2073 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2074 07b55e75 2018-05-10 stsp return;
2075 9f7d7167 2018-04-29 stsp
2076 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2077 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2078 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2079 07b55e75 2018-05-10 stsp if (entry) {
2080 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2081 07b55e75 2018-05-10 stsp nscrolled++;
2082 07b55e75 2018-05-10 stsp }
2083 07b55e75 2018-05-10 stsp }
2084 aa075928 2018-05-10 stsp }
2085 aa075928 2018-05-10 stsp
2086 aa075928 2018-05-10 stsp static const struct got_error *
2087 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2088 aa075928 2018-05-10 stsp {
2089 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2090 5e224a3e 2019-02-22 stsp int errcode;
2091 8a42fca8 2019-02-22 stsp
2092 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2093 aa075928 2018-05-10 stsp
2094 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2095 ffe38506 2020-12-01 naddy if (ta->log_complete)
2096 5e224a3e 2019-02-22 stsp break;
2097 b295e71b 2019-02-22 stsp
2098 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2099 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2100 7aafa0d1 2019-02-22 stsp if (errcode)
2101 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2102 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2103 7c1452c1 2020-03-26 stsp
2104 7c1452c1 2020-03-26 stsp /*
2105 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2106 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2107 7c1452c1 2020-03-26 stsp */
2108 7c1452c1 2020-03-26 stsp if (!wait)
2109 7c1452c1 2020-03-26 stsp break;
2110 7c1452c1 2020-03-26 stsp
2111 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2112 ffe38506 2020-12-01 naddy show_log_view(view);
2113 7c1452c1 2020-03-26 stsp update_panels();
2114 7c1452c1 2020-03-26 stsp doupdate();
2115 7c1452c1 2020-03-26 stsp
2116 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2117 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2118 82954512 2020-02-03 stsp if (errcode)
2119 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2120 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2121 82954512 2020-02-03 stsp
2122 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2123 ffe38506 2020-12-01 naddy show_log_view(view);
2124 7c1452c1 2020-03-26 stsp update_panels();
2125 7c1452c1 2020-03-26 stsp doupdate();
2126 5e224a3e 2019-02-22 stsp }
2127 5e224a3e 2019-02-22 stsp
2128 5e224a3e 2019-02-22 stsp return NULL;
2129 5e224a3e 2019-02-22 stsp }
2130 5e224a3e 2019-02-22 stsp
2131 5e224a3e 2019-02-22 stsp static const struct got_error *
2132 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2133 9b058f45 2022-06-30 mark {
2134 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2135 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2136 9b058f45 2022-06-30 mark
2137 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2138 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2139 9b058f45 2022-06-30 mark view->nscrolled = 0;
2140 9b058f45 2022-06-30 mark
2141 9b058f45 2022-06-30 mark return err;
2142 9b058f45 2022-06-30 mark }
2143 9b058f45 2022-06-30 mark
2144 9b058f45 2022-06-30 mark static const struct got_error *
2145 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2146 5e224a3e 2019-02-22 stsp {
2147 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2148 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2149 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2150 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2151 5e224a3e 2019-02-22 stsp
2152 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2153 5e224a3e 2019-02-22 stsp return NULL;
2154 5e224a3e 2019-02-22 stsp
2155 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2156 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2157 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2158 08ebd0a9 2019-02-22 stsp /*
2159 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2160 08ebd0a9 2019-02-22 stsp */
2161 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2162 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2163 5e224a3e 2019-02-22 stsp if (err)
2164 5e224a3e 2019-02-22 stsp return err;
2165 7aafa0d1 2019-02-22 stsp }
2166 b295e71b 2019-02-22 stsp
2167 7aafa0d1 2019-02-22 stsp do {
2168 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2169 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2170 88048b54 2019-02-21 stsp break;
2171 88048b54 2019-02-21 stsp
2172 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2173 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2174 aa075928 2018-05-10 stsp
2175 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2176 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2177 dd0a52c1 2018-05-20 stsp break;
2178 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2179 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2180 aa075928 2018-05-10 stsp
2181 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2182 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2183 9b058f45 2022-06-30 mark else
2184 9b058f45 2022-06-30 mark view->nscrolled = 0;
2185 9b058f45 2022-06-30 mark
2186 dd0a52c1 2018-05-20 stsp return err;
2187 07b55e75 2018-05-10 stsp }
2188 4a7f7875 2018-05-10 stsp
2189 cd0acaa7 2018-05-20 stsp static const struct got_error *
2190 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2191 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2192 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2193 cd0acaa7 2018-05-20 stsp {
2194 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2195 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2196 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2197 cd0acaa7 2018-05-20 stsp
2198 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2199 15a94983 2018-12-23 stsp if (diff_view == NULL)
2200 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2201 ea5e7bb5 2018-08-01 stsp
2202 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2203 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2204 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2205 e5a0f69f 2018-08-18 stsp if (err == NULL)
2206 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2207 cd0acaa7 2018-05-20 stsp return err;
2208 4a7f7875 2018-05-10 stsp }
2209 4a7f7875 2018-05-10 stsp
2210 80ddbec8 2018-04-29 stsp static const struct got_error *
2211 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2212 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2213 9343a5fb 2018-06-23 stsp {
2214 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2215 941e9f74 2019-05-21 stsp
2216 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2217 941e9f74 2019-05-21 stsp if (parent == NULL)
2218 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2219 941e9f74 2019-05-21 stsp
2220 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2221 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2222 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2223 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2224 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2225 941e9f74 2019-05-21 stsp s->tree = subtree;
2226 941e9f74 2019-05-21 stsp s->selected = 0;
2227 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2228 941e9f74 2019-05-21 stsp return NULL;
2229 941e9f74 2019-05-21 stsp }
2230 941e9f74 2019-05-21 stsp
2231 941e9f74 2019-05-21 stsp static const struct got_error *
2232 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2233 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2234 941e9f74 2019-05-21 stsp {
2235 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2236 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2237 941e9f74 2019-05-21 stsp const char *p;
2238 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2239 9343a5fb 2018-06-23 stsp
2240 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2241 941e9f74 2019-05-21 stsp p = path;
2242 941e9f74 2019-05-21 stsp while (*p) {
2243 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2244 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2245 56e0773d 2019-11-28 stsp char *te_name;
2246 33cbf02b 2020-01-12 stsp
2247 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2248 33cbf02b 2020-01-12 stsp p++;
2249 941e9f74 2019-05-21 stsp
2250 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2251 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2252 941e9f74 2019-05-21 stsp if (slash == NULL)
2253 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2254 33cbf02b 2020-01-12 stsp else
2255 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2256 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2257 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2258 56e0773d 2019-11-28 stsp break;
2259 941e9f74 2019-05-21 stsp }
2260 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2261 56e0773d 2019-11-28 stsp if (te == NULL) {
2262 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2263 56e0773d 2019-11-28 stsp free(te_name);
2264 941e9f74 2019-05-21 stsp break;
2265 941e9f74 2019-05-21 stsp }
2266 56e0773d 2019-11-28 stsp free(te_name);
2267 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2268 941e9f74 2019-05-21 stsp
2269 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2270 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2271 b03c880f 2019-05-21 stsp
2272 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2273 941e9f74 2019-05-21 stsp if (slash)
2274 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2275 941e9f74 2019-05-21 stsp else
2276 941e9f74 2019-05-21 stsp subpath = strdup(path);
2277 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2278 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2279 941e9f74 2019-05-21 stsp break;
2280 941e9f74 2019-05-21 stsp }
2281 941e9f74 2019-05-21 stsp
2282 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2283 941e9f74 2019-05-21 stsp subpath);
2284 941e9f74 2019-05-21 stsp if (err)
2285 941e9f74 2019-05-21 stsp break;
2286 941e9f74 2019-05-21 stsp
2287 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2288 941e9f74 2019-05-21 stsp free(tree_id);
2289 941e9f74 2019-05-21 stsp if (err)
2290 941e9f74 2019-05-21 stsp break;
2291 941e9f74 2019-05-21 stsp
2292 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2293 941e9f74 2019-05-21 stsp if (err) {
2294 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2295 941e9f74 2019-05-21 stsp break;
2296 941e9f74 2019-05-21 stsp }
2297 941e9f74 2019-05-21 stsp if (slash == NULL)
2298 941e9f74 2019-05-21 stsp break;
2299 941e9f74 2019-05-21 stsp free(subpath);
2300 941e9f74 2019-05-21 stsp subpath = NULL;
2301 941e9f74 2019-05-21 stsp p = slash;
2302 941e9f74 2019-05-21 stsp }
2303 941e9f74 2019-05-21 stsp
2304 941e9f74 2019-05-21 stsp free(subpath);
2305 1a76625f 2018-10-22 stsp return err;
2306 61266923 2020-01-14 stsp }
2307 61266923 2020-01-14 stsp
2308 61266923 2020-01-14 stsp static const struct got_error *
2309 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2310 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2311 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2312 55cccc34 2020-02-20 stsp {
2313 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2314 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2315 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2316 55cccc34 2020-02-20 stsp
2317 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2318 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2319 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2320 55cccc34 2020-02-20 stsp
2321 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2322 bc573f3b 2021-07-10 stsp if (err)
2323 55cccc34 2020-02-20 stsp return err;
2324 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2325 55cccc34 2020-02-20 stsp
2326 55cccc34 2020-02-20 stsp *new_view = tree_view;
2327 55cccc34 2020-02-20 stsp
2328 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2329 55cccc34 2020-02-20 stsp return NULL;
2330 55cccc34 2020-02-20 stsp
2331 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2332 55cccc34 2020-02-20 stsp }
2333 55cccc34 2020-02-20 stsp
2334 55cccc34 2020-02-20 stsp static const struct got_error *
2335 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2336 61266923 2020-01-14 stsp {
2337 61266923 2020-01-14 stsp sigset_t sigset;
2338 61266923 2020-01-14 stsp int errcode;
2339 61266923 2020-01-14 stsp
2340 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2341 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2342 61266923 2020-01-14 stsp
2343 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2344 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2345 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2346 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2347 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2348 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2349 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2350 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2351 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2352 61266923 2020-01-14 stsp
2353 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2354 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2355 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2356 61266923 2020-01-14 stsp
2357 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2358 61266923 2020-01-14 stsp if (errcode)
2359 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2360 61266923 2020-01-14 stsp
2361 61266923 2020-01-14 stsp return NULL;
2362 1a76625f 2018-10-22 stsp }
2363 1a76625f 2018-10-22 stsp
2364 1a76625f 2018-10-22 stsp static void *
2365 1a76625f 2018-10-22 stsp log_thread(void *arg)
2366 1a76625f 2018-10-22 stsp {
2367 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2368 1a76625f 2018-10-22 stsp int errcode = 0;
2369 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2370 1a76625f 2018-10-22 stsp int done = 0;
2371 61266923 2020-01-14 stsp
2372 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2373 61266923 2020-01-14 stsp if (err)
2374 61266923 2020-01-14 stsp return (void *)err;
2375 1a76625f 2018-10-22 stsp
2376 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2377 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2378 1a76625f 2018-10-22 stsp if (err) {
2379 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2380 1a76625f 2018-10-22 stsp return (void *)err;
2381 1a76625f 2018-10-22 stsp err = NULL;
2382 1a76625f 2018-10-22 stsp done = 1;
2383 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2384 1a76625f 2018-10-22 stsp a->commits_needed--;
2385 1a76625f 2018-10-22 stsp
2386 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2387 3abe8080 2019-04-10 stsp if (errcode) {
2388 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2389 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2390 3abe8080 2019-04-10 stsp break;
2391 3abe8080 2019-04-10 stsp } else if (*a->quit)
2392 1a76625f 2018-10-22 stsp done = 1;
2393 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2394 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2395 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2396 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2397 1a76625f 2018-10-22 stsp }
2398 1a76625f 2018-10-22 stsp
2399 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2400 7c1452c1 2020-03-26 stsp if (errcode) {
2401 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2402 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2403 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2404 7c1452c1 2020-03-26 stsp break;
2405 7c1452c1 2020-03-26 stsp }
2406 7c1452c1 2020-03-26 stsp
2407 1a76625f 2018-10-22 stsp if (done)
2408 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2409 7c1452c1 2020-03-26 stsp else {
2410 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2411 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2412 7c1452c1 2020-03-26 stsp &tog_mutex);
2413 7c1452c1 2020-03-26 stsp if (errcode)
2414 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2415 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2416 21355643 2020-12-06 stsp if (*a->quit)
2417 21355643 2020-12-06 stsp done = 1;
2418 7c1452c1 2020-03-26 stsp }
2419 1a76625f 2018-10-22 stsp }
2420 1a76625f 2018-10-22 stsp
2421 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2422 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2423 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2424 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2425 1a76625f 2018-10-22 stsp }
2426 3abe8080 2019-04-10 stsp a->log_complete = 1;
2427 1a76625f 2018-10-22 stsp return (void *)err;
2428 1a76625f 2018-10-22 stsp }
2429 1a76625f 2018-10-22 stsp
2430 1a76625f 2018-10-22 stsp static const struct got_error *
2431 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2432 1a76625f 2018-10-22 stsp {
2433 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2434 1a76625f 2018-10-22 stsp int errcode;
2435 1a76625f 2018-10-22 stsp
2436 1a76625f 2018-10-22 stsp if (s->thread) {
2437 1a76625f 2018-10-22 stsp s->quit = 1;
2438 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2439 1a76625f 2018-10-22 stsp if (errcode)
2440 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2441 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2442 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2443 1a76625f 2018-10-22 stsp if (errcode)
2444 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2445 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2446 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2447 1a76625f 2018-10-22 stsp if (errcode)
2448 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2449 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2450 1a76625f 2018-10-22 stsp if (errcode)
2451 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2452 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2453 1a76625f 2018-10-22 stsp s->thread = NULL;
2454 1a76625f 2018-10-22 stsp }
2455 1a76625f 2018-10-22 stsp
2456 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2457 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2458 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2459 74467cc8 2022-06-15 stsp }
2460 74467cc8 2022-06-15 stsp
2461 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2462 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2463 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2464 74467cc8 2022-06-15 stsp if (err == NULL)
2465 74467cc8 2022-06-15 stsp err = pack_err;
2466 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2467 1a76625f 2018-10-22 stsp }
2468 1a76625f 2018-10-22 stsp
2469 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2470 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2471 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2472 1a76625f 2018-10-22 stsp }
2473 1a76625f 2018-10-22 stsp
2474 9343a5fb 2018-06-23 stsp return err;
2475 9343a5fb 2018-06-23 stsp }
2476 9343a5fb 2018-06-23 stsp
2477 9343a5fb 2018-06-23 stsp static const struct got_error *
2478 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2479 1a76625f 2018-10-22 stsp {
2480 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2481 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2482 276b94a1 2020-11-13 naddy int errcode;
2483 1a76625f 2018-10-22 stsp
2484 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2485 276b94a1 2020-11-13 naddy
2486 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2487 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2488 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2489 276b94a1 2020-11-13 naddy
2490 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2491 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2492 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2493 276b94a1 2020-11-13 naddy
2494 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2495 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2496 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2497 1a76625f 2018-10-22 stsp free(s->start_id);
2498 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2499 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2500 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2501 1a76625f 2018-10-22 stsp return err;
2502 1a76625f 2018-10-22 stsp }
2503 1a76625f 2018-10-22 stsp
2504 1a76625f 2018-10-22 stsp static const struct got_error *
2505 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2506 60493ae3 2019-06-20 stsp {
2507 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2508 60493ae3 2019-06-20 stsp
2509 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2510 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2511 60493ae3 2019-06-20 stsp return NULL;
2512 60493ae3 2019-06-20 stsp }
2513 60493ae3 2019-06-20 stsp
2514 60493ae3 2019-06-20 stsp static const struct got_error *
2515 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2516 60493ae3 2019-06-20 stsp {
2517 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2518 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2519 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2520 60493ae3 2019-06-20 stsp
2521 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2522 f9686aa5 2020-03-27 stsp show_log_view(view);
2523 f9686aa5 2020-03-27 stsp update_panels();
2524 f9686aa5 2020-03-27 stsp doupdate();
2525 f9686aa5 2020-03-27 stsp
2526 96e2b566 2019-07-08 stsp if (s->search_entry) {
2527 13add988 2019-10-15 stsp int errcode, ch;
2528 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2529 13add988 2019-10-15 stsp if (errcode)
2530 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2531 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2532 13add988 2019-10-15 stsp ch = wgetch(view->window);
2533 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2534 13add988 2019-10-15 stsp if (errcode)
2535 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2536 13add988 2019-10-15 stsp "pthread_mutex_lock");
2537 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2538 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2539 678cbce5 2019-07-28 stsp return NULL;
2540 678cbce5 2019-07-28 stsp }
2541 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2542 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2543 96e2b566 2019-07-08 stsp else
2544 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2545 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2546 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2547 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2548 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2549 364ac6fd 2022-06-18 stsp
2550 364ac6fd 2022-06-18 stsp /*
2551 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2552 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2553 f704b35c 2022-06-18 stsp * might have changed.
2554 364ac6fd 2022-06-18 stsp */
2555 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2556 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2557 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2558 364ac6fd 2022-06-18 stsp else
2559 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2560 4bfe9f0a 2022-06-18 stsp } else {
2561 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2562 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2563 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2564 364ac6fd 2022-06-18 stsp else
2565 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2566 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2567 4bfe9f0a 2022-06-18 stsp }
2568 20be8d96 2019-06-21 stsp } else {
2569 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2570 20be8d96 2019-06-21 stsp }
2571 60493ae3 2019-06-20 stsp
2572 60493ae3 2019-06-20 stsp while (1) {
2573 13add988 2019-10-15 stsp int have_match = 0;
2574 13add988 2019-10-15 stsp
2575 60493ae3 2019-06-20 stsp if (entry == NULL) {
2576 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2577 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2578 f9967bca 2020-03-27 stsp view->search_next_done =
2579 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2580 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2581 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2582 f801134a 2019-06-25 stsp return NULL;
2583 60493ae3 2019-06-20 stsp }
2584 96e2b566 2019-07-08 stsp /*
2585 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2586 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2587 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2588 96e2b566 2019-07-08 stsp */
2589 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2590 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2591 60493ae3 2019-06-20 stsp }
2592 60493ae3 2019-06-20 stsp
2593 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2594 13add988 2019-10-15 stsp &view->regex);
2595 5943eee2 2019-08-13 stsp if (err)
2596 13add988 2019-10-15 stsp break;
2597 13add988 2019-10-15 stsp if (have_match) {
2598 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2599 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2600 60493ae3 2019-06-20 stsp break;
2601 60493ae3 2019-06-20 stsp }
2602 13add988 2019-10-15 stsp
2603 96e2b566 2019-07-08 stsp s->search_entry = entry;
2604 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2605 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2606 b1bf1435 2019-06-21 stsp else
2607 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2608 60493ae3 2019-06-20 stsp }
2609 60493ae3 2019-06-20 stsp
2610 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2611 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2612 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2613 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2614 60493ae3 2019-06-20 stsp if (err)
2615 60493ae3 2019-06-20 stsp return err;
2616 ead14cbe 2019-06-21 stsp cur++;
2617 ead14cbe 2019-06-21 stsp }
2618 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2619 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2620 60493ae3 2019-06-20 stsp if (err)
2621 60493ae3 2019-06-20 stsp return err;
2622 ead14cbe 2019-06-21 stsp cur--;
2623 60493ae3 2019-06-20 stsp }
2624 60493ae3 2019-06-20 stsp }
2625 60493ae3 2019-06-20 stsp
2626 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2627 96e2b566 2019-07-08 stsp
2628 60493ae3 2019-06-20 stsp return NULL;
2629 60493ae3 2019-06-20 stsp }
2630 60493ae3 2019-06-20 stsp
2631 60493ae3 2019-06-20 stsp static const struct got_error *
2632 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2633 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2634 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2635 80ddbec8 2018-04-29 stsp {
2636 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2637 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2638 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2639 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2640 1a76625f 2018-10-22 stsp int errcode;
2641 80ddbec8 2018-04-29 stsp
2642 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2643 f135c941 2020-02-20 stsp free(s->in_repo_path);
2644 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2645 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2646 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2647 f135c941 2020-02-20 stsp }
2648 ecb28ae0 2018-07-16 stsp
2649 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2650 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2651 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2652 78756c87 2020-11-24 stsp
2653 fb2756b9 2018-08-04 stsp s->repo = repo;
2654 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2655 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2656 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2657 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2658 9cd7cbd1 2020-12-07 stsp goto done;
2659 9cd7cbd1 2020-12-07 stsp }
2660 9cd7cbd1 2020-12-07 stsp }
2661 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2662 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2663 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2664 5036bf37 2018-09-24 stsp goto done;
2665 5036bf37 2018-09-24 stsp }
2666 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2667 e5a0f69f 2018-08-18 stsp
2668 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2669 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2670 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2671 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2672 11b20872 2019-11-08 stsp if (err)
2673 11b20872 2019-11-08 stsp goto done;
2674 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2675 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2676 11b20872 2019-11-08 stsp if (err) {
2677 11b20872 2019-11-08 stsp free_colors(&s->colors);
2678 11b20872 2019-11-08 stsp goto done;
2679 11b20872 2019-11-08 stsp }
2680 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2681 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2682 11b20872 2019-11-08 stsp if (err) {
2683 11b20872 2019-11-08 stsp free_colors(&s->colors);
2684 11b20872 2019-11-08 stsp goto done;
2685 11b20872 2019-11-08 stsp }
2686 11b20872 2019-11-08 stsp }
2687 11b20872 2019-11-08 stsp
2688 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2689 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2690 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2691 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2692 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2693 1a76625f 2018-10-22 stsp
2694 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2695 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2696 74467cc8 2022-06-15 stsp if (err)
2697 74467cc8 2022-06-15 stsp goto done;
2698 74467cc8 2022-06-15 stsp }
2699 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2700 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2701 0ae84acc 2022-06-15 tracey if (err)
2702 0ae84acc 2022-06-15 tracey goto done;
2703 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2704 b672a97a 2020-01-27 stsp !s->log_branches);
2705 1a76625f 2018-10-22 stsp if (err)
2706 1a76625f 2018-10-22 stsp goto done;
2707 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2708 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2709 c5b78334 2020-01-12 stsp if (err)
2710 c5b78334 2020-01-12 stsp goto done;
2711 1a76625f 2018-10-22 stsp
2712 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2713 1a76625f 2018-10-22 stsp if (errcode) {
2714 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2715 1a76625f 2018-10-22 stsp goto done;
2716 1a76625f 2018-10-22 stsp }
2717 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2718 7c1452c1 2020-03-26 stsp if (errcode) {
2719 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2720 7c1452c1 2020-03-26 stsp goto done;
2721 7c1452c1 2020-03-26 stsp }
2722 1a76625f 2018-10-22 stsp
2723 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2724 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2725 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2726 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2727 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2728 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2729 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2730 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2731 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2732 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2733 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2734 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2735 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2736 ba4f502b 2018-08-04 stsp done:
2737 1a76625f 2018-10-22 stsp if (err)
2738 1a76625f 2018-10-22 stsp close_log_view(view);
2739 ba4f502b 2018-08-04 stsp return err;
2740 ba4f502b 2018-08-04 stsp }
2741 ba4f502b 2018-08-04 stsp
2742 e5a0f69f 2018-08-18 stsp static const struct got_error *
2743 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2744 ba4f502b 2018-08-04 stsp {
2745 f2f6d207 2020-11-24 stsp const struct got_error *err;
2746 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2747 ba4f502b 2018-08-04 stsp
2748 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2749 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2750 2b380cc8 2018-10-24 stsp &s->thread_args);
2751 2b380cc8 2018-10-24 stsp if (errcode)
2752 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2753 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2754 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2755 f2f6d207 2020-11-24 stsp if (err)
2756 f2f6d207 2020-11-24 stsp return err;
2757 f2f6d207 2020-11-24 stsp }
2758 2b380cc8 2018-10-24 stsp }
2759 2b380cc8 2018-10-24 stsp
2760 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2761 b880cc75 2022-06-30 mark }
2762 b880cc75 2022-06-30 mark
2763 b880cc75 2022-06-30 mark static void
2764 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2765 b880cc75 2022-06-30 mark {
2766 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2767 b880cc75 2022-06-30 mark
2768 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2769 b880cc75 2022-06-30 mark view->count = 0;
2770 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2771 b880cc75 2022-06-30 mark return;
2772 b880cc75 2022-06-30 mark
2773 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2774 b880cc75 2022-06-30 mark || home)
2775 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2776 b880cc75 2022-06-30 mark
2777 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2778 b880cc75 2022-06-30 mark --s->selected;
2779 b880cc75 2022-06-30 mark else
2780 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2781 b880cc75 2022-06-30 mark
2782 b880cc75 2022-06-30 mark select_commit(s);
2783 b880cc75 2022-06-30 mark return;
2784 b880cc75 2022-06-30 mark }
2785 b880cc75 2022-06-30 mark
2786 b880cc75 2022-06-30 mark static const struct got_error *
2787 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2788 b880cc75 2022-06-30 mark {
2789 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2790 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2791 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2792 b880cc75 2022-06-30 mark
2793 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2794 b880cc75 2022-06-30 mark if (first == NULL) {
2795 b880cc75 2022-06-30 mark view->count = 0;
2796 b880cc75 2022-06-30 mark return NULL;
2797 b880cc75 2022-06-30 mark }
2798 b880cc75 2022-06-30 mark
2799 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2800 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2801 b880cc75 2022-06-30 mark return NULL;
2802 b880cc75 2022-06-30 mark
2803 b880cc75 2022-06-30 mark if (!page) {
2804 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2805 b880cc75 2022-06-30 mark
2806 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2807 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2808 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2809 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2810 b880cc75 2022-06-30 mark ++s->selected;
2811 b880cc75 2022-06-30 mark else
2812 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2813 b880cc75 2022-06-30 mark } else if (s->thread_args.log_complete) {
2814 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2815 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2816 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2817 b880cc75 2022-06-30 mark else
2818 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2819 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2820 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2821 b880cc75 2022-06-30 mark } else {
2822 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2823 b880cc75 2022-06-30 mark if (err)
2824 b880cc75 2022-06-30 mark return err;
2825 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2826 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2827 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2828 b880cc75 2022-06-30 mark }
2829 b880cc75 2022-06-30 mark }
2830 b880cc75 2022-06-30 mark if (err)
2831 b880cc75 2022-06-30 mark return err;
2832 b880cc75 2022-06-30 mark
2833 9b058f45 2022-06-30 mark /*
2834 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2835 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2836 9b058f45 2022-06-30 mark */
2837 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2838 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2839 9b058f45 2022-06-30 mark
2840 b880cc75 2022-06-30 mark select_commit(s);
2841 b880cc75 2022-06-30 mark
2842 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2843 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2844 b880cc75 2022-06-30 mark view->count = 0;
2845 b880cc75 2022-06-30 mark
2846 b880cc75 2022-06-30 mark return NULL;
2847 e5a0f69f 2018-08-18 stsp }
2848 04cc582a 2018-08-01 stsp
2849 9b058f45 2022-06-30 mark /*
2850 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2851 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2852 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2853 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2854 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2855 9b058f45 2022-06-30 mark */
2856 9b058f45 2022-06-30 mark static void
2857 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2858 9b058f45 2022-06-30 mark {
2859 9b058f45 2022-06-30 mark char *mode;
2860 9b058f45 2022-06-30 mark
2861 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2862 9b058f45 2022-06-30 mark
2863 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2864 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2865 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2866 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2867 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2868 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2869 9b058f45 2022-06-30 mark }
2870 9b058f45 2022-06-30 mark }
2871 9b058f45 2022-06-30 mark
2872 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2873 e5a0f69f 2018-08-18 stsp static const struct got_error *
2874 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2875 9b058f45 2022-06-30 mark {
2876 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2877 9b058f45 2022-06-30 mark
2878 9b058f45 2022-06-30 mark view->nlines = y;
2879 9b058f45 2022-06-30 mark err = view_resize(view);
2880 9b058f45 2022-06-30 mark if (err)
2881 9b058f45 2022-06-30 mark return err;
2882 9b058f45 2022-06-30 mark
2883 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2884 9b058f45 2022-06-30 mark
2885 9b058f45 2022-06-30 mark return err;
2886 9b058f45 2022-06-30 mark }
2887 9b058f45 2022-06-30 mark
2888 9b058f45 2022-06-30 mark static const struct got_error *
2889 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2890 e5a0f69f 2018-08-18 stsp {
2891 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2892 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2893 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2894 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2895 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2896 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0, n, nscroll = view->nlines - 1;
2897 80ddbec8 2018-04-29 stsp
2898 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2899 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2900 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2901 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2902 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2903 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2904 fb280deb 2021-08-30 stsp }
2905 b880cc75 2022-06-30 mark return err;
2906 528dedf3 2021-08-30 stsp }
2907 528dedf3 2021-08-30 stsp
2908 528dedf3 2021-08-30 stsp switch (ch) {
2909 1e37a5c2 2019-05-12 jcs case 'q':
2910 1e37a5c2 2019-05-12 jcs s->quit = 1;
2911 145b6838 2022-06-16 stsp break;
2912 145b6838 2022-06-16 stsp case '0':
2913 145b6838 2022-06-16 stsp view->x = 0;
2914 145b6838 2022-06-16 stsp break;
2915 145b6838 2022-06-16 stsp case '$':
2916 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2917 640cd7ff 2022-06-22 mark view->count = 0;
2918 145b6838 2022-06-16 stsp break;
2919 145b6838 2022-06-16 stsp case KEY_RIGHT:
2920 145b6838 2022-06-16 stsp case 'l':
2921 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2922 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2923 640cd7ff 2022-06-22 mark else
2924 640cd7ff 2022-06-22 mark view->count = 0;
2925 1e37a5c2 2019-05-12 jcs break;
2926 145b6838 2022-06-16 stsp case KEY_LEFT:
2927 145b6838 2022-06-16 stsp case 'h':
2928 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2929 640cd7ff 2022-06-22 mark if (view->x <= 0)
2930 640cd7ff 2022-06-22 mark view->count = 0;
2931 145b6838 2022-06-16 stsp break;
2932 1e37a5c2 2019-05-12 jcs case 'k':
2933 1e37a5c2 2019-05-12 jcs case KEY_UP:
2934 1e37a5c2 2019-05-12 jcs case '<':
2935 1e37a5c2 2019-05-12 jcs case ',':
2936 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2937 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2938 912a3f79 2021-08-30 j break;
2939 912a3f79 2021-08-30 j case 'g':
2940 912a3f79 2021-08-30 j case KEY_HOME:
2941 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2942 640cd7ff 2022-06-22 mark view->count = 0;
2943 1e37a5c2 2019-05-12 jcs break;
2944 83cc4199 2022-06-13 stsp case CTRL('u'):
2945 33c3719a 2022-06-15 stsp case 'u':
2946 83cc4199 2022-06-13 stsp nscroll /= 2;
2947 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2948 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2949 a4292ac5 2019-05-12 jcs case CTRL('b'):
2950 61417565 2022-06-20 mark case 'b':
2951 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2952 1e37a5c2 2019-05-12 jcs break;
2953 1e37a5c2 2019-05-12 jcs case 'j':
2954 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2955 1e37a5c2 2019-05-12 jcs case '>':
2956 1e37a5c2 2019-05-12 jcs case '.':
2957 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2958 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2959 912a3f79 2021-08-30 j break;
2960 912a3f79 2021-08-30 j case 'G':
2961 912a3f79 2021-08-30 j case KEY_END: {
2962 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2963 912a3f79 2021-08-30 j * traverse them all. */
2964 640cd7ff 2022-06-22 mark view->count = 0;
2965 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2966 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2967 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2968 80ddbec8 2018-04-29 stsp }
2969 912a3f79 2021-08-30 j
2970 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2971 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2972 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2973 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2974 f3bc9f1d 2021-09-05 naddy break;
2975 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2976 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2977 f3bc9f1d 2021-09-05 naddy }
2978 f3bc9f1d 2021-09-05 naddy if (n > 0)
2979 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2980 2b779855 2020-12-05 naddy select_commit(s);
2981 1e37a5c2 2019-05-12 jcs break;
2982 912a3f79 2021-08-30 j }
2983 80b7e8da 2022-06-11 stsp case CTRL('d'):
2984 33c3719a 2022-06-15 stsp case 'd':
2985 83cc4199 2022-06-13 stsp nscroll /= 2;
2986 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2987 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2988 61417565 2022-06-20 mark case CTRL('f'):
2989 48bb96f0 2022-06-20 naddy case 'f':
2990 b880cc75 2022-06-30 mark case ' ':
2991 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
2992 1e37a5c2 2019-05-12 jcs break;
2993 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2994 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2995 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2996 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2997 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2998 2b779855 2020-12-05 naddy select_commit(s);
2999 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3000 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3001 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3002 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3003 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3004 0bf7f153 2020-12-02 naddy }
3005 1e37a5c2 2019-05-12 jcs break;
3006 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3007 9b058f45 2022-06-30 mark case '\r': {
3008 640cd7ff 2022-06-22 mark view->count = 0;
3009 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3010 e5a0f69f 2018-08-18 stsp break;
3011 9b058f45 2022-06-30 mark
3012 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3013 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3014 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3015 9b058f45 2022-06-30 mark
3016 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3017 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3018 78756c87 2020-11-24 stsp view, s->repo);
3019 1e37a5c2 2019-05-12 jcs if (err)
3020 1e37a5c2 2019-05-12 jcs break;
3021 9b058f45 2022-06-30 mark
3022 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3023 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3024 9b058f45 2022-06-30 mark if (err)
3025 9b058f45 2022-06-30 mark break;
3026 9b058f45 2022-06-30 mark }
3027 9b058f45 2022-06-30 mark
3028 e78dc838 2020-12-04 stsp view->focussed = 0;
3029 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3030 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3031 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3032 9b058f45 2022-06-30 mark
3033 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3034 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3035 f7013a22 2018-10-24 stsp if (err)
3036 1e37a5c2 2019-05-12 jcs return err;
3037 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3038 0dbbbe90 2022-06-17 op if (err)
3039 0dbbbe90 2022-06-17 op return err;
3040 e78dc838 2020-12-04 stsp view->focus_child = 1;
3041 1e37a5c2 2019-05-12 jcs } else
3042 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3043 1e37a5c2 2019-05-12 jcs break;
3044 9b058f45 2022-06-30 mark }
3045 1e37a5c2 2019-05-12 jcs case 't':
3046 640cd7ff 2022-06-22 mark view->count = 0;
3047 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3048 5036bf37 2018-09-24 stsp break;
3049 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3050 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3051 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3052 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3053 4e97c21c 2020-12-06 stsp s->repo);
3054 1e37a5c2 2019-05-12 jcs if (err)
3055 e5a0f69f 2018-08-18 stsp break;
3056 e78dc838 2020-12-04 stsp view->focussed = 0;
3057 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3058 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3059 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3060 1e37a5c2 2019-05-12 jcs if (err)
3061 1e37a5c2 2019-05-12 jcs return err;
3062 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3063 0dbbbe90 2022-06-17 op if (err)
3064 0dbbbe90 2022-06-17 op return err;
3065 e78dc838 2020-12-04 stsp view->focus_child = 1;
3066 1e37a5c2 2019-05-12 jcs } else
3067 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3068 1e37a5c2 2019-05-12 jcs break;
3069 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3070 21355643 2020-12-06 stsp case CTRL('l'):
3071 21355643 2020-12-06 stsp case 'B':
3072 640cd7ff 2022-06-22 mark view->count = 0;
3073 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3074 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3075 1e37a5c2 2019-05-12 jcs break;
3076 21355643 2020-12-06 stsp err = stop_log_thread(s);
3077 74cfe85e 2020-10-20 stsp if (err)
3078 74cfe85e 2020-10-20 stsp return err;
3079 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3080 21355643 2020-12-06 stsp char *parent_path;
3081 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3082 21355643 2020-12-06 stsp if (err)
3083 21355643 2020-12-06 stsp return err;
3084 21355643 2020-12-06 stsp free(s->in_repo_path);
3085 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3086 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3087 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3088 21355643 2020-12-06 stsp struct got_object_id *start_id;
3089 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3090 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3091 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3092 21355643 2020-12-06 stsp if (err)
3093 21355643 2020-12-06 stsp return err;
3094 21355643 2020-12-06 stsp free(s->start_id);
3095 21355643 2020-12-06 stsp s->start_id = start_id;
3096 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3097 21355643 2020-12-06 stsp } else /* 'B' */
3098 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3099 21355643 2020-12-06 stsp
3100 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3101 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3102 b0dd8d27 2022-06-16 stsp if (err)
3103 b0dd8d27 2022-06-16 stsp return err;
3104 b0dd8d27 2022-06-16 stsp }
3105 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3106 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3107 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3108 74cfe85e 2020-10-20 stsp if (err)
3109 21355643 2020-12-06 stsp return err;
3110 51a10b52 2020-12-26 stsp tog_free_refs();
3111 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3112 ca51c541 2020-12-07 stsp if (err)
3113 ca51c541 2020-12-07 stsp return err;
3114 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3115 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3116 d01904d4 2019-06-25 stsp if (err)
3117 d01904d4 2019-06-25 stsp return err;
3118 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3119 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3120 b672a97a 2020-01-27 stsp if (err)
3121 b672a97a 2020-01-27 stsp return err;
3122 21355643 2020-12-06 stsp free_commits(&s->commits);
3123 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3124 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3125 21355643 2020-12-06 stsp s->selected_entry = NULL;
3126 21355643 2020-12-06 stsp s->selected = 0;
3127 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3128 21355643 2020-12-06 stsp s->quit = 0;
3129 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3130 dfee752e 2022-06-18 op s->matched_entry = NULL;
3131 dfee752e 2022-06-18 op s->search_entry = NULL;
3132 d01904d4 2019-06-25 stsp break;
3133 6458efa5 2020-11-24 stsp case 'r':
3134 640cd7ff 2022-06-22 mark view->count = 0;
3135 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3136 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3137 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3138 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3139 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3140 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3141 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3142 6458efa5 2020-11-24 stsp if (err) {
3143 6458efa5 2020-11-24 stsp view_close(ref_view);
3144 6458efa5 2020-11-24 stsp return err;
3145 6458efa5 2020-11-24 stsp }
3146 e78dc838 2020-12-04 stsp view->focussed = 0;
3147 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3148 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3149 6458efa5 2020-11-24 stsp err = view_close_child(view);
3150 6458efa5 2020-11-24 stsp if (err)
3151 6458efa5 2020-11-24 stsp return err;
3152 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3153 0dbbbe90 2022-06-17 op if (err)
3154 0dbbbe90 2022-06-17 op return err;
3155 e78dc838 2020-12-04 stsp view->focus_child = 1;
3156 6458efa5 2020-11-24 stsp } else
3157 6458efa5 2020-11-24 stsp *new_view = ref_view;
3158 6458efa5 2020-11-24 stsp break;
3159 1e37a5c2 2019-05-12 jcs default:
3160 640cd7ff 2022-06-22 mark view->count = 0;
3161 1e37a5c2 2019-05-12 jcs break;
3162 899d86c2 2018-05-10 stsp }
3163 e5a0f69f 2018-08-18 stsp
3164 80ddbec8 2018-04-29 stsp return err;
3165 80ddbec8 2018-04-29 stsp }
3166 80ddbec8 2018-04-29 stsp
3167 4ed7e80c 2018-05-20 stsp static const struct got_error *
3168 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3169 c2db6724 2019-01-04 stsp {
3170 c2db6724 2019-01-04 stsp const struct got_error *error;
3171 c2db6724 2019-01-04 stsp
3172 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3173 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3174 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3175 37c06ea4 2019-07-15 stsp #endif
3176 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3177 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3178 c2db6724 2019-01-04 stsp
3179 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3180 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3181 c2db6724 2019-01-04 stsp
3182 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3183 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3184 c2db6724 2019-01-04 stsp
3185 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3186 c2db6724 2019-01-04 stsp if (error != NULL)
3187 c2db6724 2019-01-04 stsp return error;
3188 c2db6724 2019-01-04 stsp
3189 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3190 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3191 c2db6724 2019-01-04 stsp
3192 c2db6724 2019-01-04 stsp return NULL;
3193 c2db6724 2019-01-04 stsp }
3194 c2db6724 2019-01-04 stsp
3195 a915003a 2019-02-05 stsp static void
3196 a915003a 2019-02-05 stsp init_curses(void)
3197 a915003a 2019-02-05 stsp {
3198 2497f032 2022-05-31 stsp /*
3199 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3200 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3201 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3202 2497f032 2022-05-31 stsp */
3203 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3204 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3205 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3206 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3207 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3208 2497f032 2022-05-31 stsp
3209 a915003a 2019-02-05 stsp initscr();
3210 a915003a 2019-02-05 stsp cbreak();
3211 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3212 a915003a 2019-02-05 stsp noecho();
3213 a915003a 2019-02-05 stsp nonl();
3214 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3215 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3216 a915003a 2019-02-05 stsp curs_set(0);
3217 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3218 6d17833f 2019-11-08 stsp start_color();
3219 6d17833f 2019-11-08 stsp use_default_colors();
3220 6d17833f 2019-11-08 stsp }
3221 a915003a 2019-02-05 stsp }
3222 a915003a 2019-02-05 stsp
3223 c2db6724 2019-01-04 stsp static const struct got_error *
3224 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3225 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3226 f135c941 2020-02-20 stsp {
3227 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3228 f135c941 2020-02-20 stsp
3229 f135c941 2020-02-20 stsp if (argc == 0) {
3230 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3231 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3232 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3233 f135c941 2020-02-20 stsp return NULL;
3234 f135c941 2020-02-20 stsp }
3235 f135c941 2020-02-20 stsp
3236 f135c941 2020-02-20 stsp if (worktree) {
3237 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3238 bfd61697 2020-11-14 stsp char *p;
3239 f135c941 2020-02-20 stsp
3240 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3241 f135c941 2020-02-20 stsp if (err)
3242 f135c941 2020-02-20 stsp return err;
3243 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3244 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3245 bfd61697 2020-11-14 stsp p) == -1) {
3246 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3247 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3248 f135c941 2020-02-20 stsp }
3249 f135c941 2020-02-20 stsp free(p);
3250 f135c941 2020-02-20 stsp } else
3251 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3252 f135c941 2020-02-20 stsp
3253 f135c941 2020-02-20 stsp return err;
3254 f135c941 2020-02-20 stsp }
3255 f135c941 2020-02-20 stsp
3256 f135c941 2020-02-20 stsp static const struct got_error *
3257 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3258 9f7d7167 2018-04-29 stsp {
3259 80ddbec8 2018-04-29 stsp const struct got_error *error;
3260 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3261 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3262 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3263 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3264 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3265 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3266 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3267 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3268 04cc582a 2018-08-01 stsp struct tog_view *view;
3269 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3270 80ddbec8 2018-04-29 stsp
3271 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3272 80ddbec8 2018-04-29 stsp switch (ch) {
3273 b672a97a 2020-01-27 stsp case 'b':
3274 b672a97a 2020-01-27 stsp log_branches = 1;
3275 b672a97a 2020-01-27 stsp break;
3276 80ddbec8 2018-04-29 stsp case 'c':
3277 80ddbec8 2018-04-29 stsp start_commit = optarg;
3278 80ddbec8 2018-04-29 stsp break;
3279 ecb28ae0 2018-07-16 stsp case 'r':
3280 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3281 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3282 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3283 9ba1d308 2019-10-21 stsp optarg);
3284 ecb28ae0 2018-07-16 stsp break;
3285 80ddbec8 2018-04-29 stsp default:
3286 17020d27 2019-03-07 stsp usage_log();
3287 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3288 80ddbec8 2018-04-29 stsp }
3289 80ddbec8 2018-04-29 stsp }
3290 80ddbec8 2018-04-29 stsp
3291 80ddbec8 2018-04-29 stsp argc -= optind;
3292 80ddbec8 2018-04-29 stsp argv += optind;
3293 80ddbec8 2018-04-29 stsp
3294 f135c941 2020-02-20 stsp if (argc > 1)
3295 f135c941 2020-02-20 stsp usage_log();
3296 963f97a1 2019-03-18 stsp
3297 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3298 0ae84acc 2022-06-15 tracey if (error != NULL)
3299 0ae84acc 2022-06-15 tracey goto done;
3300 0ae84acc 2022-06-15 tracey
3301 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3302 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3303 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3304 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3305 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3306 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3307 c156c7a4 2020-12-18 stsp goto done;
3308 a1fbf39a 2019-08-11 stsp if (worktree)
3309 6962eb72 2020-02-20 stsp repo_path =
3310 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3311 a1fbf39a 2019-08-11 stsp else
3312 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3313 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3314 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3315 c156c7a4 2020-12-18 stsp goto done;
3316 c156c7a4 2020-12-18 stsp }
3317 ecb28ae0 2018-07-16 stsp }
3318 ecb28ae0 2018-07-16 stsp
3319 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3320 80ddbec8 2018-04-29 stsp if (error != NULL)
3321 ecb28ae0 2018-07-16 stsp goto done;
3322 80ddbec8 2018-04-29 stsp
3323 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3324 f135c941 2020-02-20 stsp repo, worktree);
3325 f135c941 2020-02-20 stsp if (error)
3326 f135c941 2020-02-20 stsp goto done;
3327 f135c941 2020-02-20 stsp
3328 f135c941 2020-02-20 stsp init_curses();
3329 f135c941 2020-02-20 stsp
3330 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3331 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3332 c02c541e 2019-03-29 stsp if (error)
3333 c02c541e 2019-03-29 stsp goto done;
3334 c02c541e 2019-03-29 stsp
3335 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3336 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3337 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3338 87670572 2020-12-26 naddy if (error)
3339 87670572 2020-12-26 naddy goto done;
3340 87670572 2020-12-26 naddy }
3341 51a10b52 2020-12-26 stsp
3342 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3343 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3344 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3345 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3346 d8f38dc4 2020-12-05 stsp if (error)
3347 d8f38dc4 2020-12-05 stsp goto done;
3348 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3349 d8f38dc4 2020-12-05 stsp } else {
3350 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3351 d8f38dc4 2020-12-05 stsp if (error == NULL)
3352 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3353 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3354 d8f38dc4 2020-12-05 stsp goto done;
3355 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3356 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3357 d8f38dc4 2020-12-05 stsp if (error)
3358 d8f38dc4 2020-12-05 stsp goto done;
3359 d8f38dc4 2020-12-05 stsp }
3360 8b473291 2019-02-21 stsp
3361 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3362 04cc582a 2018-08-01 stsp if (view == NULL) {
3363 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3364 04cc582a 2018-08-01 stsp goto done;
3365 04cc582a 2018-08-01 stsp }
3366 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3367 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3368 ba4f502b 2018-08-04 stsp if (error)
3369 ba4f502b 2018-08-04 stsp goto done;
3370 2fc00ff4 2019-08-31 stsp if (worktree) {
3371 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3372 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3373 2fc00ff4 2019-08-31 stsp worktree = NULL;
3374 2fc00ff4 2019-08-31 stsp }
3375 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3376 ecb28ae0 2018-07-16 stsp done:
3377 f135c941 2020-02-20 stsp free(in_repo_path);
3378 ecb28ae0 2018-07-16 stsp free(repo_path);
3379 ecb28ae0 2018-07-16 stsp free(cwd);
3380 899d86c2 2018-05-10 stsp free(start_id);
3381 d8f38dc4 2020-12-05 stsp free(label);
3382 d8f38dc4 2020-12-05 stsp if (ref)
3383 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3384 1d0f4054 2021-06-17 stsp if (repo) {
3385 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3386 1d0f4054 2021-06-17 stsp if (error == NULL)
3387 1d0f4054 2021-06-17 stsp error = close_err;
3388 1d0f4054 2021-06-17 stsp }
3389 ec142235 2019-03-07 stsp if (worktree)
3390 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3391 0ae84acc 2022-06-15 tracey if (pack_fds) {
3392 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3393 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3394 0ae84acc 2022-06-15 tracey if (error == NULL)
3395 0ae84acc 2022-06-15 tracey error = pack_err;
3396 0ae84acc 2022-06-15 tracey }
3397 51a10b52 2020-12-26 stsp tog_free_refs();
3398 80ddbec8 2018-04-29 stsp return error;
3399 9f7d7167 2018-04-29 stsp }
3400 9f7d7167 2018-04-29 stsp
3401 4ed7e80c 2018-05-20 stsp __dead static void
3402 9f7d7167 2018-04-29 stsp usage_diff(void)
3403 9f7d7167 2018-04-29 stsp {
3404 80ddbec8 2018-04-29 stsp endwin();
3405 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3406 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3407 9f7d7167 2018-04-29 stsp exit(1);
3408 b304db33 2018-05-20 stsp }
3409 b304db33 2018-05-20 stsp
3410 6d17833f 2019-11-08 stsp static int
3411 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3412 41605754 2020-11-12 stsp regmatch_t *regmatch)
3413 6d17833f 2019-11-08 stsp {
3414 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3415 6d17833f 2019-11-08 stsp }
3416 6d17833f 2019-11-08 stsp
3417 336075a4 2022-06-25 op static struct tog_color *
3418 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3419 6d17833f 2019-11-08 stsp {
3420 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3421 6d17833f 2019-11-08 stsp
3422 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3423 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3424 6d17833f 2019-11-08 stsp return tc;
3425 6d17833f 2019-11-08 stsp }
3426 6d17833f 2019-11-08 stsp
3427 6d17833f 2019-11-08 stsp return NULL;
3428 6d17833f 2019-11-08 stsp }
3429 6d17833f 2019-11-08 stsp
3430 4ed7e80c 2018-05-20 stsp static const struct got_error *
3431 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3432 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3433 41605754 2020-11-12 stsp {
3434 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3435 44a87665 2022-06-16 stsp char *exstr = NULL;
3436 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3437 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3438 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3439 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3440 41605754 2020-11-12 stsp
3441 41605754 2020-11-12 stsp *wtotal = 0;
3442 1853e0f4 2022-06-16 stsp
3443 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3444 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3445 41605754 2020-11-12 stsp
3446 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3447 44a87665 2022-06-16 stsp if (err)
3448 44a87665 2022-06-16 stsp return err;
3449 44a87665 2022-06-16 stsp
3450 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3451 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3452 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3453 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3454 44a87665 2022-06-16 stsp goto done;
3455 44a87665 2022-06-16 stsp }
3456 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3457 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3458 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3459 1853e0f4 2022-06-16 stsp goto done;
3460 1853e0f4 2022-06-16 stsp }
3461 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3462 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3463 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3464 1853e0f4 2022-06-16 stsp goto done;
3465 1853e0f4 2022-06-16 stsp }
3466 145b6838 2022-06-16 stsp
3467 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3468 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3469 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3470 1853e0f4 2022-06-16 stsp if (err)
3471 1853e0f4 2022-06-16 stsp goto done;
3472 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3473 145b6838 2022-06-16 stsp if (n) {
3474 1853e0f4 2022-06-16 stsp free(wline);
3475 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3476 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3477 1853e0f4 2022-06-16 stsp if (err)
3478 1853e0f4 2022-06-16 stsp goto done;
3479 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3480 1853e0f4 2022-06-16 stsp wlimit -= width;
3481 1853e0f4 2022-06-16 stsp *wtotal += width;
3482 41605754 2020-11-12 stsp }
3483 41605754 2020-11-12 stsp
3484 41605754 2020-11-12 stsp if (wlimit > 0) {
3485 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3486 1853e0f4 2022-06-16 stsp size_t wlen;
3487 1853e0f4 2022-06-16 stsp
3488 1853e0f4 2022-06-16 stsp free(wline);
3489 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3490 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3491 1853e0f4 2022-06-16 stsp if (err)
3492 1853e0f4 2022-06-16 stsp goto done;
3493 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3494 1853e0f4 2022-06-16 stsp while (i < wlen) {
3495 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3496 1853e0f4 2022-06-16 stsp if (width == -1) {
3497 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3498 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3499 1853e0f4 2022-06-16 stsp goto done;
3500 1853e0f4 2022-06-16 stsp }
3501 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3502 1853e0f4 2022-06-16 stsp break;
3503 61417565 2022-06-20 mark w += width;
3504 1853e0f4 2022-06-16 stsp i++;
3505 41605754 2020-11-12 stsp }
3506 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3507 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3508 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3509 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3510 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3511 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3512 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3513 41605754 2020-11-12 stsp }
3514 41605754 2020-11-12 stsp }
3515 41605754 2020-11-12 stsp
3516 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3517 1853e0f4 2022-06-16 stsp free(wline);
3518 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3519 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3520 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3521 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3522 1853e0f4 2022-06-16 stsp if (err)
3523 1853e0f4 2022-06-16 stsp goto done;
3524 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3525 1853e0f4 2022-06-16 stsp } else {
3526 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3527 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3528 1853e0f4 2022-06-16 stsp if (err)
3529 1853e0f4 2022-06-16 stsp goto done;
3530 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3531 1853e0f4 2022-06-16 stsp }
3532 1853e0f4 2022-06-16 stsp *wtotal += width2;
3533 41605754 2020-11-12 stsp }
3534 1853e0f4 2022-06-16 stsp done:
3535 145b6838 2022-06-16 stsp free(wline);
3536 44a87665 2022-06-16 stsp free(exstr);
3537 1853e0f4 2022-06-16 stsp free(seg0);
3538 1853e0f4 2022-06-16 stsp free(seg1);
3539 1853e0f4 2022-06-16 stsp free(seg2);
3540 1853e0f4 2022-06-16 stsp return err;
3541 41605754 2020-11-12 stsp }
3542 41605754 2020-11-12 stsp
3543 41605754 2020-11-12 stsp static const struct got_error *
3544 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3545 26ed57b2 2018-05-19 stsp {
3546 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3547 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3548 61e69b96 2018-05-20 stsp const struct got_error *err;
3549 fe621944 2020-11-10 stsp int nprinted = 0;
3550 b304db33 2018-05-20 stsp char *line;
3551 826082fe 2020-12-10 stsp size_t linesize = 0;
3552 826082fe 2020-12-10 stsp ssize_t linelen;
3553 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3554 61e69b96 2018-05-20 stsp wchar_t *wline;
3555 e0b650dd 2018-05-20 stsp int width;
3556 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3557 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3558 fe621944 2020-11-10 stsp off_t line_offset;
3559 26ed57b2 2018-05-19 stsp
3560 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3561 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3562 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3563 fe621944 2020-11-10 stsp
3564 f7d12f7e 2018-08-01 stsp werase(view->window);
3565 a3404814 2018-09-02 stsp
3566 a3404814 2018-09-02 stsp if (header) {
3567 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3568 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3569 135a2da0 2020-11-11 stsp header) == -1)
3570 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3571 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3572 ccda2f4d 2022-06-16 stsp 0, 0);
3573 135a2da0 2020-11-11 stsp free(line);
3574 135a2da0 2020-11-11 stsp if (err)
3575 a3404814 2018-09-02 stsp return err;
3576 a3404814 2018-09-02 stsp
3577 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3578 a3404814 2018-09-02 stsp wstandout(view->window);
3579 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3580 e54cc94a 2020-11-11 stsp free(wline);
3581 e54cc94a 2020-11-11 stsp wline = NULL;
3582 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3583 a3404814 2018-09-02 stsp wstandend(view->window);
3584 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3585 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3586 26ed57b2 2018-05-19 stsp
3587 a3404814 2018-09-02 stsp if (max_lines <= 1)
3588 a3404814 2018-09-02 stsp return NULL;
3589 a3404814 2018-09-02 stsp max_lines--;
3590 a3404814 2018-09-02 stsp }
3591 a3404814 2018-09-02 stsp
3592 89f1a395 2020-12-01 naddy s->eof = 0;
3593 145b6838 2022-06-16 stsp view->maxx = 0;
3594 826082fe 2020-12-10 stsp line = NULL;
3595 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3596 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3597 826082fe 2020-12-10 stsp if (linelen == -1) {
3598 826082fe 2020-12-10 stsp if (feof(s->f)) {
3599 826082fe 2020-12-10 stsp s->eof = 1;
3600 826082fe 2020-12-10 stsp break;
3601 826082fe 2020-12-10 stsp }
3602 826082fe 2020-12-10 stsp free(line);
3603 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3604 61e69b96 2018-05-20 stsp }
3605 145b6838 2022-06-16 stsp
3606 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3607 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3608 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3609 1853e0f4 2022-06-16 stsp if (err) {
3610 1853e0f4 2022-06-16 stsp free(line);
3611 1853e0f4 2022-06-16 stsp return err;
3612 1853e0f4 2022-06-16 stsp }
3613 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3614 1853e0f4 2022-06-16 stsp free(wline);
3615 1853e0f4 2022-06-16 stsp wline = NULL;
3616 1853e0f4 2022-06-16 stsp
3617 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3618 f26dddb7 2019-11-08 stsp if (tc)
3619 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3620 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3621 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3622 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3623 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3624 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3625 41605754 2020-11-12 stsp if (err) {
3626 41605754 2020-11-12 stsp free(line);
3627 41605754 2020-11-12 stsp return err;
3628 41605754 2020-11-12 stsp }
3629 41605754 2020-11-12 stsp } else {
3630 969c159c 2022-06-16 stsp int skip;
3631 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3632 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3633 969c159c 2022-06-16 stsp if (err) {
3634 969c159c 2022-06-16 stsp free(line);
3635 969c159c 2022-06-16 stsp return err;
3636 969c159c 2022-06-16 stsp }
3637 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3638 969c159c 2022-06-16 stsp free(wline);
3639 41605754 2020-11-12 stsp wline = NULL;
3640 41605754 2020-11-12 stsp }
3641 f26dddb7 2019-11-08 stsp if (tc)
3642 6d17833f 2019-11-08 stsp wattr_off(view->window,
3643 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3644 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3645 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3646 fe621944 2020-11-10 stsp nprinted++;
3647 826082fe 2020-12-10 stsp }
3648 826082fe 2020-12-10 stsp free(line);
3649 fe621944 2020-11-10 stsp if (nprinted >= 1)
3650 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3651 89f1a395 2020-12-01 naddy (nprinted - 1);
3652 fe621944 2020-11-10 stsp else
3653 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3654 26ed57b2 2018-05-19 stsp
3655 9b058f45 2022-06-30 mark view_border(view);
3656 c3e9aa98 2019-05-13 jcs
3657 89f1a395 2020-12-01 naddy if (s->eof) {
3658 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3659 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3660 c3e9aa98 2019-05-13 jcs nprinted++;
3661 c3e9aa98 2019-05-13 jcs }
3662 c3e9aa98 2019-05-13 jcs
3663 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3664 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3665 c3e9aa98 2019-05-13 jcs if (err) {
3666 c3e9aa98 2019-05-13 jcs return err;
3667 c3e9aa98 2019-05-13 jcs }
3668 26ed57b2 2018-05-19 stsp
3669 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3670 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3671 e54cc94a 2020-11-11 stsp free(wline);
3672 e54cc94a 2020-11-11 stsp wline = NULL;
3673 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3674 c3e9aa98 2019-05-13 jcs }
3675 c3e9aa98 2019-05-13 jcs
3676 26ed57b2 2018-05-19 stsp return NULL;
3677 abd2672a 2018-12-23 stsp }
3678 abd2672a 2018-12-23 stsp
3679 abd2672a 2018-12-23 stsp static char *
3680 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3681 abd2672a 2018-12-23 stsp {
3682 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3683 09867e48 2019-08-13 stsp char *p, *s;
3684 09867e48 2019-08-13 stsp
3685 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3686 09867e48 2019-08-13 stsp if (tm == NULL)
3687 09867e48 2019-08-13 stsp return NULL;
3688 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3689 09867e48 2019-08-13 stsp if (s == NULL)
3690 09867e48 2019-08-13 stsp return NULL;
3691 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3692 abd2672a 2018-12-23 stsp if (p)
3693 abd2672a 2018-12-23 stsp *p = '\0';
3694 abd2672a 2018-12-23 stsp return s;
3695 9f7d7167 2018-04-29 stsp }
3696 9f7d7167 2018-04-29 stsp
3697 4ed7e80c 2018-05-20 stsp static const struct got_error *
3698 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3699 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3700 0208f208 2020-05-05 stsp {
3701 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3702 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3703 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3704 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3705 0208f208 2020-05-05 stsp
3706 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3707 0208f208 2020-05-05 stsp if (qid != NULL) {
3708 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3709 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3710 d7b5a0e8 2022-04-20 stsp &qid->id);
3711 0208f208 2020-05-05 stsp if (err)
3712 0208f208 2020-05-05 stsp return err;
3713 0208f208 2020-05-05 stsp
3714 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3715 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3716 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3717 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3718 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3719 aa8b5dd0 2021-08-01 stsp }
3720 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3721 0208f208 2020-05-05 stsp
3722 0208f208 2020-05-05 stsp }
3723 0208f208 2020-05-05 stsp
3724 0208f208 2020-05-05 stsp if (tree_id1) {
3725 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3726 0208f208 2020-05-05 stsp if (err)
3727 0208f208 2020-05-05 stsp goto done;
3728 0208f208 2020-05-05 stsp }
3729 0208f208 2020-05-05 stsp
3730 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3731 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3732 0208f208 2020-05-05 stsp if (err)
3733 0208f208 2020-05-05 stsp goto done;
3734 0208f208 2020-05-05 stsp
3735 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3736 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3737 0208f208 2020-05-05 stsp done:
3738 0208f208 2020-05-05 stsp if (tree1)
3739 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3740 0208f208 2020-05-05 stsp if (tree2)
3741 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3742 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3743 0208f208 2020-05-05 stsp return err;
3744 0208f208 2020-05-05 stsp }
3745 0208f208 2020-05-05 stsp
3746 0208f208 2020-05-05 stsp static const struct got_error *
3747 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3748 abd2672a 2018-12-23 stsp {
3749 fe621944 2020-11-10 stsp off_t *p;
3750 fe621944 2020-11-10 stsp
3751 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3752 fe621944 2020-11-10 stsp if (p == NULL)
3753 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3754 fe621944 2020-11-10 stsp *line_offsets = p;
3755 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3756 fe621944 2020-11-10 stsp (*nlines)++;
3757 fe621944 2020-11-10 stsp return NULL;
3758 fe621944 2020-11-10 stsp }
3759 fe621944 2020-11-10 stsp
3760 fe621944 2020-11-10 stsp static const struct got_error *
3761 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3762 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3763 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3764 fe621944 2020-11-10 stsp {
3765 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3766 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3767 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3768 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3769 45d799e2 2018-12-23 stsp time_t committer_time;
3770 45d799e2 2018-12-23 stsp const char *author, *committer;
3771 8b473291 2019-02-21 stsp char *refs_str = NULL;
3772 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3773 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3774 fe621944 2020-11-10 stsp off_t outoff = 0;
3775 fe621944 2020-11-10 stsp int n;
3776 abd2672a 2018-12-23 stsp
3777 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3778 0208f208 2020-05-05 stsp
3779 8b473291 2019-02-21 stsp if (refs) {
3780 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3781 8b473291 2019-02-21 stsp if (err)
3782 8b473291 2019-02-21 stsp return err;
3783 8b473291 2019-02-21 stsp }
3784 8b473291 2019-02-21 stsp
3785 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3786 abd2672a 2018-12-23 stsp if (err)
3787 abd2672a 2018-12-23 stsp return err;
3788 abd2672a 2018-12-23 stsp
3789 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3790 15a94983 2018-12-23 stsp if (err) {
3791 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3792 15a94983 2018-12-23 stsp goto done;
3793 15a94983 2018-12-23 stsp }
3794 abd2672a 2018-12-23 stsp
3795 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3796 fe621944 2020-11-10 stsp if (err)
3797 fe621944 2020-11-10 stsp goto done;
3798 fe621944 2020-11-10 stsp
3799 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3800 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3801 fe621944 2020-11-10 stsp if (n < 0) {
3802 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3803 abd2672a 2018-12-23 stsp goto done;
3804 abd2672a 2018-12-23 stsp }
3805 fe621944 2020-11-10 stsp outoff += n;
3806 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3807 fe621944 2020-11-10 stsp if (err)
3808 fe621944 2020-11-10 stsp goto done;
3809 fe621944 2020-11-10 stsp
3810 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3811 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3812 fe621944 2020-11-10 stsp if (n < 0) {
3813 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3814 abd2672a 2018-12-23 stsp goto done;
3815 abd2672a 2018-12-23 stsp }
3816 fe621944 2020-11-10 stsp outoff += n;
3817 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3818 fe621944 2020-11-10 stsp if (err)
3819 fe621944 2020-11-10 stsp goto done;
3820 fe621944 2020-11-10 stsp
3821 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3822 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3823 fe621944 2020-11-10 stsp if (datestr) {
3824 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3825 fe621944 2020-11-10 stsp if (n < 0) {
3826 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3827 fe621944 2020-11-10 stsp goto done;
3828 fe621944 2020-11-10 stsp }
3829 fe621944 2020-11-10 stsp outoff += n;
3830 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3831 fe621944 2020-11-10 stsp if (err)
3832 fe621944 2020-11-10 stsp goto done;
3833 abd2672a 2018-12-23 stsp }
3834 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3835 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3836 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3837 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3838 fe621944 2020-11-10 stsp if (n < 0) {
3839 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3840 fe621944 2020-11-10 stsp goto done;
3841 fe621944 2020-11-10 stsp }
3842 fe621944 2020-11-10 stsp outoff += n;
3843 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3844 fe621944 2020-11-10 stsp if (err)
3845 fe621944 2020-11-10 stsp goto done;
3846 abd2672a 2018-12-23 stsp }
3847 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3848 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3849 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3850 9f98ca05 2021-09-24 stsp int pn = 1;
3851 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3852 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3853 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3854 9f98ca05 2021-09-24 stsp if (err)
3855 9f98ca05 2021-09-24 stsp goto done;
3856 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3857 9f98ca05 2021-09-24 stsp if (n < 0) {
3858 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3859 9f98ca05 2021-09-24 stsp goto done;
3860 9f98ca05 2021-09-24 stsp }
3861 9f98ca05 2021-09-24 stsp outoff += n;
3862 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3863 9f98ca05 2021-09-24 stsp if (err)
3864 9f98ca05 2021-09-24 stsp goto done;
3865 9f98ca05 2021-09-24 stsp free(id_str);
3866 9f98ca05 2021-09-24 stsp id_str = NULL;
3867 9f98ca05 2021-09-24 stsp }
3868 9f98ca05 2021-09-24 stsp }
3869 9f98ca05 2021-09-24 stsp
3870 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3871 5943eee2 2019-08-13 stsp if (err)
3872 5943eee2 2019-08-13 stsp goto done;
3873 fe621944 2020-11-10 stsp s = logmsg;
3874 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3875 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3876 fe621944 2020-11-10 stsp if (n < 0) {
3877 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3878 fe621944 2020-11-10 stsp goto done;
3879 fe621944 2020-11-10 stsp }
3880 fe621944 2020-11-10 stsp outoff += n;
3881 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3882 fe621944 2020-11-10 stsp if (err)
3883 fe621944 2020-11-10 stsp goto done;
3884 abd2672a 2018-12-23 stsp }
3885 fe621944 2020-11-10 stsp
3886 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3887 0208f208 2020-05-05 stsp if (err)
3888 0208f208 2020-05-05 stsp goto done;
3889 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3890 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3891 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3892 fe621944 2020-11-10 stsp if (n < 0) {
3893 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3894 fe621944 2020-11-10 stsp goto done;
3895 fe621944 2020-11-10 stsp }
3896 fe621944 2020-11-10 stsp outoff += n;
3897 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3898 fe621944 2020-11-10 stsp if (err)
3899 fe621944 2020-11-10 stsp goto done;
3900 0208f208 2020-05-05 stsp free((char *)pe->path);
3901 0208f208 2020-05-05 stsp free(pe->data);
3902 0208f208 2020-05-05 stsp }
3903 fe621944 2020-11-10 stsp
3904 0208f208 2020-05-05 stsp fputc('\n', outfile);
3905 fe621944 2020-11-10 stsp outoff++;
3906 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3907 abd2672a 2018-12-23 stsp done:
3908 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3909 abd2672a 2018-12-23 stsp free(id_str);
3910 5943eee2 2019-08-13 stsp free(logmsg);
3911 8b473291 2019-02-21 stsp free(refs_str);
3912 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3913 7510f233 2020-08-09 stsp if (err) {
3914 ae6a6978 2020-08-09 stsp free(*line_offsets);
3915 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3916 ae6a6978 2020-08-09 stsp *nlines = 0;
3917 7510f233 2020-08-09 stsp }
3918 fe621944 2020-11-10 stsp return err;
3919 abd2672a 2018-12-23 stsp }
3920 abd2672a 2018-12-23 stsp
3921 abd2672a 2018-12-23 stsp static const struct got_error *
3922 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3923 26ed57b2 2018-05-19 stsp {
3924 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3925 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3926 15a94983 2018-12-23 stsp int obj_type;
3927 fe621944 2020-11-10 stsp
3928 fe621944 2020-11-10 stsp free(s->line_offsets);
3929 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3930 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3931 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3932 fe621944 2020-11-10 stsp s->nlines = 0;
3933 26ed57b2 2018-05-19 stsp
3934 511a516b 2018-05-19 stsp f = got_opentemp();
3935 48ae06ee 2018-10-18 stsp if (f == NULL) {
3936 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3937 48ae06ee 2018-10-18 stsp goto done;
3938 48ae06ee 2018-10-18 stsp }
3939 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3940 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3941 fb43ecf1 2019-02-11 stsp goto done;
3942 fb43ecf1 2019-02-11 stsp }
3943 48ae06ee 2018-10-18 stsp s->f = f;
3944 26ed57b2 2018-05-19 stsp
3945 15a94983 2018-12-23 stsp if (s->id1)
3946 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3947 15a94983 2018-12-23 stsp else
3948 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3949 15a94983 2018-12-23 stsp if (err)
3950 15a94983 2018-12-23 stsp goto done;
3951 15a94983 2018-12-23 stsp
3952 15a94983 2018-12-23 stsp switch (obj_type) {
3953 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3954 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3955 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3956 f9d37699 2022-06-28 stsp s->label1, s->label2, s->diff_context,
3957 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3958 26ed57b2 2018-05-19 stsp break;
3959 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3960 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3961 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3962 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3963 f9d37699 2022-06-28 stsp s->repo, s->f);
3964 26ed57b2 2018-05-19 stsp break;
3965 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3966 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3967 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3968 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3969 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3970 abd2672a 2018-12-23 stsp
3971 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3972 abd2672a 2018-12-23 stsp if (err)
3973 3ffacbe1 2020-02-02 tracey goto done;
3974 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3975 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3976 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3977 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3978 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3979 f44b1f58 2020-02-02 tracey if (err)
3980 f44b1f58 2020-02-02 tracey goto done;
3981 f44b1f58 2020-02-02 tracey } else {
3982 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3983 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3984 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3985 fe621944 2020-11-10 stsp err = write_commit_info(
3986 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3987 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3988 f44b1f58 2020-02-02 tracey if (err)
3989 f44b1f58 2020-02-02 tracey goto done;
3990 f5404e4e 2020-02-02 tracey break;
3991 15a087fe 2019-02-21 stsp }
3992 abd2672a 2018-12-23 stsp }
3993 abd2672a 2018-12-23 stsp }
3994 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3995 abd2672a 2018-12-23 stsp
3996 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3997 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
3998 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3999 f9d37699 2022-06-28 stsp s->repo, s->f);
4000 26ed57b2 2018-05-19 stsp break;
4001 abd2672a 2018-12-23 stsp }
4002 26ed57b2 2018-05-19 stsp default:
4003 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4004 48ae06ee 2018-10-18 stsp break;
4005 26ed57b2 2018-05-19 stsp }
4006 f44b1f58 2020-02-02 tracey if (err)
4007 f44b1f58 2020-02-02 tracey goto done;
4008 48ae06ee 2018-10-18 stsp done:
4009 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4010 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4011 48ae06ee 2018-10-18 stsp return err;
4012 48ae06ee 2018-10-18 stsp }
4013 26ed57b2 2018-05-19 stsp
4014 f5215bb9 2019-02-22 stsp static void
4015 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4016 f5215bb9 2019-02-22 stsp {
4017 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4018 f5215bb9 2019-02-22 stsp update_panels();
4019 f5215bb9 2019-02-22 stsp doupdate();
4020 f44b1f58 2020-02-02 tracey }
4021 f44b1f58 2020-02-02 tracey
4022 f44b1f58 2020-02-02 tracey static const struct got_error *
4023 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4024 f44b1f58 2020-02-02 tracey {
4025 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4026 f44b1f58 2020-02-02 tracey
4027 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4028 f44b1f58 2020-02-02 tracey return NULL;
4029 f44b1f58 2020-02-02 tracey }
4030 f44b1f58 2020-02-02 tracey
4031 f44b1f58 2020-02-02 tracey static const struct got_error *
4032 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4033 f44b1f58 2020-02-02 tracey {
4034 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4035 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4036 f44b1f58 2020-02-02 tracey int lineno;
4037 cb713507 2022-06-16 stsp char *line = NULL;
4038 826082fe 2020-12-10 stsp size_t linesize = 0;
4039 826082fe 2020-12-10 stsp ssize_t linelen;
4040 f44b1f58 2020-02-02 tracey
4041 f44b1f58 2020-02-02 tracey if (!view->searching) {
4042 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4043 f44b1f58 2020-02-02 tracey return NULL;
4044 f44b1f58 2020-02-02 tracey }
4045 f44b1f58 2020-02-02 tracey
4046 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4047 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4048 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4049 f44b1f58 2020-02-02 tracey else
4050 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4051 487cd7d2 2021-12-17 stsp } else
4052 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4053 f44b1f58 2020-02-02 tracey
4054 f44b1f58 2020-02-02 tracey while (1) {
4055 f44b1f58 2020-02-02 tracey off_t offset;
4056 f44b1f58 2020-02-02 tracey
4057 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4058 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4059 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4060 f44b1f58 2020-02-02 tracey break;
4061 f44b1f58 2020-02-02 tracey }
4062 f44b1f58 2020-02-02 tracey
4063 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4064 f44b1f58 2020-02-02 tracey lineno = 1;
4065 f44b1f58 2020-02-02 tracey else
4066 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4067 f44b1f58 2020-02-02 tracey }
4068 f44b1f58 2020-02-02 tracey
4069 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4070 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4071 f44b1f58 2020-02-02 tracey free(line);
4072 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4073 f44b1f58 2020-02-02 tracey }
4074 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4075 cb713507 2022-06-16 stsp if (linelen != -1) {
4076 cb713507 2022-06-16 stsp char *exstr;
4077 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4078 cb713507 2022-06-16 stsp if (err)
4079 cb713507 2022-06-16 stsp break;
4080 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4081 cb713507 2022-06-16 stsp &view->regmatch)) {
4082 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4083 cb713507 2022-06-16 stsp s->matched_line = lineno;
4084 cb713507 2022-06-16 stsp free(exstr);
4085 cb713507 2022-06-16 stsp break;
4086 cb713507 2022-06-16 stsp }
4087 cb713507 2022-06-16 stsp free(exstr);
4088 f44b1f58 2020-02-02 tracey }
4089 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4090 f44b1f58 2020-02-02 tracey lineno++;
4091 f44b1f58 2020-02-02 tracey else
4092 f44b1f58 2020-02-02 tracey lineno--;
4093 f44b1f58 2020-02-02 tracey }
4094 826082fe 2020-12-10 stsp free(line);
4095 f44b1f58 2020-02-02 tracey
4096 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4097 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4098 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4099 f44b1f58 2020-02-02 tracey }
4100 f44b1f58 2020-02-02 tracey
4101 145b6838 2022-06-16 stsp return err;
4102 f5215bb9 2019-02-22 stsp }
4103 f5215bb9 2019-02-22 stsp
4104 48ae06ee 2018-10-18 stsp static const struct got_error *
4105 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4106 b72706c3 2022-06-01 stsp {
4107 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4108 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4109 b72706c3 2022-06-01 stsp
4110 b72706c3 2022-06-01 stsp free(s->id1);
4111 b72706c3 2022-06-01 stsp s->id1 = NULL;
4112 b72706c3 2022-06-01 stsp free(s->id2);
4113 b72706c3 2022-06-01 stsp s->id2 = NULL;
4114 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4115 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4116 b72706c3 2022-06-01 stsp s->f = NULL;
4117 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4118 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4119 b72706c3 2022-06-01 stsp s->f1 = NULL;
4120 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4121 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4122 b72706c3 2022-06-01 stsp s->f2 = NULL;
4123 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4124 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4125 f9d37699 2022-06-28 stsp s->fd1 = -1;
4126 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4127 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4128 f9d37699 2022-06-28 stsp s->fd2 = -1;
4129 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4130 b72706c3 2022-06-01 stsp free(s->line_offsets);
4131 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4132 b72706c3 2022-06-01 stsp s->nlines = 0;
4133 b72706c3 2022-06-01 stsp return err;
4134 b72706c3 2022-06-01 stsp }
4135 b72706c3 2022-06-01 stsp
4136 b72706c3 2022-06-01 stsp static const struct got_error *
4137 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4138 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4139 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4140 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4141 48ae06ee 2018-10-18 stsp {
4142 48ae06ee 2018-10-18 stsp const struct got_error *err;
4143 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4144 5dc9f4bc 2018-08-04 stsp
4145 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4146 f9d37699 2022-06-28 stsp s->fd1 = -1;
4147 f9d37699 2022-06-28 stsp s->fd2 = -1;
4148 b72706c3 2022-06-01 stsp
4149 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4150 15a94983 2018-12-23 stsp int type1, type2;
4151 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4152 15a94983 2018-12-23 stsp if (err)
4153 15a94983 2018-12-23 stsp return err;
4154 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4155 15a94983 2018-12-23 stsp if (err)
4156 15a94983 2018-12-23 stsp return err;
4157 15a94983 2018-12-23 stsp
4158 15a94983 2018-12-23 stsp if (type1 != type2)
4159 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4160 15a94983 2018-12-23 stsp }
4161 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4162 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4163 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4164 f44b1f58 2020-02-02 tracey s->repo = repo;
4165 f44b1f58 2020-02-02 tracey s->id1 = id1;
4166 f44b1f58 2020-02-02 tracey s->id2 = id2;
4167 3dbaef42 2020-11-24 stsp s->label1 = label1;
4168 3dbaef42 2020-11-24 stsp s->label2 = label2;
4169 48ae06ee 2018-10-18 stsp
4170 15a94983 2018-12-23 stsp if (id1) {
4171 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4172 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4173 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4174 48ae06ee 2018-10-18 stsp } else
4175 5465d566 2020-02-01 tracey s->id1 = NULL;
4176 48ae06ee 2018-10-18 stsp
4177 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4178 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4179 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4180 b72706c3 2022-06-01 stsp goto done;
4181 48ae06ee 2018-10-18 stsp }
4182 b72706c3 2022-06-01 stsp
4183 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4184 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4185 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4186 a00719e9 2022-06-17 stsp goto done;
4187 a00719e9 2022-06-17 stsp }
4188 a00719e9 2022-06-17 stsp
4189 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4190 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4191 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4192 b72706c3 2022-06-01 stsp goto done;
4193 b72706c3 2022-06-01 stsp }
4194 b72706c3 2022-06-01 stsp
4195 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4196 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4197 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4198 f9d37699 2022-06-28 stsp goto done;
4199 f9d37699 2022-06-28 stsp }
4200 f9d37699 2022-06-28 stsp
4201 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4202 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4203 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4204 f9d37699 2022-06-28 stsp goto done;
4205 f9d37699 2022-06-28 stsp }
4206 f9d37699 2022-06-28 stsp
4207 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4208 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4209 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4210 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4211 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4212 5465d566 2020-02-01 tracey s->log_view = log_view;
4213 5465d566 2020-02-01 tracey s->repo = repo;
4214 6d17833f 2019-11-08 stsp
4215 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4216 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4217 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4218 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4219 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4220 6d17833f 2019-11-08 stsp if (err)
4221 b72706c3 2022-06-01 stsp goto done;
4222 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4223 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4224 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4225 b72706c3 2022-06-01 stsp if (err)
4226 b72706c3 2022-06-01 stsp goto done;
4227 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4228 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4229 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4230 b72706c3 2022-06-01 stsp if (err)
4231 b72706c3 2022-06-01 stsp goto done;
4232 6d17833f 2019-11-08 stsp
4233 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4234 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4235 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4236 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4237 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4238 b72706c3 2022-06-01 stsp if (err)
4239 b72706c3 2022-06-01 stsp goto done;
4240 11b20872 2019-11-08 stsp
4241 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4242 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4243 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4244 b72706c3 2022-06-01 stsp if (err)
4245 b72706c3 2022-06-01 stsp goto done;
4246 11b20872 2019-11-08 stsp
4247 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4248 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4250 b72706c3 2022-06-01 stsp if (err)
4251 b72706c3 2022-06-01 stsp goto done;
4252 6d17833f 2019-11-08 stsp }
4253 5dc9f4bc 2018-08-04 stsp
4254 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4255 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4256 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4257 f5215bb9 2019-02-22 stsp
4258 5465d566 2020-02-01 tracey err = create_diff(s);
4259 48ae06ee 2018-10-18 stsp
4260 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4261 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4262 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4263 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4264 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4265 b72706c3 2022-06-01 stsp done:
4266 b72706c3 2022-06-01 stsp if (err)
4267 b72706c3 2022-06-01 stsp close_diff_view(view);
4268 e5a0f69f 2018-08-18 stsp return err;
4269 5dc9f4bc 2018-08-04 stsp }
4270 5dc9f4bc 2018-08-04 stsp
4271 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4272 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4273 5dc9f4bc 2018-08-04 stsp {
4274 a3404814 2018-09-02 stsp const struct got_error *err;
4275 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4276 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4277 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4278 a3404814 2018-09-02 stsp
4279 a3404814 2018-09-02 stsp if (s->id1) {
4280 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4281 a3404814 2018-09-02 stsp if (err)
4282 a3404814 2018-09-02 stsp return err;
4283 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4284 3dbaef42 2020-11-24 stsp } else
4285 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4286 3dbaef42 2020-11-24 stsp
4287 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4288 a3404814 2018-09-02 stsp if (err)
4289 a3404814 2018-09-02 stsp return err;
4290 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4291 26ed57b2 2018-05-19 stsp
4292 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4293 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4294 a3404814 2018-09-02 stsp free(id_str1);
4295 a3404814 2018-09-02 stsp free(id_str2);
4296 a3404814 2018-09-02 stsp return err;
4297 a3404814 2018-09-02 stsp }
4298 a3404814 2018-09-02 stsp free(id_str1);
4299 a3404814 2018-09-02 stsp free(id_str2);
4300 a3404814 2018-09-02 stsp
4301 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4302 267bb3b8 2021-08-01 stsp free(header);
4303 267bb3b8 2021-08-01 stsp return err;
4304 15a087fe 2019-02-21 stsp }
4305 15a087fe 2019-02-21 stsp
4306 15a087fe 2019-02-21 stsp static const struct got_error *
4307 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4308 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4309 15a087fe 2019-02-21 stsp {
4310 d7a04538 2019-02-21 stsp const struct got_error *err;
4311 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4312 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4313 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4314 15a087fe 2019-02-21 stsp
4315 15a087fe 2019-02-21 stsp free(s->id2);
4316 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4317 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4318 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4319 15a087fe 2019-02-21 stsp
4320 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4321 d7a04538 2019-02-21 stsp if (err)
4322 d7a04538 2019-02-21 stsp return err;
4323 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4324 15a087fe 2019-02-21 stsp free(s->id1);
4325 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4326 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4327 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4328 15a087fe 2019-02-21 stsp return NULL;
4329 0cf4efb1 2018-09-29 stsp }
4330 0cf4efb1 2018-09-29 stsp
4331 0cf4efb1 2018-09-29 stsp static const struct got_error *
4332 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4333 e5a0f69f 2018-08-18 stsp {
4334 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4335 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4336 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4337 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4338 826082fe 2020-12-10 stsp char *line = NULL;
4339 826082fe 2020-12-10 stsp size_t linesize = 0;
4340 826082fe 2020-12-10 stsp ssize_t linelen;
4341 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4342 e5a0f69f 2018-08-18 stsp
4343 e5a0f69f 2018-08-18 stsp switch (ch) {
4344 145b6838 2022-06-16 stsp case '0':
4345 145b6838 2022-06-16 stsp view->x = 0;
4346 145b6838 2022-06-16 stsp break;
4347 145b6838 2022-06-16 stsp case '$':
4348 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4349 640cd7ff 2022-06-22 mark view->count = 0;
4350 145b6838 2022-06-16 stsp break;
4351 145b6838 2022-06-16 stsp case KEY_RIGHT:
4352 145b6838 2022-06-16 stsp case 'l':
4353 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4354 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4355 640cd7ff 2022-06-22 mark else
4356 640cd7ff 2022-06-22 mark view->count = 0;
4357 145b6838 2022-06-16 stsp break;
4358 145b6838 2022-06-16 stsp case KEY_LEFT:
4359 145b6838 2022-06-16 stsp case 'h':
4360 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4361 640cd7ff 2022-06-22 mark if (view->x <= 0)
4362 640cd7ff 2022-06-22 mark view->count = 0;
4363 145b6838 2022-06-16 stsp break;
4364 64453f7e 2020-11-21 stsp case 'a':
4365 3dbaef42 2020-11-24 stsp case 'w':
4366 3dbaef42 2020-11-24 stsp if (ch == 'a')
4367 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4368 3dbaef42 2020-11-24 stsp if (ch == 'w')
4369 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4370 64453f7e 2020-11-21 stsp wclear(view->window);
4371 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4372 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4373 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4374 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4375 64453f7e 2020-11-21 stsp err = create_diff(s);
4376 640cd7ff 2022-06-22 mark view->count = 0;
4377 912a3f79 2021-08-30 j break;
4378 912a3f79 2021-08-30 j case 'g':
4379 912a3f79 2021-08-30 j case KEY_HOME:
4380 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4381 640cd7ff 2022-06-22 mark view->count = 0;
4382 64453f7e 2020-11-21 stsp break;
4383 912a3f79 2021-08-30 j case 'G':
4384 912a3f79 2021-08-30 j case KEY_END:
4385 640cd7ff 2022-06-22 mark view->count = 0;
4386 912a3f79 2021-08-30 j if (s->eof)
4387 912a3f79 2021-08-30 j break;
4388 912a3f79 2021-08-30 j
4389 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4390 912a3f79 2021-08-30 j s->eof = 1;
4391 912a3f79 2021-08-30 j break;
4392 1e37a5c2 2019-05-12 jcs case 'k':
4393 1e37a5c2 2019-05-12 jcs case KEY_UP:
4394 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4395 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4396 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4397 640cd7ff 2022-06-22 mark else
4398 640cd7ff 2022-06-22 mark view->count = 0;
4399 1e37a5c2 2019-05-12 jcs break;
4400 83cc4199 2022-06-13 stsp case CTRL('u'):
4401 33c3719a 2022-06-15 stsp case 'u':
4402 83cc4199 2022-06-13 stsp nscroll /= 2;
4403 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4404 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4405 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4406 61417565 2022-06-20 mark case 'b':
4407 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4408 640cd7ff 2022-06-22 mark view->count = 0;
4409 26ed57b2 2018-05-19 stsp break;
4410 640cd7ff 2022-06-22 mark }
4411 1e37a5c2 2019-05-12 jcs i = 0;
4412 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4413 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4414 1e37a5c2 2019-05-12 jcs break;
4415 1e37a5c2 2019-05-12 jcs case 'j':
4416 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4417 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4418 1e37a5c2 2019-05-12 jcs if (!s->eof)
4419 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4420 640cd7ff 2022-06-22 mark else
4421 640cd7ff 2022-06-22 mark view->count = 0;
4422 1e37a5c2 2019-05-12 jcs break;
4423 83cc4199 2022-06-13 stsp case CTRL('d'):
4424 33c3719a 2022-06-15 stsp case 'd':
4425 83cc4199 2022-06-13 stsp nscroll /= 2;
4426 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4427 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4428 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4429 61417565 2022-06-20 mark case 'f':
4430 1e37a5c2 2019-05-12 jcs case ' ':
4431 640cd7ff 2022-06-22 mark if (s->eof) {
4432 640cd7ff 2022-06-22 mark view->count = 0;
4433 1e37a5c2 2019-05-12 jcs break;
4434 640cd7ff 2022-06-22 mark }
4435 1e37a5c2 2019-05-12 jcs i = 0;
4436 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4437 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4438 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4439 826082fe 2020-12-10 stsp if (linelen == -1) {
4440 826082fe 2020-12-10 stsp if (feof(s->f)) {
4441 826082fe 2020-12-10 stsp s->eof = 1;
4442 826082fe 2020-12-10 stsp } else
4443 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4444 34bc9ec9 2019-02-22 stsp break;
4445 826082fe 2020-12-10 stsp }
4446 1e37a5c2 2019-05-12 jcs }
4447 826082fe 2020-12-10 stsp free(line);
4448 1e37a5c2 2019-05-12 jcs break;
4449 1e37a5c2 2019-05-12 jcs case '[':
4450 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4451 1e37a5c2 2019-05-12 jcs s->diff_context--;
4452 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4453 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4454 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4455 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4456 27829c9e 2020-11-21 stsp s->nlines) {
4457 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4458 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4459 27829c9e 2020-11-21 stsp }
4460 640cd7ff 2022-06-22 mark } else
4461 640cd7ff 2022-06-22 mark view->count = 0;
4462 1e37a5c2 2019-05-12 jcs break;
4463 1e37a5c2 2019-05-12 jcs case ']':
4464 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4465 1e37a5c2 2019-05-12 jcs s->diff_context++;
4466 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4467 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4468 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4469 640cd7ff 2022-06-22 mark } else
4470 640cd7ff 2022-06-22 mark view->count = 0;
4471 1e37a5c2 2019-05-12 jcs break;
4472 1e37a5c2 2019-05-12 jcs case '<':
4473 1e37a5c2 2019-05-12 jcs case ',':
4474 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4475 640cd7ff 2022-06-22 mark view->count = 0;
4476 48ae06ee 2018-10-18 stsp break;
4477 640cd7ff 2022-06-22 mark }
4478 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4479 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4480 6524637e 2019-02-21 stsp
4481 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4482 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4483 1e37a5c2 2019-05-12 jcs if (err)
4484 1e37a5c2 2019-05-12 jcs break;
4485 15a087fe 2019-02-21 stsp
4486 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4487 5a8b5076 2020-12-05 stsp break;
4488 5a8b5076 2020-12-05 stsp
4489 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4490 1e37a5c2 2019-05-12 jcs if (err)
4491 1e37a5c2 2019-05-12 jcs break;
4492 15a087fe 2019-02-21 stsp
4493 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4494 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4495 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4496 145b6838 2022-06-16 stsp view->x = 0;
4497 15a087fe 2019-02-21 stsp
4498 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4499 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4500 1e37a5c2 2019-05-12 jcs break;
4501 1e37a5c2 2019-05-12 jcs case '>':
4502 1e37a5c2 2019-05-12 jcs case '.':
4503 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4504 640cd7ff 2022-06-22 mark view->count = 0;
4505 15a087fe 2019-02-21 stsp break;
4506 640cd7ff 2022-06-22 mark }
4507 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4508 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4509 5e224a3e 2019-02-22 stsp
4510 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4511 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4512 1e37a5c2 2019-05-12 jcs if (err)
4513 5a8b5076 2020-12-05 stsp break;
4514 5a8b5076 2020-12-05 stsp
4515 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4516 1e37a5c2 2019-05-12 jcs break;
4517 15a087fe 2019-02-21 stsp
4518 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4519 1e37a5c2 2019-05-12 jcs if (err)
4520 1e37a5c2 2019-05-12 jcs break;
4521 15a087fe 2019-02-21 stsp
4522 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4523 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4524 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4525 145b6838 2022-06-16 stsp view->x = 0;
4526 1e37a5c2 2019-05-12 jcs
4527 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4528 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4529 1e37a5c2 2019-05-12 jcs break;
4530 1e37a5c2 2019-05-12 jcs default:
4531 640cd7ff 2022-06-22 mark view->count = 0;
4532 1e37a5c2 2019-05-12 jcs break;
4533 26ed57b2 2018-05-19 stsp }
4534 e5a0f69f 2018-08-18 stsp
4535 bcbd79e2 2018-08-19 stsp return err;
4536 26ed57b2 2018-05-19 stsp }
4537 26ed57b2 2018-05-19 stsp
4538 4ed7e80c 2018-05-20 stsp static const struct got_error *
4539 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4540 9f7d7167 2018-04-29 stsp {
4541 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4542 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4543 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4544 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4545 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4546 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4547 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4548 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4549 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4550 3dbaef42 2020-11-24 stsp const char *errstr;
4551 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4552 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4553 70ac5f84 2019-03-28 stsp
4554 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4555 26ed57b2 2018-05-19 stsp switch (ch) {
4556 64453f7e 2020-11-21 stsp case 'a':
4557 64453f7e 2020-11-21 stsp force_text_diff = 1;
4558 3dbaef42 2020-11-24 stsp break;
4559 3dbaef42 2020-11-24 stsp case 'C':
4560 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4561 3dbaef42 2020-11-24 stsp &errstr);
4562 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4563 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4564 5a20d08d 2022-02-09 op errstr, errstr);
4565 64453f7e 2020-11-21 stsp break;
4566 09b5bff8 2020-02-23 naddy case 'r':
4567 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4568 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4569 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4570 09b5bff8 2020-02-23 naddy optarg);
4571 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4572 09b5bff8 2020-02-23 naddy break;
4573 3dbaef42 2020-11-24 stsp case 'w':
4574 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4575 3dbaef42 2020-11-24 stsp break;
4576 26ed57b2 2018-05-19 stsp default:
4577 17020d27 2019-03-07 stsp usage_diff();
4578 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4579 26ed57b2 2018-05-19 stsp }
4580 26ed57b2 2018-05-19 stsp }
4581 26ed57b2 2018-05-19 stsp
4582 26ed57b2 2018-05-19 stsp argc -= optind;
4583 26ed57b2 2018-05-19 stsp argv += optind;
4584 26ed57b2 2018-05-19 stsp
4585 26ed57b2 2018-05-19 stsp if (argc == 0) {
4586 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4587 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4588 15a94983 2018-12-23 stsp id_str1 = argv[0];
4589 15a94983 2018-12-23 stsp id_str2 = argv[1];
4590 26ed57b2 2018-05-19 stsp } else
4591 26ed57b2 2018-05-19 stsp usage_diff();
4592 eb6600df 2019-01-04 stsp
4593 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4594 0ae84acc 2022-06-15 tracey if (error)
4595 0ae84acc 2022-06-15 tracey goto done;
4596 0ae84acc 2022-06-15 tracey
4597 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4598 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4599 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4600 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4601 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4602 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4603 c156c7a4 2020-12-18 stsp goto done;
4604 a273ac94 2020-02-23 naddy if (worktree)
4605 a273ac94 2020-02-23 naddy repo_path =
4606 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4607 a273ac94 2020-02-23 naddy else
4608 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4609 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4610 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4611 c156c7a4 2020-12-18 stsp goto done;
4612 c156c7a4 2020-12-18 stsp }
4613 a273ac94 2020-02-23 naddy }
4614 a273ac94 2020-02-23 naddy
4615 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4616 eb6600df 2019-01-04 stsp if (error)
4617 eb6600df 2019-01-04 stsp goto done;
4618 26ed57b2 2018-05-19 stsp
4619 a273ac94 2020-02-23 naddy init_curses();
4620 a273ac94 2020-02-23 naddy
4621 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4622 51a10b52 2020-12-26 stsp if (error)
4623 51a10b52 2020-12-26 stsp goto done;
4624 51a10b52 2020-12-26 stsp
4625 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4626 26ed57b2 2018-05-19 stsp if (error)
4627 26ed57b2 2018-05-19 stsp goto done;
4628 26ed57b2 2018-05-19 stsp
4629 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4630 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4631 26ed57b2 2018-05-19 stsp if (error)
4632 26ed57b2 2018-05-19 stsp goto done;
4633 26ed57b2 2018-05-19 stsp
4634 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4635 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4636 26ed57b2 2018-05-19 stsp if (error)
4637 26ed57b2 2018-05-19 stsp goto done;
4638 26ed57b2 2018-05-19 stsp
4639 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4640 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4641 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4642 ea5e7bb5 2018-08-01 stsp goto done;
4643 ea5e7bb5 2018-08-01 stsp }
4644 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4645 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4646 5dc9f4bc 2018-08-04 stsp if (error)
4647 5dc9f4bc 2018-08-04 stsp goto done;
4648 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4649 26ed57b2 2018-05-19 stsp done:
4650 3dbaef42 2020-11-24 stsp free(label1);
4651 3dbaef42 2020-11-24 stsp free(label2);
4652 c02c541e 2019-03-29 stsp free(repo_path);
4653 a273ac94 2020-02-23 naddy free(cwd);
4654 1d0f4054 2021-06-17 stsp if (repo) {
4655 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4656 1d0f4054 2021-06-17 stsp if (error == NULL)
4657 1d0f4054 2021-06-17 stsp error = close_err;
4658 1d0f4054 2021-06-17 stsp }
4659 a273ac94 2020-02-23 naddy if (worktree)
4660 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4661 0ae84acc 2022-06-15 tracey if (pack_fds) {
4662 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4663 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4664 0ae84acc 2022-06-15 tracey if (error == NULL)
4665 0ae84acc 2022-06-15 tracey error = pack_err;
4666 0ae84acc 2022-06-15 tracey }
4667 51a10b52 2020-12-26 stsp tog_free_refs();
4668 26ed57b2 2018-05-19 stsp return error;
4669 9f7d7167 2018-04-29 stsp }
4670 9f7d7167 2018-04-29 stsp
4671 4ed7e80c 2018-05-20 stsp __dead static void
4672 9f7d7167 2018-04-29 stsp usage_blame(void)
4673 9f7d7167 2018-04-29 stsp {
4674 80ddbec8 2018-04-29 stsp endwin();
4675 87411fa9 2022-06-16 stsp fprintf(stderr,
4676 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4677 9f7d7167 2018-04-29 stsp getprogname());
4678 9f7d7167 2018-04-29 stsp exit(1);
4679 9f7d7167 2018-04-29 stsp }
4680 84451b3e 2018-07-10 stsp
4681 84451b3e 2018-07-10 stsp struct tog_blame_line {
4682 84451b3e 2018-07-10 stsp int annotated;
4683 84451b3e 2018-07-10 stsp struct got_object_id *id;
4684 84451b3e 2018-07-10 stsp };
4685 9f7d7167 2018-04-29 stsp
4686 4ed7e80c 2018-05-20 stsp static const struct got_error *
4687 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4688 84451b3e 2018-07-10 stsp {
4689 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4690 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4691 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4692 84451b3e 2018-07-10 stsp const struct got_error *err;
4693 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4694 826082fe 2020-12-10 stsp char *line = NULL;
4695 826082fe 2020-12-10 stsp size_t linesize = 0;
4696 826082fe 2020-12-10 stsp ssize_t linelen;
4697 84451b3e 2018-07-10 stsp wchar_t *wline;
4698 27a741e5 2019-09-11 stsp int width;
4699 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4700 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4701 ab089a2a 2018-07-12 stsp char *id_str;
4702 11b20872 2019-11-08 stsp struct tog_color *tc;
4703 ab089a2a 2018-07-12 stsp
4704 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4705 ab089a2a 2018-07-12 stsp if (err)
4706 ab089a2a 2018-07-12 stsp return err;
4707 84451b3e 2018-07-10 stsp
4708 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4709 f7d12f7e 2018-08-01 stsp werase(view->window);
4710 84451b3e 2018-07-10 stsp
4711 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4712 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4713 ab089a2a 2018-07-12 stsp free(id_str);
4714 ab089a2a 2018-07-12 stsp return err;
4715 ab089a2a 2018-07-12 stsp }
4716 ab089a2a 2018-07-12 stsp
4717 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4718 ab089a2a 2018-07-12 stsp free(line);
4719 2550e4c3 2018-07-13 stsp line = NULL;
4720 1cae65b4 2019-09-22 stsp if (err)
4721 1cae65b4 2019-09-22 stsp return err;
4722 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4723 a3404814 2018-09-02 stsp wstandout(view->window);
4724 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4725 11b20872 2019-11-08 stsp if (tc)
4726 11b20872 2019-11-08 stsp wattr_on(view->window,
4727 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4728 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4729 11b20872 2019-11-08 stsp if (tc)
4730 11b20872 2019-11-08 stsp wattr_off(view->window,
4731 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4733 a3404814 2018-09-02 stsp wstandend(view->window);
4734 2550e4c3 2018-07-13 stsp free(wline);
4735 2550e4c3 2018-07-13 stsp wline = NULL;
4736 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4737 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4738 ab089a2a 2018-07-12 stsp
4739 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4740 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4741 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4742 ab089a2a 2018-07-12 stsp free(id_str);
4743 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4744 ab089a2a 2018-07-12 stsp }
4745 ab089a2a 2018-07-12 stsp free(id_str);
4746 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4747 3f60a8ef 2018-07-10 stsp free(line);
4748 2550e4c3 2018-07-13 stsp line = NULL;
4749 3f60a8ef 2018-07-10 stsp if (err)
4750 3f60a8ef 2018-07-10 stsp return err;
4751 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4752 2550e4c3 2018-07-13 stsp free(wline);
4753 2550e4c3 2018-07-13 stsp wline = NULL;
4754 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4755 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4756 3f60a8ef 2018-07-10 stsp
4757 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4758 145b6838 2022-06-16 stsp view->maxx = 0;
4759 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4760 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4761 826082fe 2020-12-10 stsp if (linelen == -1) {
4762 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4763 826082fe 2020-12-10 stsp s->eof = 1;
4764 826082fe 2020-12-10 stsp break;
4765 826082fe 2020-12-10 stsp }
4766 84451b3e 2018-07-10 stsp free(line);
4767 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4768 84451b3e 2018-07-10 stsp }
4769 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4770 826082fe 2020-12-10 stsp continue;
4771 1853e0f4 2022-06-16 stsp
4772 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4773 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4774 1853e0f4 2022-06-16 stsp if (err) {
4775 1853e0f4 2022-06-16 stsp free(line);
4776 1853e0f4 2022-06-16 stsp return err;
4777 1853e0f4 2022-06-16 stsp }
4778 1853e0f4 2022-06-16 stsp free(wline);
4779 1853e0f4 2022-06-16 stsp wline = NULL;
4780 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4781 84451b3e 2018-07-10 stsp
4782 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4783 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4784 b700b5d6 2018-07-10 stsp
4785 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4786 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4787 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4788 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4789 27a741e5 2019-09-11 stsp !(view->focussed &&
4790 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4791 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4792 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4793 8d0fe45a 2019-08-12 stsp char *id_str;
4794 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4795 87411fa9 2022-06-16 stsp blame_line->id);
4796 8d0fe45a 2019-08-12 stsp if (err) {
4797 8d0fe45a 2019-08-12 stsp free(line);
4798 8d0fe45a 2019-08-12 stsp return err;
4799 8d0fe45a 2019-08-12 stsp }
4800 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4801 11b20872 2019-11-08 stsp if (tc)
4802 11b20872 2019-11-08 stsp wattr_on(view->window,
4803 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4804 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4805 11b20872 2019-11-08 stsp if (tc)
4806 11b20872 2019-11-08 stsp wattr_off(view->window,
4807 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4808 8d0fe45a 2019-08-12 stsp free(id_str);
4809 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4810 8d0fe45a 2019-08-12 stsp } else {
4811 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4812 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4813 84451b3e 2018-07-10 stsp }
4814 ee41ec32 2018-07-10 stsp } else {
4815 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4816 ee41ec32 2018-07-10 stsp prev_id = NULL;
4817 ee41ec32 2018-07-10 stsp }
4818 84451b3e 2018-07-10 stsp
4819 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4820 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4821 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4822 27a741e5 2019-09-11 stsp
4823 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4824 41605754 2020-11-12 stsp width = 9;
4825 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4826 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4827 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4828 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4829 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4830 41605754 2020-11-12 stsp if (err) {
4831 41605754 2020-11-12 stsp free(line);
4832 41605754 2020-11-12 stsp return err;
4833 41605754 2020-11-12 stsp }
4834 41605754 2020-11-12 stsp width += 9;
4835 41605754 2020-11-12 stsp } else {
4836 4cb9d869 2022-06-16 stsp int skip;
4837 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4838 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4839 4cb9d869 2022-06-16 stsp if (err) {
4840 4cb9d869 2022-06-16 stsp free(line);
4841 4cb9d869 2022-06-16 stsp return err;
4842 145b6838 2022-06-16 stsp }
4843 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4844 a75b3e2d 2022-06-16 stsp width += 9;
4845 41605754 2020-11-12 stsp free(wline);
4846 41605754 2020-11-12 stsp wline = NULL;
4847 41605754 2020-11-12 stsp }
4848 41605754 2020-11-12 stsp
4849 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4850 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4851 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4852 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4853 84451b3e 2018-07-10 stsp }
4854 826082fe 2020-12-10 stsp free(line);
4855 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4856 84451b3e 2018-07-10 stsp
4857 9b058f45 2022-06-30 mark view_border(view);
4858 84451b3e 2018-07-10 stsp
4859 84451b3e 2018-07-10 stsp return NULL;
4860 84451b3e 2018-07-10 stsp }
4861 84451b3e 2018-07-10 stsp
4862 84451b3e 2018-07-10 stsp static const struct got_error *
4863 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4864 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4865 84451b3e 2018-07-10 stsp {
4866 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4867 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4868 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4869 1a76625f 2018-10-22 stsp int errcode;
4870 84451b3e 2018-07-10 stsp
4871 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4872 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4873 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4874 84451b3e 2018-07-10 stsp
4875 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4876 1a76625f 2018-10-22 stsp if (errcode)
4877 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4878 84451b3e 2018-07-10 stsp
4879 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4880 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4881 d68a0a7d 2018-07-10 stsp goto done;
4882 d68a0a7d 2018-07-10 stsp }
4883 d68a0a7d 2018-07-10 stsp
4884 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4885 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4886 d68a0a7d 2018-07-10 stsp
4887 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4888 d68a0a7d 2018-07-10 stsp if (line->annotated)
4889 d68a0a7d 2018-07-10 stsp goto done;
4890 d68a0a7d 2018-07-10 stsp
4891 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4892 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4893 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4894 84451b3e 2018-07-10 stsp goto done;
4895 84451b3e 2018-07-10 stsp }
4896 84451b3e 2018-07-10 stsp line->annotated = 1;
4897 84451b3e 2018-07-10 stsp done:
4898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4899 1a76625f 2018-10-22 stsp if (errcode)
4900 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4901 84451b3e 2018-07-10 stsp return err;
4902 84451b3e 2018-07-10 stsp }
4903 84451b3e 2018-07-10 stsp
4904 84451b3e 2018-07-10 stsp static void *
4905 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4906 84451b3e 2018-07-10 stsp {
4907 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4908 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4909 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4910 1b484788 2022-06-28 tracey int errcode, fd = -1;
4911 1b484788 2022-06-28 tracey
4912 1b484788 2022-06-28 tracey fd = got_opentempfd();
4913 1b484788 2022-06-28 tracey if (fd == -1)
4914 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4915 18430de3 2018-07-10 stsp
4916 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4917 61266923 2020-01-14 stsp if (err)
4918 61266923 2020-01-14 stsp return (void *)err;
4919 61266923 2020-01-14 stsp
4920 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4921 1b484788 2022-06-28 tracey blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
4922 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4923 fc06ba56 2019-08-22 stsp err = NULL;
4924 18430de3 2018-07-10 stsp
4925 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4926 1a76625f 2018-10-22 stsp if (errcode)
4927 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4928 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4929 18430de3 2018-07-10 stsp
4930 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4931 1d0f4054 2021-06-17 stsp if (err == NULL)
4932 1d0f4054 2021-06-17 stsp err = close_err;
4933 c9beca56 2018-07-22 stsp ta->repo = NULL;
4934 c9beca56 2018-07-22 stsp *ta->complete = 1;
4935 18430de3 2018-07-10 stsp
4936 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4937 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4938 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4939 18430de3 2018-07-10 stsp
4940 1b484788 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
4941 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
4942 1b484788 2022-06-28 tracey
4943 18430de3 2018-07-10 stsp return (void *)err;
4944 84451b3e 2018-07-10 stsp }
4945 84451b3e 2018-07-10 stsp
4946 245d91c1 2018-07-12 stsp static struct got_object_id *
4947 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4948 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4949 245d91c1 2018-07-12 stsp {
4950 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4951 8d0fe45a 2019-08-12 stsp
4952 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4953 8d0fe45a 2019-08-12 stsp return NULL;
4954 b880a918 2018-07-10 stsp
4955 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4956 245d91c1 2018-07-12 stsp if (!line->annotated)
4957 245d91c1 2018-07-12 stsp return NULL;
4958 245d91c1 2018-07-12 stsp
4959 245d91c1 2018-07-12 stsp return line->id;
4960 b880a918 2018-07-10 stsp }
4961 245d91c1 2018-07-12 stsp
4962 b880a918 2018-07-10 stsp static const struct got_error *
4963 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4964 a70480e0 2018-06-23 stsp {
4965 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4966 245d91c1 2018-07-12 stsp int i;
4967 245d91c1 2018-07-12 stsp
4968 245d91c1 2018-07-12 stsp if (blame->thread) {
4969 1a76625f 2018-10-22 stsp int errcode;
4970 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4971 1a76625f 2018-10-22 stsp if (errcode)
4972 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4973 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4974 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4975 1a76625f 2018-10-22 stsp if (errcode)
4976 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4978 1a76625f 2018-10-22 stsp if (errcode)
4979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4980 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4981 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4982 245d91c1 2018-07-12 stsp err = NULL;
4983 245d91c1 2018-07-12 stsp blame->thread = NULL;
4984 245d91c1 2018-07-12 stsp }
4985 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4986 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4987 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4988 1d0f4054 2021-06-17 stsp if (err == NULL)
4989 1d0f4054 2021-06-17 stsp err = close_err;
4990 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4991 245d91c1 2018-07-12 stsp }
4992 245d91c1 2018-07-12 stsp if (blame->f) {
4993 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4994 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4995 245d91c1 2018-07-12 stsp blame->f = NULL;
4996 245d91c1 2018-07-12 stsp }
4997 57670559 2018-12-23 stsp if (blame->lines) {
4998 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4999 57670559 2018-12-23 stsp free(blame->lines[i].id);
5000 57670559 2018-12-23 stsp free(blame->lines);
5001 57670559 2018-12-23 stsp blame->lines = NULL;
5002 57670559 2018-12-23 stsp }
5003 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5004 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5005 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5006 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5007 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5008 0ae84acc 2022-06-15 tracey if (err == NULL)
5009 0ae84acc 2022-06-15 tracey err = pack_err;
5010 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5011 0ae84acc 2022-06-15 tracey }
5012 245d91c1 2018-07-12 stsp return err;
5013 245d91c1 2018-07-12 stsp }
5014 245d91c1 2018-07-12 stsp
5015 245d91c1 2018-07-12 stsp static const struct got_error *
5016 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5017 fc06ba56 2019-08-22 stsp {
5018 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5019 fc06ba56 2019-08-22 stsp int *done = arg;
5020 fc06ba56 2019-08-22 stsp int errcode;
5021 fc06ba56 2019-08-22 stsp
5022 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5023 fc06ba56 2019-08-22 stsp if (errcode)
5024 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5025 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5026 fc06ba56 2019-08-22 stsp
5027 fc06ba56 2019-08-22 stsp if (*done)
5028 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5029 fc06ba56 2019-08-22 stsp
5030 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5031 fc06ba56 2019-08-22 stsp if (errcode)
5032 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5033 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5034 fc06ba56 2019-08-22 stsp
5035 fc06ba56 2019-08-22 stsp return err;
5036 fc06ba56 2019-08-22 stsp }
5037 fc06ba56 2019-08-22 stsp
5038 fc06ba56 2019-08-22 stsp static const struct got_error *
5039 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5040 245d91c1 2018-07-12 stsp {
5041 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5042 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5043 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5044 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5045 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5046 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5047 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5048 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5049 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5050 a70480e0 2018-06-23 stsp
5051 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5052 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5053 27d434c2 2018-09-15 stsp if (err)
5054 15a94983 2018-12-23 stsp return err;
5055 eb81bc23 2022-06-28 tracey
5056 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5057 eb81bc23 2022-06-28 tracey if (fd == -1) {
5058 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5059 eb81bc23 2022-06-28 tracey goto done;
5060 eb81bc23 2022-06-28 tracey }
5061 a44927cc 2022-04-07 stsp
5062 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5063 a44927cc 2022-04-07 stsp if (err)
5064 a44927cc 2022-04-07 stsp goto done;
5065 27d434c2 2018-09-15 stsp
5066 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5067 84451b3e 2018-07-10 stsp if (err)
5068 84451b3e 2018-07-10 stsp goto done;
5069 27d434c2 2018-09-15 stsp
5070 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5071 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5072 84451b3e 2018-07-10 stsp goto done;
5073 84451b3e 2018-07-10 stsp }
5074 a70480e0 2018-06-23 stsp
5075 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5076 a70480e0 2018-06-23 stsp if (err)
5077 a70480e0 2018-06-23 stsp goto done;
5078 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5079 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5080 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5081 84451b3e 2018-07-10 stsp goto done;
5082 84451b3e 2018-07-10 stsp }
5083 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5084 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5085 1fddf795 2021-01-20 stsp if (err)
5086 1fddf795 2021-01-20 stsp goto done;
5087 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5088 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5089 84451b3e 2018-07-10 stsp goto done;
5090 1fddf795 2021-01-20 stsp }
5091 b02560ec 2019-08-19 stsp
5092 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5093 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5094 b02560ec 2019-08-19 stsp blame->nlines--;
5095 a70480e0 2018-06-23 stsp
5096 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5097 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5098 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5099 84451b3e 2018-07-10 stsp goto done;
5100 84451b3e 2018-07-10 stsp }
5101 a70480e0 2018-06-23 stsp
5102 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5103 bd24772e 2018-07-11 stsp if (err)
5104 bd24772e 2018-07-11 stsp goto done;
5105 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5106 0ae84acc 2022-06-15 tracey pack_fds);
5107 0ae84acc 2022-06-15 tracey if (err)
5108 0ae84acc 2022-06-15 tracey goto done;
5109 bd24772e 2018-07-11 stsp
5110 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5111 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5112 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5113 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5114 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5115 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5116 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5117 245d91c1 2018-07-12 stsp goto done;
5118 245d91c1 2018-07-12 stsp }
5119 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5120 245d91c1 2018-07-12 stsp
5121 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5122 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5123 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5124 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5125 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5126 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5127 a5388363 2020-12-01 naddy s->blame_complete = 0;
5128 f5a09613 2020-12-13 naddy
5129 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5130 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5131 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5132 f5a09613 2020-12-13 naddy s->selected_line = 1;
5133 f5a09613 2020-12-13 naddy }
5134 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5135 245d91c1 2018-07-12 stsp
5136 245d91c1 2018-07-12 stsp done:
5137 a44927cc 2022-04-07 stsp if (commit)
5138 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5139 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5140 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5141 245d91c1 2018-07-12 stsp if (blob)
5142 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5143 27d434c2 2018-09-15 stsp free(obj_id);
5144 245d91c1 2018-07-12 stsp if (err)
5145 245d91c1 2018-07-12 stsp stop_blame(blame);
5146 245d91c1 2018-07-12 stsp return err;
5147 245d91c1 2018-07-12 stsp }
5148 245d91c1 2018-07-12 stsp
5149 245d91c1 2018-07-12 stsp static const struct got_error *
5150 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5151 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5152 245d91c1 2018-07-12 stsp {
5153 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5154 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5155 dbc6a6b6 2018-07-12 stsp
5156 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5157 245d91c1 2018-07-12 stsp
5158 c4843652 2019-08-12 stsp s->path = strdup(path);
5159 c4843652 2019-08-12 stsp if (s->path == NULL)
5160 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5161 c4843652 2019-08-12 stsp
5162 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5163 c4843652 2019-08-12 stsp if (err) {
5164 c4843652 2019-08-12 stsp free(s->path);
5165 7cbe629d 2018-08-04 stsp return err;
5166 c4843652 2019-08-12 stsp }
5167 245d91c1 2018-07-12 stsp
5168 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5169 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5170 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5171 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5172 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5173 fb2756b9 2018-08-04 stsp s->repo = repo;
5174 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5175 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5176 7cbe629d 2018-08-04 stsp
5177 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5178 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5179 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5180 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5181 11b20872 2019-11-08 stsp if (err)
5182 11b20872 2019-11-08 stsp return err;
5183 11b20872 2019-11-08 stsp }
5184 11b20872 2019-11-08 stsp
5185 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5186 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5187 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5188 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5189 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5190 e5a0f69f 2018-08-18 stsp
5191 a5388363 2020-12-01 naddy return run_blame(view);
5192 7cbe629d 2018-08-04 stsp }
5193 7cbe629d 2018-08-04 stsp
5194 e5a0f69f 2018-08-18 stsp static const struct got_error *
5195 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5196 7cbe629d 2018-08-04 stsp {
5197 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5198 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5199 7cbe629d 2018-08-04 stsp
5200 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5201 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5202 e5a0f69f 2018-08-18 stsp
5203 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5204 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5205 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5206 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5207 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5208 7cbe629d 2018-08-04 stsp }
5209 e5a0f69f 2018-08-18 stsp
5210 e5a0f69f 2018-08-18 stsp free(s->path);
5211 11b20872 2019-11-08 stsp free_colors(&s->colors);
5212 e5a0f69f 2018-08-18 stsp return err;
5213 7cbe629d 2018-08-04 stsp }
5214 7cbe629d 2018-08-04 stsp
5215 7cbe629d 2018-08-04 stsp static const struct got_error *
5216 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5217 6c4c42e0 2019-06-24 stsp {
5218 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5219 6c4c42e0 2019-06-24 stsp
5220 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5221 6c4c42e0 2019-06-24 stsp return NULL;
5222 6c4c42e0 2019-06-24 stsp }
5223 6c4c42e0 2019-06-24 stsp
5224 6c4c42e0 2019-06-24 stsp static const struct got_error *
5225 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5226 6c4c42e0 2019-06-24 stsp {
5227 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5228 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5229 6c4c42e0 2019-06-24 stsp int lineno;
5230 cb713507 2022-06-16 stsp char *line = NULL;
5231 826082fe 2020-12-10 stsp size_t linesize = 0;
5232 826082fe 2020-12-10 stsp ssize_t linelen;
5233 6c4c42e0 2019-06-24 stsp
5234 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5235 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5236 6c4c42e0 2019-06-24 stsp return NULL;
5237 6c4c42e0 2019-06-24 stsp }
5238 6c4c42e0 2019-06-24 stsp
5239 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5240 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5241 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5242 6c4c42e0 2019-06-24 stsp else
5243 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5244 487cd7d2 2021-12-17 stsp } else
5245 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5246 6c4c42e0 2019-06-24 stsp
5247 6c4c42e0 2019-06-24 stsp while (1) {
5248 6c4c42e0 2019-06-24 stsp off_t offset;
5249 6c4c42e0 2019-06-24 stsp
5250 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5251 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5252 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5253 6c4c42e0 2019-06-24 stsp break;
5254 6c4c42e0 2019-06-24 stsp }
5255 2246482e 2019-06-25 stsp
5256 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5257 6c4c42e0 2019-06-24 stsp lineno = 1;
5258 6c4c42e0 2019-06-24 stsp else
5259 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5260 6c4c42e0 2019-06-24 stsp }
5261 6c4c42e0 2019-06-24 stsp
5262 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5263 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5264 6c4c42e0 2019-06-24 stsp free(line);
5265 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5266 6c4c42e0 2019-06-24 stsp }
5267 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5268 cb713507 2022-06-16 stsp if (linelen != -1) {
5269 cb713507 2022-06-16 stsp char *exstr;
5270 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5271 cb713507 2022-06-16 stsp if (err)
5272 cb713507 2022-06-16 stsp break;
5273 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5274 cb713507 2022-06-16 stsp &view->regmatch)) {
5275 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5276 cb713507 2022-06-16 stsp s->matched_line = lineno;
5277 cb713507 2022-06-16 stsp free(exstr);
5278 cb713507 2022-06-16 stsp break;
5279 cb713507 2022-06-16 stsp }
5280 cb713507 2022-06-16 stsp free(exstr);
5281 6c4c42e0 2019-06-24 stsp }
5282 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5283 6c4c42e0 2019-06-24 stsp lineno++;
5284 6c4c42e0 2019-06-24 stsp else
5285 6c4c42e0 2019-06-24 stsp lineno--;
5286 6c4c42e0 2019-06-24 stsp }
5287 826082fe 2020-12-10 stsp free(line);
5288 6c4c42e0 2019-06-24 stsp
5289 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5290 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5291 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5292 6c4c42e0 2019-06-24 stsp }
5293 6c4c42e0 2019-06-24 stsp
5294 145b6838 2022-06-16 stsp return err;
5295 6c4c42e0 2019-06-24 stsp }
5296 6c4c42e0 2019-06-24 stsp
5297 6c4c42e0 2019-06-24 stsp static const struct got_error *
5298 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5299 7cbe629d 2018-08-04 stsp {
5300 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5301 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5302 2b380cc8 2018-10-24 stsp int errcode;
5303 2b380cc8 2018-10-24 stsp
5304 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5305 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5306 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5307 2b380cc8 2018-10-24 stsp if (errcode)
5308 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5309 51fe7530 2019-08-19 stsp
5310 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5311 2b380cc8 2018-10-24 stsp }
5312 e5a0f69f 2018-08-18 stsp
5313 51fe7530 2019-08-19 stsp if (s->blame_complete)
5314 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5315 51fe7530 2019-08-19 stsp
5316 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5317 e5a0f69f 2018-08-18 stsp
5318 9b058f45 2022-06-30 mark view_border(view);
5319 e5a0f69f 2018-08-18 stsp return err;
5320 e5a0f69f 2018-08-18 stsp }
5321 e5a0f69f 2018-08-18 stsp
5322 e5a0f69f 2018-08-18 stsp static const struct got_error *
5323 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5324 e5a0f69f 2018-08-18 stsp {
5325 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5326 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5327 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5328 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5329 9b058f45 2022-06-30 mark
5330 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5331 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5332 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5333 9b058f45 2022-06-30 mark --eos; /* border */
5334 7cbe629d 2018-08-04 stsp
5335 e5a0f69f 2018-08-18 stsp switch (ch) {
5336 145b6838 2022-06-16 stsp case '0':
5337 145b6838 2022-06-16 stsp view->x = 0;
5338 145b6838 2022-06-16 stsp break;
5339 145b6838 2022-06-16 stsp case '$':
5340 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5341 640cd7ff 2022-06-22 mark view->count = 0;
5342 145b6838 2022-06-16 stsp break;
5343 145b6838 2022-06-16 stsp case KEY_RIGHT:
5344 145b6838 2022-06-16 stsp case 'l':
5345 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5346 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5347 640cd7ff 2022-06-22 mark else
5348 640cd7ff 2022-06-22 mark view->count = 0;
5349 145b6838 2022-06-16 stsp break;
5350 145b6838 2022-06-16 stsp case KEY_LEFT:
5351 145b6838 2022-06-16 stsp case 'h':
5352 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5353 640cd7ff 2022-06-22 mark if (view->x <= 0)
5354 640cd7ff 2022-06-22 mark view->count = 0;
5355 145b6838 2022-06-16 stsp break;
5356 1e37a5c2 2019-05-12 jcs case 'q':
5357 1e37a5c2 2019-05-12 jcs s->done = 1;
5358 4deef56f 2021-09-02 naddy break;
5359 4deef56f 2021-09-02 naddy case 'g':
5360 4deef56f 2021-09-02 naddy case KEY_HOME:
5361 4deef56f 2021-09-02 naddy s->selected_line = 1;
5362 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5363 640cd7ff 2022-06-22 mark view->count = 0;
5364 4deef56f 2021-09-02 naddy break;
5365 4deef56f 2021-09-02 naddy case 'G':
5366 4deef56f 2021-09-02 naddy case KEY_END:
5367 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5368 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5369 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5370 4deef56f 2021-09-02 naddy } else {
5371 9b058f45 2022-06-30 mark s->selected_line = eos;
5372 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5373 4deef56f 2021-09-02 naddy }
5374 640cd7ff 2022-06-22 mark view->count = 0;
5375 1e37a5c2 2019-05-12 jcs break;
5376 1e37a5c2 2019-05-12 jcs case 'k':
5377 1e37a5c2 2019-05-12 jcs case KEY_UP:
5378 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5379 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5380 1e37a5c2 2019-05-12 jcs s->selected_line--;
5381 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5382 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5383 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5384 640cd7ff 2022-06-22 mark else
5385 640cd7ff 2022-06-22 mark view->count = 0;
5386 1e37a5c2 2019-05-12 jcs break;
5387 83cc4199 2022-06-13 stsp case CTRL('u'):
5388 33c3719a 2022-06-15 stsp case 'u':
5389 83cc4199 2022-06-13 stsp nscroll /= 2;
5390 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5391 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5392 ea025d1d 2020-02-22 naddy case CTRL('b'):
5393 61417565 2022-06-20 mark case 'b':
5394 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5395 640cd7ff 2022-06-22 mark if (view->count > 1)
5396 640cd7ff 2022-06-22 mark nscroll += nscroll;
5397 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5398 640cd7ff 2022-06-22 mark view->count = 0;
5399 e5a0f69f 2018-08-18 stsp break;
5400 1e37a5c2 2019-05-12 jcs }
5401 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5402 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5403 1e37a5c2 2019-05-12 jcs else
5404 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5405 1e37a5c2 2019-05-12 jcs break;
5406 1e37a5c2 2019-05-12 jcs case 'j':
5407 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5408 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5409 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5410 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5411 1e37a5c2 2019-05-12 jcs s->selected_line++;
5412 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5413 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5414 640cd7ff 2022-06-22 mark else
5415 640cd7ff 2022-06-22 mark view->count = 0;
5416 1e37a5c2 2019-05-12 jcs break;
5417 61417565 2022-06-20 mark case 'c':
5418 1e37a5c2 2019-05-12 jcs case 'p': {
5419 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5420 640cd7ff 2022-06-22 mark
5421 640cd7ff 2022-06-22 mark view->count = 0;
5422 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5423 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5424 1e37a5c2 2019-05-12 jcs if (id == NULL)
5425 e5a0f69f 2018-08-18 stsp break;
5426 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5427 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5428 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5429 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5430 1e37a5c2 2019-05-12 jcs int obj_type;
5431 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5432 1e37a5c2 2019-05-12 jcs s->repo, id);
5433 e5a0f69f 2018-08-18 stsp if (err)
5434 e5a0f69f 2018-08-18 stsp break;
5435 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5436 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5437 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5438 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5439 e5a0f69f 2018-08-18 stsp break;
5440 e5a0f69f 2018-08-18 stsp }
5441 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5442 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5443 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5444 a44927cc 2022-04-07 stsp if (err)
5445 a44927cc 2022-04-07 stsp break;
5446 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5447 a44927cc 2022-04-07 stsp pcommit, s->path);
5448 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5449 e5a0f69f 2018-08-18 stsp if (err) {
5450 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5451 1e37a5c2 2019-05-12 jcs err = NULL;
5452 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5453 e5a0f69f 2018-08-18 stsp break;
5454 e5a0f69f 2018-08-18 stsp }
5455 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5456 1e37a5c2 2019-05-12 jcs blob_id);
5457 1e37a5c2 2019-05-12 jcs free(blob_id);
5458 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5459 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5460 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5461 e5a0f69f 2018-08-18 stsp break;
5462 1e37a5c2 2019-05-12 jcs }
5463 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5464 d7b5a0e8 2022-04-20 stsp &pid->id);
5465 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5466 1e37a5c2 2019-05-12 jcs } else {
5467 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5468 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5469 1e37a5c2 2019-05-12 jcs break;
5470 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5471 1e37a5c2 2019-05-12 jcs id);
5472 1e37a5c2 2019-05-12 jcs }
5473 1e37a5c2 2019-05-12 jcs if (err)
5474 e5a0f69f 2018-08-18 stsp break;
5475 1e37a5c2 2019-05-12 jcs s->done = 1;
5476 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5477 1e37a5c2 2019-05-12 jcs s->done = 0;
5478 1e37a5c2 2019-05-12 jcs if (thread_err)
5479 1e37a5c2 2019-05-12 jcs break;
5480 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5481 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5482 a5388363 2020-12-01 naddy err = run_blame(view);
5483 1e37a5c2 2019-05-12 jcs if (err)
5484 1e37a5c2 2019-05-12 jcs break;
5485 1e37a5c2 2019-05-12 jcs break;
5486 1e37a5c2 2019-05-12 jcs }
5487 61417565 2022-06-20 mark case 'C': {
5488 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5489 640cd7ff 2022-06-22 mark
5490 640cd7ff 2022-06-22 mark view->count = 0;
5491 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5492 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5493 1e37a5c2 2019-05-12 jcs break;
5494 1e37a5c2 2019-05-12 jcs s->done = 1;
5495 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5496 1e37a5c2 2019-05-12 jcs s->done = 0;
5497 1e37a5c2 2019-05-12 jcs if (thread_err)
5498 1e37a5c2 2019-05-12 jcs break;
5499 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5500 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5501 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5502 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5503 a5388363 2020-12-01 naddy err = run_blame(view);
5504 1e37a5c2 2019-05-12 jcs if (err)
5505 1e37a5c2 2019-05-12 jcs break;
5506 1e37a5c2 2019-05-12 jcs break;
5507 1e37a5c2 2019-05-12 jcs }
5508 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5509 1e37a5c2 2019-05-12 jcs case '\r': {
5510 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5511 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5512 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5513 640cd7ff 2022-06-22 mark
5514 640cd7ff 2022-06-22 mark view->count = 0;
5515 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5516 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5517 1e37a5c2 2019-05-12 jcs if (id == NULL)
5518 1e37a5c2 2019-05-12 jcs break;
5519 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5520 1e37a5c2 2019-05-12 jcs if (err)
5521 1e37a5c2 2019-05-12 jcs break;
5522 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5523 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5524 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5525 9b058f45 2022-06-30 mark
5526 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5527 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5528 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5529 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5530 1e37a5c2 2019-05-12 jcs break;
5531 15a94983 2018-12-23 stsp }
5532 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5533 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5534 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5535 1e37a5c2 2019-05-12 jcs if (err) {
5536 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5537 1e37a5c2 2019-05-12 jcs break;
5538 1e37a5c2 2019-05-12 jcs }
5539 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5540 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5541 9b058f45 2022-06-30 mark if (err)
5542 9b058f45 2022-06-30 mark break;
5543 9b058f45 2022-06-30 mark }
5544 9b058f45 2022-06-30 mark
5545 e78dc838 2020-12-04 stsp view->focussed = 0;
5546 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5547 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5548 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5549 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5550 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5551 1e37a5c2 2019-05-12 jcs if (err)
5552 34bc9ec9 2019-02-22 stsp break;
5553 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5554 0dbbbe90 2022-06-17 op if (err)
5555 0dbbbe90 2022-06-17 op break;
5556 e78dc838 2020-12-04 stsp view->focus_child = 1;
5557 1e37a5c2 2019-05-12 jcs } else
5558 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5559 1e37a5c2 2019-05-12 jcs if (err)
5560 e5a0f69f 2018-08-18 stsp break;
5561 1e37a5c2 2019-05-12 jcs break;
5562 1e37a5c2 2019-05-12 jcs }
5563 83cc4199 2022-06-13 stsp case CTRL('d'):
5564 33c3719a 2022-06-15 stsp case 'd':
5565 83cc4199 2022-06-13 stsp nscroll /= 2;
5566 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5567 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5568 ea025d1d 2020-02-22 naddy case CTRL('f'):
5569 61417565 2022-06-20 mark case 'f':
5570 1e37a5c2 2019-05-12 jcs case ' ':
5571 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5572 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5573 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5574 640cd7ff 2022-06-22 mark view->count = 0;
5575 e5a0f69f 2018-08-18 stsp break;
5576 1e37a5c2 2019-05-12 jcs }
5577 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5578 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5579 83cc4199 2022-06-13 stsp s->selected_line +=
5580 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5581 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5582 1e37a5c2 2019-05-12 jcs }
5583 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5584 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5585 1e37a5c2 2019-05-12 jcs else
5586 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5587 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5588 1e37a5c2 2019-05-12 jcs break;
5589 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5590 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5591 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5592 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5593 1e37a5c2 2019-05-12 jcs }
5594 1e37a5c2 2019-05-12 jcs break;
5595 1e37a5c2 2019-05-12 jcs default:
5596 640cd7ff 2022-06-22 mark view->count = 0;
5597 1e37a5c2 2019-05-12 jcs break;
5598 a70480e0 2018-06-23 stsp }
5599 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5600 a70480e0 2018-06-23 stsp }
5601 a70480e0 2018-06-23 stsp
5602 a70480e0 2018-06-23 stsp static const struct got_error *
5603 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5604 9f7d7167 2018-04-29 stsp {
5605 a70480e0 2018-06-23 stsp const struct got_error *error;
5606 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5607 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5608 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5609 0587e10c 2020-07-23 stsp char *link_target = NULL;
5610 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5611 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5612 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5613 a70480e0 2018-06-23 stsp int ch;
5614 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5615 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5616 a70480e0 2018-06-23 stsp
5617 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5618 a70480e0 2018-06-23 stsp switch (ch) {
5619 a70480e0 2018-06-23 stsp case 'c':
5620 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5621 a70480e0 2018-06-23 stsp break;
5622 69069811 2018-08-02 stsp case 'r':
5623 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5624 69069811 2018-08-02 stsp if (repo_path == NULL)
5625 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5626 9ba1d308 2019-10-21 stsp optarg);
5627 69069811 2018-08-02 stsp break;
5628 a70480e0 2018-06-23 stsp default:
5629 17020d27 2019-03-07 stsp usage_blame();
5630 a70480e0 2018-06-23 stsp /* NOTREACHED */
5631 a70480e0 2018-06-23 stsp }
5632 a70480e0 2018-06-23 stsp }
5633 a70480e0 2018-06-23 stsp
5634 a70480e0 2018-06-23 stsp argc -= optind;
5635 a70480e0 2018-06-23 stsp argv += optind;
5636 a70480e0 2018-06-23 stsp
5637 f135c941 2020-02-20 stsp if (argc != 1)
5638 a70480e0 2018-06-23 stsp usage_blame();
5639 6962eb72 2020-02-20 stsp
5640 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5641 0ae84acc 2022-06-15 tracey if (error != NULL)
5642 0ae84acc 2022-06-15 tracey goto done;
5643 0ae84acc 2022-06-15 tracey
5644 69069811 2018-08-02 stsp if (repo_path == NULL) {
5645 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5646 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5647 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5648 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5649 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5650 c156c7a4 2020-12-18 stsp goto done;
5651 f135c941 2020-02-20 stsp if (worktree)
5652 eb41ed75 2019-02-05 stsp repo_path =
5653 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5654 f135c941 2020-02-20 stsp else
5655 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5656 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5657 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5658 c156c7a4 2020-12-18 stsp goto done;
5659 c156c7a4 2020-12-18 stsp }
5660 f135c941 2020-02-20 stsp }
5661 a915003a 2019-02-05 stsp
5662 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5663 c02c541e 2019-03-29 stsp if (error != NULL)
5664 8e94dd5b 2019-01-04 stsp goto done;
5665 69069811 2018-08-02 stsp
5666 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5667 f135c941 2020-02-20 stsp worktree);
5668 c02c541e 2019-03-29 stsp if (error)
5669 92205607 2019-01-04 stsp goto done;
5670 69069811 2018-08-02 stsp
5671 f135c941 2020-02-20 stsp init_curses();
5672 f135c941 2020-02-20 stsp
5673 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5674 51a10b52 2020-12-26 stsp if (error)
5675 51a10b52 2020-12-26 stsp goto done;
5676 51a10b52 2020-12-26 stsp
5677 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5678 eb41ed75 2019-02-05 stsp if (error)
5679 69069811 2018-08-02 stsp goto done;
5680 a70480e0 2018-06-23 stsp
5681 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5682 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5683 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5684 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5685 a70480e0 2018-06-23 stsp if (error != NULL)
5686 66b4983c 2018-06-23 stsp goto done;
5687 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5688 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5689 a70480e0 2018-06-23 stsp } else {
5690 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5691 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5692 a70480e0 2018-06-23 stsp }
5693 a19e88aa 2018-06-23 stsp if (error != NULL)
5694 8b473291 2019-02-21 stsp goto done;
5695 8b473291 2019-02-21 stsp
5696 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5697 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5698 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5699 e1cd8fed 2018-08-01 stsp goto done;
5700 e1cd8fed 2018-08-01 stsp }
5701 0587e10c 2020-07-23 stsp
5702 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5703 a44927cc 2022-04-07 stsp if (error)
5704 a44927cc 2022-04-07 stsp goto done;
5705 a44927cc 2022-04-07 stsp
5706 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5707 a44927cc 2022-04-07 stsp commit, repo);
5708 7cbe629d 2018-08-04 stsp if (error)
5709 7cbe629d 2018-08-04 stsp goto done;
5710 0587e10c 2020-07-23 stsp
5711 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5712 78756c87 2020-11-24 stsp commit_id, repo);
5713 0587e10c 2020-07-23 stsp if (error)
5714 0587e10c 2020-07-23 stsp goto done;
5715 12314ad4 2019-08-31 stsp if (worktree) {
5716 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5717 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5718 12314ad4 2019-08-31 stsp worktree = NULL;
5719 12314ad4 2019-08-31 stsp }
5720 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5721 a70480e0 2018-06-23 stsp done:
5722 69069811 2018-08-02 stsp free(repo_path);
5723 f135c941 2020-02-20 stsp free(in_repo_path);
5724 0587e10c 2020-07-23 stsp free(link_target);
5725 69069811 2018-08-02 stsp free(cwd);
5726 a70480e0 2018-06-23 stsp free(commit_id);
5727 a44927cc 2022-04-07 stsp if (commit)
5728 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5729 eb41ed75 2019-02-05 stsp if (worktree)
5730 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5731 1d0f4054 2021-06-17 stsp if (repo) {
5732 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5733 1d0f4054 2021-06-17 stsp if (error == NULL)
5734 1d0f4054 2021-06-17 stsp error = close_err;
5735 1d0f4054 2021-06-17 stsp }
5736 0ae84acc 2022-06-15 tracey if (pack_fds) {
5737 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5738 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5739 0ae84acc 2022-06-15 tracey if (error == NULL)
5740 0ae84acc 2022-06-15 tracey error = pack_err;
5741 0ae84acc 2022-06-15 tracey }
5742 51a10b52 2020-12-26 stsp tog_free_refs();
5743 a70480e0 2018-06-23 stsp return error;
5744 ffd1d5e5 2018-06-23 stsp }
5745 ffd1d5e5 2018-06-23 stsp
5746 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5747 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5748 ffd1d5e5 2018-06-23 stsp {
5749 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5750 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5751 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5752 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5753 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5754 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5755 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5756 ffd1d5e5 2018-06-23 stsp
5757 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5758 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5759 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5760 9b058f45 2022-06-30 mark --limit; /* border */
5761 ffd1d5e5 2018-06-23 stsp
5762 f7d12f7e 2018-08-01 stsp werase(view->window);
5763 ffd1d5e5 2018-06-23 stsp
5764 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5765 ffd1d5e5 2018-06-23 stsp return NULL;
5766 ffd1d5e5 2018-06-23 stsp
5767 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5768 ccda2f4d 2022-06-16 stsp 0, 0);
5769 ffd1d5e5 2018-06-23 stsp if (err)
5770 ffd1d5e5 2018-06-23 stsp return err;
5771 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5772 a3404814 2018-09-02 stsp wstandout(view->window);
5773 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5774 11b20872 2019-11-08 stsp if (tc)
5775 11b20872 2019-11-08 stsp wattr_on(view->window,
5776 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5777 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5778 11b20872 2019-11-08 stsp if (tc)
5779 11b20872 2019-11-08 stsp wattr_off(view->window,
5780 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5781 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5782 a3404814 2018-09-02 stsp wstandend(view->window);
5783 2550e4c3 2018-07-13 stsp free(wline);
5784 2550e4c3 2018-07-13 stsp wline = NULL;
5785 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5786 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5787 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5788 ffd1d5e5 2018-06-23 stsp return NULL;
5789 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5790 ccda2f4d 2022-06-16 stsp 0, 0);
5791 ce52c690 2018-06-23 stsp if (err)
5792 ce52c690 2018-06-23 stsp return err;
5793 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5794 2550e4c3 2018-07-13 stsp free(wline);
5795 2550e4c3 2018-07-13 stsp wline = NULL;
5796 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5797 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5798 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5799 ffd1d5e5 2018-06-23 stsp return NULL;
5800 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5801 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5802 a1eca9bb 2018-06-23 stsp return NULL;
5803 ffd1d5e5 2018-06-23 stsp
5804 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5805 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5806 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5807 0cf4efb1 2018-09-29 stsp if (view->focussed)
5808 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5809 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5810 ffd1d5e5 2018-06-23 stsp }
5811 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5812 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5813 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5814 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5815 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5816 ffd1d5e5 2018-06-23 stsp return NULL;
5817 ffd1d5e5 2018-06-23 stsp n = 1;
5818 ffd1d5e5 2018-06-23 stsp } else {
5819 ffd1d5e5 2018-06-23 stsp n = 0;
5820 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5821 ffd1d5e5 2018-06-23 stsp }
5822 ffd1d5e5 2018-06-23 stsp
5823 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5824 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5825 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5826 848d6979 2019-08-12 stsp const char *modestr = "";
5827 56e0773d 2019-11-28 stsp mode_t mode;
5828 1d13200f 2018-07-12 stsp
5829 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5830 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5831 56e0773d 2019-11-28 stsp
5832 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5833 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5834 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5835 1d13200f 2018-07-12 stsp if (err)
5836 638f9024 2019-05-13 stsp return got_error_from_errno(
5837 230a42bd 2019-05-11 jcs "got_object_id_str");
5838 1d13200f 2018-07-12 stsp }
5839 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5840 63c5ca5d 2019-08-24 stsp modestr = "$";
5841 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5842 0d6c6ee3 2020-05-20 stsp int i;
5843 0d6c6ee3 2020-05-20 stsp
5844 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5845 d86d3b18 2020-12-01 naddy te, s->repo);
5846 0d6c6ee3 2020-05-20 stsp if (err) {
5847 0d6c6ee3 2020-05-20 stsp free(id_str);
5848 0d6c6ee3 2020-05-20 stsp return err;
5849 0d6c6ee3 2020-05-20 stsp }
5850 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5851 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5852 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5853 0d6c6ee3 2020-05-20 stsp }
5854 848d6979 2019-08-12 stsp modestr = "@";
5855 0d6c6ee3 2020-05-20 stsp }
5856 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5857 848d6979 2019-08-12 stsp modestr = "/";
5858 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5859 848d6979 2019-08-12 stsp modestr = "*";
5860 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5861 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5862 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5863 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5864 1d13200f 2018-07-12 stsp free(id_str);
5865 0d6c6ee3 2020-05-20 stsp free(link_target);
5866 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5867 1d13200f 2018-07-12 stsp }
5868 1d13200f 2018-07-12 stsp free(id_str);
5869 0d6c6ee3 2020-05-20 stsp free(link_target);
5870 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5871 ccda2f4d 2022-06-16 stsp 0, 0);
5872 ffd1d5e5 2018-06-23 stsp if (err) {
5873 ffd1d5e5 2018-06-23 stsp free(line);
5874 ffd1d5e5 2018-06-23 stsp break;
5875 ffd1d5e5 2018-06-23 stsp }
5876 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5877 0cf4efb1 2018-09-29 stsp if (view->focussed)
5878 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5879 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5880 ffd1d5e5 2018-06-23 stsp }
5881 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5882 f26dddb7 2019-11-08 stsp if (tc)
5883 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5884 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5885 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5886 f26dddb7 2019-11-08 stsp if (tc)
5887 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5888 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5889 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5890 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5891 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5892 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5893 ffd1d5e5 2018-06-23 stsp free(line);
5894 2550e4c3 2018-07-13 stsp free(wline);
5895 2550e4c3 2018-07-13 stsp wline = NULL;
5896 ffd1d5e5 2018-06-23 stsp n++;
5897 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5898 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5899 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5900 ffd1d5e5 2018-06-23 stsp break;
5901 ffd1d5e5 2018-06-23 stsp }
5902 ffd1d5e5 2018-06-23 stsp
5903 ffd1d5e5 2018-06-23 stsp return err;
5904 ffd1d5e5 2018-06-23 stsp }
5905 ffd1d5e5 2018-06-23 stsp
5906 ffd1d5e5 2018-06-23 stsp static void
5907 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5908 ffd1d5e5 2018-06-23 stsp {
5909 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5910 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5911 fa86c4bf 2020-11-29 stsp int i = 0;
5912 ffd1d5e5 2018-06-23 stsp
5913 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5914 ffd1d5e5 2018-06-23 stsp return;
5915 ffd1d5e5 2018-06-23 stsp
5916 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5917 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5918 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5919 fa86c4bf 2020-11-29 stsp if (!isroot)
5920 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5921 fa86c4bf 2020-11-29 stsp break;
5922 fa86c4bf 2020-11-29 stsp }
5923 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5924 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5925 ffd1d5e5 2018-06-23 stsp }
5926 ffd1d5e5 2018-06-23 stsp }
5927 ffd1d5e5 2018-06-23 stsp
5928 9b058f45 2022-06-30 mark static const struct got_error *
5929 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
5930 ffd1d5e5 2018-06-23 stsp {
5931 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
5932 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5933 ffd1d5e5 2018-06-23 stsp int n = 0;
5934 ffd1d5e5 2018-06-23 stsp
5935 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5936 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5937 694d3271 2020-12-01 naddy s->first_displayed_entry);
5938 694d3271 2020-12-01 naddy else
5939 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5940 56e0773d 2019-11-28 stsp
5941 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5942 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
5943 9b058f45 2022-06-30 mark if (last)
5944 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
5945 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5946 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5947 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5948 768394f3 2019-01-24 stsp }
5949 ffd1d5e5 2018-06-23 stsp }
5950 9b058f45 2022-06-30 mark
5951 9b058f45 2022-06-30 mark return NULL;
5952 ffd1d5e5 2018-06-23 stsp }
5953 ffd1d5e5 2018-06-23 stsp
5954 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5955 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5956 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5957 ffd1d5e5 2018-06-23 stsp {
5958 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5959 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5960 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5961 ffd1d5e5 2018-06-23 stsp
5962 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5963 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5964 56e0773d 2019-11-28 stsp + 1 /* slash */;
5965 ce52c690 2018-06-23 stsp if (te)
5966 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5967 ce52c690 2018-06-23 stsp
5968 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5969 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5970 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5971 ffd1d5e5 2018-06-23 stsp
5972 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5973 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5974 d9765a41 2018-06-23 stsp while (pt) {
5975 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5976 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5977 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5978 cb2ebc8a 2018-06-23 stsp goto done;
5979 cb2ebc8a 2018-06-23 stsp }
5980 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5981 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5982 cb2ebc8a 2018-06-23 stsp goto done;
5983 cb2ebc8a 2018-06-23 stsp }
5984 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5985 ffd1d5e5 2018-06-23 stsp }
5986 ce52c690 2018-06-23 stsp if (te) {
5987 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5988 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5989 ce52c690 2018-06-23 stsp goto done;
5990 ce52c690 2018-06-23 stsp }
5991 cb2ebc8a 2018-06-23 stsp }
5992 ce52c690 2018-06-23 stsp done:
5993 ce52c690 2018-06-23 stsp if (err) {
5994 ce52c690 2018-06-23 stsp free(*path);
5995 ce52c690 2018-06-23 stsp *path = NULL;
5996 ce52c690 2018-06-23 stsp }
5997 ce52c690 2018-06-23 stsp return err;
5998 ce52c690 2018-06-23 stsp }
5999 ce52c690 2018-06-23 stsp
6000 ce52c690 2018-06-23 stsp static const struct got_error *
6001 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6002 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6003 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6004 ce52c690 2018-06-23 stsp {
6005 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6006 ce52c690 2018-06-23 stsp char *path;
6007 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6008 a0de39f3 2019-08-09 stsp
6009 a0de39f3 2019-08-09 stsp *new_view = NULL;
6010 69efd4c4 2018-07-18 stsp
6011 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6012 ce52c690 2018-06-23 stsp if (err)
6013 ce52c690 2018-06-23 stsp return err;
6014 ffd1d5e5 2018-06-23 stsp
6015 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6016 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6017 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6018 83ce39e3 2019-08-12 stsp goto done;
6019 83ce39e3 2019-08-12 stsp }
6020 cdf1ee82 2018-08-01 stsp
6021 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6022 e5a0f69f 2018-08-18 stsp if (err) {
6023 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6024 fc06ba56 2019-08-22 stsp err = NULL;
6025 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6026 e5a0f69f 2018-08-18 stsp } else
6027 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6028 83ce39e3 2019-08-12 stsp done:
6029 83ce39e3 2019-08-12 stsp free(path);
6030 69efd4c4 2018-07-18 stsp return err;
6031 69efd4c4 2018-07-18 stsp }
6032 69efd4c4 2018-07-18 stsp
6033 69efd4c4 2018-07-18 stsp static const struct got_error *
6034 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6035 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6036 69efd4c4 2018-07-18 stsp {
6037 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6038 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6039 69efd4c4 2018-07-18 stsp char *path;
6040 a0de39f3 2019-08-09 stsp
6041 a0de39f3 2019-08-09 stsp *new_view = NULL;
6042 69efd4c4 2018-07-18 stsp
6043 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6044 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6045 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6046 e5a0f69f 2018-08-18 stsp
6047 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6048 69efd4c4 2018-07-18 stsp if (err)
6049 69efd4c4 2018-07-18 stsp return err;
6050 69efd4c4 2018-07-18 stsp
6051 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6052 4e97c21c 2020-12-06 stsp path, 0);
6053 ba4f502b 2018-08-04 stsp if (err)
6054 e5a0f69f 2018-08-18 stsp view_close(log_view);
6055 e5a0f69f 2018-08-18 stsp else
6056 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6057 cb2ebc8a 2018-06-23 stsp free(path);
6058 cb2ebc8a 2018-06-23 stsp return err;
6059 ffd1d5e5 2018-06-23 stsp }
6060 ffd1d5e5 2018-06-23 stsp
6061 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6062 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6063 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6064 ffd1d5e5 2018-06-23 stsp {
6065 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6066 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6067 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6068 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6069 ffd1d5e5 2018-06-23 stsp
6070 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6071 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6072 bc573f3b 2021-07-10 stsp
6073 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6074 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6075 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6076 ffd1d5e5 2018-06-23 stsp
6077 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6078 bc573f3b 2021-07-10 stsp if (err)
6079 bc573f3b 2021-07-10 stsp goto done;
6080 bc573f3b 2021-07-10 stsp
6081 bc573f3b 2021-07-10 stsp /*
6082 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6083 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6084 bc573f3b 2021-07-10 stsp * closed on demand.
6085 bc573f3b 2021-07-10 stsp */
6086 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6087 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6088 bc573f3b 2021-07-10 stsp if (err)
6089 bc573f3b 2021-07-10 stsp goto done;
6090 bc573f3b 2021-07-10 stsp s->tree = s->root;
6091 bc573f3b 2021-07-10 stsp
6092 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6093 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6094 ffd1d5e5 2018-06-23 stsp goto done;
6095 ffd1d5e5 2018-06-23 stsp
6096 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6097 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6098 ffd1d5e5 2018-06-23 stsp goto done;
6099 ffd1d5e5 2018-06-23 stsp }
6100 ffd1d5e5 2018-06-23 stsp
6101 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6102 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6103 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6104 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6105 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6106 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6107 9cd7cbd1 2020-12-07 stsp goto done;
6108 9cd7cbd1 2020-12-07 stsp }
6109 9cd7cbd1 2020-12-07 stsp }
6110 fb2756b9 2018-08-04 stsp s->repo = repo;
6111 c0b01bdb 2019-11-08 stsp
6112 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6113 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6114 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6115 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6116 c0b01bdb 2019-11-08 stsp if (err)
6117 c0b01bdb 2019-11-08 stsp goto done;
6118 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6119 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6120 bc573f3b 2021-07-10 stsp if (err)
6121 c0b01bdb 2019-11-08 stsp goto done;
6122 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6123 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6124 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6125 bc573f3b 2021-07-10 stsp if (err)
6126 c0b01bdb 2019-11-08 stsp goto done;
6127 e5a0f69f 2018-08-18 stsp
6128 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6129 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6130 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6131 bc573f3b 2021-07-10 stsp if (err)
6132 11b20872 2019-11-08 stsp goto done;
6133 11b20872 2019-11-08 stsp
6134 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6135 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6136 bc573f3b 2021-07-10 stsp if (err)
6137 c0b01bdb 2019-11-08 stsp goto done;
6138 c0b01bdb 2019-11-08 stsp }
6139 c0b01bdb 2019-11-08 stsp
6140 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6141 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6142 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6143 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6144 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6145 ad80ab7b 2018-08-04 stsp done:
6146 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6147 bc573f3b 2021-07-10 stsp if (commit)
6148 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6149 bc573f3b 2021-07-10 stsp if (err)
6150 bc573f3b 2021-07-10 stsp close_tree_view(view);
6151 ad80ab7b 2018-08-04 stsp return err;
6152 ad80ab7b 2018-08-04 stsp }
6153 ad80ab7b 2018-08-04 stsp
6154 e5a0f69f 2018-08-18 stsp static const struct got_error *
6155 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6156 ad80ab7b 2018-08-04 stsp {
6157 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6158 ad80ab7b 2018-08-04 stsp
6159 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6160 fb2756b9 2018-08-04 stsp free(s->tree_label);
6161 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6162 6484ec90 2018-09-29 stsp free(s->commit_id);
6163 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6164 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6165 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6166 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6167 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6168 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6169 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6170 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6171 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6172 ad80ab7b 2018-08-04 stsp free(parent);
6173 ad80ab7b 2018-08-04 stsp
6174 ad80ab7b 2018-08-04 stsp }
6175 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6176 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6177 bc573f3b 2021-07-10 stsp if (s->root)
6178 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6179 7c32bd05 2019-06-22 stsp return NULL;
6180 7c32bd05 2019-06-22 stsp }
6181 7c32bd05 2019-06-22 stsp
6182 7c32bd05 2019-06-22 stsp static const struct got_error *
6183 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6184 7c32bd05 2019-06-22 stsp {
6185 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6186 7c32bd05 2019-06-22 stsp
6187 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6188 7c32bd05 2019-06-22 stsp return NULL;
6189 7c32bd05 2019-06-22 stsp }
6190 7c32bd05 2019-06-22 stsp
6191 7c32bd05 2019-06-22 stsp static int
6192 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6193 7c32bd05 2019-06-22 stsp {
6194 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6195 7c32bd05 2019-06-22 stsp
6196 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6197 56e0773d 2019-11-28 stsp 0) == 0;
6198 7c32bd05 2019-06-22 stsp }
6199 7c32bd05 2019-06-22 stsp
6200 7c32bd05 2019-06-22 stsp static const struct got_error *
6201 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6202 7c32bd05 2019-06-22 stsp {
6203 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6204 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6205 7c32bd05 2019-06-22 stsp
6206 7c32bd05 2019-06-22 stsp if (!view->searching) {
6207 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6208 7c32bd05 2019-06-22 stsp return NULL;
6209 7c32bd05 2019-06-22 stsp }
6210 7c32bd05 2019-06-22 stsp
6211 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6212 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6213 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6214 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6215 56e0773d 2019-11-28 stsp s->selected_entry);
6216 7c32bd05 2019-06-22 stsp else
6217 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6218 56e0773d 2019-11-28 stsp } else {
6219 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6220 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6221 56e0773d 2019-11-28 stsp else
6222 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6223 56e0773d 2019-11-28 stsp s->selected_entry);
6224 7c32bd05 2019-06-22 stsp }
6225 7c32bd05 2019-06-22 stsp } else {
6226 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6227 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6228 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6229 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6230 56e0773d 2019-11-28 stsp else
6231 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6232 7c32bd05 2019-06-22 stsp }
6233 7c32bd05 2019-06-22 stsp
6234 7c32bd05 2019-06-22 stsp while (1) {
6235 56e0773d 2019-11-28 stsp if (te == NULL) {
6236 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6237 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6238 ac66afb8 2019-06-24 stsp return NULL;
6239 ac66afb8 2019-06-24 stsp }
6240 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6241 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6242 56e0773d 2019-11-28 stsp else
6243 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6244 7c32bd05 2019-06-22 stsp }
6245 7c32bd05 2019-06-22 stsp
6246 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6247 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6248 56e0773d 2019-11-28 stsp s->matched_entry = te;
6249 7c32bd05 2019-06-22 stsp break;
6250 7c32bd05 2019-06-22 stsp }
6251 7c32bd05 2019-06-22 stsp
6252 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6253 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6254 56e0773d 2019-11-28 stsp else
6255 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6256 7c32bd05 2019-06-22 stsp }
6257 e5a0f69f 2018-08-18 stsp
6258 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6259 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6260 7c32bd05 2019-06-22 stsp s->selected = 0;
6261 7c32bd05 2019-06-22 stsp }
6262 7c32bd05 2019-06-22 stsp
6263 e5a0f69f 2018-08-18 stsp return NULL;
6264 ad80ab7b 2018-08-04 stsp }
6265 ad80ab7b 2018-08-04 stsp
6266 ad80ab7b 2018-08-04 stsp static const struct got_error *
6267 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6268 ad80ab7b 2018-08-04 stsp {
6269 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6270 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6271 e5a0f69f 2018-08-18 stsp char *parent_path;
6272 ad80ab7b 2018-08-04 stsp
6273 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6274 e5a0f69f 2018-08-18 stsp if (err)
6275 e5a0f69f 2018-08-18 stsp return err;
6276 ffd1d5e5 2018-06-23 stsp
6277 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6278 e5a0f69f 2018-08-18 stsp free(parent_path);
6279 669b5ffa 2018-10-07 stsp
6280 9b058f45 2022-06-30 mark view_border(view);
6281 e5a0f69f 2018-08-18 stsp return err;
6282 e5a0f69f 2018-08-18 stsp }
6283 ce52c690 2018-06-23 stsp
6284 e5a0f69f 2018-08-18 stsp static const struct got_error *
6285 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6286 e5a0f69f 2018-08-18 stsp {
6287 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6288 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6289 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6290 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6291 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6292 ffd1d5e5 2018-06-23 stsp
6293 e5a0f69f 2018-08-18 stsp switch (ch) {
6294 1e37a5c2 2019-05-12 jcs case 'i':
6295 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6296 640cd7ff 2022-06-22 mark view->count = 0;
6297 1e37a5c2 2019-05-12 jcs break;
6298 1e37a5c2 2019-05-12 jcs case 'l':
6299 640cd7ff 2022-06-22 mark view->count = 0;
6300 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6301 ffd1d5e5 2018-06-23 stsp break;
6302 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6303 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6304 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6305 e78dc838 2020-12-04 stsp view->focussed = 0;
6306 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6307 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6308 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6309 1e37a5c2 2019-05-12 jcs if (err)
6310 1e37a5c2 2019-05-12 jcs return err;
6311 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6312 0dbbbe90 2022-06-17 op if (err)
6313 0dbbbe90 2022-06-17 op return err;
6314 e78dc838 2020-12-04 stsp view->focus_child = 1;
6315 1e37a5c2 2019-05-12 jcs } else
6316 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6317 152c1c93 2020-11-29 stsp break;
6318 152c1c93 2020-11-29 stsp case 'r':
6319 640cd7ff 2022-06-22 mark view->count = 0;
6320 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6321 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6322 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6323 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6324 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6325 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6326 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6327 152c1c93 2020-11-29 stsp if (err) {
6328 152c1c93 2020-11-29 stsp view_close(ref_view);
6329 152c1c93 2020-11-29 stsp return err;
6330 152c1c93 2020-11-29 stsp }
6331 e78dc838 2020-12-04 stsp view->focussed = 0;
6332 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6333 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6334 152c1c93 2020-11-29 stsp err = view_close_child(view);
6335 152c1c93 2020-11-29 stsp if (err)
6336 152c1c93 2020-11-29 stsp return err;
6337 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6338 0dbbbe90 2022-06-17 op if (err)
6339 0dbbbe90 2022-06-17 op return err;
6340 e78dc838 2020-12-04 stsp view->focus_child = 1;
6341 152c1c93 2020-11-29 stsp } else
6342 152c1c93 2020-11-29 stsp *new_view = ref_view;
6343 e4526bf5 2021-09-03 naddy break;
6344 e4526bf5 2021-09-03 naddy case 'g':
6345 e4526bf5 2021-09-03 naddy case KEY_HOME:
6346 e4526bf5 2021-09-03 naddy s->selected = 0;
6347 640cd7ff 2022-06-22 mark view->count = 0;
6348 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6349 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6350 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6351 e4526bf5 2021-09-03 naddy else
6352 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6353 1e37a5c2 2019-05-12 jcs break;
6354 e4526bf5 2021-09-03 naddy case 'G':
6355 9b058f45 2022-06-30 mark case KEY_END: {
6356 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6357 9b058f45 2022-06-30 mark
6358 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6359 9b058f45 2022-06-30 mark --eos; /* border */
6360 e4526bf5 2021-09-03 naddy s->selected = 0;
6361 640cd7ff 2022-06-22 mark view->count = 0;
6362 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6363 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6364 e4526bf5 2021-09-03 naddy if (te == NULL) {
6365 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6366 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6367 e4526bf5 2021-09-03 naddy n++;
6368 e4526bf5 2021-09-03 naddy }
6369 e4526bf5 2021-09-03 naddy break;
6370 e4526bf5 2021-09-03 naddy }
6371 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6372 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6373 e4526bf5 2021-09-03 naddy }
6374 e4526bf5 2021-09-03 naddy if (n > 0)
6375 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6376 e4526bf5 2021-09-03 naddy break;
6377 9b058f45 2022-06-30 mark }
6378 1e37a5c2 2019-05-12 jcs case 'k':
6379 1e37a5c2 2019-05-12 jcs case KEY_UP:
6380 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6381 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6382 1e37a5c2 2019-05-12 jcs s->selected--;
6383 fa86c4bf 2020-11-29 stsp break;
6384 1e37a5c2 2019-05-12 jcs }
6385 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6386 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6387 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6388 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6389 640cd7ff 2022-06-22 mark view->count = 0;
6390 1e37a5c2 2019-05-12 jcs break;
6391 83cc4199 2022-06-13 stsp case CTRL('u'):
6392 33c3719a 2022-06-15 stsp case 'u':
6393 83cc4199 2022-06-13 stsp nscroll /= 2;
6394 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6395 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6396 ea025d1d 2020-02-22 naddy case CTRL('b'):
6397 61417565 2022-06-20 mark case 'b':
6398 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6399 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6400 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6401 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6402 fa86c4bf 2020-11-29 stsp } else {
6403 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6404 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6405 fa86c4bf 2020-11-29 stsp }
6406 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6407 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6408 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6409 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6410 640cd7ff 2022-06-22 mark view->count = 0;
6411 1e37a5c2 2019-05-12 jcs break;
6412 1e37a5c2 2019-05-12 jcs case 'j':
6413 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6414 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6415 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6416 1e37a5c2 2019-05-12 jcs s->selected++;
6417 1e37a5c2 2019-05-12 jcs break;
6418 1e37a5c2 2019-05-12 jcs }
6419 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6420 640cd7ff 2022-06-22 mark == NULL) {
6421 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6422 640cd7ff 2022-06-22 mark view->count = 0;
6423 1e37a5c2 2019-05-12 jcs break;
6424 640cd7ff 2022-06-22 mark }
6425 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6426 1e37a5c2 2019-05-12 jcs break;
6427 83cc4199 2022-06-13 stsp case CTRL('d'):
6428 33c3719a 2022-06-15 stsp case 'd':
6429 83cc4199 2022-06-13 stsp nscroll /= 2;
6430 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6431 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6432 ea025d1d 2020-02-22 naddy case CTRL('f'):
6433 61417565 2022-06-20 mark case 'f':
6434 48bb96f0 2022-06-20 naddy case ' ':
6435 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6436 1e37a5c2 2019-05-12 jcs == NULL) {
6437 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6438 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6439 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6440 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6441 640cd7ff 2022-06-22 mark else
6442 640cd7ff 2022-06-22 mark view->count = 0;
6443 1e37a5c2 2019-05-12 jcs break;
6444 1e37a5c2 2019-05-12 jcs }
6445 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6446 1e37a5c2 2019-05-12 jcs break;
6447 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6448 1e37a5c2 2019-05-12 jcs case '\r':
6449 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6450 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6451 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6452 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6453 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6454 640cd7ff 2022-06-22 mark view->count = 0;
6455 1e37a5c2 2019-05-12 jcs break;
6456 640cd7ff 2022-06-22 mark }
6457 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6458 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6459 1e37a5c2 2019-05-12 jcs entry);
6460 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6461 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6462 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6463 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6464 1e37a5c2 2019-05-12 jcs s->selected_entry =
6465 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6466 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6467 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6468 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6469 9b058f45 2022-06-30 mark if (err)
6470 9b058f45 2022-06-30 mark break;
6471 9b058f45 2022-06-30 mark }
6472 1e37a5c2 2019-05-12 jcs free(parent);
6473 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6474 56e0773d 2019-11-28 stsp s->selected_entry))) {
6475 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6476 640cd7ff 2022-06-22 mark view->count = 0;
6477 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6478 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6479 1e37a5c2 2019-05-12 jcs if (err)
6480 1e37a5c2 2019-05-12 jcs break;
6481 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6482 941e9f74 2019-05-21 stsp if (err) {
6483 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6484 1e37a5c2 2019-05-12 jcs break;
6485 1e37a5c2 2019-05-12 jcs }
6486 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6487 56e0773d 2019-11-28 stsp s->selected_entry))) {
6488 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6489 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6490 1e37a5c2 2019-05-12 jcs
6491 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6492 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6493 9b058f45 2022-06-30 mark
6494 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6495 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6496 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6497 1e37a5c2 2019-05-12 jcs if (err)
6498 1e37a5c2 2019-05-12 jcs break;
6499 9b058f45 2022-06-30 mark
6500 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6501 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6502 9b058f45 2022-06-30 mark if (err)
6503 9b058f45 2022-06-30 mark break;
6504 9b058f45 2022-06-30 mark }
6505 9b058f45 2022-06-30 mark
6506 640cd7ff 2022-06-22 mark view->count = 0;
6507 e78dc838 2020-12-04 stsp view->focussed = 0;
6508 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6509 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6510 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6511 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6512 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6513 669b5ffa 2018-10-07 stsp if (err)
6514 669b5ffa 2018-10-07 stsp return err;
6515 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6516 0dbbbe90 2022-06-17 op if (err)
6517 0dbbbe90 2022-06-17 op return err;
6518 e78dc838 2020-12-04 stsp view->focus_child = 1;
6519 669b5ffa 2018-10-07 stsp } else
6520 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6521 1e37a5c2 2019-05-12 jcs }
6522 1e37a5c2 2019-05-12 jcs break;
6523 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6524 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6525 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6526 640cd7ff 2022-06-22 mark view->count = 0;
6527 1e37a5c2 2019-05-12 jcs break;
6528 1e37a5c2 2019-05-12 jcs default:
6529 640cd7ff 2022-06-22 mark view->count = 0;
6530 1e37a5c2 2019-05-12 jcs break;
6531 ffd1d5e5 2018-06-23 stsp }
6532 e5a0f69f 2018-08-18 stsp
6533 ffd1d5e5 2018-06-23 stsp return err;
6534 9f7d7167 2018-04-29 stsp }
6535 9f7d7167 2018-04-29 stsp
6536 ffd1d5e5 2018-06-23 stsp __dead static void
6537 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6538 ffd1d5e5 2018-06-23 stsp {
6539 ffd1d5e5 2018-06-23 stsp endwin();
6540 87411fa9 2022-06-16 stsp fprintf(stderr,
6541 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6542 ffd1d5e5 2018-06-23 stsp getprogname());
6543 ffd1d5e5 2018-06-23 stsp exit(1);
6544 ffd1d5e5 2018-06-23 stsp }
6545 ffd1d5e5 2018-06-23 stsp
6546 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6547 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6548 ffd1d5e5 2018-06-23 stsp {
6549 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6550 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6551 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6552 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6553 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6554 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6555 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6556 4e97c21c 2020-12-06 stsp char *label = NULL;
6557 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6558 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6559 ffd1d5e5 2018-06-23 stsp int ch;
6560 5221c383 2018-08-01 stsp struct tog_view *view;
6561 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6562 70ac5f84 2019-03-28 stsp
6563 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6564 ffd1d5e5 2018-06-23 stsp switch (ch) {
6565 ffd1d5e5 2018-06-23 stsp case 'c':
6566 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6567 74283ab8 2020-02-07 stsp break;
6568 74283ab8 2020-02-07 stsp case 'r':
6569 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6570 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6571 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6572 74283ab8 2020-02-07 stsp optarg);
6573 ffd1d5e5 2018-06-23 stsp break;
6574 ffd1d5e5 2018-06-23 stsp default:
6575 e99e2d15 2020-11-24 naddy usage_tree();
6576 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6577 ffd1d5e5 2018-06-23 stsp }
6578 ffd1d5e5 2018-06-23 stsp }
6579 ffd1d5e5 2018-06-23 stsp
6580 ffd1d5e5 2018-06-23 stsp argc -= optind;
6581 ffd1d5e5 2018-06-23 stsp argv += optind;
6582 ffd1d5e5 2018-06-23 stsp
6583 55cccc34 2020-02-20 stsp if (argc > 1)
6584 e99e2d15 2020-11-24 naddy usage_tree();
6585 0ae84acc 2022-06-15 tracey
6586 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6587 0ae84acc 2022-06-15 tracey if (error != NULL)
6588 0ae84acc 2022-06-15 tracey goto done;
6589 74283ab8 2020-02-07 stsp
6590 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6591 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6592 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6593 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6594 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6595 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6596 c156c7a4 2020-12-18 stsp goto done;
6597 55cccc34 2020-02-20 stsp if (worktree)
6598 52185f70 2019-02-05 stsp repo_path =
6599 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6600 55cccc34 2020-02-20 stsp else
6601 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6602 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6603 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6604 c156c7a4 2020-12-18 stsp goto done;
6605 c156c7a4 2020-12-18 stsp }
6606 55cccc34 2020-02-20 stsp }
6607 a915003a 2019-02-05 stsp
6608 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6609 c02c541e 2019-03-29 stsp if (error != NULL)
6610 52185f70 2019-02-05 stsp goto done;
6611 d188b9a6 2019-01-04 stsp
6612 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6613 55cccc34 2020-02-20 stsp repo, worktree);
6614 55cccc34 2020-02-20 stsp if (error)
6615 55cccc34 2020-02-20 stsp goto done;
6616 55cccc34 2020-02-20 stsp
6617 55cccc34 2020-02-20 stsp init_curses();
6618 55cccc34 2020-02-20 stsp
6619 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6620 c02c541e 2019-03-29 stsp if (error)
6621 52185f70 2019-02-05 stsp goto done;
6622 ffd1d5e5 2018-06-23 stsp
6623 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6624 51a10b52 2020-12-26 stsp if (error)
6625 51a10b52 2020-12-26 stsp goto done;
6626 51a10b52 2020-12-26 stsp
6627 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6628 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6629 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6630 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6631 4e97c21c 2020-12-06 stsp if (error)
6632 4e97c21c 2020-12-06 stsp goto done;
6633 4e97c21c 2020-12-06 stsp head_ref_name = label;
6634 4e97c21c 2020-12-06 stsp } else {
6635 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6636 4e97c21c 2020-12-06 stsp if (error == NULL)
6637 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6638 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6639 4e97c21c 2020-12-06 stsp goto done;
6640 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6641 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6642 4e97c21c 2020-12-06 stsp if (error)
6643 4e97c21c 2020-12-06 stsp goto done;
6644 4e97c21c 2020-12-06 stsp }
6645 ffd1d5e5 2018-06-23 stsp
6646 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6647 a44927cc 2022-04-07 stsp if (error)
6648 a44927cc 2022-04-07 stsp goto done;
6649 a44927cc 2022-04-07 stsp
6650 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6651 5221c383 2018-08-01 stsp if (view == NULL) {
6652 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6653 5221c383 2018-08-01 stsp goto done;
6654 5221c383 2018-08-01 stsp }
6655 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6656 ad80ab7b 2018-08-04 stsp if (error)
6657 ad80ab7b 2018-08-04 stsp goto done;
6658 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6659 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6660 d91faf3b 2020-12-01 naddy in_repo_path);
6661 55cccc34 2020-02-20 stsp if (error)
6662 55cccc34 2020-02-20 stsp goto done;
6663 55cccc34 2020-02-20 stsp }
6664 55cccc34 2020-02-20 stsp
6665 55cccc34 2020-02-20 stsp if (worktree) {
6666 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6667 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6668 55cccc34 2020-02-20 stsp worktree = NULL;
6669 55cccc34 2020-02-20 stsp }
6670 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6671 ffd1d5e5 2018-06-23 stsp done:
6672 52185f70 2019-02-05 stsp free(repo_path);
6673 e4a0e26d 2020-02-20 stsp free(cwd);
6674 ffd1d5e5 2018-06-23 stsp free(commit_id);
6675 4e97c21c 2020-12-06 stsp free(label);
6676 486cd271 2020-12-06 stsp if (ref)
6677 486cd271 2020-12-06 stsp got_ref_close(ref);
6678 1d0f4054 2021-06-17 stsp if (repo) {
6679 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6680 1d0f4054 2021-06-17 stsp if (error == NULL)
6681 1d0f4054 2021-06-17 stsp error = close_err;
6682 1d0f4054 2021-06-17 stsp }
6683 0ae84acc 2022-06-15 tracey if (pack_fds) {
6684 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6685 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6686 0ae84acc 2022-06-15 tracey if (error == NULL)
6687 0ae84acc 2022-06-15 tracey error = pack_err;
6688 0ae84acc 2022-06-15 tracey }
6689 51a10b52 2020-12-26 stsp tog_free_refs();
6690 ffd1d5e5 2018-06-23 stsp return error;
6691 6458efa5 2020-11-24 stsp }
6692 6458efa5 2020-11-24 stsp
6693 6458efa5 2020-11-24 stsp static const struct got_error *
6694 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6695 6458efa5 2020-11-24 stsp {
6696 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6697 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6698 6458efa5 2020-11-24 stsp
6699 6458efa5 2020-11-24 stsp s->nrefs = 0;
6700 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6701 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6702 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6703 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6704 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6705 6458efa5 2020-11-24 stsp continue;
6706 6458efa5 2020-11-24 stsp
6707 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6708 6458efa5 2020-11-24 stsp if (re == NULL)
6709 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6710 6458efa5 2020-11-24 stsp
6711 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6712 8924d611 2020-12-26 stsp if (re->ref == NULL)
6713 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6714 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6715 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6716 6458efa5 2020-11-24 stsp }
6717 6458efa5 2020-11-24 stsp
6718 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6719 6458efa5 2020-11-24 stsp return NULL;
6720 6458efa5 2020-11-24 stsp }
6721 6458efa5 2020-11-24 stsp
6722 336075a4 2022-06-25 op static void
6723 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6724 6458efa5 2020-11-24 stsp {
6725 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6726 6458efa5 2020-11-24 stsp
6727 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6728 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6729 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6730 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6731 6458efa5 2020-11-24 stsp free(re);
6732 6458efa5 2020-11-24 stsp }
6733 6458efa5 2020-11-24 stsp }
6734 6458efa5 2020-11-24 stsp
6735 6458efa5 2020-11-24 stsp static const struct got_error *
6736 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6737 6458efa5 2020-11-24 stsp {
6738 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6739 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6740 6458efa5 2020-11-24 stsp
6741 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6742 6458efa5 2020-11-24 stsp s->repo = repo;
6743 6458efa5 2020-11-24 stsp
6744 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6745 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6746 6458efa5 2020-11-24 stsp
6747 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6748 6458efa5 2020-11-24 stsp if (err)
6749 6458efa5 2020-11-24 stsp return err;
6750 34ba6917 2020-11-30 stsp
6751 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6752 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6753 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6754 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6755 6458efa5 2020-11-24 stsp if (err)
6756 6458efa5 2020-11-24 stsp goto done;
6757 6458efa5 2020-11-24 stsp
6758 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6759 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6760 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6761 6458efa5 2020-11-24 stsp if (err)
6762 6458efa5 2020-11-24 stsp goto done;
6763 6458efa5 2020-11-24 stsp
6764 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6765 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6766 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6767 cc488aa7 2022-01-23 stsp if (err)
6768 cc488aa7 2022-01-23 stsp goto done;
6769 cc488aa7 2022-01-23 stsp
6770 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6771 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6772 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6773 6458efa5 2020-11-24 stsp if (err)
6774 6458efa5 2020-11-24 stsp goto done;
6775 6458efa5 2020-11-24 stsp }
6776 6458efa5 2020-11-24 stsp
6777 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6778 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6779 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6780 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6781 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6782 6458efa5 2020-11-24 stsp done:
6783 6458efa5 2020-11-24 stsp if (err)
6784 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6785 6458efa5 2020-11-24 stsp return err;
6786 6458efa5 2020-11-24 stsp }
6787 6458efa5 2020-11-24 stsp
6788 6458efa5 2020-11-24 stsp static const struct got_error *
6789 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6790 6458efa5 2020-11-24 stsp {
6791 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6792 6458efa5 2020-11-24 stsp
6793 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6794 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6795 6458efa5 2020-11-24 stsp
6796 6458efa5 2020-11-24 stsp return NULL;
6797 9f7d7167 2018-04-29 stsp }
6798 ce5b7c56 2019-07-09 stsp
6799 6458efa5 2020-11-24 stsp static const struct got_error *
6800 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6801 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6802 6458efa5 2020-11-24 stsp {
6803 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6804 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6805 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6806 6458efa5 2020-11-24 stsp int obj_type;
6807 6458efa5 2020-11-24 stsp
6808 c42c9805 2020-11-24 stsp *commit_id = NULL;
6809 6458efa5 2020-11-24 stsp
6810 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6811 6458efa5 2020-11-24 stsp if (err)
6812 6458efa5 2020-11-24 stsp return err;
6813 6458efa5 2020-11-24 stsp
6814 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6815 6458efa5 2020-11-24 stsp if (err)
6816 6458efa5 2020-11-24 stsp goto done;
6817 6458efa5 2020-11-24 stsp
6818 6458efa5 2020-11-24 stsp switch (obj_type) {
6819 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6820 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6821 6458efa5 2020-11-24 stsp break;
6822 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6823 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6824 6458efa5 2020-11-24 stsp if (err)
6825 6458efa5 2020-11-24 stsp goto done;
6826 c42c9805 2020-11-24 stsp free(obj_id);
6827 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6828 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6829 c42c9805 2020-11-24 stsp if (err)
6830 6458efa5 2020-11-24 stsp goto done;
6831 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6832 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6833 c42c9805 2020-11-24 stsp goto done;
6834 c42c9805 2020-11-24 stsp }
6835 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6836 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6837 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6838 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6839 c42c9805 2020-11-24 stsp goto done;
6840 c42c9805 2020-11-24 stsp }
6841 6458efa5 2020-11-24 stsp break;
6842 6458efa5 2020-11-24 stsp default:
6843 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6844 c42c9805 2020-11-24 stsp break;
6845 6458efa5 2020-11-24 stsp }
6846 6458efa5 2020-11-24 stsp
6847 c42c9805 2020-11-24 stsp done:
6848 c42c9805 2020-11-24 stsp if (tag)
6849 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6850 c42c9805 2020-11-24 stsp if (err) {
6851 c42c9805 2020-11-24 stsp free(*commit_id);
6852 c42c9805 2020-11-24 stsp *commit_id = NULL;
6853 c42c9805 2020-11-24 stsp }
6854 c42c9805 2020-11-24 stsp return err;
6855 c42c9805 2020-11-24 stsp }
6856 c42c9805 2020-11-24 stsp
6857 c42c9805 2020-11-24 stsp static const struct got_error *
6858 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6859 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6860 c42c9805 2020-11-24 stsp {
6861 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6862 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6863 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6864 c42c9805 2020-11-24 stsp
6865 c42c9805 2020-11-24 stsp *new_view = NULL;
6866 c42c9805 2020-11-24 stsp
6867 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6868 c42c9805 2020-11-24 stsp if (err) {
6869 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6870 c42c9805 2020-11-24 stsp return err;
6871 c42c9805 2020-11-24 stsp else
6872 c42c9805 2020-11-24 stsp return NULL;
6873 c42c9805 2020-11-24 stsp }
6874 c42c9805 2020-11-24 stsp
6875 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6876 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6877 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6878 6458efa5 2020-11-24 stsp goto done;
6879 6458efa5 2020-11-24 stsp }
6880 6458efa5 2020-11-24 stsp
6881 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6882 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6883 6458efa5 2020-11-24 stsp done:
6884 6458efa5 2020-11-24 stsp if (err)
6885 6458efa5 2020-11-24 stsp view_close(log_view);
6886 6458efa5 2020-11-24 stsp else
6887 6458efa5 2020-11-24 stsp *new_view = log_view;
6888 c42c9805 2020-11-24 stsp free(commit_id);
6889 6458efa5 2020-11-24 stsp return err;
6890 6458efa5 2020-11-24 stsp }
6891 6458efa5 2020-11-24 stsp
6892 ce5b7c56 2019-07-09 stsp static void
6893 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6894 6458efa5 2020-11-24 stsp {
6895 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6896 34ba6917 2020-11-30 stsp int i = 0;
6897 6458efa5 2020-11-24 stsp
6898 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6899 6458efa5 2020-11-24 stsp return;
6900 6458efa5 2020-11-24 stsp
6901 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6902 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6903 34ba6917 2020-11-30 stsp if (re == NULL)
6904 34ba6917 2020-11-30 stsp break;
6905 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6906 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6907 6458efa5 2020-11-24 stsp }
6908 6458efa5 2020-11-24 stsp }
6909 6458efa5 2020-11-24 stsp
6910 9b058f45 2022-06-30 mark static const struct got_error *
6911 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6912 6458efa5 2020-11-24 stsp {
6913 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6914 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6915 6458efa5 2020-11-24 stsp int n = 0;
6916 6458efa5 2020-11-24 stsp
6917 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6918 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6919 6458efa5 2020-11-24 stsp else
6920 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6921 6458efa5 2020-11-24 stsp
6922 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6923 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6924 9b058f45 2022-06-30 mark if (last)
6925 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
6926 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6927 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6928 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6929 6458efa5 2020-11-24 stsp }
6930 6458efa5 2020-11-24 stsp }
6931 9b058f45 2022-06-30 mark
6932 9b058f45 2022-06-30 mark return NULL;
6933 6458efa5 2020-11-24 stsp }
6934 6458efa5 2020-11-24 stsp
6935 6458efa5 2020-11-24 stsp static const struct got_error *
6936 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6937 6458efa5 2020-11-24 stsp {
6938 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6939 6458efa5 2020-11-24 stsp
6940 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6941 6458efa5 2020-11-24 stsp return NULL;
6942 6458efa5 2020-11-24 stsp }
6943 6458efa5 2020-11-24 stsp
6944 6458efa5 2020-11-24 stsp static int
6945 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6946 6458efa5 2020-11-24 stsp {
6947 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6948 6458efa5 2020-11-24 stsp
6949 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6950 6458efa5 2020-11-24 stsp 0) == 0;
6951 6458efa5 2020-11-24 stsp }
6952 6458efa5 2020-11-24 stsp
6953 6458efa5 2020-11-24 stsp static const struct got_error *
6954 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6955 6458efa5 2020-11-24 stsp {
6956 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6957 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6958 6458efa5 2020-11-24 stsp
6959 6458efa5 2020-11-24 stsp if (!view->searching) {
6960 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6961 6458efa5 2020-11-24 stsp return NULL;
6962 6458efa5 2020-11-24 stsp }
6963 6458efa5 2020-11-24 stsp
6964 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6965 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6966 6458efa5 2020-11-24 stsp if (s->selected_entry)
6967 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6968 6458efa5 2020-11-24 stsp else
6969 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6970 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6971 6458efa5 2020-11-24 stsp } else {
6972 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6973 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6974 6458efa5 2020-11-24 stsp else
6975 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6976 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6977 6458efa5 2020-11-24 stsp }
6978 6458efa5 2020-11-24 stsp } else {
6979 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6980 487cd7d2 2021-12-17 stsp re = s->selected_entry;
6981 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6982 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6983 6458efa5 2020-11-24 stsp else
6984 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6985 6458efa5 2020-11-24 stsp }
6986 6458efa5 2020-11-24 stsp
6987 6458efa5 2020-11-24 stsp while (1) {
6988 6458efa5 2020-11-24 stsp if (re == NULL) {
6989 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6990 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6991 6458efa5 2020-11-24 stsp return NULL;
6992 6458efa5 2020-11-24 stsp }
6993 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6994 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6995 6458efa5 2020-11-24 stsp else
6996 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6997 6458efa5 2020-11-24 stsp }
6998 6458efa5 2020-11-24 stsp
6999 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7000 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7001 6458efa5 2020-11-24 stsp s->matched_entry = re;
7002 6458efa5 2020-11-24 stsp break;
7003 6458efa5 2020-11-24 stsp }
7004 6458efa5 2020-11-24 stsp
7005 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7006 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7007 6458efa5 2020-11-24 stsp else
7008 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7009 6458efa5 2020-11-24 stsp }
7010 6458efa5 2020-11-24 stsp
7011 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7012 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7013 6458efa5 2020-11-24 stsp s->selected = 0;
7014 6458efa5 2020-11-24 stsp }
7015 6458efa5 2020-11-24 stsp
7016 6458efa5 2020-11-24 stsp return NULL;
7017 6458efa5 2020-11-24 stsp }
7018 6458efa5 2020-11-24 stsp
7019 6458efa5 2020-11-24 stsp static const struct got_error *
7020 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7021 6458efa5 2020-11-24 stsp {
7022 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7023 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7024 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7025 6458efa5 2020-11-24 stsp char *line = NULL;
7026 6458efa5 2020-11-24 stsp wchar_t *wline;
7027 6458efa5 2020-11-24 stsp struct tog_color *tc;
7028 6458efa5 2020-11-24 stsp int width, n;
7029 6458efa5 2020-11-24 stsp int limit = view->nlines;
7030 6458efa5 2020-11-24 stsp
7031 6458efa5 2020-11-24 stsp werase(view->window);
7032 6458efa5 2020-11-24 stsp
7033 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7034 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7035 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7036 9b058f45 2022-06-30 mark --limit; /* border */
7037 6458efa5 2020-11-24 stsp
7038 6458efa5 2020-11-24 stsp if (limit == 0)
7039 6458efa5 2020-11-24 stsp return NULL;
7040 6458efa5 2020-11-24 stsp
7041 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7042 6458efa5 2020-11-24 stsp
7043 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7044 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7045 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7046 6458efa5 2020-11-24 stsp
7047 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7048 6458efa5 2020-11-24 stsp if (err) {
7049 6458efa5 2020-11-24 stsp free(line);
7050 6458efa5 2020-11-24 stsp return err;
7051 6458efa5 2020-11-24 stsp }
7052 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7053 6458efa5 2020-11-24 stsp wstandout(view->window);
7054 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7055 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7056 6458efa5 2020-11-24 stsp wstandend(view->window);
7057 6458efa5 2020-11-24 stsp free(wline);
7058 6458efa5 2020-11-24 stsp wline = NULL;
7059 6458efa5 2020-11-24 stsp free(line);
7060 6458efa5 2020-11-24 stsp line = NULL;
7061 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7062 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7063 6458efa5 2020-11-24 stsp if (--limit <= 0)
7064 6458efa5 2020-11-24 stsp return NULL;
7065 6458efa5 2020-11-24 stsp
7066 6458efa5 2020-11-24 stsp n = 0;
7067 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7068 6458efa5 2020-11-24 stsp char *line = NULL;
7069 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7070 6458efa5 2020-11-24 stsp
7071 b4996bee 2022-06-16 stsp if (s->show_date) {
7072 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7073 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7074 b4996bee 2022-06-16 stsp struct got_object_id *id;
7075 b4996bee 2022-06-16 stsp struct tm tm;
7076 b4996bee 2022-06-16 stsp time_t t;
7077 b4996bee 2022-06-16 stsp
7078 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7079 b4996bee 2022-06-16 stsp if (err)
7080 b4996bee 2022-06-16 stsp return err;
7081 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7082 b4996bee 2022-06-16 stsp if (err) {
7083 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7084 b4996bee 2022-06-16 stsp free(id);
7085 b4996bee 2022-06-16 stsp return err;
7086 b4996bee 2022-06-16 stsp }
7087 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7088 b4996bee 2022-06-16 stsp id);
7089 b4996bee 2022-06-16 stsp if (err) {
7090 b4996bee 2022-06-16 stsp free(id);
7091 b4996bee 2022-06-16 stsp return err;
7092 b4996bee 2022-06-16 stsp }
7093 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7094 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7095 b4996bee 2022-06-16 stsp } else {
7096 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7097 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7098 b4996bee 2022-06-16 stsp }
7099 b4996bee 2022-06-16 stsp free(id);
7100 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7101 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7102 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7103 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7104 b4996bee 2022-06-16 stsp }
7105 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7106 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7107 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7108 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7109 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7110 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7111 6458efa5 2020-11-24 stsp struct got_object_id *id;
7112 6458efa5 2020-11-24 stsp char *id_str;
7113 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7114 6458efa5 2020-11-24 stsp if (err)
7115 6458efa5 2020-11-24 stsp return err;
7116 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7117 6458efa5 2020-11-24 stsp if (err) {
7118 6458efa5 2020-11-24 stsp free(id);
7119 6458efa5 2020-11-24 stsp return err;
7120 6458efa5 2020-11-24 stsp }
7121 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7122 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7123 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7124 6458efa5 2020-11-24 stsp free(id);
7125 6458efa5 2020-11-24 stsp free(id_str);
7126 6458efa5 2020-11-24 stsp return err;
7127 6458efa5 2020-11-24 stsp }
7128 6458efa5 2020-11-24 stsp free(id);
7129 6458efa5 2020-11-24 stsp free(id_str);
7130 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7131 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7132 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7133 6458efa5 2020-11-24 stsp
7134 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7135 ccda2f4d 2022-06-16 stsp 0, 0);
7136 6458efa5 2020-11-24 stsp if (err) {
7137 6458efa5 2020-11-24 stsp free(line);
7138 6458efa5 2020-11-24 stsp return err;
7139 6458efa5 2020-11-24 stsp }
7140 6458efa5 2020-11-24 stsp if (n == s->selected) {
7141 6458efa5 2020-11-24 stsp if (view->focussed)
7142 6458efa5 2020-11-24 stsp wstandout(view->window);
7143 6458efa5 2020-11-24 stsp s->selected_entry = re;
7144 6458efa5 2020-11-24 stsp }
7145 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7146 6458efa5 2020-11-24 stsp if (tc)
7147 6458efa5 2020-11-24 stsp wattr_on(view->window,
7148 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7149 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7150 6458efa5 2020-11-24 stsp if (tc)
7151 6458efa5 2020-11-24 stsp wattr_off(view->window,
7152 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7153 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7154 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7155 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7156 6458efa5 2020-11-24 stsp wstandend(view->window);
7157 6458efa5 2020-11-24 stsp free(line);
7158 6458efa5 2020-11-24 stsp free(wline);
7159 6458efa5 2020-11-24 stsp wline = NULL;
7160 6458efa5 2020-11-24 stsp n++;
7161 6458efa5 2020-11-24 stsp s->ndisplayed++;
7162 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7163 6458efa5 2020-11-24 stsp
7164 6458efa5 2020-11-24 stsp limit--;
7165 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7166 6458efa5 2020-11-24 stsp }
7167 6458efa5 2020-11-24 stsp
7168 9b058f45 2022-06-30 mark view_border(view);
7169 6458efa5 2020-11-24 stsp return err;
7170 6458efa5 2020-11-24 stsp }
7171 6458efa5 2020-11-24 stsp
7172 6458efa5 2020-11-24 stsp static const struct got_error *
7173 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7174 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7175 c42c9805 2020-11-24 stsp {
7176 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7177 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7178 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7179 c42c9805 2020-11-24 stsp
7180 c42c9805 2020-11-24 stsp *new_view = NULL;
7181 c42c9805 2020-11-24 stsp
7182 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7183 c42c9805 2020-11-24 stsp if (err) {
7184 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7185 c42c9805 2020-11-24 stsp return err;
7186 c42c9805 2020-11-24 stsp else
7187 c42c9805 2020-11-24 stsp return NULL;
7188 c42c9805 2020-11-24 stsp }
7189 c42c9805 2020-11-24 stsp
7190 c42c9805 2020-11-24 stsp
7191 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7192 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7193 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7194 c42c9805 2020-11-24 stsp goto done;
7195 c42c9805 2020-11-24 stsp }
7196 c42c9805 2020-11-24 stsp
7197 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7198 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7199 c42c9805 2020-11-24 stsp if (err)
7200 c42c9805 2020-11-24 stsp goto done;
7201 c42c9805 2020-11-24 stsp
7202 c42c9805 2020-11-24 stsp *new_view = tree_view;
7203 c42c9805 2020-11-24 stsp done:
7204 c42c9805 2020-11-24 stsp free(commit_id);
7205 c42c9805 2020-11-24 stsp return err;
7206 c42c9805 2020-11-24 stsp }
7207 c42c9805 2020-11-24 stsp static const struct got_error *
7208 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7209 6458efa5 2020-11-24 stsp {
7210 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7211 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7212 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7213 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7214 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7215 6458efa5 2020-11-24 stsp
7216 6458efa5 2020-11-24 stsp switch (ch) {
7217 6458efa5 2020-11-24 stsp case 'i':
7218 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7219 640cd7ff 2022-06-22 mark view->count = 0;
7220 7f66531d 2021-11-16 stsp break;
7221 b4996bee 2022-06-16 stsp case 'm':
7222 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7223 640cd7ff 2022-06-22 mark view->count = 0;
7224 b4996bee 2022-06-16 stsp break;
7225 07a065fe 2021-11-20 stsp case 'o':
7226 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7227 640cd7ff 2022-06-22 mark view->count = 0;
7228 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7229 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7230 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7231 50617b77 2021-11-20 stsp if (err)
7232 50617b77 2021-11-20 stsp break;
7233 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7234 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7235 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7236 7f66531d 2021-11-16 stsp if (err)
7237 7f66531d 2021-11-16 stsp break;
7238 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7239 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7240 6458efa5 2020-11-24 stsp break;
7241 6458efa5 2020-11-24 stsp case KEY_ENTER:
7242 6458efa5 2020-11-24 stsp case '\r':
7243 640cd7ff 2022-06-22 mark view->count = 0;
7244 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7245 6458efa5 2020-11-24 stsp break;
7246 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7247 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7248 9b058f45 2022-06-30 mark
7249 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7250 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7251 9b058f45 2022-06-30 mark if (err)
7252 9b058f45 2022-06-30 mark break;
7253 9b058f45 2022-06-30 mark
7254 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7255 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7256 9b058f45 2022-06-30 mark if (err)
7257 9b058f45 2022-06-30 mark break;
7258 9b058f45 2022-06-30 mark }
7259 9b058f45 2022-06-30 mark
7260 e78dc838 2020-12-04 stsp view->focussed = 0;
7261 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7262 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7263 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7264 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7265 6458efa5 2020-11-24 stsp err = view_close_child(view);
7266 6458efa5 2020-11-24 stsp if (err)
7267 6458efa5 2020-11-24 stsp return err;
7268 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7269 0dbbbe90 2022-06-17 op if (err)
7270 0dbbbe90 2022-06-17 op return err;
7271 e78dc838 2020-12-04 stsp view->focus_child = 1;
7272 6458efa5 2020-11-24 stsp } else
7273 6458efa5 2020-11-24 stsp *new_view = log_view;
7274 6458efa5 2020-11-24 stsp break;
7275 c42c9805 2020-11-24 stsp case 't':
7276 640cd7ff 2022-06-22 mark view->count = 0;
7277 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7278 c42c9805 2020-11-24 stsp break;
7279 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7280 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7281 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7282 c42c9805 2020-11-24 stsp s->repo);
7283 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7284 c42c9805 2020-11-24 stsp break;
7285 e78dc838 2020-12-04 stsp view->focussed = 0;
7286 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7287 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7288 c42c9805 2020-11-24 stsp err = view_close_child(view);
7289 c42c9805 2020-11-24 stsp if (err)
7290 c42c9805 2020-11-24 stsp return err;
7291 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7292 0dbbbe90 2022-06-17 op if (err)
7293 0dbbbe90 2022-06-17 op return err;
7294 e78dc838 2020-12-04 stsp view->focus_child = 1;
7295 c42c9805 2020-11-24 stsp } else
7296 c42c9805 2020-11-24 stsp *new_view = tree_view;
7297 c42c9805 2020-11-24 stsp break;
7298 e4526bf5 2021-09-03 naddy case 'g':
7299 e4526bf5 2021-09-03 naddy case KEY_HOME:
7300 e4526bf5 2021-09-03 naddy s->selected = 0;
7301 640cd7ff 2022-06-22 mark view->count = 0;
7302 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7303 e4526bf5 2021-09-03 naddy break;
7304 e4526bf5 2021-09-03 naddy case 'G':
7305 9b058f45 2022-06-30 mark case KEY_END: {
7306 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7307 9b058f45 2022-06-30 mark
7308 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7309 9b058f45 2022-06-30 mark --eos; /* border */
7310 e4526bf5 2021-09-03 naddy s->selected = 0;
7311 640cd7ff 2022-06-22 mark view->count = 0;
7312 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7313 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7314 e4526bf5 2021-09-03 naddy if (re == NULL)
7315 e4526bf5 2021-09-03 naddy break;
7316 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7317 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7318 e4526bf5 2021-09-03 naddy }
7319 e4526bf5 2021-09-03 naddy if (n > 0)
7320 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7321 e4526bf5 2021-09-03 naddy break;
7322 9b058f45 2022-06-30 mark }
7323 6458efa5 2020-11-24 stsp case 'k':
7324 6458efa5 2020-11-24 stsp case KEY_UP:
7325 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7326 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7327 6458efa5 2020-11-24 stsp s->selected--;
7328 6458efa5 2020-11-24 stsp break;
7329 34ba6917 2020-11-30 stsp }
7330 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7331 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7332 640cd7ff 2022-06-22 mark view->count = 0;
7333 6458efa5 2020-11-24 stsp break;
7334 83cc4199 2022-06-13 stsp case CTRL('u'):
7335 33c3719a 2022-06-15 stsp case 'u':
7336 83cc4199 2022-06-13 stsp nscroll /= 2;
7337 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7338 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7339 6458efa5 2020-11-24 stsp case CTRL('b'):
7340 61417565 2022-06-20 mark case 'b':
7341 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7342 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7343 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7344 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7345 640cd7ff 2022-06-22 mark view->count = 0;
7346 6458efa5 2020-11-24 stsp break;
7347 6458efa5 2020-11-24 stsp case 'j':
7348 6458efa5 2020-11-24 stsp case KEY_DOWN:
7349 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7350 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7351 6458efa5 2020-11-24 stsp s->selected++;
7352 6458efa5 2020-11-24 stsp break;
7353 6458efa5 2020-11-24 stsp }
7354 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7355 6458efa5 2020-11-24 stsp /* can't scroll any further */
7356 640cd7ff 2022-06-22 mark view->count = 0;
7357 6458efa5 2020-11-24 stsp break;
7358 640cd7ff 2022-06-22 mark }
7359 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7360 6458efa5 2020-11-24 stsp break;
7361 83cc4199 2022-06-13 stsp case CTRL('d'):
7362 33c3719a 2022-06-15 stsp case 'd':
7363 83cc4199 2022-06-13 stsp nscroll /= 2;
7364 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7365 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7366 6458efa5 2020-11-24 stsp case CTRL('f'):
7367 61417565 2022-06-20 mark case 'f':
7368 48bb96f0 2022-06-20 naddy case ' ':
7369 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7370 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7371 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7372 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7373 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7374 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7375 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7376 640cd7ff 2022-06-22 mark view->count = 0;
7377 6458efa5 2020-11-24 stsp break;
7378 6458efa5 2020-11-24 stsp }
7379 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7380 6458efa5 2020-11-24 stsp break;
7381 6458efa5 2020-11-24 stsp case CTRL('l'):
7382 640cd7ff 2022-06-22 mark view->count = 0;
7383 8924d611 2020-12-26 stsp tog_free_refs();
7384 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7385 8924d611 2020-12-26 stsp if (err)
7386 8924d611 2020-12-26 stsp break;
7387 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7388 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7389 6458efa5 2020-11-24 stsp break;
7390 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7391 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7392 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7393 6458efa5 2020-11-24 stsp break;
7394 6458efa5 2020-11-24 stsp default:
7395 640cd7ff 2022-06-22 mark view->count = 0;
7396 6458efa5 2020-11-24 stsp break;
7397 6458efa5 2020-11-24 stsp }
7398 6458efa5 2020-11-24 stsp
7399 6458efa5 2020-11-24 stsp return err;
7400 6458efa5 2020-11-24 stsp }
7401 6458efa5 2020-11-24 stsp
7402 6458efa5 2020-11-24 stsp __dead static void
7403 6458efa5 2020-11-24 stsp usage_ref(void)
7404 6458efa5 2020-11-24 stsp {
7405 6458efa5 2020-11-24 stsp endwin();
7406 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7407 6458efa5 2020-11-24 stsp getprogname());
7408 6458efa5 2020-11-24 stsp exit(1);
7409 6458efa5 2020-11-24 stsp }
7410 6458efa5 2020-11-24 stsp
7411 6458efa5 2020-11-24 stsp static const struct got_error *
7412 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7413 6458efa5 2020-11-24 stsp {
7414 6458efa5 2020-11-24 stsp const struct got_error *error;
7415 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7416 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7417 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7418 6458efa5 2020-11-24 stsp int ch;
7419 6458efa5 2020-11-24 stsp struct tog_view *view;
7420 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7421 6458efa5 2020-11-24 stsp
7422 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7423 6458efa5 2020-11-24 stsp switch (ch) {
7424 6458efa5 2020-11-24 stsp case 'r':
7425 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7426 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7427 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7428 6458efa5 2020-11-24 stsp optarg);
7429 6458efa5 2020-11-24 stsp break;
7430 6458efa5 2020-11-24 stsp default:
7431 e99e2d15 2020-11-24 naddy usage_ref();
7432 6458efa5 2020-11-24 stsp /* NOTREACHED */
7433 6458efa5 2020-11-24 stsp }
7434 6458efa5 2020-11-24 stsp }
7435 6458efa5 2020-11-24 stsp
7436 6458efa5 2020-11-24 stsp argc -= optind;
7437 6458efa5 2020-11-24 stsp argv += optind;
7438 6458efa5 2020-11-24 stsp
7439 6458efa5 2020-11-24 stsp if (argc > 1)
7440 e99e2d15 2020-11-24 naddy usage_ref();
7441 0ae84acc 2022-06-15 tracey
7442 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7443 0ae84acc 2022-06-15 tracey if (error != NULL)
7444 0ae84acc 2022-06-15 tracey goto done;
7445 6458efa5 2020-11-24 stsp
7446 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7447 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7448 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7449 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7450 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7451 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7452 c156c7a4 2020-12-18 stsp goto done;
7453 6458efa5 2020-11-24 stsp if (worktree)
7454 6458efa5 2020-11-24 stsp repo_path =
7455 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7456 6458efa5 2020-11-24 stsp else
7457 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7458 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7459 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7460 c156c7a4 2020-12-18 stsp goto done;
7461 c156c7a4 2020-12-18 stsp }
7462 6458efa5 2020-11-24 stsp }
7463 6458efa5 2020-11-24 stsp
7464 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7465 6458efa5 2020-11-24 stsp if (error != NULL)
7466 6458efa5 2020-11-24 stsp goto done;
7467 6458efa5 2020-11-24 stsp
7468 6458efa5 2020-11-24 stsp init_curses();
7469 6458efa5 2020-11-24 stsp
7470 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7471 51a10b52 2020-12-26 stsp if (error)
7472 51a10b52 2020-12-26 stsp goto done;
7473 51a10b52 2020-12-26 stsp
7474 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7475 6458efa5 2020-11-24 stsp if (error)
7476 6458efa5 2020-11-24 stsp goto done;
7477 6458efa5 2020-11-24 stsp
7478 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7479 6458efa5 2020-11-24 stsp if (view == NULL) {
7480 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7481 6458efa5 2020-11-24 stsp goto done;
7482 6458efa5 2020-11-24 stsp }
7483 6458efa5 2020-11-24 stsp
7484 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7485 6458efa5 2020-11-24 stsp if (error)
7486 6458efa5 2020-11-24 stsp goto done;
7487 6458efa5 2020-11-24 stsp
7488 6458efa5 2020-11-24 stsp if (worktree) {
7489 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7490 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7491 6458efa5 2020-11-24 stsp worktree = NULL;
7492 6458efa5 2020-11-24 stsp }
7493 6458efa5 2020-11-24 stsp error = view_loop(view);
7494 6458efa5 2020-11-24 stsp done:
7495 6458efa5 2020-11-24 stsp free(repo_path);
7496 6458efa5 2020-11-24 stsp free(cwd);
7497 1d0f4054 2021-06-17 stsp if (repo) {
7498 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7499 1d0f4054 2021-06-17 stsp if (close_err)
7500 1d0f4054 2021-06-17 stsp error = close_err;
7501 1d0f4054 2021-06-17 stsp }
7502 0ae84acc 2022-06-15 tracey if (pack_fds) {
7503 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7504 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7505 0ae84acc 2022-06-15 tracey if (error == NULL)
7506 0ae84acc 2022-06-15 tracey error = pack_err;
7507 0ae84acc 2022-06-15 tracey }
7508 51a10b52 2020-12-26 stsp tog_free_refs();
7509 6458efa5 2020-11-24 stsp return error;
7510 6458efa5 2020-11-24 stsp }
7511 6458efa5 2020-11-24 stsp
7512 9b058f45 2022-06-30 mark /*
7513 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7514 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7515 9b058f45 2022-06-30 mark */
7516 6458efa5 2020-11-24 stsp static void
7517 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7518 9b058f45 2022-06-30 mark {
7519 9b058f45 2022-06-30 mark switch (view->type) {
7520 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7521 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7522 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7523 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7524 9b058f45 2022-06-30 mark 1);
7525 9b058f45 2022-06-30 mark break;
7526 9b058f45 2022-06-30 mark }
7527 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7528 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7529 9b058f45 2022-06-30 mark else
7530 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7531 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7532 9b058f45 2022-06-30 mark break;
7533 9b058f45 2022-06-30 mark }
7534 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7535 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7536 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7537 9b058f45 2022-06-30 mark break;
7538 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7539 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7540 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7541 9b058f45 2022-06-30 mark break;
7542 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7543 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7544 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7545 9b058f45 2022-06-30 mark break;
7546 9b058f45 2022-06-30 mark default:
7547 9b058f45 2022-06-30 mark break;
7548 9b058f45 2022-06-30 mark }
7549 9b058f45 2022-06-30 mark
7550 9b058f45 2022-06-30 mark view->offset = 0;
7551 9b058f45 2022-06-30 mark }
7552 9b058f45 2022-06-30 mark
7553 9b058f45 2022-06-30 mark /*
7554 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7555 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7556 9b058f45 2022-06-30 mark */
7557 9b058f45 2022-06-30 mark static const struct got_error *
7558 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7559 9b058f45 2022-06-30 mark {
7560 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7561 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7562 9b058f45 2022-06-30 mark int *selected = NULL;
7563 9b058f45 2022-06-30 mark int header, offset;
7564 9b058f45 2022-06-30 mark
7565 9b058f45 2022-06-30 mark switch (view->type) {
7566 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7567 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7568 9b058f45 2022-06-30 mark header = 3;
7569 9b058f45 2022-06-30 mark scrolld = NULL;
7570 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7571 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7572 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7573 9b058f45 2022-06-30 mark s->selected_line -= offset;
7574 9b058f45 2022-06-30 mark view->offset = offset;
7575 9b058f45 2022-06-30 mark }
7576 9b058f45 2022-06-30 mark break;
7577 9b058f45 2022-06-30 mark }
7578 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7579 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7580 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7581 9b058f45 2022-06-30 mark header = 3;
7582 9b058f45 2022-06-30 mark selected = &s->selected;
7583 9b058f45 2022-06-30 mark break;
7584 9b058f45 2022-06-30 mark }
7585 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7586 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7587 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7588 9b058f45 2022-06-30 mark header = 3;
7589 9b058f45 2022-06-30 mark selected = &s->selected;
7590 9b058f45 2022-06-30 mark break;
7591 9b058f45 2022-06-30 mark }
7592 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7593 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7594 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7595 9b058f45 2022-06-30 mark header = 5;
7596 9b058f45 2022-06-30 mark selected = &s->selected;
7597 9b058f45 2022-06-30 mark break;
7598 9b058f45 2022-06-30 mark }
7599 9b058f45 2022-06-30 mark default:
7600 9b058f45 2022-06-30 mark selected = NULL;
7601 9b058f45 2022-06-30 mark scrolld = NULL;
7602 9b058f45 2022-06-30 mark header = 0;
7603 9b058f45 2022-06-30 mark break;
7604 9b058f45 2022-06-30 mark }
7605 9b058f45 2022-06-30 mark
7606 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7607 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7608 9b058f45 2022-06-30 mark view->offset = offset;
7609 9b058f45 2022-06-30 mark if (scrolld && offset) {
7610 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7611 9b058f45 2022-06-30 mark *selected -= offset;
7612 9b058f45 2022-06-30 mark }
7613 9b058f45 2022-06-30 mark }
7614 9b058f45 2022-06-30 mark
7615 9b058f45 2022-06-30 mark return err;
7616 9b058f45 2022-06-30 mark }
7617 9b058f45 2022-06-30 mark
7618 9b058f45 2022-06-30 mark static void
7619 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7620 ce5b7c56 2019-07-09 stsp {
7621 6059809a 2020-12-17 stsp size_t i;
7622 9f7d7167 2018-04-29 stsp
7623 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7624 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7625 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7626 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7627 ce5b7c56 2019-07-09 stsp }
7628 6879ba42 2020-10-01 naddy fputc('\n', fp);
7629 ce5b7c56 2019-07-09 stsp }
7630 ce5b7c56 2019-07-09 stsp
7631 4ed7e80c 2018-05-20 stsp __dead static void
7632 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7633 9f7d7167 2018-04-29 stsp {
7634 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7635 6879ba42 2020-10-01 naddy
7636 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7637 6879ba42 2020-10-01 naddy getprogname());
7638 6879ba42 2020-10-01 naddy if (hflag) {
7639 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7640 6879ba42 2020-10-01 naddy list_commands(fp);
7641 ee85c5e8 2020-02-29 stsp }
7642 6879ba42 2020-10-01 naddy exit(status);
7643 9f7d7167 2018-04-29 stsp }
7644 9f7d7167 2018-04-29 stsp
7645 c2301be8 2018-04-30 stsp static char **
7646 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7647 c2301be8 2018-04-30 stsp {
7648 ee85c5e8 2020-02-29 stsp va_list ap;
7649 c2301be8 2018-04-30 stsp char **argv;
7650 ee85c5e8 2020-02-29 stsp int i;
7651 c2301be8 2018-04-30 stsp
7652 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7653 ee85c5e8 2020-02-29 stsp
7654 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7655 c2301be8 2018-04-30 stsp if (argv == NULL)
7656 c2301be8 2018-04-30 stsp err(1, "calloc");
7657 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7658 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7659 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7660 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7661 c2301be8 2018-04-30 stsp }
7662 c2301be8 2018-04-30 stsp
7663 ee85c5e8 2020-02-29 stsp va_end(ap);
7664 c2301be8 2018-04-30 stsp return argv;
7665 ee85c5e8 2020-02-29 stsp }
7666 ee85c5e8 2020-02-29 stsp
7667 ee85c5e8 2020-02-29 stsp /*
7668 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7669 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7670 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7671 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7672 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7673 ee85c5e8 2020-02-29 stsp */
7674 ee85c5e8 2020-02-29 stsp static const struct got_error *
7675 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7676 ee85c5e8 2020-02-29 stsp {
7677 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7678 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7679 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7680 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7681 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7682 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7683 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7684 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7685 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7686 84de9106 2020-12-26 stsp
7687 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7688 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7689 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7690 ee85c5e8 2020-02-29 stsp
7691 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7692 0ae84acc 2022-06-15 tracey if (error != NULL)
7693 0ae84acc 2022-06-15 tracey goto done;
7694 0ae84acc 2022-06-15 tracey
7695 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7696 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7697 ee85c5e8 2020-02-29 stsp goto done;
7698 ee85c5e8 2020-02-29 stsp
7699 ee85c5e8 2020-02-29 stsp if (worktree)
7700 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7701 ee85c5e8 2020-02-29 stsp else
7702 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7703 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7704 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7705 ee85c5e8 2020-02-29 stsp goto done;
7706 ee85c5e8 2020-02-29 stsp }
7707 ee85c5e8 2020-02-29 stsp
7708 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7709 ee85c5e8 2020-02-29 stsp if (error != NULL)
7710 ee85c5e8 2020-02-29 stsp goto done;
7711 ee85c5e8 2020-02-29 stsp
7712 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7713 ee85c5e8 2020-02-29 stsp repo, worktree);
7714 ee85c5e8 2020-02-29 stsp if (error)
7715 ee85c5e8 2020-02-29 stsp goto done;
7716 ee85c5e8 2020-02-29 stsp
7717 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7718 84de9106 2020-12-26 stsp if (error)
7719 84de9106 2020-12-26 stsp goto done;
7720 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7721 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7722 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7723 ee85c5e8 2020-02-29 stsp if (error)
7724 ee85c5e8 2020-02-29 stsp goto done;
7725 ee85c5e8 2020-02-29 stsp
7726 ee85c5e8 2020-02-29 stsp if (worktree) {
7727 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7728 ee85c5e8 2020-02-29 stsp worktree = NULL;
7729 ee85c5e8 2020-02-29 stsp }
7730 ee85c5e8 2020-02-29 stsp
7731 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7732 a44927cc 2022-04-07 stsp if (error)
7733 a44927cc 2022-04-07 stsp goto done;
7734 a44927cc 2022-04-07 stsp
7735 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7736 ee85c5e8 2020-02-29 stsp if (error) {
7737 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7738 ee85c5e8 2020-02-29 stsp goto done;
7739 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7740 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7741 6879ba42 2020-10-01 naddy usage(1, 1);
7742 ee85c5e8 2020-02-29 stsp /* not reached */
7743 ee85c5e8 2020-02-29 stsp }
7744 ee85c5e8 2020-02-29 stsp
7745 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7746 1d0f4054 2021-06-17 stsp if (error == NULL)
7747 1d0f4054 2021-06-17 stsp error = close_err;
7748 ee85c5e8 2020-02-29 stsp repo = NULL;
7749 ee85c5e8 2020-02-29 stsp
7750 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7751 ee85c5e8 2020-02-29 stsp if (error)
7752 ee85c5e8 2020-02-29 stsp goto done;
7753 ee85c5e8 2020-02-29 stsp
7754 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7755 ee85c5e8 2020-02-29 stsp argc = 4;
7756 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7757 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7758 ee85c5e8 2020-02-29 stsp done:
7759 1d0f4054 2021-06-17 stsp if (repo) {
7760 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7761 1d0f4054 2021-06-17 stsp if (error == NULL)
7762 1d0f4054 2021-06-17 stsp error = close_err;
7763 1d0f4054 2021-06-17 stsp }
7764 a44927cc 2022-04-07 stsp if (commit)
7765 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7766 ee85c5e8 2020-02-29 stsp if (worktree)
7767 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7768 0ae84acc 2022-06-15 tracey if (pack_fds) {
7769 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7770 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7771 0ae84acc 2022-06-15 tracey if (error == NULL)
7772 0ae84acc 2022-06-15 tracey error = pack_err;
7773 0ae84acc 2022-06-15 tracey }
7774 ee85c5e8 2020-02-29 stsp free(id);
7775 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7776 ee85c5e8 2020-02-29 stsp free(commit_id);
7777 ee85c5e8 2020-02-29 stsp free(cwd);
7778 ee85c5e8 2020-02-29 stsp free(repo_path);
7779 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7780 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7781 ee85c5e8 2020-02-29 stsp int i;
7782 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7783 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7784 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7785 ee85c5e8 2020-02-29 stsp }
7786 87670572 2020-12-26 naddy tog_free_refs();
7787 ee85c5e8 2020-02-29 stsp return error;
7788 c2301be8 2018-04-30 stsp }
7789 c2301be8 2018-04-30 stsp
7790 9f7d7167 2018-04-29 stsp int
7791 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7792 9f7d7167 2018-04-29 stsp {
7793 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7794 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7795 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7796 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7797 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7798 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7799 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7800 83cd27f8 2020-01-13 stsp };
7801 9f7d7167 2018-04-29 stsp
7802 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7803 9f7d7167 2018-04-29 stsp
7804 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7805 9f7d7167 2018-04-29 stsp switch (ch) {
7806 9f7d7167 2018-04-29 stsp case 'h':
7807 9f7d7167 2018-04-29 stsp hflag = 1;
7808 9f7d7167 2018-04-29 stsp break;
7809 53ccebc2 2019-07-30 stsp case 'V':
7810 53ccebc2 2019-07-30 stsp Vflag = 1;
7811 53ccebc2 2019-07-30 stsp break;
7812 9f7d7167 2018-04-29 stsp default:
7813 6879ba42 2020-10-01 naddy usage(hflag, 1);
7814 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7815 9f7d7167 2018-04-29 stsp }
7816 9f7d7167 2018-04-29 stsp }
7817 9f7d7167 2018-04-29 stsp
7818 9f7d7167 2018-04-29 stsp argc -= optind;
7819 9f7d7167 2018-04-29 stsp argv += optind;
7820 9814e6a3 2020-09-27 naddy optind = 1;
7821 c2301be8 2018-04-30 stsp optreset = 1;
7822 9f7d7167 2018-04-29 stsp
7823 53ccebc2 2019-07-30 stsp if (Vflag) {
7824 53ccebc2 2019-07-30 stsp got_version_print_str();
7825 6879ba42 2020-10-01 naddy return 0;
7826 53ccebc2 2019-07-30 stsp }
7827 4010e238 2020-12-04 stsp
7828 4010e238 2020-12-04 stsp #ifndef PROFILE
7829 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7830 4010e238 2020-12-04 stsp NULL) == -1)
7831 4010e238 2020-12-04 stsp err(1, "pledge");
7832 4010e238 2020-12-04 stsp #endif
7833 53ccebc2 2019-07-30 stsp
7834 c2301be8 2018-04-30 stsp if (argc == 0) {
7835 f29d3e89 2018-06-23 stsp if (hflag)
7836 6879ba42 2020-10-01 naddy usage(hflag, 0);
7837 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7838 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7839 c2301be8 2018-04-30 stsp argc = 1;
7840 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7841 c2301be8 2018-04-30 stsp } else {
7842 6059809a 2020-12-17 stsp size_t i;
7843 9f7d7167 2018-04-29 stsp
7844 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7845 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7846 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7847 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7848 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7849 9f7d7167 2018-04-29 stsp break;
7850 9f7d7167 2018-04-29 stsp }
7851 9f7d7167 2018-04-29 stsp }
7852 ee85c5e8 2020-02-29 stsp }
7853 3642c4c6 2019-07-09 stsp
7854 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7855 ee85c5e8 2020-02-29 stsp if (argc != 1)
7856 6879ba42 2020-10-01 naddy usage(0, 1);
7857 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7858 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7859 ee85c5e8 2020-02-29 stsp } else {
7860 ee85c5e8 2020-02-29 stsp if (hflag)
7861 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7862 ee85c5e8 2020-02-29 stsp else
7863 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7864 9f7d7167 2018-04-29 stsp }
7865 9f7d7167 2018-04-29 stsp
7866 9f7d7167 2018-04-29 stsp endwin();
7867 b46c1e04 2020-09-20 naddy putchar('\n');
7868 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7869 a2f4a359 2020-02-28 stsp int i;
7870 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7871 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7872 a2f4a359 2020-02-28 stsp free(cmd_argv);
7873 a2f4a359 2020-02-28 stsp }
7874 a2f4a359 2020-02-28 stsp
7875 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7876 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7877 9f7d7167 2018-04-29 stsp return 0;
7878 9f7d7167 2018-04-29 stsp }