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 24b9cfdc 2022-06-30 stsp }
783 24b9cfdc 2022-06-30 stsp
784 24b9cfdc 2022-06-30 stsp static int
785 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
786 24b9cfdc 2022-06-30 stsp {
787 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
788 6131ff18 2022-06-20 mark }
789 6131ff18 2022-06-20 mark
790 9b058f45 2022-06-30 mark static void
791 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
792 9b058f45 2022-06-30 mark {
793 9b058f45 2022-06-30 mark PANEL *panel;
794 9b058f45 2022-06-30 mark const struct tog_view *view_above;
795 6131ff18 2022-06-20 mark
796 9b058f45 2022-06-30 mark if (view->parent)
797 9b058f45 2022-06-30 mark return view_border(view->parent);
798 9b058f45 2022-06-30 mark
799 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
800 9b058f45 2022-06-30 mark if (panel == NULL)
801 9b058f45 2022-06-30 mark return;
802 9b058f45 2022-06-30 mark
803 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
804 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
805 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
806 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
807 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
808 9b058f45 2022-06-30 mark else
809 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
810 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
811 9b058f45 2022-06-30 mark }
812 9b058f45 2022-06-30 mark
813 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
814 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
815 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
816 9b058f45 2022-06-30 mark
817 4d8c2215 2018-08-19 stsp static const struct got_error *
818 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
819 f7d12f7e 2018-08-01 stsp {
820 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
821 9b058f45 2022-06-30 mark int dif, nlines, ncols;
822 f7d12f7e 2018-08-01 stsp
823 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
824 9b058f45 2022-06-30 mark
825 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
826 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
827 0cf4efb1 2018-09-29 stsp else
828 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
829 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
830 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
831 0cf4efb1 2018-09-29 stsp else
832 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
833 6d0fee91 2018-08-01 stsp
834 4dd27a72 2022-06-29 stsp if (view->child) {
835 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
836 9b058f45 2022-06-30 mark
837 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
838 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
839 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
840 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
841 0dbbbe90 2022-06-17 op ncols = COLS;
842 0dbbbe90 2022-06-17 op
843 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
844 5c60c32a 2018-10-18 stsp if (view->child->focussed)
845 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
846 5c60c32a 2018-10-18 stsp else
847 5c60c32a 2018-10-18 stsp show_panel(view->panel);
848 5c60c32a 2018-10-18 stsp } else {
849 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
850 0dbbbe90 2022-06-17 op
851 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
852 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
853 5c60c32a 2018-10-18 stsp }
854 9b058f45 2022-06-30 mark /*
855 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
856 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
857 9b058f45 2022-06-30 mark */
858 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
859 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
860 9b058f45 2022-06-30 mark
861 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
862 9b058f45 2022-06-30 mark view->lines - ts->selected) {
863 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
864 9b058f45 2022-06-30 mark view->lines - ts->selected -
865 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
866 9b058f45 2022-06-30 mark err = request_log_commits(view);
867 9b058f45 2022-06-30 mark if (err)
868 9b058f45 2022-06-30 mark return err;
869 9b058f45 2022-06-30 mark }
870 9b058f45 2022-06-30 mark }
871 9b058f45 2022-06-30 mark
872 9b058f45 2022-06-30 mark /*
873 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
874 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
875 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
876 9b058f45 2022-06-30 mark */
877 9b058f45 2022-06-30 mark if (hs) {
878 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
879 9b058f45 2022-06-30 mark if (err)
880 9b058f45 2022-06-30 mark return err;
881 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
882 9b058f45 2022-06-30 mark err = offset_selection_down(view);
883 9b058f45 2022-06-30 mark if (err)
884 9b058f45 2022-06-30 mark return err;
885 9b058f45 2022-06-30 mark }
886 9b058f45 2022-06-30 mark view_border(view);
887 9b058f45 2022-06-30 mark update_panels();
888 9b058f45 2022-06-30 mark doupdate();
889 9b058f45 2022-06-30 mark show_panel(view->child->panel);
890 9b058f45 2022-06-30 mark nlines = view->nlines;
891 9b058f45 2022-06-30 mark }
892 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
893 0dbbbe90 2022-06-17 op ncols = COLS;
894 669b5ffa 2018-10-07 stsp
895 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
896 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
897 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
898 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
899 0dbbbe90 2022-06-17 op wclear(view->window);
900 0dbbbe90 2022-06-17 op
901 0dbbbe90 2022-06-17 op view->nlines = nlines;
902 0dbbbe90 2022-06-17 op view->ncols = ncols;
903 0dbbbe90 2022-06-17 op view->lines = LINES;
904 0dbbbe90 2022-06-17 op view->cols = COLS;
905 0dbbbe90 2022-06-17 op
906 5c60c32a 2018-10-18 stsp return NULL;
907 669b5ffa 2018-10-07 stsp }
908 669b5ffa 2018-10-07 stsp
909 669b5ffa 2018-10-07 stsp static const struct got_error *
910 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
911 669b5ffa 2018-10-07 stsp {
912 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
913 669b5ffa 2018-10-07 stsp
914 669b5ffa 2018-10-07 stsp if (view->child == NULL)
915 669b5ffa 2018-10-07 stsp return NULL;
916 669b5ffa 2018-10-07 stsp
917 669b5ffa 2018-10-07 stsp err = view_close(view->child);
918 669b5ffa 2018-10-07 stsp view->child = NULL;
919 669b5ffa 2018-10-07 stsp return err;
920 669b5ffa 2018-10-07 stsp }
921 669b5ffa 2018-10-07 stsp
922 0dbbbe90 2022-06-17 op static const struct got_error *
923 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
924 669b5ffa 2018-10-07 stsp {
925 669b5ffa 2018-10-07 stsp view->child = child;
926 669b5ffa 2018-10-07 stsp child->parent = view;
927 0dbbbe90 2022-06-17 op
928 0dbbbe90 2022-06-17 op return view_resize(view);
929 bfddd0d9 2018-09-29 stsp }
930 bfddd0d9 2018-09-29 stsp
931 34bc9ec9 2019-02-22 stsp static void
932 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
933 25791caa 2018-10-24 stsp {
934 25791caa 2018-10-24 stsp int cols, lines;
935 25791caa 2018-10-24 stsp struct winsize size;
936 25791caa 2018-10-24 stsp
937 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
938 25791caa 2018-10-24 stsp cols = 80; /* Default */
939 25791caa 2018-10-24 stsp lines = 24;
940 25791caa 2018-10-24 stsp } else {
941 25791caa 2018-10-24 stsp cols = size.ws_col;
942 25791caa 2018-10-24 stsp lines = size.ws_row;
943 25791caa 2018-10-24 stsp }
944 25791caa 2018-10-24 stsp resize_term(lines, cols);
945 2b49a8ae 2019-06-22 stsp }
946 2b49a8ae 2019-06-22 stsp
947 2b49a8ae 2019-06-22 stsp static const struct got_error *
948 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
949 2b49a8ae 2019-06-22 stsp {
950 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
951 9b058f45 2022-06-30 mark struct tog_view *v = view;
952 2b49a8ae 2019-06-22 stsp char pattern[1024];
953 2b49a8ae 2019-06-22 stsp int ret;
954 c0c4acc8 2021-01-24 stsp
955 c0c4acc8 2021-01-24 stsp if (view->search_started) {
956 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
957 c0c4acc8 2021-01-24 stsp view->searching = 0;
958 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
959 c0c4acc8 2021-01-24 stsp }
960 c0c4acc8 2021-01-24 stsp view->search_started = 0;
961 2b49a8ae 2019-06-22 stsp
962 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
963 2b49a8ae 2019-06-22 stsp return NULL;
964 2b49a8ae 2019-06-22 stsp
965 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
966 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
967 9b058f45 2022-06-30 mark v = view->child;
968 2b49a8ae 2019-06-22 stsp
969 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
970 9b058f45 2022-06-30 mark wclrtoeol(v->window);
971 9b058f45 2022-06-30 mark
972 2b49a8ae 2019-06-22 stsp nocbreak();
973 2b49a8ae 2019-06-22 stsp echo();
974 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
975 9b058f45 2022-06-30 mark wrefresh(v->window);
976 2b49a8ae 2019-06-22 stsp cbreak();
977 2b49a8ae 2019-06-22 stsp noecho();
978 2b49a8ae 2019-06-22 stsp if (ret == ERR)
979 2b49a8ae 2019-06-22 stsp return NULL;
980 2b49a8ae 2019-06-22 stsp
981 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
982 7c32bd05 2019-06-22 stsp err = view->search_start(view);
983 7c32bd05 2019-06-22 stsp if (err) {
984 7c32bd05 2019-06-22 stsp regfree(&view->regex);
985 7c32bd05 2019-06-22 stsp return err;
986 7c32bd05 2019-06-22 stsp }
987 c0c4acc8 2021-01-24 stsp view->search_started = 1;
988 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
989 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
990 2b49a8ae 2019-06-22 stsp view->search_next(view);
991 2b49a8ae 2019-06-22 stsp }
992 2b49a8ae 2019-06-22 stsp
993 2b49a8ae 2019-06-22 stsp return NULL;
994 0cf4efb1 2018-09-29 stsp }
995 6d0fee91 2018-08-01 stsp
996 640cd7ff 2022-06-22 mark /*
997 640cd7ff 2022-06-22 mark * Compute view->count from numeric user input. User has five-tenths of a
998 640cd7ff 2022-06-22 mark * second to follow each numeric keypress with another number to form count.
999 640cd7ff 2022-06-22 mark * Return first non-numeric input or ERR and assign total to view->count.
1000 640cd7ff 2022-06-22 mark * XXX Should we add support for user-defined timeout?
1001 640cd7ff 2022-06-22 mark */
1002 640cd7ff 2022-06-22 mark static int
1003 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1004 640cd7ff 2022-06-22 mark {
1005 9b058f45 2022-06-30 mark struct tog_view *v = view;
1006 9b058f45 2022-06-30 mark int x, n = 0;
1007 640cd7ff 2022-06-22 mark
1008 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1009 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1010 9b058f45 2022-06-30 mark v = view->child;
1011 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1012 9b058f45 2022-06-30 mark v = view->parent;
1013 9b058f45 2022-06-30 mark
1014 640cd7ff 2022-06-22 mark view->count = 0;
1015 640cd7ff 2022-06-22 mark halfdelay(5); /* block for half a second */
1016 9b058f45 2022-06-30 mark wattron(v->window, A_BOLD);
1017 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1018 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1019 9b058f45 2022-06-30 mark waddch(v->window, ':');
1020 640cd7ff 2022-06-22 mark
1021 640cd7ff 2022-06-22 mark do {
1022 9b058f45 2022-06-30 mark x = getcurx(v->window);
1023 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1024 9b058f45 2022-06-30 mark waddch(v->window, c);
1025 9b058f45 2022-06-30 mark wrefresh(v->window);
1026 9b058f45 2022-06-30 mark }
1027 9b058f45 2022-06-30 mark
1028 640cd7ff 2022-06-22 mark /*
1029 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1030 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1031 640cd7ff 2022-06-22 mark */
1032 640cd7ff 2022-06-22 mark if (n >= 9999999)
1033 640cd7ff 2022-06-22 mark n = 9999999;
1034 640cd7ff 2022-06-22 mark else
1035 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1036 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1037 640cd7ff 2022-06-22 mark
1038 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1039 640cd7ff 2022-06-22 mark view->count = n;
1040 640cd7ff 2022-06-22 mark
1041 9b058f45 2022-06-30 mark wattroff(v->window, A_BOLD);
1042 640cd7ff 2022-06-22 mark cbreak(); /* return to blocking */
1043 640cd7ff 2022-06-22 mark return c;
1044 640cd7ff 2022-06-22 mark }
1045 640cd7ff 2022-06-22 mark
1046 0cf4efb1 2018-09-29 stsp static const struct got_error *
1047 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1048 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1049 e5a0f69f 2018-08-18 stsp {
1050 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1051 669b5ffa 2018-10-07 stsp struct tog_view *v;
1052 1a76625f 2018-10-22 stsp int ch, errcode;
1053 e5a0f69f 2018-08-18 stsp
1054 e5a0f69f 2018-08-18 stsp *new = NULL;
1055 8f4ed634 2020-03-26 stsp
1056 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1057 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1058 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1059 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1060 640cd7ff 2022-06-22 mark view->count = 0;
1061 640cd7ff 2022-06-22 mark }
1062 e5a0f69f 2018-08-18 stsp
1063 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1064 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1065 82954512 2020-02-03 stsp if (errcode)
1066 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1067 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1068 3da8ef85 2021-09-21 stsp sched_yield();
1069 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1070 82954512 2020-02-03 stsp if (errcode)
1071 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1072 82954512 2020-02-03 stsp "pthread_mutex_lock");
1073 60493ae3 2019-06-20 stsp view->search_next(view);
1074 60493ae3 2019-06-20 stsp return NULL;
1075 60493ae3 2019-06-20 stsp }
1076 60493ae3 2019-06-20 stsp
1077 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1078 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1080 1a76625f 2018-10-22 stsp if (errcode)
1081 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1082 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1083 640cd7ff 2022-06-22 mark ch = view->ch;
1084 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1085 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1086 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1087 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1088 640cd7ff 2022-06-22 mark }
1089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1090 1a76625f 2018-10-22 stsp if (errcode)
1091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1092 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1093 25791caa 2018-10-24 stsp
1094 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1095 25791caa 2018-10-24 stsp tog_resizeterm();
1096 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1097 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1098 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1099 25791caa 2018-10-24 stsp err = view_resize(v);
1100 25791caa 2018-10-24 stsp if (err)
1101 25791caa 2018-10-24 stsp return err;
1102 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1103 25791caa 2018-10-24 stsp if (err)
1104 25791caa 2018-10-24 stsp return err;
1105 cdfcfb03 2020-12-06 stsp if (v->child) {
1106 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1107 cdfcfb03 2020-12-06 stsp if (err)
1108 cdfcfb03 2020-12-06 stsp return err;
1109 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1110 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1111 cdfcfb03 2020-12-06 stsp if (err)
1112 cdfcfb03 2020-12-06 stsp return err;
1113 cdfcfb03 2020-12-06 stsp }
1114 25791caa 2018-10-24 stsp }
1115 25791caa 2018-10-24 stsp }
1116 25791caa 2018-10-24 stsp
1117 e5a0f69f 2018-08-18 stsp switch (ch) {
1118 1e37a5c2 2019-05-12 jcs case '\t':
1119 640cd7ff 2022-06-22 mark view->count = 0;
1120 1e37a5c2 2019-05-12 jcs if (view->child) {
1121 e78dc838 2020-12-04 stsp view->focussed = 0;
1122 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1123 e78dc838 2020-12-04 stsp view->focus_child = 1;
1124 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1125 e78dc838 2020-12-04 stsp view->focussed = 0;
1126 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1127 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1128 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1129 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1130 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1131 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1132 9b058f45 2022-06-30 mark if (err)
1133 9b058f45 2022-06-30 mark return err;
1134 9b058f45 2022-06-30 mark }
1135 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1136 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1137 9b058f45 2022-06-30 mark if (err)
1138 9b058f45 2022-06-30 mark return err;
1139 9b058f45 2022-06-30 mark }
1140 1e37a5c2 2019-05-12 jcs }
1141 1e37a5c2 2019-05-12 jcs break;
1142 1e37a5c2 2019-05-12 jcs case 'q':
1143 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1144 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1145 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1146 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1147 9b058f45 2022-06-30 mark if (err)
1148 9b058f45 2022-06-30 mark break;
1149 9b058f45 2022-06-30 mark }
1150 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1151 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1152 9b058f45 2022-06-30 mark }
1153 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1154 9970f7fc 2020-12-03 stsp view->dying = 1;
1155 1e37a5c2 2019-05-12 jcs break;
1156 1e37a5c2 2019-05-12 jcs case 'Q':
1157 1e37a5c2 2019-05-12 jcs *done = 1;
1158 1e37a5c2 2019-05-12 jcs break;
1159 61417565 2022-06-20 mark case 'F':
1160 640cd7ff 2022-06-22 mark view->count = 0;
1161 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1162 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1163 1e37a5c2 2019-05-12 jcs break;
1164 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1165 e78dc838 2020-12-04 stsp view->focussed = 0;
1166 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1167 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1168 1e37a5c2 2019-05-12 jcs } else
1169 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1170 1e37a5c2 2019-05-12 jcs if (err)
1171 1e37a5c2 2019-05-12 jcs break;
1172 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1173 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1174 1e37a5c2 2019-05-12 jcs } else {
1175 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1176 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1177 e78dc838 2020-12-04 stsp view->focussed = 1;
1178 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1179 1e37a5c2 2019-05-12 jcs } else {
1180 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1181 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1182 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1183 669b5ffa 2018-10-07 stsp }
1184 1e37a5c2 2019-05-12 jcs if (err)
1185 1e37a5c2 2019-05-12 jcs break;
1186 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1187 1e37a5c2 2019-05-12 jcs }
1188 9b058f45 2022-06-30 mark if (err)
1189 9b058f45 2022-06-30 mark break;
1190 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1191 9b058f45 2022-06-30 mark err = request_log_commits(view);
1192 9b058f45 2022-06-30 mark if (err)
1193 9b058f45 2022-06-30 mark break;
1194 9b058f45 2022-06-30 mark }
1195 9b058f45 2022-06-30 mark if (view->parent)
1196 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1197 9b058f45 2022-06-30 mark if (!err)
1198 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1199 1e37a5c2 2019-05-12 jcs break;
1200 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1201 60493ae3 2019-06-20 stsp break;
1202 60493ae3 2019-06-20 stsp case '/':
1203 640cd7ff 2022-06-22 mark view->count = 0;
1204 60493ae3 2019-06-20 stsp if (view->search_start)
1205 2b49a8ae 2019-06-22 stsp view_search_start(view);
1206 60493ae3 2019-06-20 stsp else
1207 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1208 1e37a5c2 2019-05-12 jcs break;
1209 b1bf1435 2019-06-21 stsp case 'N':
1210 60493ae3 2019-06-20 stsp case 'n':
1211 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1212 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1213 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1214 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1215 60493ae3 2019-06-20 stsp view->search_next(view);
1216 60493ae3 2019-06-20 stsp } else
1217 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1218 60493ae3 2019-06-20 stsp break;
1219 1e37a5c2 2019-05-12 jcs default:
1220 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1221 1e37a5c2 2019-05-12 jcs break;
1222 e5a0f69f 2018-08-18 stsp }
1223 e5a0f69f 2018-08-18 stsp
1224 e5a0f69f 2018-08-18 stsp return err;
1225 e5a0f69f 2018-08-18 stsp }
1226 e5a0f69f 2018-08-18 stsp
1227 336075a4 2022-06-25 op static int
1228 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1229 a3404814 2018-09-02 stsp {
1230 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1231 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1232 669b5ffa 2018-10-07 stsp return 0;
1233 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1234 669b5ffa 2018-10-07 stsp return 0;
1235 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1236 a3404814 2018-09-02 stsp return 0;
1237 a3404814 2018-09-02 stsp
1238 669b5ffa 2018-10-07 stsp return view->focussed;
1239 a3404814 2018-09-02 stsp }
1240 a3404814 2018-09-02 stsp
1241 bcbd79e2 2018-08-19 stsp static const struct got_error *
1242 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1243 e5a0f69f 2018-08-18 stsp {
1244 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1245 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1246 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1247 fd823528 2018-10-22 stsp int fast_refresh = 10;
1248 1a76625f 2018-10-22 stsp int done = 0, errcode;
1249 e5a0f69f 2018-08-18 stsp
1250 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1251 1a76625f 2018-10-22 stsp if (errcode)
1252 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1253 1a76625f 2018-10-22 stsp
1254 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1255 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1256 e5a0f69f 2018-08-18 stsp
1257 1004088d 2018-09-29 stsp view->focussed = 1;
1258 878940b7 2018-09-29 stsp err = view->show(view);
1259 0cf4efb1 2018-09-29 stsp if (err)
1260 0cf4efb1 2018-09-29 stsp return err;
1261 0cf4efb1 2018-09-29 stsp update_panels();
1262 0cf4efb1 2018-09-29 stsp doupdate();
1263 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1264 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1265 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1266 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1267 fd823528 2018-10-22 stsp
1268 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1269 e5a0f69f 2018-08-18 stsp if (err)
1270 e5a0f69f 2018-08-18 stsp break;
1271 9970f7fc 2020-12-03 stsp if (view->dying) {
1272 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1273 669b5ffa 2018-10-07 stsp
1274 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1275 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1276 9970f7fc 2020-12-03 stsp entry);
1277 e78dc838 2020-12-04 stsp else if (view->parent)
1278 669b5ffa 2018-10-07 stsp prev = view->parent;
1279 669b5ffa 2018-10-07 stsp
1280 e78dc838 2020-12-04 stsp if (view->parent) {
1281 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1282 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1283 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1284 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1285 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1286 0dbbbe90 2022-06-17 op if (err)
1287 0dbbbe90 2022-06-17 op break;
1288 e78dc838 2020-12-04 stsp } else
1289 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1290 669b5ffa 2018-10-07 stsp
1291 9970f7fc 2020-12-03 stsp err = view_close(view);
1292 fb59748f 2020-12-05 stsp if (err)
1293 e5a0f69f 2018-08-18 stsp goto done;
1294 669b5ffa 2018-10-07 stsp
1295 e78dc838 2020-12-04 stsp view = NULL;
1296 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1297 e78dc838 2020-12-04 stsp if (v->focussed)
1298 e78dc838 2020-12-04 stsp break;
1299 0cf4efb1 2018-09-29 stsp }
1300 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1301 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1302 e78dc838 2020-12-04 stsp if (prev)
1303 e78dc838 2020-12-04 stsp view = prev;
1304 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1305 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1306 e78dc838 2020-12-04 stsp tog_view_list_head);
1307 e78dc838 2020-12-04 stsp }
1308 e78dc838 2020-12-04 stsp if (view) {
1309 e78dc838 2020-12-04 stsp if (view->focus_child) {
1310 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1311 e78dc838 2020-12-04 stsp view = view->child;
1312 e78dc838 2020-12-04 stsp } else
1313 e78dc838 2020-12-04 stsp view->focussed = 1;
1314 e78dc838 2020-12-04 stsp }
1315 e78dc838 2020-12-04 stsp }
1316 e5a0f69f 2018-08-18 stsp }
1317 bcbd79e2 2018-08-19 stsp if (new_view) {
1318 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1319 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1320 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1321 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1322 86c66b02 2018-10-18 stsp continue;
1323 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1324 86c66b02 2018-10-18 stsp err = view_close(v);
1325 86c66b02 2018-10-18 stsp if (err)
1326 86c66b02 2018-10-18 stsp goto done;
1327 86c66b02 2018-10-18 stsp break;
1328 86c66b02 2018-10-18 stsp }
1329 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1330 fed7eaa8 2018-10-24 stsp view = new_view;
1331 0ae84acc 2022-06-15 tracey }
1332 669b5ffa 2018-10-07 stsp if (view) {
1333 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1334 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1335 e78dc838 2020-12-04 stsp view = view->child;
1336 e78dc838 2020-12-04 stsp } else {
1337 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1338 e78dc838 2020-12-04 stsp view = view->parent;
1339 1a76625f 2018-10-22 stsp }
1340 e78dc838 2020-12-04 stsp show_panel(view->panel);
1341 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1342 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1343 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1344 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1345 669b5ffa 2018-10-07 stsp if (err)
1346 1a76625f 2018-10-22 stsp goto done;
1347 669b5ffa 2018-10-07 stsp }
1348 669b5ffa 2018-10-07 stsp err = view->show(view);
1349 0cf4efb1 2018-09-29 stsp if (err)
1350 1a76625f 2018-10-22 stsp goto done;
1351 669b5ffa 2018-10-07 stsp if (view->child) {
1352 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1353 669b5ffa 2018-10-07 stsp if (err)
1354 1a76625f 2018-10-22 stsp goto done;
1355 669b5ffa 2018-10-07 stsp }
1356 1a76625f 2018-10-22 stsp update_panels();
1357 1a76625f 2018-10-22 stsp doupdate();
1358 0cf4efb1 2018-09-29 stsp }
1359 e5a0f69f 2018-08-18 stsp }
1360 e5a0f69f 2018-08-18 stsp done:
1361 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1362 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1363 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1364 e5a0f69f 2018-08-18 stsp view_close(view);
1365 e5a0f69f 2018-08-18 stsp }
1366 1a76625f 2018-10-22 stsp
1367 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1368 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1369 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1370 1a76625f 2018-10-22 stsp
1371 e5a0f69f 2018-08-18 stsp return err;
1372 ea5e7bb5 2018-08-01 stsp }
1373 ea5e7bb5 2018-08-01 stsp
1374 4ed7e80c 2018-05-20 stsp __dead static void
1375 9f7d7167 2018-04-29 stsp usage_log(void)
1376 9f7d7167 2018-04-29 stsp {
1377 80ddbec8 2018-04-29 stsp endwin();
1378 c70c5802 2018-08-01 stsp fprintf(stderr,
1379 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1380 9f7d7167 2018-04-29 stsp getprogname());
1381 9f7d7167 2018-04-29 stsp exit(1);
1382 80ddbec8 2018-04-29 stsp }
1383 80ddbec8 2018-04-29 stsp
1384 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1385 80ddbec8 2018-04-29 stsp static const struct got_error *
1386 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1387 963b370f 2018-05-20 stsp {
1388 00dfcb92 2018-06-11 stsp char *vis = NULL;
1389 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1390 963b370f 2018-05-20 stsp
1391 963b370f 2018-05-20 stsp *ws = NULL;
1392 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1393 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1394 00dfcb92 2018-06-11 stsp int vislen;
1395 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1396 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1397 00dfcb92 2018-06-11 stsp
1398 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1399 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1400 00dfcb92 2018-06-11 stsp if (err)
1401 00dfcb92 2018-06-11 stsp return err;
1402 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1403 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1404 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1405 a7f50699 2018-06-11 stsp goto done;
1406 a7f50699 2018-06-11 stsp }
1407 00dfcb92 2018-06-11 stsp }
1408 963b370f 2018-05-20 stsp
1409 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1410 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1411 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1412 a7f50699 2018-06-11 stsp goto done;
1413 a7f50699 2018-06-11 stsp }
1414 963b370f 2018-05-20 stsp
1415 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1416 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1417 a7f50699 2018-06-11 stsp done:
1418 00dfcb92 2018-06-11 stsp free(vis);
1419 963b370f 2018-05-20 stsp if (err) {
1420 963b370f 2018-05-20 stsp free(*ws);
1421 963b370f 2018-05-20 stsp *ws = NULL;
1422 963b370f 2018-05-20 stsp *wlen = 0;
1423 963b370f 2018-05-20 stsp }
1424 963b370f 2018-05-20 stsp return err;
1425 145b6838 2022-06-16 stsp }
1426 145b6838 2022-06-16 stsp
1427 145b6838 2022-06-16 stsp static const struct got_error *
1428 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1429 145b6838 2022-06-16 stsp {
1430 145b6838 2022-06-16 stsp char *dst;
1431 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1432 145b6838 2022-06-16 stsp
1433 145b6838 2022-06-16 stsp *ptr = NULL;
1434 145b6838 2022-06-16 stsp n = len = strlen(src);
1435 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1436 145b6838 2022-06-16 stsp if (dst == NULL)
1437 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1438 145b6838 2022-06-16 stsp
1439 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1440 145b6838 2022-06-16 stsp const char c = src[idx];
1441 145b6838 2022-06-16 stsp
1442 145b6838 2022-06-16 stsp if (c == '\t') {
1443 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1444 367ddf28 2022-06-16 mark char *p;
1445 367ddf28 2022-06-16 mark
1446 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1447 6e1c41ad 2022-06-16 mark if (p == NULL) {
1448 6e1c41ad 2022-06-16 mark free(dst);
1449 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1450 6e1c41ad 2022-06-16 mark
1451 6e1c41ad 2022-06-16 mark }
1452 6e1c41ad 2022-06-16 mark dst = p;
1453 145b6838 2022-06-16 stsp n += nb;
1454 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1455 145b6838 2022-06-16 stsp sz += nb;
1456 145b6838 2022-06-16 stsp } else
1457 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1458 145b6838 2022-06-16 stsp ++idx;
1459 145b6838 2022-06-16 stsp }
1460 145b6838 2022-06-16 stsp
1461 145b6838 2022-06-16 stsp dst[sz] = '\0';
1462 145b6838 2022-06-16 stsp *ptr = dst;
1463 145b6838 2022-06-16 stsp return NULL;
1464 963b370f 2018-05-20 stsp }
1465 963b370f 2018-05-20 stsp
1466 4e4a9ac8 2022-06-17 op /*
1467 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1468 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1469 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1470 4e4a9ac8 2022-06-17 op * *rcol.
1471 ccda2f4d 2022-06-16 stsp */
1472 4e4a9ac8 2022-06-17 op static int
1473 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1474 4e4a9ac8 2022-06-17 op {
1475 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1476 ccda2f4d 2022-06-16 stsp
1477 4e4a9ac8 2022-06-17 op if (n == 0) {
1478 4e4a9ac8 2022-06-17 op *rcol = cols;
1479 4e4a9ac8 2022-06-17 op return off;
1480 4e4a9ac8 2022-06-17 op }
1481 ccda2f4d 2022-06-16 stsp
1482 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1483 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1484 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1485 4e4a9ac8 2022-06-17 op else
1486 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1487 ccda2f4d 2022-06-16 stsp
1488 4e4a9ac8 2022-06-17 op if (width == -1) {
1489 4e4a9ac8 2022-06-17 op width = 1;
1490 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1491 ccda2f4d 2022-06-16 stsp }
1492 ccda2f4d 2022-06-16 stsp
1493 4e4a9ac8 2022-06-17 op if (cols + width > n)
1494 4e4a9ac8 2022-06-17 op break;
1495 4e4a9ac8 2022-06-17 op cols += width;
1496 ccda2f4d 2022-06-16 stsp }
1497 ccda2f4d 2022-06-16 stsp
1498 4e4a9ac8 2022-06-17 op *rcol = cols;
1499 4e4a9ac8 2022-06-17 op return i;
1500 ccda2f4d 2022-06-16 stsp }
1501 ccda2f4d 2022-06-16 stsp
1502 ccda2f4d 2022-06-16 stsp /*
1503 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1504 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1505 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1506 ccda2f4d 2022-06-16 stsp */
1507 ccda2f4d 2022-06-16 stsp static const struct got_error *
1508 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1509 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1510 ccda2f4d 2022-06-16 stsp {
1511 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1512 4e4a9ac8 2022-06-17 op int cols;
1513 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1514 145b6838 2022-06-16 stsp char *exstr = NULL;
1515 963b370f 2018-05-20 stsp size_t wlen;
1516 4e4a9ac8 2022-06-17 op int i, scrollx;
1517 963b370f 2018-05-20 stsp
1518 963b370f 2018-05-20 stsp *wlinep = NULL;
1519 b700b5d6 2018-07-10 stsp *widthp = 0;
1520 963b370f 2018-05-20 stsp
1521 145b6838 2022-06-16 stsp if (expand) {
1522 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1523 145b6838 2022-06-16 stsp if (err)
1524 145b6838 2022-06-16 stsp return err;
1525 145b6838 2022-06-16 stsp }
1526 145b6838 2022-06-16 stsp
1527 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1528 145b6838 2022-06-16 stsp free(exstr);
1529 963b370f 2018-05-20 stsp if (err)
1530 963b370f 2018-05-20 stsp return err;
1531 963b370f 2018-05-20 stsp
1532 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1533 ccda2f4d 2022-06-16 stsp
1534 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1535 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1536 3f670bfb 2020-12-10 stsp wlen--;
1537 3f670bfb 2020-12-10 stsp }
1538 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1539 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1540 3f670bfb 2020-12-10 stsp wlen--;
1541 3f670bfb 2020-12-10 stsp }
1542 3f670bfb 2020-12-10 stsp
1543 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1544 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1545 27a741e5 2019-09-11 stsp
1546 b700b5d6 2018-07-10 stsp if (widthp)
1547 b700b5d6 2018-07-10 stsp *widthp = cols;
1548 ccda2f4d 2022-06-16 stsp if (scrollxp)
1549 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1550 963b370f 2018-05-20 stsp if (err)
1551 963b370f 2018-05-20 stsp free(wline);
1552 963b370f 2018-05-20 stsp else
1553 963b370f 2018-05-20 stsp *wlinep = wline;
1554 963b370f 2018-05-20 stsp return err;
1555 963b370f 2018-05-20 stsp }
1556 963b370f 2018-05-20 stsp
1557 8b473291 2019-02-21 stsp static const struct got_error*
1558 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1559 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1560 8b473291 2019-02-21 stsp {
1561 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1562 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1563 8b473291 2019-02-21 stsp char *s;
1564 8b473291 2019-02-21 stsp const char *name;
1565 8b473291 2019-02-21 stsp
1566 8b473291 2019-02-21 stsp *refs_str = NULL;
1567 8b473291 2019-02-21 stsp
1568 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1569 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1570 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1571 52b5abe1 2019-08-13 stsp int cmp;
1572 52b5abe1 2019-08-13 stsp
1573 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1574 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1575 8b473291 2019-02-21 stsp continue;
1576 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1577 8b473291 2019-02-21 stsp name += 5;
1578 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1579 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1580 7143d404 2019-03-12 stsp continue;
1581 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1582 8b473291 2019-02-21 stsp name += 6;
1583 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1584 8b473291 2019-02-21 stsp name += 8;
1585 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1586 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1587 79cc719f 2020-04-24 stsp continue;
1588 79cc719f 2020-04-24 stsp }
1589 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1590 48cae60d 2020-09-22 stsp if (err)
1591 48cae60d 2020-09-22 stsp break;
1592 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1593 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1594 5d844a1e 2019-08-13 stsp if (err) {
1595 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1596 48cae60d 2020-09-22 stsp free(ref_id);
1597 5d844a1e 2019-08-13 stsp break;
1598 48cae60d 2020-09-22 stsp }
1599 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1600 5d844a1e 2019-08-13 stsp err = NULL;
1601 5d844a1e 2019-08-13 stsp tag = NULL;
1602 5d844a1e 2019-08-13 stsp }
1603 52b5abe1 2019-08-13 stsp }
1604 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1605 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1606 48cae60d 2020-09-22 stsp free(ref_id);
1607 52b5abe1 2019-08-13 stsp if (tag)
1608 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1609 52b5abe1 2019-08-13 stsp if (cmp != 0)
1610 52b5abe1 2019-08-13 stsp continue;
1611 8b473291 2019-02-21 stsp s = *refs_str;
1612 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1613 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1615 8b473291 2019-02-21 stsp free(s);
1616 8b473291 2019-02-21 stsp *refs_str = NULL;
1617 8b473291 2019-02-21 stsp break;
1618 8b473291 2019-02-21 stsp }
1619 8b473291 2019-02-21 stsp free(s);
1620 8b473291 2019-02-21 stsp }
1621 8b473291 2019-02-21 stsp
1622 8b473291 2019-02-21 stsp return err;
1623 8b473291 2019-02-21 stsp }
1624 8b473291 2019-02-21 stsp
1625 963b370f 2018-05-20 stsp static const struct got_error *
1626 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1627 27a741e5 2019-09-11 stsp int col_tab_align)
1628 5813d178 2019-03-09 stsp {
1629 e6b8b890 2020-12-29 naddy char *smallerthan;
1630 5813d178 2019-03-09 stsp
1631 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1632 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1633 5813d178 2019-03-09 stsp author = smallerthan + 1;
1634 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1635 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1636 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1637 5813d178 2019-03-09 stsp }
1638 5813d178 2019-03-09 stsp
1639 5813d178 2019-03-09 stsp static const struct got_error *
1640 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1641 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1642 8fdc79fe 2020-12-01 naddy int author_display_cols)
1643 80ddbec8 2018-04-29 stsp {
1644 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1645 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1646 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1647 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1648 5813d178 2019-03-09 stsp char *author = NULL;
1649 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1650 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1651 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1652 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1653 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1654 ccb26ccd 2018-11-05 stsp struct tm tm;
1655 45d799e2 2018-12-23 stsp time_t committer_time;
1656 11b20872 2019-11-08 stsp struct tog_color *tc;
1657 80ddbec8 2018-04-29 stsp
1658 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1659 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1660 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1661 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1662 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1663 b39d25c7 2018-07-10 stsp
1664 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1665 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1666 b39d25c7 2018-07-10 stsp else
1667 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1668 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1669 11b20872 2019-11-08 stsp if (tc)
1670 11b20872 2019-11-08 stsp wattr_on(view->window,
1671 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1672 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1673 11b20872 2019-11-08 stsp if (tc)
1674 11b20872 2019-11-08 stsp wattr_off(view->window,
1675 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1676 27a741e5 2019-09-11 stsp col = limit;
1677 b39d25c7 2018-07-10 stsp if (col > avail)
1678 b39d25c7 2018-07-10 stsp goto done;
1679 6570a66d 2019-11-08 stsp
1680 6570a66d 2019-11-08 stsp if (avail >= 120) {
1681 6570a66d 2019-11-08 stsp char *id_str;
1682 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1683 6570a66d 2019-11-08 stsp if (err)
1684 6570a66d 2019-11-08 stsp goto done;
1685 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1686 11b20872 2019-11-08 stsp if (tc)
1687 11b20872 2019-11-08 stsp wattr_on(view->window,
1688 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1689 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1690 11b20872 2019-11-08 stsp if (tc)
1691 11b20872 2019-11-08 stsp wattr_off(view->window,
1692 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1693 6570a66d 2019-11-08 stsp free(id_str);
1694 6570a66d 2019-11-08 stsp col += 9;
1695 6570a66d 2019-11-08 stsp if (col > avail)
1696 6570a66d 2019-11-08 stsp goto done;
1697 6570a66d 2019-11-08 stsp }
1698 b39d25c7 2018-07-10 stsp
1699 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1700 5813d178 2019-03-09 stsp if (author == NULL) {
1701 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1702 80ddbec8 2018-04-29 stsp goto done;
1703 80ddbec8 2018-04-29 stsp }
1704 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1705 bb737323 2018-05-20 stsp if (err)
1706 bb737323 2018-05-20 stsp goto done;
1707 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1708 11b20872 2019-11-08 stsp if (tc)
1709 11b20872 2019-11-08 stsp wattr_on(view->window,
1710 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1711 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1712 11b20872 2019-11-08 stsp if (tc)
1713 11b20872 2019-11-08 stsp wattr_off(view->window,
1714 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1715 bb737323 2018-05-20 stsp col += author_width;
1716 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1717 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1718 bb737323 2018-05-20 stsp col++;
1719 bb737323 2018-05-20 stsp author_width++;
1720 bb737323 2018-05-20 stsp }
1721 9c2eaf34 2018-05-20 stsp if (col > avail)
1722 9c2eaf34 2018-05-20 stsp goto done;
1723 80ddbec8 2018-04-29 stsp
1724 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1725 5943eee2 2019-08-13 stsp if (err)
1726 6d9fbc00 2018-04-29 stsp goto done;
1727 bb737323 2018-05-20 stsp logmsg = logmsg0;
1728 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1729 bb737323 2018-05-20 stsp logmsg++;
1730 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1731 bb737323 2018-05-20 stsp if (newline)
1732 bb737323 2018-05-20 stsp *newline = '\0';
1733 ccda2f4d 2022-06-16 stsp limit = avail - col;
1734 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1735 4d1f6af3 2022-06-17 op limit--; /* for the border */
1736 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1737 ccda2f4d 2022-06-16 stsp limit, col, 1);
1738 29688b02 2022-06-16 stsp if (err)
1739 29688b02 2022-06-16 stsp goto done;
1740 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1741 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1742 27a741e5 2019-09-11 stsp while (col < avail) {
1743 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1744 bb737323 2018-05-20 stsp col++;
1745 881b2d3e 2018-04-30 stsp }
1746 80ddbec8 2018-04-29 stsp done:
1747 80ddbec8 2018-04-29 stsp free(logmsg0);
1748 bb737323 2018-05-20 stsp free(wlogmsg);
1749 5813d178 2019-03-09 stsp free(author);
1750 bb737323 2018-05-20 stsp free(wauthor);
1751 80ddbec8 2018-04-29 stsp free(line);
1752 80ddbec8 2018-04-29 stsp return err;
1753 80ddbec8 2018-04-29 stsp }
1754 26ed57b2 2018-05-19 stsp
1755 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1756 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1757 899d86c2 2018-05-10 stsp struct got_object_id *id)
1758 80ddbec8 2018-04-29 stsp {
1759 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1760 80ddbec8 2018-04-29 stsp
1761 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1762 80ddbec8 2018-04-29 stsp if (entry == NULL)
1763 899d86c2 2018-05-10 stsp return NULL;
1764 99db9666 2018-05-07 stsp
1765 899d86c2 2018-05-10 stsp entry->id = id;
1766 99db9666 2018-05-07 stsp entry->commit = commit;
1767 899d86c2 2018-05-10 stsp return entry;
1768 99db9666 2018-05-07 stsp }
1769 80ddbec8 2018-04-29 stsp
1770 99db9666 2018-05-07 stsp static void
1771 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1772 99db9666 2018-05-07 stsp {
1773 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1774 99db9666 2018-05-07 stsp
1775 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1776 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1777 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1778 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1779 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1780 99db9666 2018-05-07 stsp free(entry);
1781 99db9666 2018-05-07 stsp }
1782 99db9666 2018-05-07 stsp
1783 99db9666 2018-05-07 stsp static void
1784 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1785 99db9666 2018-05-07 stsp {
1786 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1787 99db9666 2018-05-07 stsp pop_commit(commits);
1788 c4972b91 2018-05-07 stsp }
1789 c4972b91 2018-05-07 stsp
1790 c4972b91 2018-05-07 stsp static const struct got_error *
1791 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1792 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1793 13add988 2019-10-15 stsp {
1794 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1795 13add988 2019-10-15 stsp regmatch_t regmatch;
1796 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1797 13add988 2019-10-15 stsp
1798 13add988 2019-10-15 stsp *have_match = 0;
1799 13add988 2019-10-15 stsp
1800 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1801 13add988 2019-10-15 stsp if (err)
1802 13add988 2019-10-15 stsp return err;
1803 13add988 2019-10-15 stsp
1804 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1805 13add988 2019-10-15 stsp if (err)
1806 13add988 2019-10-15 stsp goto done;
1807 13add988 2019-10-15 stsp
1808 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1809 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1810 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1811 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1812 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1813 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1814 13add988 2019-10-15 stsp *have_match = 1;
1815 13add988 2019-10-15 stsp done:
1816 13add988 2019-10-15 stsp free(id_str);
1817 13add988 2019-10-15 stsp free(logmsg);
1818 13add988 2019-10-15 stsp return err;
1819 13add988 2019-10-15 stsp }
1820 13add988 2019-10-15 stsp
1821 13add988 2019-10-15 stsp static const struct got_error *
1822 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1823 c4972b91 2018-05-07 stsp {
1824 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1825 9ba79e04 2018-06-11 stsp
1826 1a76625f 2018-10-22 stsp /*
1827 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1828 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1829 1a76625f 2018-10-22 stsp * while updating the display.
1830 1a76625f 2018-10-22 stsp */
1831 4e0d2870 2020-12-07 naddy do {
1832 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1833 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1834 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1835 1a76625f 2018-10-22 stsp int errcode;
1836 899d86c2 2018-05-10 stsp
1837 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1838 4e0d2870 2020-12-07 naddy NULL, NULL);
1839 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1840 ecb28ae0 2018-07-16 stsp break;
1841 899d86c2 2018-05-10 stsp
1842 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1843 9ba79e04 2018-06-11 stsp if (err)
1844 9ba79e04 2018-06-11 stsp break;
1845 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1846 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1847 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1848 9ba79e04 2018-06-11 stsp break;
1849 9ba79e04 2018-06-11 stsp }
1850 93e45b7c 2018-09-24 stsp
1851 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1852 1a76625f 2018-10-22 stsp if (errcode) {
1853 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1854 13add988 2019-10-15 stsp "pthread_mutex_lock");
1855 1a76625f 2018-10-22 stsp break;
1856 1a76625f 2018-10-22 stsp }
1857 1a76625f 2018-10-22 stsp
1858 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1859 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1860 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1861 1a76625f 2018-10-22 stsp
1862 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1863 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1864 7c1452c1 2020-03-26 stsp int have_match;
1865 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1866 7c1452c1 2020-03-26 stsp if (err)
1867 7c1452c1 2020-03-26 stsp break;
1868 7c1452c1 2020-03-26 stsp if (have_match)
1869 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1870 13add988 2019-10-15 stsp }
1871 13add988 2019-10-15 stsp
1872 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1873 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1874 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1875 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1876 7c1452c1 2020-03-26 stsp if (err)
1877 13add988 2019-10-15 stsp break;
1878 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1879 899d86c2 2018-05-10 stsp
1880 9ba79e04 2018-06-11 stsp return err;
1881 0553a4e3 2018-04-30 stsp }
1882 0553a4e3 2018-04-30 stsp
1883 2b779855 2020-12-05 naddy static void
1884 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1885 2b779855 2020-12-05 naddy {
1886 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1887 2b779855 2020-12-05 naddy int ncommits = 0;
1888 2b779855 2020-12-05 naddy
1889 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1890 2b779855 2020-12-05 naddy while (entry) {
1891 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1892 2b779855 2020-12-05 naddy s->selected_entry = entry;
1893 2b779855 2020-12-05 naddy break;
1894 2b779855 2020-12-05 naddy }
1895 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1896 2b779855 2020-12-05 naddy ncommits++;
1897 2b779855 2020-12-05 naddy }
1898 2b779855 2020-12-05 naddy }
1899 2b779855 2020-12-05 naddy
1900 0553a4e3 2018-04-30 stsp static const struct got_error *
1901 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1902 0553a4e3 2018-04-30 stsp {
1903 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1904 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1905 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1906 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1907 60493ae3 2019-06-20 stsp int width;
1908 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1909 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1910 8b473291 2019-02-21 stsp char *refs_str = NULL;
1911 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1912 11b20872 2019-11-08 stsp struct tog_color *tc;
1913 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1914 0553a4e3 2018-04-30 stsp
1915 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1916 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1917 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1918 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1919 1a76625f 2018-10-22 stsp if (err)
1920 ecb28ae0 2018-07-16 stsp return err;
1921 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1922 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1923 d2075bf3 2020-12-25 stsp if (refs) {
1924 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1925 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1926 d2075bf3 2020-12-25 stsp if (err)
1927 d2075bf3 2020-12-25 stsp goto done;
1928 d2075bf3 2020-12-25 stsp }
1929 867c6645 2018-07-10 stsp }
1930 359bfafd 2019-02-22 stsp
1931 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1932 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1933 1a76625f 2018-10-22 stsp
1934 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1935 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1936 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1937 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1938 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1939 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1940 8f4ed634 2020-03-26 stsp goto done;
1941 8f4ed634 2020-03-26 stsp }
1942 8f4ed634 2020-03-26 stsp } else {
1943 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1944 f9686aa5 2020-03-27 stsp
1945 f9686aa5 2020-03-27 stsp if (view->searching) {
1946 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1947 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1948 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1949 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1950 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1951 f9686aa5 2020-03-27 stsp search_str = "searching...";
1952 f9686aa5 2020-03-27 stsp }
1953 f9686aa5 2020-03-27 stsp
1954 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1955 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1956 f9686aa5 2020-03-27 stsp search_str ? search_str :
1957 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1958 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1959 8f4ed634 2020-03-26 stsp goto done;
1960 8f4ed634 2020-03-26 stsp }
1961 8b473291 2019-02-21 stsp }
1962 1a76625f 2018-10-22 stsp
1963 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1964 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1965 87411fa9 2022-06-16 stsp "........................................",
1966 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1967 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1968 1a76625f 2018-10-22 stsp header = NULL;
1969 1a76625f 2018-10-22 stsp goto done;
1970 1a76625f 2018-10-22 stsp }
1971 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1972 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1973 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1974 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1975 1a76625f 2018-10-22 stsp header = NULL;
1976 1a76625f 2018-10-22 stsp goto done;
1977 ecb28ae0 2018-07-16 stsp }
1978 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1979 1a76625f 2018-10-22 stsp if (err)
1980 1a76625f 2018-10-22 stsp goto done;
1981 867c6645 2018-07-10 stsp
1982 2814baeb 2018-08-01 stsp werase(view->window);
1983 867c6645 2018-07-10 stsp
1984 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1985 a3404814 2018-09-02 stsp wstandout(view->window);
1986 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1987 11b20872 2019-11-08 stsp if (tc)
1988 11b20872 2019-11-08 stsp wattr_on(view->window,
1989 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1990 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1991 11b20872 2019-11-08 stsp if (tc)
1992 11b20872 2019-11-08 stsp wattr_off(view->window,
1993 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1994 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1995 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1996 1a76625f 2018-10-22 stsp width++;
1997 1a76625f 2018-10-22 stsp }
1998 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1999 a3404814 2018-09-02 stsp wstandend(view->window);
2000 ecb28ae0 2018-07-16 stsp free(wline);
2001 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2002 1a76625f 2018-10-22 stsp goto done;
2003 0553a4e3 2018-04-30 stsp
2004 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2005 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2006 5813d178 2019-03-09 stsp ncommits = 0;
2007 145b6838 2022-06-16 stsp view->maxx = 0;
2008 5813d178 2019-03-09 stsp while (entry) {
2009 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2010 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2011 5813d178 2019-03-09 stsp int width;
2012 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2013 5813d178 2019-03-09 stsp break;
2014 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2015 5813d178 2019-03-09 stsp if (author == NULL) {
2016 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2017 5813d178 2019-03-09 stsp goto done;
2018 5813d178 2019-03-09 stsp }
2019 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2020 27a741e5 2019-09-11 stsp date_display_cols);
2021 5813d178 2019-03-09 stsp if (author_cols < width)
2022 5813d178 2019-03-09 stsp author_cols = width;
2023 5813d178 2019-03-09 stsp free(wauthor);
2024 5813d178 2019-03-09 stsp free(author);
2025 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2026 145b6838 2022-06-16 stsp if (err)
2027 145b6838 2022-06-16 stsp goto done;
2028 145b6838 2022-06-16 stsp msg = msg0;
2029 145b6838 2022-06-16 stsp while (*msg == '\n')
2030 145b6838 2022-06-16 stsp ++msg;
2031 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2032 29688b02 2022-06-16 stsp *eol = '\0';
2033 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2034 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2035 29688b02 2022-06-16 stsp if (err)
2036 29688b02 2022-06-16 stsp goto done;
2037 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2038 145b6838 2022-06-16 stsp free(msg0);
2039 29688b02 2022-06-16 stsp free(wmsg);
2040 7ca04879 2019-10-19 stsp ncommits++;
2041 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2042 5813d178 2019-03-09 stsp }
2043 5813d178 2019-03-09 stsp
2044 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2045 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2046 867c6645 2018-07-10 stsp ncommits = 0;
2047 899d86c2 2018-05-10 stsp while (entry) {
2048 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2049 899d86c2 2018-05-10 stsp break;
2050 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2051 2814baeb 2018-08-01 stsp wstandout(view->window);
2052 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2053 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2054 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2055 2814baeb 2018-08-01 stsp wstandend(view->window);
2056 0553a4e3 2018-04-30 stsp if (err)
2057 60493ae3 2019-06-20 stsp goto done;
2058 0553a4e3 2018-04-30 stsp ncommits++;
2059 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2060 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2061 80ddbec8 2018-04-29 stsp }
2062 80ddbec8 2018-04-29 stsp
2063 9b058f45 2022-06-30 mark view_border(view);
2064 1a76625f 2018-10-22 stsp done:
2065 1a76625f 2018-10-22 stsp free(id_str);
2066 8b473291 2019-02-21 stsp free(refs_str);
2067 1a76625f 2018-10-22 stsp free(ncommits_str);
2068 1a76625f 2018-10-22 stsp free(header);
2069 80ddbec8 2018-04-29 stsp return err;
2070 9f7d7167 2018-04-29 stsp }
2071 07b55e75 2018-05-10 stsp
2072 07b55e75 2018-05-10 stsp static void
2073 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2074 07b55e75 2018-05-10 stsp {
2075 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2076 07b55e75 2018-05-10 stsp int nscrolled = 0;
2077 07b55e75 2018-05-10 stsp
2078 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2079 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2080 07b55e75 2018-05-10 stsp return;
2081 9f7d7167 2018-04-29 stsp
2082 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2083 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2084 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2085 07b55e75 2018-05-10 stsp if (entry) {
2086 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2087 07b55e75 2018-05-10 stsp nscrolled++;
2088 07b55e75 2018-05-10 stsp }
2089 07b55e75 2018-05-10 stsp }
2090 aa075928 2018-05-10 stsp }
2091 aa075928 2018-05-10 stsp
2092 aa075928 2018-05-10 stsp static const struct got_error *
2093 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2094 aa075928 2018-05-10 stsp {
2095 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2096 5e224a3e 2019-02-22 stsp int errcode;
2097 8a42fca8 2019-02-22 stsp
2098 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2099 aa075928 2018-05-10 stsp
2100 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2101 ffe38506 2020-12-01 naddy if (ta->log_complete)
2102 5e224a3e 2019-02-22 stsp break;
2103 b295e71b 2019-02-22 stsp
2104 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2105 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2106 7aafa0d1 2019-02-22 stsp if (errcode)
2107 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2108 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2109 7c1452c1 2020-03-26 stsp
2110 7c1452c1 2020-03-26 stsp /*
2111 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2112 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2113 7c1452c1 2020-03-26 stsp */
2114 7c1452c1 2020-03-26 stsp if (!wait)
2115 7c1452c1 2020-03-26 stsp break;
2116 7c1452c1 2020-03-26 stsp
2117 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2118 ffe38506 2020-12-01 naddy show_log_view(view);
2119 7c1452c1 2020-03-26 stsp update_panels();
2120 7c1452c1 2020-03-26 stsp doupdate();
2121 7c1452c1 2020-03-26 stsp
2122 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2123 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2124 82954512 2020-02-03 stsp if (errcode)
2125 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2126 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2127 82954512 2020-02-03 stsp
2128 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2129 ffe38506 2020-12-01 naddy show_log_view(view);
2130 7c1452c1 2020-03-26 stsp update_panels();
2131 7c1452c1 2020-03-26 stsp doupdate();
2132 5e224a3e 2019-02-22 stsp }
2133 5e224a3e 2019-02-22 stsp
2134 5e224a3e 2019-02-22 stsp return NULL;
2135 5e224a3e 2019-02-22 stsp }
2136 5e224a3e 2019-02-22 stsp
2137 5e224a3e 2019-02-22 stsp static const struct got_error *
2138 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2139 9b058f45 2022-06-30 mark {
2140 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2141 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2142 9b058f45 2022-06-30 mark
2143 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2144 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2145 9b058f45 2022-06-30 mark view->nscrolled = 0;
2146 9b058f45 2022-06-30 mark
2147 9b058f45 2022-06-30 mark return err;
2148 9b058f45 2022-06-30 mark }
2149 9b058f45 2022-06-30 mark
2150 9b058f45 2022-06-30 mark static const struct got_error *
2151 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2152 5e224a3e 2019-02-22 stsp {
2153 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2154 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2155 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2156 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2157 5e224a3e 2019-02-22 stsp
2158 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2159 5e224a3e 2019-02-22 stsp return NULL;
2160 5e224a3e 2019-02-22 stsp
2161 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2162 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2163 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2164 08ebd0a9 2019-02-22 stsp /*
2165 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2166 08ebd0a9 2019-02-22 stsp */
2167 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2168 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2169 5e224a3e 2019-02-22 stsp if (err)
2170 5e224a3e 2019-02-22 stsp return err;
2171 7aafa0d1 2019-02-22 stsp }
2172 b295e71b 2019-02-22 stsp
2173 7aafa0d1 2019-02-22 stsp do {
2174 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2175 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2176 88048b54 2019-02-21 stsp break;
2177 88048b54 2019-02-21 stsp
2178 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2179 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2180 aa075928 2018-05-10 stsp
2181 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2182 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2183 dd0a52c1 2018-05-20 stsp break;
2184 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2185 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2186 aa075928 2018-05-10 stsp
2187 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2188 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2189 9b058f45 2022-06-30 mark else
2190 9b058f45 2022-06-30 mark view->nscrolled = 0;
2191 9b058f45 2022-06-30 mark
2192 dd0a52c1 2018-05-20 stsp return err;
2193 07b55e75 2018-05-10 stsp }
2194 4a7f7875 2018-05-10 stsp
2195 cd0acaa7 2018-05-20 stsp static const struct got_error *
2196 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2197 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2198 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2199 cd0acaa7 2018-05-20 stsp {
2200 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2201 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2202 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2203 cd0acaa7 2018-05-20 stsp
2204 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2205 15a94983 2018-12-23 stsp if (diff_view == NULL)
2206 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2207 ea5e7bb5 2018-08-01 stsp
2208 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2209 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2210 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2211 e5a0f69f 2018-08-18 stsp if (err == NULL)
2212 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2213 cd0acaa7 2018-05-20 stsp return err;
2214 4a7f7875 2018-05-10 stsp }
2215 4a7f7875 2018-05-10 stsp
2216 80ddbec8 2018-04-29 stsp static const struct got_error *
2217 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2218 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2219 9343a5fb 2018-06-23 stsp {
2220 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2221 941e9f74 2019-05-21 stsp
2222 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2223 941e9f74 2019-05-21 stsp if (parent == NULL)
2224 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2225 941e9f74 2019-05-21 stsp
2226 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2227 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2228 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2229 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2230 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2231 941e9f74 2019-05-21 stsp s->tree = subtree;
2232 941e9f74 2019-05-21 stsp s->selected = 0;
2233 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2234 941e9f74 2019-05-21 stsp return NULL;
2235 941e9f74 2019-05-21 stsp }
2236 941e9f74 2019-05-21 stsp
2237 941e9f74 2019-05-21 stsp static const struct got_error *
2238 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2239 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2240 941e9f74 2019-05-21 stsp {
2241 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2242 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2243 941e9f74 2019-05-21 stsp const char *p;
2244 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2245 9343a5fb 2018-06-23 stsp
2246 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2247 941e9f74 2019-05-21 stsp p = path;
2248 941e9f74 2019-05-21 stsp while (*p) {
2249 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2250 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2251 56e0773d 2019-11-28 stsp char *te_name;
2252 33cbf02b 2020-01-12 stsp
2253 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2254 33cbf02b 2020-01-12 stsp p++;
2255 941e9f74 2019-05-21 stsp
2256 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2257 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2258 941e9f74 2019-05-21 stsp if (slash == NULL)
2259 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2260 33cbf02b 2020-01-12 stsp else
2261 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2262 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2263 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2264 56e0773d 2019-11-28 stsp break;
2265 941e9f74 2019-05-21 stsp }
2266 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2267 56e0773d 2019-11-28 stsp if (te == NULL) {
2268 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2269 56e0773d 2019-11-28 stsp free(te_name);
2270 941e9f74 2019-05-21 stsp break;
2271 941e9f74 2019-05-21 stsp }
2272 56e0773d 2019-11-28 stsp free(te_name);
2273 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2274 941e9f74 2019-05-21 stsp
2275 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2276 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2277 b03c880f 2019-05-21 stsp
2278 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2279 941e9f74 2019-05-21 stsp if (slash)
2280 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2281 941e9f74 2019-05-21 stsp else
2282 941e9f74 2019-05-21 stsp subpath = strdup(path);
2283 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2284 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2285 941e9f74 2019-05-21 stsp break;
2286 941e9f74 2019-05-21 stsp }
2287 941e9f74 2019-05-21 stsp
2288 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2289 941e9f74 2019-05-21 stsp subpath);
2290 941e9f74 2019-05-21 stsp if (err)
2291 941e9f74 2019-05-21 stsp break;
2292 941e9f74 2019-05-21 stsp
2293 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2294 941e9f74 2019-05-21 stsp free(tree_id);
2295 941e9f74 2019-05-21 stsp if (err)
2296 941e9f74 2019-05-21 stsp break;
2297 941e9f74 2019-05-21 stsp
2298 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2299 941e9f74 2019-05-21 stsp if (err) {
2300 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2301 941e9f74 2019-05-21 stsp break;
2302 941e9f74 2019-05-21 stsp }
2303 941e9f74 2019-05-21 stsp if (slash == NULL)
2304 941e9f74 2019-05-21 stsp break;
2305 941e9f74 2019-05-21 stsp free(subpath);
2306 941e9f74 2019-05-21 stsp subpath = NULL;
2307 941e9f74 2019-05-21 stsp p = slash;
2308 941e9f74 2019-05-21 stsp }
2309 941e9f74 2019-05-21 stsp
2310 941e9f74 2019-05-21 stsp free(subpath);
2311 1a76625f 2018-10-22 stsp return err;
2312 61266923 2020-01-14 stsp }
2313 61266923 2020-01-14 stsp
2314 61266923 2020-01-14 stsp static const struct got_error *
2315 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2316 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2317 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2318 55cccc34 2020-02-20 stsp {
2319 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2320 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2321 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2322 55cccc34 2020-02-20 stsp
2323 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2324 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2325 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2326 55cccc34 2020-02-20 stsp
2327 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2328 bc573f3b 2021-07-10 stsp if (err)
2329 55cccc34 2020-02-20 stsp return err;
2330 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2331 55cccc34 2020-02-20 stsp
2332 55cccc34 2020-02-20 stsp *new_view = tree_view;
2333 55cccc34 2020-02-20 stsp
2334 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2335 55cccc34 2020-02-20 stsp return NULL;
2336 55cccc34 2020-02-20 stsp
2337 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2338 55cccc34 2020-02-20 stsp }
2339 55cccc34 2020-02-20 stsp
2340 55cccc34 2020-02-20 stsp static const struct got_error *
2341 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2342 61266923 2020-01-14 stsp {
2343 61266923 2020-01-14 stsp sigset_t sigset;
2344 61266923 2020-01-14 stsp int errcode;
2345 61266923 2020-01-14 stsp
2346 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2347 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2348 61266923 2020-01-14 stsp
2349 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2350 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2351 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2352 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2353 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2354 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2355 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2356 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2357 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2358 61266923 2020-01-14 stsp
2359 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2360 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2361 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2362 61266923 2020-01-14 stsp
2363 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2364 61266923 2020-01-14 stsp if (errcode)
2365 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2366 61266923 2020-01-14 stsp
2367 61266923 2020-01-14 stsp return NULL;
2368 1a76625f 2018-10-22 stsp }
2369 1a76625f 2018-10-22 stsp
2370 1a76625f 2018-10-22 stsp static void *
2371 1a76625f 2018-10-22 stsp log_thread(void *arg)
2372 1a76625f 2018-10-22 stsp {
2373 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2374 1a76625f 2018-10-22 stsp int errcode = 0;
2375 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2376 1a76625f 2018-10-22 stsp int done = 0;
2377 61266923 2020-01-14 stsp
2378 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2379 61266923 2020-01-14 stsp if (err)
2380 61266923 2020-01-14 stsp return (void *)err;
2381 1a76625f 2018-10-22 stsp
2382 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2383 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2384 1a76625f 2018-10-22 stsp if (err) {
2385 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2386 1a76625f 2018-10-22 stsp return (void *)err;
2387 1a76625f 2018-10-22 stsp err = NULL;
2388 1a76625f 2018-10-22 stsp done = 1;
2389 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2390 1a76625f 2018-10-22 stsp a->commits_needed--;
2391 1a76625f 2018-10-22 stsp
2392 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2393 3abe8080 2019-04-10 stsp if (errcode) {
2394 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2395 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2396 3abe8080 2019-04-10 stsp break;
2397 3abe8080 2019-04-10 stsp } else if (*a->quit)
2398 1a76625f 2018-10-22 stsp done = 1;
2399 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2400 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2401 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2402 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2403 1a76625f 2018-10-22 stsp }
2404 1a76625f 2018-10-22 stsp
2405 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2406 7c1452c1 2020-03-26 stsp if (errcode) {
2407 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2408 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2409 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2410 7c1452c1 2020-03-26 stsp break;
2411 7c1452c1 2020-03-26 stsp }
2412 7c1452c1 2020-03-26 stsp
2413 1a76625f 2018-10-22 stsp if (done)
2414 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2415 7c1452c1 2020-03-26 stsp else {
2416 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2417 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2418 7c1452c1 2020-03-26 stsp &tog_mutex);
2419 7c1452c1 2020-03-26 stsp if (errcode)
2420 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2421 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2422 21355643 2020-12-06 stsp if (*a->quit)
2423 21355643 2020-12-06 stsp done = 1;
2424 7c1452c1 2020-03-26 stsp }
2425 1a76625f 2018-10-22 stsp }
2426 1a76625f 2018-10-22 stsp
2427 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2428 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2429 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2430 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2431 1a76625f 2018-10-22 stsp }
2432 3abe8080 2019-04-10 stsp a->log_complete = 1;
2433 1a76625f 2018-10-22 stsp return (void *)err;
2434 1a76625f 2018-10-22 stsp }
2435 1a76625f 2018-10-22 stsp
2436 1a76625f 2018-10-22 stsp static const struct got_error *
2437 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2438 1a76625f 2018-10-22 stsp {
2439 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2440 1a76625f 2018-10-22 stsp int errcode;
2441 1a76625f 2018-10-22 stsp
2442 1a76625f 2018-10-22 stsp if (s->thread) {
2443 1a76625f 2018-10-22 stsp s->quit = 1;
2444 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2445 1a76625f 2018-10-22 stsp if (errcode)
2446 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2447 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2448 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2449 1a76625f 2018-10-22 stsp if (errcode)
2450 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2451 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2452 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2453 1a76625f 2018-10-22 stsp if (errcode)
2454 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2455 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2456 1a76625f 2018-10-22 stsp if (errcode)
2457 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2458 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2459 1a76625f 2018-10-22 stsp s->thread = NULL;
2460 1a76625f 2018-10-22 stsp }
2461 1a76625f 2018-10-22 stsp
2462 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2463 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2464 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2465 74467cc8 2022-06-15 stsp }
2466 74467cc8 2022-06-15 stsp
2467 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2468 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2469 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2470 74467cc8 2022-06-15 stsp if (err == NULL)
2471 74467cc8 2022-06-15 stsp err = pack_err;
2472 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2473 1a76625f 2018-10-22 stsp }
2474 1a76625f 2018-10-22 stsp
2475 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2476 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2477 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2478 1a76625f 2018-10-22 stsp }
2479 1a76625f 2018-10-22 stsp
2480 9343a5fb 2018-06-23 stsp return err;
2481 9343a5fb 2018-06-23 stsp }
2482 9343a5fb 2018-06-23 stsp
2483 9343a5fb 2018-06-23 stsp static const struct got_error *
2484 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2485 1a76625f 2018-10-22 stsp {
2486 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2487 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2488 276b94a1 2020-11-13 naddy int errcode;
2489 1a76625f 2018-10-22 stsp
2490 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2491 276b94a1 2020-11-13 naddy
2492 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2493 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2494 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2495 276b94a1 2020-11-13 naddy
2496 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2497 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2498 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2499 276b94a1 2020-11-13 naddy
2500 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2501 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2502 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2503 1a76625f 2018-10-22 stsp free(s->start_id);
2504 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2505 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2506 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2507 1a76625f 2018-10-22 stsp return err;
2508 1a76625f 2018-10-22 stsp }
2509 1a76625f 2018-10-22 stsp
2510 1a76625f 2018-10-22 stsp static const struct got_error *
2511 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2512 60493ae3 2019-06-20 stsp {
2513 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2514 60493ae3 2019-06-20 stsp
2515 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2516 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2517 60493ae3 2019-06-20 stsp return NULL;
2518 60493ae3 2019-06-20 stsp }
2519 60493ae3 2019-06-20 stsp
2520 60493ae3 2019-06-20 stsp static const struct got_error *
2521 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2522 60493ae3 2019-06-20 stsp {
2523 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2524 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2525 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2526 60493ae3 2019-06-20 stsp
2527 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2528 f9686aa5 2020-03-27 stsp show_log_view(view);
2529 f9686aa5 2020-03-27 stsp update_panels();
2530 f9686aa5 2020-03-27 stsp doupdate();
2531 f9686aa5 2020-03-27 stsp
2532 96e2b566 2019-07-08 stsp if (s->search_entry) {
2533 13add988 2019-10-15 stsp int errcode, ch;
2534 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2535 13add988 2019-10-15 stsp if (errcode)
2536 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2537 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2538 13add988 2019-10-15 stsp ch = wgetch(view->window);
2539 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2540 13add988 2019-10-15 stsp if (errcode)
2541 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2542 13add988 2019-10-15 stsp "pthread_mutex_lock");
2543 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2544 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2545 678cbce5 2019-07-28 stsp return NULL;
2546 678cbce5 2019-07-28 stsp }
2547 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2548 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2549 96e2b566 2019-07-08 stsp else
2550 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2551 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2552 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2553 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2554 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2555 364ac6fd 2022-06-18 stsp
2556 364ac6fd 2022-06-18 stsp /*
2557 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2558 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2559 f704b35c 2022-06-18 stsp * might have changed.
2560 364ac6fd 2022-06-18 stsp */
2561 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2562 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2563 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2564 364ac6fd 2022-06-18 stsp else
2565 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2566 4bfe9f0a 2022-06-18 stsp } else {
2567 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2568 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2569 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2570 364ac6fd 2022-06-18 stsp else
2571 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2572 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2573 4bfe9f0a 2022-06-18 stsp }
2574 20be8d96 2019-06-21 stsp } else {
2575 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2576 20be8d96 2019-06-21 stsp }
2577 60493ae3 2019-06-20 stsp
2578 60493ae3 2019-06-20 stsp while (1) {
2579 13add988 2019-10-15 stsp int have_match = 0;
2580 13add988 2019-10-15 stsp
2581 60493ae3 2019-06-20 stsp if (entry == NULL) {
2582 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2583 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2584 f9967bca 2020-03-27 stsp view->search_next_done =
2585 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2586 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2587 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2588 f801134a 2019-06-25 stsp return NULL;
2589 60493ae3 2019-06-20 stsp }
2590 96e2b566 2019-07-08 stsp /*
2591 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2592 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2593 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2594 96e2b566 2019-07-08 stsp */
2595 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2596 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2597 60493ae3 2019-06-20 stsp }
2598 60493ae3 2019-06-20 stsp
2599 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2600 13add988 2019-10-15 stsp &view->regex);
2601 5943eee2 2019-08-13 stsp if (err)
2602 13add988 2019-10-15 stsp break;
2603 13add988 2019-10-15 stsp if (have_match) {
2604 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2605 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2606 60493ae3 2019-06-20 stsp break;
2607 60493ae3 2019-06-20 stsp }
2608 13add988 2019-10-15 stsp
2609 96e2b566 2019-07-08 stsp s->search_entry = entry;
2610 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2611 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2612 b1bf1435 2019-06-21 stsp else
2613 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2614 60493ae3 2019-06-20 stsp }
2615 60493ae3 2019-06-20 stsp
2616 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2617 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
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_DOWN);
2620 60493ae3 2019-06-20 stsp if (err)
2621 60493ae3 2019-06-20 stsp return err;
2622 ead14cbe 2019-06-21 stsp cur++;
2623 ead14cbe 2019-06-21 stsp }
2624 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2625 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2626 60493ae3 2019-06-20 stsp if (err)
2627 60493ae3 2019-06-20 stsp return err;
2628 ead14cbe 2019-06-21 stsp cur--;
2629 60493ae3 2019-06-20 stsp }
2630 60493ae3 2019-06-20 stsp }
2631 60493ae3 2019-06-20 stsp
2632 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2633 96e2b566 2019-07-08 stsp
2634 60493ae3 2019-06-20 stsp return NULL;
2635 60493ae3 2019-06-20 stsp }
2636 60493ae3 2019-06-20 stsp
2637 60493ae3 2019-06-20 stsp static const struct got_error *
2638 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2639 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2640 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2641 80ddbec8 2018-04-29 stsp {
2642 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2643 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2644 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2645 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2646 1a76625f 2018-10-22 stsp int errcode;
2647 80ddbec8 2018-04-29 stsp
2648 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2649 f135c941 2020-02-20 stsp free(s->in_repo_path);
2650 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2651 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2652 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2653 f135c941 2020-02-20 stsp }
2654 ecb28ae0 2018-07-16 stsp
2655 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2656 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2657 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2658 78756c87 2020-11-24 stsp
2659 fb2756b9 2018-08-04 stsp s->repo = repo;
2660 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2661 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2662 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2663 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2664 9cd7cbd1 2020-12-07 stsp goto done;
2665 9cd7cbd1 2020-12-07 stsp }
2666 9cd7cbd1 2020-12-07 stsp }
2667 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2668 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2669 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2670 5036bf37 2018-09-24 stsp goto done;
2671 5036bf37 2018-09-24 stsp }
2672 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2673 e5a0f69f 2018-08-18 stsp
2674 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2675 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2676 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2677 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2678 11b20872 2019-11-08 stsp if (err)
2679 11b20872 2019-11-08 stsp goto done;
2680 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2681 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
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 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2687 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2688 11b20872 2019-11-08 stsp if (err) {
2689 11b20872 2019-11-08 stsp free_colors(&s->colors);
2690 11b20872 2019-11-08 stsp goto done;
2691 11b20872 2019-11-08 stsp }
2692 11b20872 2019-11-08 stsp }
2693 11b20872 2019-11-08 stsp
2694 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2695 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2696 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2697 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2698 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2699 1a76625f 2018-10-22 stsp
2700 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2701 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2702 74467cc8 2022-06-15 stsp if (err)
2703 74467cc8 2022-06-15 stsp goto done;
2704 74467cc8 2022-06-15 stsp }
2705 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2706 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2707 0ae84acc 2022-06-15 tracey if (err)
2708 0ae84acc 2022-06-15 tracey goto done;
2709 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2710 b672a97a 2020-01-27 stsp !s->log_branches);
2711 1a76625f 2018-10-22 stsp if (err)
2712 1a76625f 2018-10-22 stsp goto done;
2713 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2714 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2715 c5b78334 2020-01-12 stsp if (err)
2716 c5b78334 2020-01-12 stsp goto done;
2717 1a76625f 2018-10-22 stsp
2718 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2719 1a76625f 2018-10-22 stsp if (errcode) {
2720 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2721 1a76625f 2018-10-22 stsp goto done;
2722 1a76625f 2018-10-22 stsp }
2723 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2724 7c1452c1 2020-03-26 stsp if (errcode) {
2725 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2726 7c1452c1 2020-03-26 stsp goto done;
2727 7c1452c1 2020-03-26 stsp }
2728 1a76625f 2018-10-22 stsp
2729 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2730 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2731 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2732 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2733 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2734 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2735 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2736 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2737 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2738 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2739 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2740 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2741 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2742 ba4f502b 2018-08-04 stsp done:
2743 1a76625f 2018-10-22 stsp if (err)
2744 1a76625f 2018-10-22 stsp close_log_view(view);
2745 ba4f502b 2018-08-04 stsp return err;
2746 ba4f502b 2018-08-04 stsp }
2747 ba4f502b 2018-08-04 stsp
2748 e5a0f69f 2018-08-18 stsp static const struct got_error *
2749 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2750 ba4f502b 2018-08-04 stsp {
2751 f2f6d207 2020-11-24 stsp const struct got_error *err;
2752 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2753 ba4f502b 2018-08-04 stsp
2754 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2755 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2756 2b380cc8 2018-10-24 stsp &s->thread_args);
2757 2b380cc8 2018-10-24 stsp if (errcode)
2758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2759 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2760 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2761 f2f6d207 2020-11-24 stsp if (err)
2762 f2f6d207 2020-11-24 stsp return err;
2763 f2f6d207 2020-11-24 stsp }
2764 2b380cc8 2018-10-24 stsp }
2765 2b380cc8 2018-10-24 stsp
2766 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2767 b880cc75 2022-06-30 mark }
2768 b880cc75 2022-06-30 mark
2769 b880cc75 2022-06-30 mark static void
2770 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2771 b880cc75 2022-06-30 mark {
2772 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2773 b880cc75 2022-06-30 mark
2774 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2775 b880cc75 2022-06-30 mark view->count = 0;
2776 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2777 b880cc75 2022-06-30 mark return;
2778 b880cc75 2022-06-30 mark
2779 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2780 b880cc75 2022-06-30 mark || home)
2781 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2782 b880cc75 2022-06-30 mark
2783 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2784 b880cc75 2022-06-30 mark --s->selected;
2785 b880cc75 2022-06-30 mark else
2786 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2787 b880cc75 2022-06-30 mark
2788 b880cc75 2022-06-30 mark select_commit(s);
2789 b880cc75 2022-06-30 mark return;
2790 b880cc75 2022-06-30 mark }
2791 b880cc75 2022-06-30 mark
2792 b880cc75 2022-06-30 mark static const struct got_error *
2793 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2794 b880cc75 2022-06-30 mark {
2795 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2796 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2797 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2798 b880cc75 2022-06-30 mark
2799 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2800 b880cc75 2022-06-30 mark if (first == NULL) {
2801 b880cc75 2022-06-30 mark view->count = 0;
2802 b880cc75 2022-06-30 mark return NULL;
2803 b880cc75 2022-06-30 mark }
2804 b880cc75 2022-06-30 mark
2805 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2806 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2807 b880cc75 2022-06-30 mark return NULL;
2808 b880cc75 2022-06-30 mark
2809 b880cc75 2022-06-30 mark if (!page) {
2810 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2811 b880cc75 2022-06-30 mark
2812 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2813 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2814 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2815 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2816 b880cc75 2022-06-30 mark ++s->selected;
2817 b880cc75 2022-06-30 mark else
2818 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2819 b880cc75 2022-06-30 mark } else if (s->thread_args.log_complete) {
2820 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2821 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2822 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2823 b880cc75 2022-06-30 mark else
2824 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2825 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2826 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2827 b880cc75 2022-06-30 mark } else {
2828 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2829 b880cc75 2022-06-30 mark if (err)
2830 b880cc75 2022-06-30 mark return err;
2831 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2832 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2833 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2834 b880cc75 2022-06-30 mark }
2835 b880cc75 2022-06-30 mark }
2836 b880cc75 2022-06-30 mark if (err)
2837 b880cc75 2022-06-30 mark return err;
2838 b880cc75 2022-06-30 mark
2839 9b058f45 2022-06-30 mark /*
2840 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2841 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2842 9b058f45 2022-06-30 mark */
2843 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2844 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2845 9b058f45 2022-06-30 mark
2846 b880cc75 2022-06-30 mark select_commit(s);
2847 b880cc75 2022-06-30 mark
2848 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2849 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2850 b880cc75 2022-06-30 mark view->count = 0;
2851 b880cc75 2022-06-30 mark
2852 b880cc75 2022-06-30 mark return NULL;
2853 e5a0f69f 2018-08-18 stsp }
2854 04cc582a 2018-08-01 stsp
2855 9b058f45 2022-06-30 mark /*
2856 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2857 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2858 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2859 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2860 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2861 9b058f45 2022-06-30 mark */
2862 9b058f45 2022-06-30 mark static void
2863 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2864 9b058f45 2022-06-30 mark {
2865 9b058f45 2022-06-30 mark char *mode;
2866 9b058f45 2022-06-30 mark
2867 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2868 9b058f45 2022-06-30 mark
2869 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2870 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2871 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2872 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2873 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2874 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2875 9b058f45 2022-06-30 mark }
2876 9b058f45 2022-06-30 mark }
2877 9b058f45 2022-06-30 mark
2878 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2879 e5a0f69f 2018-08-18 stsp static const struct got_error *
2880 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2881 9b058f45 2022-06-30 mark {
2882 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2883 9b058f45 2022-06-30 mark
2884 9b058f45 2022-06-30 mark view->nlines = y;
2885 9b058f45 2022-06-30 mark err = view_resize(view);
2886 9b058f45 2022-06-30 mark if (err)
2887 9b058f45 2022-06-30 mark return err;
2888 9b058f45 2022-06-30 mark
2889 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2890 9b058f45 2022-06-30 mark
2891 9b058f45 2022-06-30 mark return err;
2892 9b058f45 2022-06-30 mark }
2893 9b058f45 2022-06-30 mark
2894 9b058f45 2022-06-30 mark static const struct got_error *
2895 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2896 e5a0f69f 2018-08-18 stsp {
2897 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2898 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2899 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2900 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2901 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2902 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0, n, nscroll = view->nlines - 1;
2903 80ddbec8 2018-04-29 stsp
2904 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2905 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2906 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2907 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2908 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2909 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2910 fb280deb 2021-08-30 stsp }
2911 b880cc75 2022-06-30 mark return err;
2912 528dedf3 2021-08-30 stsp }
2913 528dedf3 2021-08-30 stsp
2914 528dedf3 2021-08-30 stsp switch (ch) {
2915 1e37a5c2 2019-05-12 jcs case 'q':
2916 1e37a5c2 2019-05-12 jcs s->quit = 1;
2917 145b6838 2022-06-16 stsp break;
2918 145b6838 2022-06-16 stsp case '0':
2919 145b6838 2022-06-16 stsp view->x = 0;
2920 145b6838 2022-06-16 stsp break;
2921 145b6838 2022-06-16 stsp case '$':
2922 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2923 640cd7ff 2022-06-22 mark view->count = 0;
2924 145b6838 2022-06-16 stsp break;
2925 145b6838 2022-06-16 stsp case KEY_RIGHT:
2926 145b6838 2022-06-16 stsp case 'l':
2927 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2928 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2929 640cd7ff 2022-06-22 mark else
2930 640cd7ff 2022-06-22 mark view->count = 0;
2931 1e37a5c2 2019-05-12 jcs break;
2932 145b6838 2022-06-16 stsp case KEY_LEFT:
2933 145b6838 2022-06-16 stsp case 'h':
2934 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2935 640cd7ff 2022-06-22 mark if (view->x <= 0)
2936 640cd7ff 2022-06-22 mark view->count = 0;
2937 145b6838 2022-06-16 stsp break;
2938 1e37a5c2 2019-05-12 jcs case 'k':
2939 1e37a5c2 2019-05-12 jcs case KEY_UP:
2940 1e37a5c2 2019-05-12 jcs case '<':
2941 1e37a5c2 2019-05-12 jcs case ',':
2942 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2943 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2944 912a3f79 2021-08-30 j break;
2945 912a3f79 2021-08-30 j case 'g':
2946 912a3f79 2021-08-30 j case KEY_HOME:
2947 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2948 640cd7ff 2022-06-22 mark view->count = 0;
2949 1e37a5c2 2019-05-12 jcs break;
2950 83cc4199 2022-06-13 stsp case CTRL('u'):
2951 33c3719a 2022-06-15 stsp case 'u':
2952 83cc4199 2022-06-13 stsp nscroll /= 2;
2953 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2954 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2955 a4292ac5 2019-05-12 jcs case CTRL('b'):
2956 61417565 2022-06-20 mark case 'b':
2957 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2958 1e37a5c2 2019-05-12 jcs break;
2959 1e37a5c2 2019-05-12 jcs case 'j':
2960 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2961 1e37a5c2 2019-05-12 jcs case '>':
2962 1e37a5c2 2019-05-12 jcs case '.':
2963 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2964 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2965 912a3f79 2021-08-30 j break;
2966 912a3f79 2021-08-30 j case 'G':
2967 912a3f79 2021-08-30 j case KEY_END: {
2968 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2969 912a3f79 2021-08-30 j * traverse them all. */
2970 640cd7ff 2022-06-22 mark view->count = 0;
2971 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2972 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2973 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2974 80ddbec8 2018-04-29 stsp }
2975 912a3f79 2021-08-30 j
2976 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2977 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2978 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2979 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2980 f3bc9f1d 2021-09-05 naddy break;
2981 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2982 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2983 f3bc9f1d 2021-09-05 naddy }
2984 f3bc9f1d 2021-09-05 naddy if (n > 0)
2985 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2986 2b779855 2020-12-05 naddy select_commit(s);
2987 1e37a5c2 2019-05-12 jcs break;
2988 912a3f79 2021-08-30 j }
2989 80b7e8da 2022-06-11 stsp case CTRL('d'):
2990 33c3719a 2022-06-15 stsp case 'd':
2991 83cc4199 2022-06-13 stsp nscroll /= 2;
2992 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2993 83cc4199 2022-06-13 stsp case KEY_NPAGE:
2994 61417565 2022-06-20 mark case CTRL('f'):
2995 48bb96f0 2022-06-20 naddy case 'f':
2996 b880cc75 2022-06-30 mark case ' ':
2997 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
2998 1e37a5c2 2019-05-12 jcs break;
2999 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3000 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3001 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3002 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3003 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3004 2b779855 2020-12-05 naddy select_commit(s);
3005 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3006 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3007 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3008 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3009 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3010 0bf7f153 2020-12-02 naddy }
3011 1e37a5c2 2019-05-12 jcs break;
3012 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3013 9b058f45 2022-06-30 mark case '\r': {
3014 640cd7ff 2022-06-22 mark view->count = 0;
3015 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3016 e5a0f69f 2018-08-18 stsp break;
3017 9b058f45 2022-06-30 mark
3018 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3019 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3020 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3021 9b058f45 2022-06-30 mark
3022 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3023 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3024 78756c87 2020-11-24 stsp view, s->repo);
3025 1e37a5c2 2019-05-12 jcs if (err)
3026 1e37a5c2 2019-05-12 jcs break;
3027 9b058f45 2022-06-30 mark
3028 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3029 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3030 9b058f45 2022-06-30 mark if (err)
3031 9b058f45 2022-06-30 mark break;
3032 9b058f45 2022-06-30 mark }
3033 9b058f45 2022-06-30 mark
3034 e78dc838 2020-12-04 stsp view->focussed = 0;
3035 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3036 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3037 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3038 9b058f45 2022-06-30 mark
3039 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3040 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3041 f7013a22 2018-10-24 stsp if (err)
3042 1e37a5c2 2019-05-12 jcs return err;
3043 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
3044 0dbbbe90 2022-06-17 op if (err)
3045 0dbbbe90 2022-06-17 op return err;
3046 e78dc838 2020-12-04 stsp view->focus_child = 1;
3047 1e37a5c2 2019-05-12 jcs } else
3048 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3049 1e37a5c2 2019-05-12 jcs break;
3050 9b058f45 2022-06-30 mark }
3051 1e37a5c2 2019-05-12 jcs case 't':
3052 640cd7ff 2022-06-22 mark view->count = 0;
3053 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3054 5036bf37 2018-09-24 stsp break;
3055 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3056 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3057 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3058 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3059 4e97c21c 2020-12-06 stsp s->repo);
3060 1e37a5c2 2019-05-12 jcs if (err)
3061 e5a0f69f 2018-08-18 stsp break;
3062 e78dc838 2020-12-04 stsp view->focussed = 0;
3063 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3065 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3066 1e37a5c2 2019-05-12 jcs if (err)
3067 1e37a5c2 2019-05-12 jcs return err;
3068 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3069 0dbbbe90 2022-06-17 op if (err)
3070 0dbbbe90 2022-06-17 op return err;
3071 e78dc838 2020-12-04 stsp view->focus_child = 1;
3072 1e37a5c2 2019-05-12 jcs } else
3073 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3074 1e37a5c2 2019-05-12 jcs break;
3075 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3076 21355643 2020-12-06 stsp case CTRL('l'):
3077 21355643 2020-12-06 stsp case 'B':
3078 640cd7ff 2022-06-22 mark view->count = 0;
3079 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3080 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3081 1e37a5c2 2019-05-12 jcs break;
3082 21355643 2020-12-06 stsp err = stop_log_thread(s);
3083 74cfe85e 2020-10-20 stsp if (err)
3084 74cfe85e 2020-10-20 stsp return err;
3085 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3086 21355643 2020-12-06 stsp char *parent_path;
3087 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3088 21355643 2020-12-06 stsp if (err)
3089 21355643 2020-12-06 stsp return err;
3090 21355643 2020-12-06 stsp free(s->in_repo_path);
3091 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3092 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3093 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3094 21355643 2020-12-06 stsp struct got_object_id *start_id;
3095 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3096 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3097 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3098 21355643 2020-12-06 stsp if (err)
3099 21355643 2020-12-06 stsp return err;
3100 21355643 2020-12-06 stsp free(s->start_id);
3101 21355643 2020-12-06 stsp s->start_id = start_id;
3102 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3103 21355643 2020-12-06 stsp } else /* 'B' */
3104 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3105 21355643 2020-12-06 stsp
3106 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3107 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3108 b0dd8d27 2022-06-16 stsp if (err)
3109 b0dd8d27 2022-06-16 stsp return err;
3110 b0dd8d27 2022-06-16 stsp }
3111 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3112 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3113 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3114 74cfe85e 2020-10-20 stsp if (err)
3115 21355643 2020-12-06 stsp return err;
3116 51a10b52 2020-12-26 stsp tog_free_refs();
3117 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3118 ca51c541 2020-12-07 stsp if (err)
3119 ca51c541 2020-12-07 stsp return err;
3120 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3121 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3122 d01904d4 2019-06-25 stsp if (err)
3123 d01904d4 2019-06-25 stsp return err;
3124 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3125 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3126 b672a97a 2020-01-27 stsp if (err)
3127 b672a97a 2020-01-27 stsp return err;
3128 21355643 2020-12-06 stsp free_commits(&s->commits);
3129 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3130 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3131 21355643 2020-12-06 stsp s->selected_entry = NULL;
3132 21355643 2020-12-06 stsp s->selected = 0;
3133 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3134 21355643 2020-12-06 stsp s->quit = 0;
3135 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3136 dfee752e 2022-06-18 op s->matched_entry = NULL;
3137 dfee752e 2022-06-18 op s->search_entry = NULL;
3138 d01904d4 2019-06-25 stsp break;
3139 6458efa5 2020-11-24 stsp case 'r':
3140 640cd7ff 2022-06-22 mark view->count = 0;
3141 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3142 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3143 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3144 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3145 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3146 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3147 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3148 6458efa5 2020-11-24 stsp if (err) {
3149 6458efa5 2020-11-24 stsp view_close(ref_view);
3150 6458efa5 2020-11-24 stsp return err;
3151 6458efa5 2020-11-24 stsp }
3152 e78dc838 2020-12-04 stsp view->focussed = 0;
3153 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3154 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3155 6458efa5 2020-11-24 stsp err = view_close_child(view);
3156 6458efa5 2020-11-24 stsp if (err)
3157 6458efa5 2020-11-24 stsp return err;
3158 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3159 0dbbbe90 2022-06-17 op if (err)
3160 0dbbbe90 2022-06-17 op return err;
3161 e78dc838 2020-12-04 stsp view->focus_child = 1;
3162 6458efa5 2020-11-24 stsp } else
3163 6458efa5 2020-11-24 stsp *new_view = ref_view;
3164 6458efa5 2020-11-24 stsp break;
3165 1e37a5c2 2019-05-12 jcs default:
3166 640cd7ff 2022-06-22 mark view->count = 0;
3167 1e37a5c2 2019-05-12 jcs break;
3168 899d86c2 2018-05-10 stsp }
3169 e5a0f69f 2018-08-18 stsp
3170 80ddbec8 2018-04-29 stsp return err;
3171 80ddbec8 2018-04-29 stsp }
3172 80ddbec8 2018-04-29 stsp
3173 4ed7e80c 2018-05-20 stsp static const struct got_error *
3174 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3175 c2db6724 2019-01-04 stsp {
3176 c2db6724 2019-01-04 stsp const struct got_error *error;
3177 c2db6724 2019-01-04 stsp
3178 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3179 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3180 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3181 37c06ea4 2019-07-15 stsp #endif
3182 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3183 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3184 c2db6724 2019-01-04 stsp
3185 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3186 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3187 c2db6724 2019-01-04 stsp
3188 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3189 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3190 c2db6724 2019-01-04 stsp
3191 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3192 c2db6724 2019-01-04 stsp if (error != NULL)
3193 c2db6724 2019-01-04 stsp return error;
3194 c2db6724 2019-01-04 stsp
3195 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3196 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3197 c2db6724 2019-01-04 stsp
3198 c2db6724 2019-01-04 stsp return NULL;
3199 c2db6724 2019-01-04 stsp }
3200 c2db6724 2019-01-04 stsp
3201 a915003a 2019-02-05 stsp static void
3202 a915003a 2019-02-05 stsp init_curses(void)
3203 a915003a 2019-02-05 stsp {
3204 2497f032 2022-05-31 stsp /*
3205 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3206 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3207 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3208 2497f032 2022-05-31 stsp */
3209 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3210 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3211 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3212 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3213 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3214 2497f032 2022-05-31 stsp
3215 a915003a 2019-02-05 stsp initscr();
3216 a915003a 2019-02-05 stsp cbreak();
3217 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3218 a915003a 2019-02-05 stsp noecho();
3219 a915003a 2019-02-05 stsp nonl();
3220 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3221 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3222 a915003a 2019-02-05 stsp curs_set(0);
3223 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3224 6d17833f 2019-11-08 stsp start_color();
3225 6d17833f 2019-11-08 stsp use_default_colors();
3226 6d17833f 2019-11-08 stsp }
3227 a915003a 2019-02-05 stsp }
3228 a915003a 2019-02-05 stsp
3229 c2db6724 2019-01-04 stsp static const struct got_error *
3230 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3231 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3232 f135c941 2020-02-20 stsp {
3233 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3234 f135c941 2020-02-20 stsp
3235 f135c941 2020-02-20 stsp if (argc == 0) {
3236 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3237 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3238 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3239 f135c941 2020-02-20 stsp return NULL;
3240 f135c941 2020-02-20 stsp }
3241 f135c941 2020-02-20 stsp
3242 f135c941 2020-02-20 stsp if (worktree) {
3243 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3244 bfd61697 2020-11-14 stsp char *p;
3245 f135c941 2020-02-20 stsp
3246 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3247 f135c941 2020-02-20 stsp if (err)
3248 f135c941 2020-02-20 stsp return err;
3249 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3250 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3251 bfd61697 2020-11-14 stsp p) == -1) {
3252 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3253 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3254 f135c941 2020-02-20 stsp }
3255 f135c941 2020-02-20 stsp free(p);
3256 f135c941 2020-02-20 stsp } else
3257 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3258 f135c941 2020-02-20 stsp
3259 f135c941 2020-02-20 stsp return err;
3260 f135c941 2020-02-20 stsp }
3261 f135c941 2020-02-20 stsp
3262 f135c941 2020-02-20 stsp static const struct got_error *
3263 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3264 9f7d7167 2018-04-29 stsp {
3265 80ddbec8 2018-04-29 stsp const struct got_error *error;
3266 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3267 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3268 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3269 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3270 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3271 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3272 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3273 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3274 04cc582a 2018-08-01 stsp struct tog_view *view;
3275 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3276 80ddbec8 2018-04-29 stsp
3277 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3278 80ddbec8 2018-04-29 stsp switch (ch) {
3279 b672a97a 2020-01-27 stsp case 'b':
3280 b672a97a 2020-01-27 stsp log_branches = 1;
3281 b672a97a 2020-01-27 stsp break;
3282 80ddbec8 2018-04-29 stsp case 'c':
3283 80ddbec8 2018-04-29 stsp start_commit = optarg;
3284 80ddbec8 2018-04-29 stsp break;
3285 ecb28ae0 2018-07-16 stsp case 'r':
3286 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3287 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3288 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3289 9ba1d308 2019-10-21 stsp optarg);
3290 ecb28ae0 2018-07-16 stsp break;
3291 80ddbec8 2018-04-29 stsp default:
3292 17020d27 2019-03-07 stsp usage_log();
3293 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3294 80ddbec8 2018-04-29 stsp }
3295 80ddbec8 2018-04-29 stsp }
3296 80ddbec8 2018-04-29 stsp
3297 80ddbec8 2018-04-29 stsp argc -= optind;
3298 80ddbec8 2018-04-29 stsp argv += optind;
3299 80ddbec8 2018-04-29 stsp
3300 f135c941 2020-02-20 stsp if (argc > 1)
3301 f135c941 2020-02-20 stsp usage_log();
3302 963f97a1 2019-03-18 stsp
3303 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3304 0ae84acc 2022-06-15 tracey if (error != NULL)
3305 0ae84acc 2022-06-15 tracey goto done;
3306 0ae84acc 2022-06-15 tracey
3307 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3308 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3309 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3310 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3311 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3312 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3313 c156c7a4 2020-12-18 stsp goto done;
3314 a1fbf39a 2019-08-11 stsp if (worktree)
3315 6962eb72 2020-02-20 stsp repo_path =
3316 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3317 a1fbf39a 2019-08-11 stsp else
3318 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3319 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3320 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3321 c156c7a4 2020-12-18 stsp goto done;
3322 c156c7a4 2020-12-18 stsp }
3323 ecb28ae0 2018-07-16 stsp }
3324 ecb28ae0 2018-07-16 stsp
3325 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3326 80ddbec8 2018-04-29 stsp if (error != NULL)
3327 ecb28ae0 2018-07-16 stsp goto done;
3328 80ddbec8 2018-04-29 stsp
3329 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3330 f135c941 2020-02-20 stsp repo, worktree);
3331 f135c941 2020-02-20 stsp if (error)
3332 f135c941 2020-02-20 stsp goto done;
3333 f135c941 2020-02-20 stsp
3334 f135c941 2020-02-20 stsp init_curses();
3335 f135c941 2020-02-20 stsp
3336 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3337 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3338 c02c541e 2019-03-29 stsp if (error)
3339 c02c541e 2019-03-29 stsp goto done;
3340 c02c541e 2019-03-29 stsp
3341 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3342 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3343 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3344 87670572 2020-12-26 naddy if (error)
3345 87670572 2020-12-26 naddy goto done;
3346 87670572 2020-12-26 naddy }
3347 51a10b52 2020-12-26 stsp
3348 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3349 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3350 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3351 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3352 d8f38dc4 2020-12-05 stsp if (error)
3353 d8f38dc4 2020-12-05 stsp goto done;
3354 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3355 d8f38dc4 2020-12-05 stsp } else {
3356 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3357 d8f38dc4 2020-12-05 stsp if (error == NULL)
3358 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3359 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3360 d8f38dc4 2020-12-05 stsp goto done;
3361 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3362 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3363 d8f38dc4 2020-12-05 stsp if (error)
3364 d8f38dc4 2020-12-05 stsp goto done;
3365 d8f38dc4 2020-12-05 stsp }
3366 8b473291 2019-02-21 stsp
3367 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3368 04cc582a 2018-08-01 stsp if (view == NULL) {
3369 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3370 04cc582a 2018-08-01 stsp goto done;
3371 04cc582a 2018-08-01 stsp }
3372 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3373 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3374 ba4f502b 2018-08-04 stsp if (error)
3375 ba4f502b 2018-08-04 stsp goto done;
3376 2fc00ff4 2019-08-31 stsp if (worktree) {
3377 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3378 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3379 2fc00ff4 2019-08-31 stsp worktree = NULL;
3380 2fc00ff4 2019-08-31 stsp }
3381 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3382 ecb28ae0 2018-07-16 stsp done:
3383 f135c941 2020-02-20 stsp free(in_repo_path);
3384 ecb28ae0 2018-07-16 stsp free(repo_path);
3385 ecb28ae0 2018-07-16 stsp free(cwd);
3386 899d86c2 2018-05-10 stsp free(start_id);
3387 d8f38dc4 2020-12-05 stsp free(label);
3388 d8f38dc4 2020-12-05 stsp if (ref)
3389 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3390 1d0f4054 2021-06-17 stsp if (repo) {
3391 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3392 1d0f4054 2021-06-17 stsp if (error == NULL)
3393 1d0f4054 2021-06-17 stsp error = close_err;
3394 1d0f4054 2021-06-17 stsp }
3395 ec142235 2019-03-07 stsp if (worktree)
3396 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3397 0ae84acc 2022-06-15 tracey if (pack_fds) {
3398 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3399 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3400 0ae84acc 2022-06-15 tracey if (error == NULL)
3401 0ae84acc 2022-06-15 tracey error = pack_err;
3402 0ae84acc 2022-06-15 tracey }
3403 51a10b52 2020-12-26 stsp tog_free_refs();
3404 80ddbec8 2018-04-29 stsp return error;
3405 9f7d7167 2018-04-29 stsp }
3406 9f7d7167 2018-04-29 stsp
3407 4ed7e80c 2018-05-20 stsp __dead static void
3408 9f7d7167 2018-04-29 stsp usage_diff(void)
3409 9f7d7167 2018-04-29 stsp {
3410 80ddbec8 2018-04-29 stsp endwin();
3411 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3412 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3413 9f7d7167 2018-04-29 stsp exit(1);
3414 b304db33 2018-05-20 stsp }
3415 b304db33 2018-05-20 stsp
3416 6d17833f 2019-11-08 stsp static int
3417 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3418 41605754 2020-11-12 stsp regmatch_t *regmatch)
3419 6d17833f 2019-11-08 stsp {
3420 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3421 6d17833f 2019-11-08 stsp }
3422 6d17833f 2019-11-08 stsp
3423 336075a4 2022-06-25 op static struct tog_color *
3424 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3425 6d17833f 2019-11-08 stsp {
3426 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3427 6d17833f 2019-11-08 stsp
3428 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3429 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3430 6d17833f 2019-11-08 stsp return tc;
3431 6d17833f 2019-11-08 stsp }
3432 6d17833f 2019-11-08 stsp
3433 6d17833f 2019-11-08 stsp return NULL;
3434 6d17833f 2019-11-08 stsp }
3435 6d17833f 2019-11-08 stsp
3436 4ed7e80c 2018-05-20 stsp static const struct got_error *
3437 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3438 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3439 41605754 2020-11-12 stsp {
3440 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3441 44a87665 2022-06-16 stsp char *exstr = NULL;
3442 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3443 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3444 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3445 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3446 41605754 2020-11-12 stsp
3447 41605754 2020-11-12 stsp *wtotal = 0;
3448 1853e0f4 2022-06-16 stsp
3449 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3450 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3451 41605754 2020-11-12 stsp
3452 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3453 44a87665 2022-06-16 stsp if (err)
3454 44a87665 2022-06-16 stsp return err;
3455 44a87665 2022-06-16 stsp
3456 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3457 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3458 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3459 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3460 44a87665 2022-06-16 stsp goto done;
3461 44a87665 2022-06-16 stsp }
3462 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3463 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3464 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3465 1853e0f4 2022-06-16 stsp goto done;
3466 1853e0f4 2022-06-16 stsp }
3467 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3468 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3469 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3470 1853e0f4 2022-06-16 stsp goto done;
3471 1853e0f4 2022-06-16 stsp }
3472 145b6838 2022-06-16 stsp
3473 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3474 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3475 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3476 1853e0f4 2022-06-16 stsp if (err)
3477 1853e0f4 2022-06-16 stsp goto done;
3478 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3479 145b6838 2022-06-16 stsp if (n) {
3480 1853e0f4 2022-06-16 stsp free(wline);
3481 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3482 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3483 1853e0f4 2022-06-16 stsp if (err)
3484 1853e0f4 2022-06-16 stsp goto done;
3485 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3486 1853e0f4 2022-06-16 stsp wlimit -= width;
3487 1853e0f4 2022-06-16 stsp *wtotal += width;
3488 41605754 2020-11-12 stsp }
3489 41605754 2020-11-12 stsp
3490 41605754 2020-11-12 stsp if (wlimit > 0) {
3491 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3492 1853e0f4 2022-06-16 stsp size_t wlen;
3493 1853e0f4 2022-06-16 stsp
3494 1853e0f4 2022-06-16 stsp free(wline);
3495 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3496 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3497 1853e0f4 2022-06-16 stsp if (err)
3498 1853e0f4 2022-06-16 stsp goto done;
3499 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3500 1853e0f4 2022-06-16 stsp while (i < wlen) {
3501 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3502 1853e0f4 2022-06-16 stsp if (width == -1) {
3503 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3504 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3505 1853e0f4 2022-06-16 stsp goto done;
3506 1853e0f4 2022-06-16 stsp }
3507 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3508 1853e0f4 2022-06-16 stsp break;
3509 61417565 2022-06-20 mark w += width;
3510 1853e0f4 2022-06-16 stsp i++;
3511 41605754 2020-11-12 stsp }
3512 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3513 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3514 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3515 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3516 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3517 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3518 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3519 41605754 2020-11-12 stsp }
3520 41605754 2020-11-12 stsp }
3521 41605754 2020-11-12 stsp
3522 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3523 1853e0f4 2022-06-16 stsp free(wline);
3524 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3525 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3526 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3527 1853e0f4 2022-06-16 stsp 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[scrollx]);
3531 1853e0f4 2022-06-16 stsp } else {
3532 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3533 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3534 1853e0f4 2022-06-16 stsp if (err)
3535 1853e0f4 2022-06-16 stsp goto done;
3536 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3537 1853e0f4 2022-06-16 stsp }
3538 1853e0f4 2022-06-16 stsp *wtotal += width2;
3539 41605754 2020-11-12 stsp }
3540 1853e0f4 2022-06-16 stsp done:
3541 145b6838 2022-06-16 stsp free(wline);
3542 44a87665 2022-06-16 stsp free(exstr);
3543 1853e0f4 2022-06-16 stsp free(seg0);
3544 1853e0f4 2022-06-16 stsp free(seg1);
3545 1853e0f4 2022-06-16 stsp free(seg2);
3546 1853e0f4 2022-06-16 stsp return err;
3547 41605754 2020-11-12 stsp }
3548 41605754 2020-11-12 stsp
3549 41605754 2020-11-12 stsp static const struct got_error *
3550 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3551 26ed57b2 2018-05-19 stsp {
3552 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3553 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3554 61e69b96 2018-05-20 stsp const struct got_error *err;
3555 fe621944 2020-11-10 stsp int nprinted = 0;
3556 b304db33 2018-05-20 stsp char *line;
3557 826082fe 2020-12-10 stsp size_t linesize = 0;
3558 826082fe 2020-12-10 stsp ssize_t linelen;
3559 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3560 61e69b96 2018-05-20 stsp wchar_t *wline;
3561 e0b650dd 2018-05-20 stsp int width;
3562 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3563 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3564 fe621944 2020-11-10 stsp off_t line_offset;
3565 26ed57b2 2018-05-19 stsp
3566 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3567 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3568 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3569 fe621944 2020-11-10 stsp
3570 f7d12f7e 2018-08-01 stsp werase(view->window);
3571 a3404814 2018-09-02 stsp
3572 a3404814 2018-09-02 stsp if (header) {
3573 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3574 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3575 135a2da0 2020-11-11 stsp header) == -1)
3576 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3577 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3578 ccda2f4d 2022-06-16 stsp 0, 0);
3579 135a2da0 2020-11-11 stsp free(line);
3580 135a2da0 2020-11-11 stsp if (err)
3581 a3404814 2018-09-02 stsp return err;
3582 a3404814 2018-09-02 stsp
3583 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3584 a3404814 2018-09-02 stsp wstandout(view->window);
3585 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3586 e54cc94a 2020-11-11 stsp free(wline);
3587 e54cc94a 2020-11-11 stsp wline = NULL;
3588 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3589 a3404814 2018-09-02 stsp wstandend(view->window);
3590 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3591 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3592 26ed57b2 2018-05-19 stsp
3593 a3404814 2018-09-02 stsp if (max_lines <= 1)
3594 a3404814 2018-09-02 stsp return NULL;
3595 a3404814 2018-09-02 stsp max_lines--;
3596 a3404814 2018-09-02 stsp }
3597 a3404814 2018-09-02 stsp
3598 89f1a395 2020-12-01 naddy s->eof = 0;
3599 145b6838 2022-06-16 stsp view->maxx = 0;
3600 826082fe 2020-12-10 stsp line = NULL;
3601 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3602 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3603 826082fe 2020-12-10 stsp if (linelen == -1) {
3604 826082fe 2020-12-10 stsp if (feof(s->f)) {
3605 826082fe 2020-12-10 stsp s->eof = 1;
3606 826082fe 2020-12-10 stsp break;
3607 826082fe 2020-12-10 stsp }
3608 826082fe 2020-12-10 stsp free(line);
3609 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3610 61e69b96 2018-05-20 stsp }
3611 145b6838 2022-06-16 stsp
3612 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3613 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3614 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3615 1853e0f4 2022-06-16 stsp if (err) {
3616 1853e0f4 2022-06-16 stsp free(line);
3617 1853e0f4 2022-06-16 stsp return err;
3618 1853e0f4 2022-06-16 stsp }
3619 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3620 1853e0f4 2022-06-16 stsp free(wline);
3621 1853e0f4 2022-06-16 stsp wline = NULL;
3622 1853e0f4 2022-06-16 stsp
3623 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3624 f26dddb7 2019-11-08 stsp if (tc)
3625 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3626 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3627 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3628 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3629 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3630 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3631 41605754 2020-11-12 stsp if (err) {
3632 41605754 2020-11-12 stsp free(line);
3633 41605754 2020-11-12 stsp return err;
3634 41605754 2020-11-12 stsp }
3635 41605754 2020-11-12 stsp } else {
3636 969c159c 2022-06-16 stsp int skip;
3637 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3638 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3639 969c159c 2022-06-16 stsp if (err) {
3640 969c159c 2022-06-16 stsp free(line);
3641 969c159c 2022-06-16 stsp return err;
3642 969c159c 2022-06-16 stsp }
3643 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3644 969c159c 2022-06-16 stsp free(wline);
3645 41605754 2020-11-12 stsp wline = NULL;
3646 41605754 2020-11-12 stsp }
3647 f26dddb7 2019-11-08 stsp if (tc)
3648 6d17833f 2019-11-08 stsp wattr_off(view->window,
3649 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3650 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3651 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3652 fe621944 2020-11-10 stsp nprinted++;
3653 826082fe 2020-12-10 stsp }
3654 826082fe 2020-12-10 stsp free(line);
3655 fe621944 2020-11-10 stsp if (nprinted >= 1)
3656 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3657 89f1a395 2020-12-01 naddy (nprinted - 1);
3658 fe621944 2020-11-10 stsp else
3659 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3660 26ed57b2 2018-05-19 stsp
3661 9b058f45 2022-06-30 mark view_border(view);
3662 c3e9aa98 2019-05-13 jcs
3663 89f1a395 2020-12-01 naddy if (s->eof) {
3664 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3665 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3666 c3e9aa98 2019-05-13 jcs nprinted++;
3667 c3e9aa98 2019-05-13 jcs }
3668 c3e9aa98 2019-05-13 jcs
3669 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3670 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3671 c3e9aa98 2019-05-13 jcs if (err) {
3672 c3e9aa98 2019-05-13 jcs return err;
3673 c3e9aa98 2019-05-13 jcs }
3674 26ed57b2 2018-05-19 stsp
3675 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3676 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3677 e54cc94a 2020-11-11 stsp free(wline);
3678 e54cc94a 2020-11-11 stsp wline = NULL;
3679 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3680 c3e9aa98 2019-05-13 jcs }
3681 c3e9aa98 2019-05-13 jcs
3682 26ed57b2 2018-05-19 stsp return NULL;
3683 abd2672a 2018-12-23 stsp }
3684 abd2672a 2018-12-23 stsp
3685 abd2672a 2018-12-23 stsp static char *
3686 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3687 abd2672a 2018-12-23 stsp {
3688 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3689 09867e48 2019-08-13 stsp char *p, *s;
3690 09867e48 2019-08-13 stsp
3691 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3692 09867e48 2019-08-13 stsp if (tm == NULL)
3693 09867e48 2019-08-13 stsp return NULL;
3694 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3695 09867e48 2019-08-13 stsp if (s == NULL)
3696 09867e48 2019-08-13 stsp return NULL;
3697 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3698 abd2672a 2018-12-23 stsp if (p)
3699 abd2672a 2018-12-23 stsp *p = '\0';
3700 abd2672a 2018-12-23 stsp return s;
3701 9f7d7167 2018-04-29 stsp }
3702 9f7d7167 2018-04-29 stsp
3703 4ed7e80c 2018-05-20 stsp static const struct got_error *
3704 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3705 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3706 0208f208 2020-05-05 stsp {
3707 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3708 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3709 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3710 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3711 0208f208 2020-05-05 stsp
3712 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3713 0208f208 2020-05-05 stsp if (qid != NULL) {
3714 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3715 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3716 d7b5a0e8 2022-04-20 stsp &qid->id);
3717 0208f208 2020-05-05 stsp if (err)
3718 0208f208 2020-05-05 stsp return err;
3719 0208f208 2020-05-05 stsp
3720 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3721 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3722 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3723 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3724 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3725 aa8b5dd0 2021-08-01 stsp }
3726 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3727 0208f208 2020-05-05 stsp
3728 0208f208 2020-05-05 stsp }
3729 0208f208 2020-05-05 stsp
3730 0208f208 2020-05-05 stsp if (tree_id1) {
3731 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3732 0208f208 2020-05-05 stsp if (err)
3733 0208f208 2020-05-05 stsp goto done;
3734 0208f208 2020-05-05 stsp }
3735 0208f208 2020-05-05 stsp
3736 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3737 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3738 0208f208 2020-05-05 stsp if (err)
3739 0208f208 2020-05-05 stsp goto done;
3740 0208f208 2020-05-05 stsp
3741 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3742 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3743 0208f208 2020-05-05 stsp done:
3744 0208f208 2020-05-05 stsp if (tree1)
3745 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3746 0208f208 2020-05-05 stsp if (tree2)
3747 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3748 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3749 0208f208 2020-05-05 stsp return err;
3750 0208f208 2020-05-05 stsp }
3751 0208f208 2020-05-05 stsp
3752 0208f208 2020-05-05 stsp static const struct got_error *
3753 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3754 abd2672a 2018-12-23 stsp {
3755 fe621944 2020-11-10 stsp off_t *p;
3756 fe621944 2020-11-10 stsp
3757 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3758 fe621944 2020-11-10 stsp if (p == NULL)
3759 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3760 fe621944 2020-11-10 stsp *line_offsets = p;
3761 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3762 fe621944 2020-11-10 stsp (*nlines)++;
3763 fe621944 2020-11-10 stsp return NULL;
3764 fe621944 2020-11-10 stsp }
3765 fe621944 2020-11-10 stsp
3766 fe621944 2020-11-10 stsp static const struct got_error *
3767 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3768 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3769 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3770 fe621944 2020-11-10 stsp {
3771 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3772 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3773 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3774 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3775 45d799e2 2018-12-23 stsp time_t committer_time;
3776 45d799e2 2018-12-23 stsp const char *author, *committer;
3777 8b473291 2019-02-21 stsp char *refs_str = NULL;
3778 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3779 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3780 fe621944 2020-11-10 stsp off_t outoff = 0;
3781 fe621944 2020-11-10 stsp int n;
3782 abd2672a 2018-12-23 stsp
3783 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3784 0208f208 2020-05-05 stsp
3785 8b473291 2019-02-21 stsp if (refs) {
3786 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3787 8b473291 2019-02-21 stsp if (err)
3788 8b473291 2019-02-21 stsp return err;
3789 8b473291 2019-02-21 stsp }
3790 8b473291 2019-02-21 stsp
3791 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3792 abd2672a 2018-12-23 stsp if (err)
3793 abd2672a 2018-12-23 stsp return err;
3794 abd2672a 2018-12-23 stsp
3795 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3796 15a94983 2018-12-23 stsp if (err) {
3797 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3798 15a94983 2018-12-23 stsp goto done;
3799 15a94983 2018-12-23 stsp }
3800 abd2672a 2018-12-23 stsp
3801 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3802 fe621944 2020-11-10 stsp if (err)
3803 fe621944 2020-11-10 stsp goto done;
3804 fe621944 2020-11-10 stsp
3805 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3806 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3807 fe621944 2020-11-10 stsp if (n < 0) {
3808 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3809 abd2672a 2018-12-23 stsp goto done;
3810 abd2672a 2018-12-23 stsp }
3811 fe621944 2020-11-10 stsp outoff += n;
3812 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3813 fe621944 2020-11-10 stsp if (err)
3814 fe621944 2020-11-10 stsp goto done;
3815 fe621944 2020-11-10 stsp
3816 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3817 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3818 fe621944 2020-11-10 stsp if (n < 0) {
3819 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3820 abd2672a 2018-12-23 stsp goto done;
3821 abd2672a 2018-12-23 stsp }
3822 fe621944 2020-11-10 stsp outoff += n;
3823 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3824 fe621944 2020-11-10 stsp if (err)
3825 fe621944 2020-11-10 stsp goto done;
3826 fe621944 2020-11-10 stsp
3827 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3828 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3829 fe621944 2020-11-10 stsp if (datestr) {
3830 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3831 fe621944 2020-11-10 stsp if (n < 0) {
3832 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3833 fe621944 2020-11-10 stsp goto done;
3834 fe621944 2020-11-10 stsp }
3835 fe621944 2020-11-10 stsp outoff += n;
3836 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3837 fe621944 2020-11-10 stsp if (err)
3838 fe621944 2020-11-10 stsp goto done;
3839 abd2672a 2018-12-23 stsp }
3840 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3841 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3842 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3843 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3844 fe621944 2020-11-10 stsp if (n < 0) {
3845 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3846 fe621944 2020-11-10 stsp goto done;
3847 fe621944 2020-11-10 stsp }
3848 fe621944 2020-11-10 stsp outoff += n;
3849 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3850 fe621944 2020-11-10 stsp if (err)
3851 fe621944 2020-11-10 stsp goto done;
3852 abd2672a 2018-12-23 stsp }
3853 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3854 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3855 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3856 9f98ca05 2021-09-24 stsp int pn = 1;
3857 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3858 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3859 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3860 9f98ca05 2021-09-24 stsp if (err)
3861 9f98ca05 2021-09-24 stsp goto done;
3862 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3863 9f98ca05 2021-09-24 stsp if (n < 0) {
3864 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3865 9f98ca05 2021-09-24 stsp goto done;
3866 9f98ca05 2021-09-24 stsp }
3867 9f98ca05 2021-09-24 stsp outoff += n;
3868 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3869 9f98ca05 2021-09-24 stsp if (err)
3870 9f98ca05 2021-09-24 stsp goto done;
3871 9f98ca05 2021-09-24 stsp free(id_str);
3872 9f98ca05 2021-09-24 stsp id_str = NULL;
3873 9f98ca05 2021-09-24 stsp }
3874 9f98ca05 2021-09-24 stsp }
3875 9f98ca05 2021-09-24 stsp
3876 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3877 5943eee2 2019-08-13 stsp if (err)
3878 5943eee2 2019-08-13 stsp goto done;
3879 fe621944 2020-11-10 stsp s = logmsg;
3880 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3881 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3882 fe621944 2020-11-10 stsp if (n < 0) {
3883 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3884 fe621944 2020-11-10 stsp goto done;
3885 fe621944 2020-11-10 stsp }
3886 fe621944 2020-11-10 stsp outoff += n;
3887 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3888 fe621944 2020-11-10 stsp if (err)
3889 fe621944 2020-11-10 stsp goto done;
3890 abd2672a 2018-12-23 stsp }
3891 fe621944 2020-11-10 stsp
3892 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3893 0208f208 2020-05-05 stsp if (err)
3894 0208f208 2020-05-05 stsp goto done;
3895 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3896 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3897 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3898 fe621944 2020-11-10 stsp if (n < 0) {
3899 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3900 fe621944 2020-11-10 stsp goto done;
3901 fe621944 2020-11-10 stsp }
3902 fe621944 2020-11-10 stsp outoff += n;
3903 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3904 fe621944 2020-11-10 stsp if (err)
3905 fe621944 2020-11-10 stsp goto done;
3906 0208f208 2020-05-05 stsp free((char *)pe->path);
3907 0208f208 2020-05-05 stsp free(pe->data);
3908 0208f208 2020-05-05 stsp }
3909 fe621944 2020-11-10 stsp
3910 0208f208 2020-05-05 stsp fputc('\n', outfile);
3911 fe621944 2020-11-10 stsp outoff++;
3912 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3913 abd2672a 2018-12-23 stsp done:
3914 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3915 abd2672a 2018-12-23 stsp free(id_str);
3916 5943eee2 2019-08-13 stsp free(logmsg);
3917 8b473291 2019-02-21 stsp free(refs_str);
3918 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3919 7510f233 2020-08-09 stsp if (err) {
3920 ae6a6978 2020-08-09 stsp free(*line_offsets);
3921 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3922 ae6a6978 2020-08-09 stsp *nlines = 0;
3923 7510f233 2020-08-09 stsp }
3924 fe621944 2020-11-10 stsp return err;
3925 abd2672a 2018-12-23 stsp }
3926 abd2672a 2018-12-23 stsp
3927 abd2672a 2018-12-23 stsp static const struct got_error *
3928 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3929 26ed57b2 2018-05-19 stsp {
3930 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3931 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3932 15a94983 2018-12-23 stsp int obj_type;
3933 fe621944 2020-11-10 stsp
3934 fe621944 2020-11-10 stsp free(s->line_offsets);
3935 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3936 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3937 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3938 fe621944 2020-11-10 stsp s->nlines = 0;
3939 26ed57b2 2018-05-19 stsp
3940 511a516b 2018-05-19 stsp f = got_opentemp();
3941 48ae06ee 2018-10-18 stsp if (f == NULL) {
3942 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3943 48ae06ee 2018-10-18 stsp goto done;
3944 48ae06ee 2018-10-18 stsp }
3945 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3946 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3947 fb43ecf1 2019-02-11 stsp goto done;
3948 fb43ecf1 2019-02-11 stsp }
3949 48ae06ee 2018-10-18 stsp s->f = f;
3950 26ed57b2 2018-05-19 stsp
3951 15a94983 2018-12-23 stsp if (s->id1)
3952 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3953 15a94983 2018-12-23 stsp else
3954 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3955 15a94983 2018-12-23 stsp if (err)
3956 15a94983 2018-12-23 stsp goto done;
3957 15a94983 2018-12-23 stsp
3958 15a94983 2018-12-23 stsp switch (obj_type) {
3959 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3960 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3961 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3962 f9d37699 2022-06-28 stsp s->label1, s->label2, s->diff_context,
3963 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3964 26ed57b2 2018-05-19 stsp break;
3965 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3966 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3967 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3968 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
3969 f9d37699 2022-06-28 stsp s->repo, s->f);
3970 26ed57b2 2018-05-19 stsp break;
3971 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3972 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3973 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3974 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3975 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3976 abd2672a 2018-12-23 stsp
3977 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3978 abd2672a 2018-12-23 stsp if (err)
3979 3ffacbe1 2020-02-02 tracey goto done;
3980 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3981 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3982 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3983 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3984 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3985 f44b1f58 2020-02-02 tracey if (err)
3986 f44b1f58 2020-02-02 tracey goto done;
3987 f44b1f58 2020-02-02 tracey } else {
3988 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3989 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3990 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3991 fe621944 2020-11-10 stsp err = write_commit_info(
3992 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3993 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3994 f44b1f58 2020-02-02 tracey if (err)
3995 f44b1f58 2020-02-02 tracey goto done;
3996 f5404e4e 2020-02-02 tracey break;
3997 15a087fe 2019-02-21 stsp }
3998 abd2672a 2018-12-23 stsp }
3999 abd2672a 2018-12-23 stsp }
4000 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4001 abd2672a 2018-12-23 stsp
4002 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4003 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4004 f9d37699 2022-06-28 stsp s->diff_context, s->ignore_whitespace, s->force_text_diff,
4005 f9d37699 2022-06-28 stsp s->repo, s->f);
4006 26ed57b2 2018-05-19 stsp break;
4007 abd2672a 2018-12-23 stsp }
4008 26ed57b2 2018-05-19 stsp default:
4009 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4010 48ae06ee 2018-10-18 stsp break;
4011 26ed57b2 2018-05-19 stsp }
4012 f44b1f58 2020-02-02 tracey if (err)
4013 f44b1f58 2020-02-02 tracey goto done;
4014 48ae06ee 2018-10-18 stsp done:
4015 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4016 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4017 48ae06ee 2018-10-18 stsp return err;
4018 48ae06ee 2018-10-18 stsp }
4019 26ed57b2 2018-05-19 stsp
4020 f5215bb9 2019-02-22 stsp static void
4021 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4022 f5215bb9 2019-02-22 stsp {
4023 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4024 f5215bb9 2019-02-22 stsp update_panels();
4025 f5215bb9 2019-02-22 stsp doupdate();
4026 f44b1f58 2020-02-02 tracey }
4027 f44b1f58 2020-02-02 tracey
4028 f44b1f58 2020-02-02 tracey static const struct got_error *
4029 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4030 f44b1f58 2020-02-02 tracey {
4031 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4032 f44b1f58 2020-02-02 tracey
4033 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4034 f44b1f58 2020-02-02 tracey return NULL;
4035 f44b1f58 2020-02-02 tracey }
4036 f44b1f58 2020-02-02 tracey
4037 f44b1f58 2020-02-02 tracey static const struct got_error *
4038 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4039 f44b1f58 2020-02-02 tracey {
4040 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4041 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4042 f44b1f58 2020-02-02 tracey int lineno;
4043 cb713507 2022-06-16 stsp char *line = NULL;
4044 826082fe 2020-12-10 stsp size_t linesize = 0;
4045 826082fe 2020-12-10 stsp ssize_t linelen;
4046 f44b1f58 2020-02-02 tracey
4047 f44b1f58 2020-02-02 tracey if (!view->searching) {
4048 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4049 f44b1f58 2020-02-02 tracey return NULL;
4050 f44b1f58 2020-02-02 tracey }
4051 f44b1f58 2020-02-02 tracey
4052 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4053 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4054 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4055 f44b1f58 2020-02-02 tracey else
4056 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4057 487cd7d2 2021-12-17 stsp } else
4058 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4059 f44b1f58 2020-02-02 tracey
4060 f44b1f58 2020-02-02 tracey while (1) {
4061 f44b1f58 2020-02-02 tracey off_t offset;
4062 f44b1f58 2020-02-02 tracey
4063 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4064 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4065 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4066 f44b1f58 2020-02-02 tracey break;
4067 f44b1f58 2020-02-02 tracey }
4068 f44b1f58 2020-02-02 tracey
4069 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4070 f44b1f58 2020-02-02 tracey lineno = 1;
4071 f44b1f58 2020-02-02 tracey else
4072 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4073 f44b1f58 2020-02-02 tracey }
4074 f44b1f58 2020-02-02 tracey
4075 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4076 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4077 f44b1f58 2020-02-02 tracey free(line);
4078 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4079 f44b1f58 2020-02-02 tracey }
4080 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4081 cb713507 2022-06-16 stsp if (linelen != -1) {
4082 cb713507 2022-06-16 stsp char *exstr;
4083 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4084 cb713507 2022-06-16 stsp if (err)
4085 cb713507 2022-06-16 stsp break;
4086 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4087 cb713507 2022-06-16 stsp &view->regmatch)) {
4088 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4089 cb713507 2022-06-16 stsp s->matched_line = lineno;
4090 cb713507 2022-06-16 stsp free(exstr);
4091 cb713507 2022-06-16 stsp break;
4092 cb713507 2022-06-16 stsp }
4093 cb713507 2022-06-16 stsp free(exstr);
4094 f44b1f58 2020-02-02 tracey }
4095 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4096 f44b1f58 2020-02-02 tracey lineno++;
4097 f44b1f58 2020-02-02 tracey else
4098 f44b1f58 2020-02-02 tracey lineno--;
4099 f44b1f58 2020-02-02 tracey }
4100 826082fe 2020-12-10 stsp free(line);
4101 f44b1f58 2020-02-02 tracey
4102 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4103 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4104 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4105 f44b1f58 2020-02-02 tracey }
4106 f44b1f58 2020-02-02 tracey
4107 145b6838 2022-06-16 stsp return err;
4108 f5215bb9 2019-02-22 stsp }
4109 f5215bb9 2019-02-22 stsp
4110 48ae06ee 2018-10-18 stsp static const struct got_error *
4111 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4112 b72706c3 2022-06-01 stsp {
4113 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4114 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4115 b72706c3 2022-06-01 stsp
4116 b72706c3 2022-06-01 stsp free(s->id1);
4117 b72706c3 2022-06-01 stsp s->id1 = NULL;
4118 b72706c3 2022-06-01 stsp free(s->id2);
4119 b72706c3 2022-06-01 stsp s->id2 = NULL;
4120 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4121 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4122 b72706c3 2022-06-01 stsp s->f = NULL;
4123 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4124 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4125 b72706c3 2022-06-01 stsp s->f1 = NULL;
4126 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4127 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4128 b72706c3 2022-06-01 stsp s->f2 = NULL;
4129 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4130 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4131 f9d37699 2022-06-28 stsp s->fd1 = -1;
4132 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4133 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4134 f9d37699 2022-06-28 stsp s->fd2 = -1;
4135 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4136 b72706c3 2022-06-01 stsp free(s->line_offsets);
4137 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4138 b72706c3 2022-06-01 stsp s->nlines = 0;
4139 b72706c3 2022-06-01 stsp return err;
4140 b72706c3 2022-06-01 stsp }
4141 b72706c3 2022-06-01 stsp
4142 b72706c3 2022-06-01 stsp static const struct got_error *
4143 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4144 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4145 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4146 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4147 48ae06ee 2018-10-18 stsp {
4148 48ae06ee 2018-10-18 stsp const struct got_error *err;
4149 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4150 5dc9f4bc 2018-08-04 stsp
4151 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4152 f9d37699 2022-06-28 stsp s->fd1 = -1;
4153 f9d37699 2022-06-28 stsp s->fd2 = -1;
4154 b72706c3 2022-06-01 stsp
4155 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4156 15a94983 2018-12-23 stsp int type1, type2;
4157 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4158 15a94983 2018-12-23 stsp if (err)
4159 15a94983 2018-12-23 stsp return err;
4160 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4161 15a94983 2018-12-23 stsp if (err)
4162 15a94983 2018-12-23 stsp return err;
4163 15a94983 2018-12-23 stsp
4164 15a94983 2018-12-23 stsp if (type1 != type2)
4165 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4166 15a94983 2018-12-23 stsp }
4167 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4168 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4169 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4170 f44b1f58 2020-02-02 tracey s->repo = repo;
4171 f44b1f58 2020-02-02 tracey s->id1 = id1;
4172 f44b1f58 2020-02-02 tracey s->id2 = id2;
4173 3dbaef42 2020-11-24 stsp s->label1 = label1;
4174 3dbaef42 2020-11-24 stsp s->label2 = label2;
4175 48ae06ee 2018-10-18 stsp
4176 15a94983 2018-12-23 stsp if (id1) {
4177 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4178 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4179 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4180 48ae06ee 2018-10-18 stsp } else
4181 5465d566 2020-02-01 tracey s->id1 = NULL;
4182 48ae06ee 2018-10-18 stsp
4183 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4184 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4185 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4186 b72706c3 2022-06-01 stsp goto done;
4187 48ae06ee 2018-10-18 stsp }
4188 b72706c3 2022-06-01 stsp
4189 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4190 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4191 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4192 a00719e9 2022-06-17 stsp goto done;
4193 a00719e9 2022-06-17 stsp }
4194 a00719e9 2022-06-17 stsp
4195 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4196 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4197 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4198 b72706c3 2022-06-01 stsp goto done;
4199 b72706c3 2022-06-01 stsp }
4200 b72706c3 2022-06-01 stsp
4201 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4202 f9d37699 2022-06-28 stsp if (s->fd1 == -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 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4208 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4209 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4210 f9d37699 2022-06-28 stsp goto done;
4211 f9d37699 2022-06-28 stsp }
4212 f9d37699 2022-06-28 stsp
4213 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4214 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4215 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4216 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4217 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4218 5465d566 2020-02-01 tracey s->log_view = log_view;
4219 5465d566 2020-02-01 tracey s->repo = repo;
4220 6d17833f 2019-11-08 stsp
4221 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4222 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4223 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4224 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4225 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4226 6d17833f 2019-11-08 stsp if (err)
4227 b72706c3 2022-06-01 stsp goto done;
4228 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4229 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4230 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4231 b72706c3 2022-06-01 stsp if (err)
4232 b72706c3 2022-06-01 stsp goto done;
4233 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4234 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4235 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4236 b72706c3 2022-06-01 stsp if (err)
4237 b72706c3 2022-06-01 stsp goto done;
4238 6d17833f 2019-11-08 stsp
4239 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4240 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4241 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4242 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4243 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
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 "^(from|via): ", TOG_COLOR_AUTHOR,
4249 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4250 b72706c3 2022-06-01 stsp if (err)
4251 b72706c3 2022-06-01 stsp goto done;
4252 11b20872 2019-11-08 stsp
4253 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4254 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4256 b72706c3 2022-06-01 stsp if (err)
4257 b72706c3 2022-06-01 stsp goto done;
4258 6d17833f 2019-11-08 stsp }
4259 5dc9f4bc 2018-08-04 stsp
4260 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4261 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4262 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4263 f5215bb9 2019-02-22 stsp
4264 5465d566 2020-02-01 tracey err = create_diff(s);
4265 48ae06ee 2018-10-18 stsp
4266 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4267 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4268 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4269 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4270 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4271 b72706c3 2022-06-01 stsp done:
4272 b72706c3 2022-06-01 stsp if (err)
4273 b72706c3 2022-06-01 stsp close_diff_view(view);
4274 e5a0f69f 2018-08-18 stsp return err;
4275 5dc9f4bc 2018-08-04 stsp }
4276 5dc9f4bc 2018-08-04 stsp
4277 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4278 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4279 5dc9f4bc 2018-08-04 stsp {
4280 a3404814 2018-09-02 stsp const struct got_error *err;
4281 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4282 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4283 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4284 a3404814 2018-09-02 stsp
4285 a3404814 2018-09-02 stsp if (s->id1) {
4286 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4287 a3404814 2018-09-02 stsp if (err)
4288 a3404814 2018-09-02 stsp return err;
4289 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4290 3dbaef42 2020-11-24 stsp } else
4291 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4292 3dbaef42 2020-11-24 stsp
4293 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4294 a3404814 2018-09-02 stsp if (err)
4295 a3404814 2018-09-02 stsp return err;
4296 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4297 26ed57b2 2018-05-19 stsp
4298 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4300 a3404814 2018-09-02 stsp free(id_str1);
4301 a3404814 2018-09-02 stsp free(id_str2);
4302 a3404814 2018-09-02 stsp return err;
4303 a3404814 2018-09-02 stsp }
4304 a3404814 2018-09-02 stsp free(id_str1);
4305 a3404814 2018-09-02 stsp free(id_str2);
4306 a3404814 2018-09-02 stsp
4307 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4308 267bb3b8 2021-08-01 stsp free(header);
4309 267bb3b8 2021-08-01 stsp return err;
4310 15a087fe 2019-02-21 stsp }
4311 15a087fe 2019-02-21 stsp
4312 15a087fe 2019-02-21 stsp static const struct got_error *
4313 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4314 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4315 15a087fe 2019-02-21 stsp {
4316 d7a04538 2019-02-21 stsp const struct got_error *err;
4317 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4318 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4319 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4320 15a087fe 2019-02-21 stsp
4321 15a087fe 2019-02-21 stsp free(s->id2);
4322 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4323 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4324 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4325 15a087fe 2019-02-21 stsp
4326 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4327 d7a04538 2019-02-21 stsp if (err)
4328 d7a04538 2019-02-21 stsp return err;
4329 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4330 15a087fe 2019-02-21 stsp free(s->id1);
4331 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4332 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4333 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4334 15a087fe 2019-02-21 stsp return NULL;
4335 0cf4efb1 2018-09-29 stsp }
4336 0cf4efb1 2018-09-29 stsp
4337 0cf4efb1 2018-09-29 stsp static const struct got_error *
4338 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4339 e5a0f69f 2018-08-18 stsp {
4340 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4341 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4342 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4343 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4344 826082fe 2020-12-10 stsp char *line = NULL;
4345 826082fe 2020-12-10 stsp size_t linesize = 0;
4346 826082fe 2020-12-10 stsp ssize_t linelen;
4347 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4348 e5a0f69f 2018-08-18 stsp
4349 e5a0f69f 2018-08-18 stsp switch (ch) {
4350 145b6838 2022-06-16 stsp case '0':
4351 145b6838 2022-06-16 stsp view->x = 0;
4352 145b6838 2022-06-16 stsp break;
4353 145b6838 2022-06-16 stsp case '$':
4354 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4355 640cd7ff 2022-06-22 mark view->count = 0;
4356 145b6838 2022-06-16 stsp break;
4357 145b6838 2022-06-16 stsp case KEY_RIGHT:
4358 145b6838 2022-06-16 stsp case 'l':
4359 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4360 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4361 640cd7ff 2022-06-22 mark else
4362 640cd7ff 2022-06-22 mark view->count = 0;
4363 145b6838 2022-06-16 stsp break;
4364 145b6838 2022-06-16 stsp case KEY_LEFT:
4365 145b6838 2022-06-16 stsp case 'h':
4366 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4367 640cd7ff 2022-06-22 mark if (view->x <= 0)
4368 640cd7ff 2022-06-22 mark view->count = 0;
4369 145b6838 2022-06-16 stsp break;
4370 64453f7e 2020-11-21 stsp case 'a':
4371 3dbaef42 2020-11-24 stsp case 'w':
4372 3dbaef42 2020-11-24 stsp if (ch == 'a')
4373 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4374 3dbaef42 2020-11-24 stsp if (ch == 'w')
4375 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4376 64453f7e 2020-11-21 stsp wclear(view->window);
4377 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
4378 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4379 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4380 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
4381 64453f7e 2020-11-21 stsp err = create_diff(s);
4382 640cd7ff 2022-06-22 mark view->count = 0;
4383 912a3f79 2021-08-30 j break;
4384 912a3f79 2021-08-30 j case 'g':
4385 912a3f79 2021-08-30 j case KEY_HOME:
4386 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4387 640cd7ff 2022-06-22 mark view->count = 0;
4388 64453f7e 2020-11-21 stsp break;
4389 912a3f79 2021-08-30 j case 'G':
4390 912a3f79 2021-08-30 j case KEY_END:
4391 640cd7ff 2022-06-22 mark view->count = 0;
4392 912a3f79 2021-08-30 j if (s->eof)
4393 912a3f79 2021-08-30 j break;
4394 912a3f79 2021-08-30 j
4395 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4396 912a3f79 2021-08-30 j s->eof = 1;
4397 912a3f79 2021-08-30 j break;
4398 1e37a5c2 2019-05-12 jcs case 'k':
4399 1e37a5c2 2019-05-12 jcs case KEY_UP:
4400 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4401 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4402 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4403 640cd7ff 2022-06-22 mark else
4404 640cd7ff 2022-06-22 mark view->count = 0;
4405 1e37a5c2 2019-05-12 jcs break;
4406 83cc4199 2022-06-13 stsp case CTRL('u'):
4407 33c3719a 2022-06-15 stsp case 'u':
4408 83cc4199 2022-06-13 stsp nscroll /= 2;
4409 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4410 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4411 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4412 61417565 2022-06-20 mark case 'b':
4413 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4414 640cd7ff 2022-06-22 mark view->count = 0;
4415 26ed57b2 2018-05-19 stsp break;
4416 640cd7ff 2022-06-22 mark }
4417 1e37a5c2 2019-05-12 jcs i = 0;
4418 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4419 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4420 1e37a5c2 2019-05-12 jcs break;
4421 1e37a5c2 2019-05-12 jcs case 'j':
4422 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4423 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4424 1e37a5c2 2019-05-12 jcs if (!s->eof)
4425 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4426 640cd7ff 2022-06-22 mark else
4427 640cd7ff 2022-06-22 mark view->count = 0;
4428 1e37a5c2 2019-05-12 jcs break;
4429 83cc4199 2022-06-13 stsp case CTRL('d'):
4430 33c3719a 2022-06-15 stsp case 'd':
4431 83cc4199 2022-06-13 stsp nscroll /= 2;
4432 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4433 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4434 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4435 61417565 2022-06-20 mark case 'f':
4436 1e37a5c2 2019-05-12 jcs case ' ':
4437 640cd7ff 2022-06-22 mark if (s->eof) {
4438 640cd7ff 2022-06-22 mark view->count = 0;
4439 1e37a5c2 2019-05-12 jcs break;
4440 640cd7ff 2022-06-22 mark }
4441 1e37a5c2 2019-05-12 jcs i = 0;
4442 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4443 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4444 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4445 826082fe 2020-12-10 stsp if (linelen == -1) {
4446 826082fe 2020-12-10 stsp if (feof(s->f)) {
4447 826082fe 2020-12-10 stsp s->eof = 1;
4448 826082fe 2020-12-10 stsp } else
4449 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4450 34bc9ec9 2019-02-22 stsp break;
4451 826082fe 2020-12-10 stsp }
4452 1e37a5c2 2019-05-12 jcs }
4453 826082fe 2020-12-10 stsp free(line);
4454 1e37a5c2 2019-05-12 jcs break;
4455 1e37a5c2 2019-05-12 jcs case '[':
4456 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4457 1e37a5c2 2019-05-12 jcs s->diff_context--;
4458 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4459 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4460 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4461 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4462 27829c9e 2020-11-21 stsp s->nlines) {
4463 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4464 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4465 27829c9e 2020-11-21 stsp }
4466 640cd7ff 2022-06-22 mark } else
4467 640cd7ff 2022-06-22 mark view->count = 0;
4468 1e37a5c2 2019-05-12 jcs break;
4469 1e37a5c2 2019-05-12 jcs case ']':
4470 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4471 1e37a5c2 2019-05-12 jcs s->diff_context++;
4472 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4473 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4474 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4475 640cd7ff 2022-06-22 mark } else
4476 640cd7ff 2022-06-22 mark view->count = 0;
4477 1e37a5c2 2019-05-12 jcs break;
4478 1e37a5c2 2019-05-12 jcs case '<':
4479 1e37a5c2 2019-05-12 jcs case ',':
4480 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4481 640cd7ff 2022-06-22 mark view->count = 0;
4482 48ae06ee 2018-10-18 stsp break;
4483 640cd7ff 2022-06-22 mark }
4484 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4485 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4486 6524637e 2019-02-21 stsp
4487 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4488 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4489 1e37a5c2 2019-05-12 jcs if (err)
4490 1e37a5c2 2019-05-12 jcs break;
4491 15a087fe 2019-02-21 stsp
4492 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4493 5a8b5076 2020-12-05 stsp break;
4494 5a8b5076 2020-12-05 stsp
4495 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4496 1e37a5c2 2019-05-12 jcs if (err)
4497 1e37a5c2 2019-05-12 jcs break;
4498 15a087fe 2019-02-21 stsp
4499 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4500 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4501 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4502 145b6838 2022-06-16 stsp view->x = 0;
4503 15a087fe 2019-02-21 stsp
4504 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4505 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4506 1e37a5c2 2019-05-12 jcs break;
4507 1e37a5c2 2019-05-12 jcs case '>':
4508 1e37a5c2 2019-05-12 jcs case '.':
4509 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4510 640cd7ff 2022-06-22 mark view->count = 0;
4511 15a087fe 2019-02-21 stsp break;
4512 640cd7ff 2022-06-22 mark }
4513 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4514 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4515 5e224a3e 2019-02-22 stsp
4516 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4517 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4518 1e37a5c2 2019-05-12 jcs if (err)
4519 5a8b5076 2020-12-05 stsp break;
4520 5a8b5076 2020-12-05 stsp
4521 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4522 1e37a5c2 2019-05-12 jcs break;
4523 15a087fe 2019-02-21 stsp
4524 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4525 1e37a5c2 2019-05-12 jcs if (err)
4526 1e37a5c2 2019-05-12 jcs break;
4527 15a087fe 2019-02-21 stsp
4528 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4529 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4530 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4531 145b6838 2022-06-16 stsp view->x = 0;
4532 1e37a5c2 2019-05-12 jcs
4533 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4534 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4535 1e37a5c2 2019-05-12 jcs break;
4536 1e37a5c2 2019-05-12 jcs default:
4537 640cd7ff 2022-06-22 mark view->count = 0;
4538 1e37a5c2 2019-05-12 jcs break;
4539 26ed57b2 2018-05-19 stsp }
4540 e5a0f69f 2018-08-18 stsp
4541 bcbd79e2 2018-08-19 stsp return err;
4542 26ed57b2 2018-05-19 stsp }
4543 26ed57b2 2018-05-19 stsp
4544 4ed7e80c 2018-05-20 stsp static const struct got_error *
4545 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4546 9f7d7167 2018-04-29 stsp {
4547 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4548 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4549 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4550 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4551 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4552 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4553 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4554 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4555 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4556 3dbaef42 2020-11-24 stsp const char *errstr;
4557 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4558 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4559 70ac5f84 2019-03-28 stsp
4560 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4561 26ed57b2 2018-05-19 stsp switch (ch) {
4562 64453f7e 2020-11-21 stsp case 'a':
4563 64453f7e 2020-11-21 stsp force_text_diff = 1;
4564 3dbaef42 2020-11-24 stsp break;
4565 3dbaef42 2020-11-24 stsp case 'C':
4566 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4567 3dbaef42 2020-11-24 stsp &errstr);
4568 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4569 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4570 5a20d08d 2022-02-09 op errstr, errstr);
4571 64453f7e 2020-11-21 stsp break;
4572 09b5bff8 2020-02-23 naddy case 'r':
4573 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4574 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4575 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4576 09b5bff8 2020-02-23 naddy optarg);
4577 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4578 09b5bff8 2020-02-23 naddy break;
4579 3dbaef42 2020-11-24 stsp case 'w':
4580 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4581 3dbaef42 2020-11-24 stsp break;
4582 26ed57b2 2018-05-19 stsp default:
4583 17020d27 2019-03-07 stsp usage_diff();
4584 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4585 26ed57b2 2018-05-19 stsp }
4586 26ed57b2 2018-05-19 stsp }
4587 26ed57b2 2018-05-19 stsp
4588 26ed57b2 2018-05-19 stsp argc -= optind;
4589 26ed57b2 2018-05-19 stsp argv += optind;
4590 26ed57b2 2018-05-19 stsp
4591 26ed57b2 2018-05-19 stsp if (argc == 0) {
4592 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4593 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4594 15a94983 2018-12-23 stsp id_str1 = argv[0];
4595 15a94983 2018-12-23 stsp id_str2 = argv[1];
4596 26ed57b2 2018-05-19 stsp } else
4597 26ed57b2 2018-05-19 stsp usage_diff();
4598 eb6600df 2019-01-04 stsp
4599 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4600 0ae84acc 2022-06-15 tracey if (error)
4601 0ae84acc 2022-06-15 tracey goto done;
4602 0ae84acc 2022-06-15 tracey
4603 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4604 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4605 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4606 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4607 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4608 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4609 c156c7a4 2020-12-18 stsp goto done;
4610 a273ac94 2020-02-23 naddy if (worktree)
4611 a273ac94 2020-02-23 naddy repo_path =
4612 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4613 a273ac94 2020-02-23 naddy else
4614 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4615 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4616 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4617 c156c7a4 2020-12-18 stsp goto done;
4618 c156c7a4 2020-12-18 stsp }
4619 a273ac94 2020-02-23 naddy }
4620 a273ac94 2020-02-23 naddy
4621 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4622 eb6600df 2019-01-04 stsp if (error)
4623 eb6600df 2019-01-04 stsp goto done;
4624 26ed57b2 2018-05-19 stsp
4625 a273ac94 2020-02-23 naddy init_curses();
4626 a273ac94 2020-02-23 naddy
4627 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4628 51a10b52 2020-12-26 stsp if (error)
4629 51a10b52 2020-12-26 stsp goto done;
4630 51a10b52 2020-12-26 stsp
4631 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4632 26ed57b2 2018-05-19 stsp if (error)
4633 26ed57b2 2018-05-19 stsp goto done;
4634 26ed57b2 2018-05-19 stsp
4635 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4636 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4637 26ed57b2 2018-05-19 stsp if (error)
4638 26ed57b2 2018-05-19 stsp goto done;
4639 26ed57b2 2018-05-19 stsp
4640 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4641 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4642 26ed57b2 2018-05-19 stsp if (error)
4643 26ed57b2 2018-05-19 stsp goto done;
4644 26ed57b2 2018-05-19 stsp
4645 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4646 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4647 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4648 ea5e7bb5 2018-08-01 stsp goto done;
4649 ea5e7bb5 2018-08-01 stsp }
4650 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4651 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4652 5dc9f4bc 2018-08-04 stsp if (error)
4653 5dc9f4bc 2018-08-04 stsp goto done;
4654 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4655 26ed57b2 2018-05-19 stsp done:
4656 3dbaef42 2020-11-24 stsp free(label1);
4657 3dbaef42 2020-11-24 stsp free(label2);
4658 c02c541e 2019-03-29 stsp free(repo_path);
4659 a273ac94 2020-02-23 naddy free(cwd);
4660 1d0f4054 2021-06-17 stsp if (repo) {
4661 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4662 1d0f4054 2021-06-17 stsp if (error == NULL)
4663 1d0f4054 2021-06-17 stsp error = close_err;
4664 1d0f4054 2021-06-17 stsp }
4665 a273ac94 2020-02-23 naddy if (worktree)
4666 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4667 0ae84acc 2022-06-15 tracey if (pack_fds) {
4668 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4669 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4670 0ae84acc 2022-06-15 tracey if (error == NULL)
4671 0ae84acc 2022-06-15 tracey error = pack_err;
4672 0ae84acc 2022-06-15 tracey }
4673 51a10b52 2020-12-26 stsp tog_free_refs();
4674 26ed57b2 2018-05-19 stsp return error;
4675 9f7d7167 2018-04-29 stsp }
4676 9f7d7167 2018-04-29 stsp
4677 4ed7e80c 2018-05-20 stsp __dead static void
4678 9f7d7167 2018-04-29 stsp usage_blame(void)
4679 9f7d7167 2018-04-29 stsp {
4680 80ddbec8 2018-04-29 stsp endwin();
4681 87411fa9 2022-06-16 stsp fprintf(stderr,
4682 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4683 9f7d7167 2018-04-29 stsp getprogname());
4684 9f7d7167 2018-04-29 stsp exit(1);
4685 9f7d7167 2018-04-29 stsp }
4686 84451b3e 2018-07-10 stsp
4687 84451b3e 2018-07-10 stsp struct tog_blame_line {
4688 84451b3e 2018-07-10 stsp int annotated;
4689 84451b3e 2018-07-10 stsp struct got_object_id *id;
4690 84451b3e 2018-07-10 stsp };
4691 9f7d7167 2018-04-29 stsp
4692 4ed7e80c 2018-05-20 stsp static const struct got_error *
4693 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4694 84451b3e 2018-07-10 stsp {
4695 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4696 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4697 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4698 84451b3e 2018-07-10 stsp const struct got_error *err;
4699 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4700 826082fe 2020-12-10 stsp char *line = NULL;
4701 826082fe 2020-12-10 stsp size_t linesize = 0;
4702 826082fe 2020-12-10 stsp ssize_t linelen;
4703 84451b3e 2018-07-10 stsp wchar_t *wline;
4704 27a741e5 2019-09-11 stsp int width;
4705 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4706 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4707 ab089a2a 2018-07-12 stsp char *id_str;
4708 11b20872 2019-11-08 stsp struct tog_color *tc;
4709 ab089a2a 2018-07-12 stsp
4710 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4711 ab089a2a 2018-07-12 stsp if (err)
4712 ab089a2a 2018-07-12 stsp return err;
4713 84451b3e 2018-07-10 stsp
4714 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4715 f7d12f7e 2018-08-01 stsp werase(view->window);
4716 84451b3e 2018-07-10 stsp
4717 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4718 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4719 ab089a2a 2018-07-12 stsp free(id_str);
4720 ab089a2a 2018-07-12 stsp return err;
4721 ab089a2a 2018-07-12 stsp }
4722 ab089a2a 2018-07-12 stsp
4723 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4724 ab089a2a 2018-07-12 stsp free(line);
4725 2550e4c3 2018-07-13 stsp line = NULL;
4726 1cae65b4 2019-09-22 stsp if (err)
4727 1cae65b4 2019-09-22 stsp return err;
4728 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4729 a3404814 2018-09-02 stsp wstandout(view->window);
4730 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4731 11b20872 2019-11-08 stsp if (tc)
4732 11b20872 2019-11-08 stsp wattr_on(view->window,
4733 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4734 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4735 11b20872 2019-11-08 stsp if (tc)
4736 11b20872 2019-11-08 stsp wattr_off(view->window,
4737 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4738 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4739 a3404814 2018-09-02 stsp wstandend(view->window);
4740 2550e4c3 2018-07-13 stsp free(wline);
4741 2550e4c3 2018-07-13 stsp wline = NULL;
4742 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4743 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4744 ab089a2a 2018-07-12 stsp
4745 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4746 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4747 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4748 ab089a2a 2018-07-12 stsp free(id_str);
4749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4750 ab089a2a 2018-07-12 stsp }
4751 ab089a2a 2018-07-12 stsp free(id_str);
4752 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4753 3f60a8ef 2018-07-10 stsp free(line);
4754 2550e4c3 2018-07-13 stsp line = NULL;
4755 3f60a8ef 2018-07-10 stsp if (err)
4756 3f60a8ef 2018-07-10 stsp return err;
4757 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4758 2550e4c3 2018-07-13 stsp free(wline);
4759 2550e4c3 2018-07-13 stsp wline = NULL;
4760 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4761 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4762 3f60a8ef 2018-07-10 stsp
4763 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4764 145b6838 2022-06-16 stsp view->maxx = 0;
4765 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4766 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4767 826082fe 2020-12-10 stsp if (linelen == -1) {
4768 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4769 826082fe 2020-12-10 stsp s->eof = 1;
4770 826082fe 2020-12-10 stsp break;
4771 826082fe 2020-12-10 stsp }
4772 84451b3e 2018-07-10 stsp free(line);
4773 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4774 84451b3e 2018-07-10 stsp }
4775 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4776 826082fe 2020-12-10 stsp continue;
4777 1853e0f4 2022-06-16 stsp
4778 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4779 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4780 1853e0f4 2022-06-16 stsp if (err) {
4781 1853e0f4 2022-06-16 stsp free(line);
4782 1853e0f4 2022-06-16 stsp return err;
4783 1853e0f4 2022-06-16 stsp }
4784 1853e0f4 2022-06-16 stsp free(wline);
4785 1853e0f4 2022-06-16 stsp wline = NULL;
4786 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4787 84451b3e 2018-07-10 stsp
4788 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4789 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4790 b700b5d6 2018-07-10 stsp
4791 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4792 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4793 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4794 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4795 27a741e5 2019-09-11 stsp !(view->focussed &&
4796 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4797 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4798 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4799 8d0fe45a 2019-08-12 stsp char *id_str;
4800 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4801 87411fa9 2022-06-16 stsp blame_line->id);
4802 8d0fe45a 2019-08-12 stsp if (err) {
4803 8d0fe45a 2019-08-12 stsp free(line);
4804 8d0fe45a 2019-08-12 stsp return err;
4805 8d0fe45a 2019-08-12 stsp }
4806 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4807 11b20872 2019-11-08 stsp if (tc)
4808 11b20872 2019-11-08 stsp wattr_on(view->window,
4809 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4810 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4811 11b20872 2019-11-08 stsp if (tc)
4812 11b20872 2019-11-08 stsp wattr_off(view->window,
4813 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4814 8d0fe45a 2019-08-12 stsp free(id_str);
4815 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4816 8d0fe45a 2019-08-12 stsp } else {
4817 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4818 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4819 84451b3e 2018-07-10 stsp }
4820 ee41ec32 2018-07-10 stsp } else {
4821 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4822 ee41ec32 2018-07-10 stsp prev_id = NULL;
4823 ee41ec32 2018-07-10 stsp }
4824 84451b3e 2018-07-10 stsp
4825 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4826 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4827 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4828 27a741e5 2019-09-11 stsp
4829 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4830 41605754 2020-11-12 stsp width = 9;
4831 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4832 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4833 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4834 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4835 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4836 41605754 2020-11-12 stsp if (err) {
4837 41605754 2020-11-12 stsp free(line);
4838 41605754 2020-11-12 stsp return err;
4839 41605754 2020-11-12 stsp }
4840 41605754 2020-11-12 stsp width += 9;
4841 41605754 2020-11-12 stsp } else {
4842 4cb9d869 2022-06-16 stsp int skip;
4843 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4844 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4845 4cb9d869 2022-06-16 stsp if (err) {
4846 4cb9d869 2022-06-16 stsp free(line);
4847 4cb9d869 2022-06-16 stsp return err;
4848 145b6838 2022-06-16 stsp }
4849 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4850 a75b3e2d 2022-06-16 stsp width += 9;
4851 41605754 2020-11-12 stsp free(wline);
4852 41605754 2020-11-12 stsp wline = NULL;
4853 41605754 2020-11-12 stsp }
4854 41605754 2020-11-12 stsp
4855 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4856 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4857 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4858 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4859 84451b3e 2018-07-10 stsp }
4860 826082fe 2020-12-10 stsp free(line);
4861 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4862 84451b3e 2018-07-10 stsp
4863 9b058f45 2022-06-30 mark view_border(view);
4864 84451b3e 2018-07-10 stsp
4865 84451b3e 2018-07-10 stsp return NULL;
4866 84451b3e 2018-07-10 stsp }
4867 84451b3e 2018-07-10 stsp
4868 84451b3e 2018-07-10 stsp static const struct got_error *
4869 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4870 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4871 84451b3e 2018-07-10 stsp {
4872 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4873 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4874 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4875 1a76625f 2018-10-22 stsp int errcode;
4876 84451b3e 2018-07-10 stsp
4877 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4878 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4879 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4880 84451b3e 2018-07-10 stsp
4881 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4882 1a76625f 2018-10-22 stsp if (errcode)
4883 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4884 84451b3e 2018-07-10 stsp
4885 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4886 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4887 d68a0a7d 2018-07-10 stsp goto done;
4888 d68a0a7d 2018-07-10 stsp }
4889 d68a0a7d 2018-07-10 stsp
4890 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4891 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4892 d68a0a7d 2018-07-10 stsp
4893 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4894 d68a0a7d 2018-07-10 stsp if (line->annotated)
4895 d68a0a7d 2018-07-10 stsp goto done;
4896 d68a0a7d 2018-07-10 stsp
4897 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4898 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4899 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4900 84451b3e 2018-07-10 stsp goto done;
4901 84451b3e 2018-07-10 stsp }
4902 84451b3e 2018-07-10 stsp line->annotated = 1;
4903 84451b3e 2018-07-10 stsp done:
4904 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4905 1a76625f 2018-10-22 stsp if (errcode)
4906 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4907 84451b3e 2018-07-10 stsp return err;
4908 84451b3e 2018-07-10 stsp }
4909 84451b3e 2018-07-10 stsp
4910 84451b3e 2018-07-10 stsp static void *
4911 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4912 84451b3e 2018-07-10 stsp {
4913 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4914 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4915 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4916 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4917 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4918 1b484788 2022-06-28 tracey
4919 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4920 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4921 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4922 e6e73e55 2022-06-30 tracey
4923 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4924 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4925 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4926 e6e73e55 2022-06-30 tracey goto done;
4927 e6e73e55 2022-06-30 tracey }
4928 18430de3 2018-07-10 stsp
4929 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4930 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4931 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4932 e6e73e55 2022-06-30 tracey goto done;
4933 e6e73e55 2022-06-30 tracey }
4934 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4935 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4936 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4937 e6e73e55 2022-06-30 tracey goto done;
4938 e6e73e55 2022-06-30 tracey }
4939 e6e73e55 2022-06-30 tracey
4940 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4941 61266923 2020-01-14 stsp if (err)
4942 e6e73e55 2022-06-30 tracey goto done;
4943 61266923 2020-01-14 stsp
4944 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4945 e6e73e55 2022-06-30 tracey blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
4946 e6e73e55 2022-06-30 tracey f2);
4947 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4948 fc06ba56 2019-08-22 stsp err = NULL;
4949 18430de3 2018-07-10 stsp
4950 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4951 e6e73e55 2022-06-30 tracey if (errcode) {
4952 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
4953 e6e73e55 2022-06-30 tracey goto done;
4954 e6e73e55 2022-06-30 tracey }
4955 18430de3 2018-07-10 stsp
4956 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4957 1d0f4054 2021-06-17 stsp if (err == NULL)
4958 1d0f4054 2021-06-17 stsp err = close_err;
4959 c9beca56 2018-07-22 stsp ta->repo = NULL;
4960 c9beca56 2018-07-22 stsp *ta->complete = 1;
4961 18430de3 2018-07-10 stsp
4962 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4963 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4964 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4965 18430de3 2018-07-10 stsp
4966 e6e73e55 2022-06-30 tracey done:
4967 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4968 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
4969 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4970 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
4971 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
4972 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4973 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
4974 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
4975 1b484788 2022-06-28 tracey
4976 18430de3 2018-07-10 stsp return (void *)err;
4977 84451b3e 2018-07-10 stsp }
4978 84451b3e 2018-07-10 stsp
4979 245d91c1 2018-07-12 stsp static struct got_object_id *
4980 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4981 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4982 245d91c1 2018-07-12 stsp {
4983 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4984 8d0fe45a 2019-08-12 stsp
4985 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4986 8d0fe45a 2019-08-12 stsp return NULL;
4987 b880a918 2018-07-10 stsp
4988 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4989 245d91c1 2018-07-12 stsp if (!line->annotated)
4990 245d91c1 2018-07-12 stsp return NULL;
4991 245d91c1 2018-07-12 stsp
4992 245d91c1 2018-07-12 stsp return line->id;
4993 b880a918 2018-07-10 stsp }
4994 245d91c1 2018-07-12 stsp
4995 b880a918 2018-07-10 stsp static const struct got_error *
4996 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4997 a70480e0 2018-06-23 stsp {
4998 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4999 245d91c1 2018-07-12 stsp int i;
5000 245d91c1 2018-07-12 stsp
5001 245d91c1 2018-07-12 stsp if (blame->thread) {
5002 1a76625f 2018-10-22 stsp int errcode;
5003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5004 1a76625f 2018-10-22 stsp if (errcode)
5005 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5006 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5007 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5008 1a76625f 2018-10-22 stsp if (errcode)
5009 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5010 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5011 1a76625f 2018-10-22 stsp if (errcode)
5012 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5013 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5014 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5015 245d91c1 2018-07-12 stsp err = NULL;
5016 245d91c1 2018-07-12 stsp blame->thread = NULL;
5017 245d91c1 2018-07-12 stsp }
5018 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5019 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5020 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5021 1d0f4054 2021-06-17 stsp if (err == NULL)
5022 1d0f4054 2021-06-17 stsp err = close_err;
5023 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5024 245d91c1 2018-07-12 stsp }
5025 245d91c1 2018-07-12 stsp if (blame->f) {
5026 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5027 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5028 245d91c1 2018-07-12 stsp blame->f = NULL;
5029 245d91c1 2018-07-12 stsp }
5030 57670559 2018-12-23 stsp if (blame->lines) {
5031 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5032 57670559 2018-12-23 stsp free(blame->lines[i].id);
5033 57670559 2018-12-23 stsp free(blame->lines);
5034 57670559 2018-12-23 stsp blame->lines = NULL;
5035 57670559 2018-12-23 stsp }
5036 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5037 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5038 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5039 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5040 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5041 0ae84acc 2022-06-15 tracey if (err == NULL)
5042 0ae84acc 2022-06-15 tracey err = pack_err;
5043 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5044 0ae84acc 2022-06-15 tracey }
5045 245d91c1 2018-07-12 stsp return err;
5046 245d91c1 2018-07-12 stsp }
5047 245d91c1 2018-07-12 stsp
5048 245d91c1 2018-07-12 stsp static const struct got_error *
5049 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5050 fc06ba56 2019-08-22 stsp {
5051 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5052 fc06ba56 2019-08-22 stsp int *done = arg;
5053 fc06ba56 2019-08-22 stsp int errcode;
5054 fc06ba56 2019-08-22 stsp
5055 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5056 fc06ba56 2019-08-22 stsp if (errcode)
5057 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5058 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5059 fc06ba56 2019-08-22 stsp
5060 fc06ba56 2019-08-22 stsp if (*done)
5061 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5062 fc06ba56 2019-08-22 stsp
5063 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5064 fc06ba56 2019-08-22 stsp if (errcode)
5065 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5066 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5067 fc06ba56 2019-08-22 stsp
5068 fc06ba56 2019-08-22 stsp return err;
5069 fc06ba56 2019-08-22 stsp }
5070 fc06ba56 2019-08-22 stsp
5071 fc06ba56 2019-08-22 stsp static const struct got_error *
5072 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5073 245d91c1 2018-07-12 stsp {
5074 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5075 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5076 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5077 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5078 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5079 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5080 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5081 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5082 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5083 a70480e0 2018-06-23 stsp
5084 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5085 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5086 27d434c2 2018-09-15 stsp if (err)
5087 15a94983 2018-12-23 stsp return err;
5088 eb81bc23 2022-06-28 tracey
5089 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5090 eb81bc23 2022-06-28 tracey if (fd == -1) {
5091 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5092 eb81bc23 2022-06-28 tracey goto done;
5093 eb81bc23 2022-06-28 tracey }
5094 a44927cc 2022-04-07 stsp
5095 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5096 a44927cc 2022-04-07 stsp if (err)
5097 a44927cc 2022-04-07 stsp goto done;
5098 27d434c2 2018-09-15 stsp
5099 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5100 84451b3e 2018-07-10 stsp if (err)
5101 84451b3e 2018-07-10 stsp goto done;
5102 27d434c2 2018-09-15 stsp
5103 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5104 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5105 84451b3e 2018-07-10 stsp goto done;
5106 84451b3e 2018-07-10 stsp }
5107 a70480e0 2018-06-23 stsp
5108 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5109 a70480e0 2018-06-23 stsp if (err)
5110 a70480e0 2018-06-23 stsp goto done;
5111 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5112 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5113 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5114 84451b3e 2018-07-10 stsp goto done;
5115 84451b3e 2018-07-10 stsp }
5116 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5117 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5118 1fddf795 2021-01-20 stsp if (err)
5119 1fddf795 2021-01-20 stsp goto done;
5120 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5121 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5122 84451b3e 2018-07-10 stsp goto done;
5123 1fddf795 2021-01-20 stsp }
5124 b02560ec 2019-08-19 stsp
5125 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5126 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5127 b02560ec 2019-08-19 stsp blame->nlines--;
5128 a70480e0 2018-06-23 stsp
5129 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5130 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5131 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5132 84451b3e 2018-07-10 stsp goto done;
5133 84451b3e 2018-07-10 stsp }
5134 a70480e0 2018-06-23 stsp
5135 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5136 bd24772e 2018-07-11 stsp if (err)
5137 bd24772e 2018-07-11 stsp goto done;
5138 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5139 0ae84acc 2022-06-15 tracey pack_fds);
5140 0ae84acc 2022-06-15 tracey if (err)
5141 0ae84acc 2022-06-15 tracey goto done;
5142 bd24772e 2018-07-11 stsp
5143 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5144 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5145 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5146 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5147 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5148 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5149 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5150 245d91c1 2018-07-12 stsp goto done;
5151 245d91c1 2018-07-12 stsp }
5152 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5153 245d91c1 2018-07-12 stsp
5154 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5155 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5156 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5157 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5158 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5159 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5160 a5388363 2020-12-01 naddy s->blame_complete = 0;
5161 f5a09613 2020-12-13 naddy
5162 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5163 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5164 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5165 f5a09613 2020-12-13 naddy s->selected_line = 1;
5166 f5a09613 2020-12-13 naddy }
5167 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5168 245d91c1 2018-07-12 stsp
5169 245d91c1 2018-07-12 stsp done:
5170 a44927cc 2022-04-07 stsp if (commit)
5171 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5172 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5173 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5174 245d91c1 2018-07-12 stsp if (blob)
5175 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5176 27d434c2 2018-09-15 stsp free(obj_id);
5177 245d91c1 2018-07-12 stsp if (err)
5178 245d91c1 2018-07-12 stsp stop_blame(blame);
5179 245d91c1 2018-07-12 stsp return err;
5180 245d91c1 2018-07-12 stsp }
5181 245d91c1 2018-07-12 stsp
5182 245d91c1 2018-07-12 stsp static const struct got_error *
5183 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5184 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5185 245d91c1 2018-07-12 stsp {
5186 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5187 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5188 dbc6a6b6 2018-07-12 stsp
5189 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5190 245d91c1 2018-07-12 stsp
5191 c4843652 2019-08-12 stsp s->path = strdup(path);
5192 c4843652 2019-08-12 stsp if (s->path == NULL)
5193 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5194 c4843652 2019-08-12 stsp
5195 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5196 c4843652 2019-08-12 stsp if (err) {
5197 c4843652 2019-08-12 stsp free(s->path);
5198 7cbe629d 2018-08-04 stsp return err;
5199 c4843652 2019-08-12 stsp }
5200 245d91c1 2018-07-12 stsp
5201 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5202 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5203 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5204 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5205 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5206 fb2756b9 2018-08-04 stsp s->repo = repo;
5207 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5208 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5209 7cbe629d 2018-08-04 stsp
5210 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5211 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5212 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5213 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5214 11b20872 2019-11-08 stsp if (err)
5215 11b20872 2019-11-08 stsp return err;
5216 11b20872 2019-11-08 stsp }
5217 11b20872 2019-11-08 stsp
5218 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5219 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5220 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5221 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5222 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5223 e5a0f69f 2018-08-18 stsp
5224 a5388363 2020-12-01 naddy return run_blame(view);
5225 7cbe629d 2018-08-04 stsp }
5226 7cbe629d 2018-08-04 stsp
5227 e5a0f69f 2018-08-18 stsp static const struct got_error *
5228 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5229 7cbe629d 2018-08-04 stsp {
5230 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5231 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5232 7cbe629d 2018-08-04 stsp
5233 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5234 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5235 e5a0f69f 2018-08-18 stsp
5236 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5237 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5238 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5239 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5240 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5241 7cbe629d 2018-08-04 stsp }
5242 e5a0f69f 2018-08-18 stsp
5243 e5a0f69f 2018-08-18 stsp free(s->path);
5244 11b20872 2019-11-08 stsp free_colors(&s->colors);
5245 e5a0f69f 2018-08-18 stsp return err;
5246 7cbe629d 2018-08-04 stsp }
5247 7cbe629d 2018-08-04 stsp
5248 7cbe629d 2018-08-04 stsp static const struct got_error *
5249 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5250 6c4c42e0 2019-06-24 stsp {
5251 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5252 6c4c42e0 2019-06-24 stsp
5253 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5254 6c4c42e0 2019-06-24 stsp return NULL;
5255 6c4c42e0 2019-06-24 stsp }
5256 6c4c42e0 2019-06-24 stsp
5257 6c4c42e0 2019-06-24 stsp static const struct got_error *
5258 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5259 6c4c42e0 2019-06-24 stsp {
5260 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5261 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5262 6c4c42e0 2019-06-24 stsp int lineno;
5263 cb713507 2022-06-16 stsp char *line = NULL;
5264 826082fe 2020-12-10 stsp size_t linesize = 0;
5265 826082fe 2020-12-10 stsp ssize_t linelen;
5266 6c4c42e0 2019-06-24 stsp
5267 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5268 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5269 6c4c42e0 2019-06-24 stsp return NULL;
5270 6c4c42e0 2019-06-24 stsp }
5271 6c4c42e0 2019-06-24 stsp
5272 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5273 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5274 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5275 6c4c42e0 2019-06-24 stsp else
5276 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5277 487cd7d2 2021-12-17 stsp } else
5278 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5279 6c4c42e0 2019-06-24 stsp
5280 6c4c42e0 2019-06-24 stsp while (1) {
5281 6c4c42e0 2019-06-24 stsp off_t offset;
5282 6c4c42e0 2019-06-24 stsp
5283 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5284 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5285 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5286 6c4c42e0 2019-06-24 stsp break;
5287 6c4c42e0 2019-06-24 stsp }
5288 2246482e 2019-06-25 stsp
5289 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5290 6c4c42e0 2019-06-24 stsp lineno = 1;
5291 6c4c42e0 2019-06-24 stsp else
5292 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5293 6c4c42e0 2019-06-24 stsp }
5294 6c4c42e0 2019-06-24 stsp
5295 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5296 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5297 6c4c42e0 2019-06-24 stsp free(line);
5298 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5299 6c4c42e0 2019-06-24 stsp }
5300 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5301 cb713507 2022-06-16 stsp if (linelen != -1) {
5302 cb713507 2022-06-16 stsp char *exstr;
5303 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5304 cb713507 2022-06-16 stsp if (err)
5305 cb713507 2022-06-16 stsp break;
5306 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5307 cb713507 2022-06-16 stsp &view->regmatch)) {
5308 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5309 cb713507 2022-06-16 stsp s->matched_line = lineno;
5310 cb713507 2022-06-16 stsp free(exstr);
5311 cb713507 2022-06-16 stsp break;
5312 cb713507 2022-06-16 stsp }
5313 cb713507 2022-06-16 stsp free(exstr);
5314 6c4c42e0 2019-06-24 stsp }
5315 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5316 6c4c42e0 2019-06-24 stsp lineno++;
5317 6c4c42e0 2019-06-24 stsp else
5318 6c4c42e0 2019-06-24 stsp lineno--;
5319 6c4c42e0 2019-06-24 stsp }
5320 826082fe 2020-12-10 stsp free(line);
5321 6c4c42e0 2019-06-24 stsp
5322 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5323 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5324 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5325 6c4c42e0 2019-06-24 stsp }
5326 6c4c42e0 2019-06-24 stsp
5327 145b6838 2022-06-16 stsp return err;
5328 6c4c42e0 2019-06-24 stsp }
5329 6c4c42e0 2019-06-24 stsp
5330 6c4c42e0 2019-06-24 stsp static const struct got_error *
5331 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5332 7cbe629d 2018-08-04 stsp {
5333 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5334 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5335 2b380cc8 2018-10-24 stsp int errcode;
5336 2b380cc8 2018-10-24 stsp
5337 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5338 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5339 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5340 2b380cc8 2018-10-24 stsp if (errcode)
5341 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5342 51fe7530 2019-08-19 stsp
5343 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5344 2b380cc8 2018-10-24 stsp }
5345 e5a0f69f 2018-08-18 stsp
5346 51fe7530 2019-08-19 stsp if (s->blame_complete)
5347 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5348 51fe7530 2019-08-19 stsp
5349 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5350 e5a0f69f 2018-08-18 stsp
5351 9b058f45 2022-06-30 mark view_border(view);
5352 e5a0f69f 2018-08-18 stsp return err;
5353 e5a0f69f 2018-08-18 stsp }
5354 e5a0f69f 2018-08-18 stsp
5355 e5a0f69f 2018-08-18 stsp static const struct got_error *
5356 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5357 e5a0f69f 2018-08-18 stsp {
5358 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5359 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5360 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5361 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5362 9b058f45 2022-06-30 mark
5363 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5364 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5365 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5366 9b058f45 2022-06-30 mark --eos; /* border */
5367 7cbe629d 2018-08-04 stsp
5368 e5a0f69f 2018-08-18 stsp switch (ch) {
5369 145b6838 2022-06-16 stsp case '0':
5370 145b6838 2022-06-16 stsp view->x = 0;
5371 145b6838 2022-06-16 stsp break;
5372 145b6838 2022-06-16 stsp case '$':
5373 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5374 640cd7ff 2022-06-22 mark view->count = 0;
5375 145b6838 2022-06-16 stsp break;
5376 145b6838 2022-06-16 stsp case KEY_RIGHT:
5377 145b6838 2022-06-16 stsp case 'l':
5378 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5379 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5380 640cd7ff 2022-06-22 mark else
5381 640cd7ff 2022-06-22 mark view->count = 0;
5382 145b6838 2022-06-16 stsp break;
5383 145b6838 2022-06-16 stsp case KEY_LEFT:
5384 145b6838 2022-06-16 stsp case 'h':
5385 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5386 640cd7ff 2022-06-22 mark if (view->x <= 0)
5387 640cd7ff 2022-06-22 mark view->count = 0;
5388 145b6838 2022-06-16 stsp break;
5389 1e37a5c2 2019-05-12 jcs case 'q':
5390 1e37a5c2 2019-05-12 jcs s->done = 1;
5391 4deef56f 2021-09-02 naddy break;
5392 4deef56f 2021-09-02 naddy case 'g':
5393 4deef56f 2021-09-02 naddy case KEY_HOME:
5394 4deef56f 2021-09-02 naddy s->selected_line = 1;
5395 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5396 640cd7ff 2022-06-22 mark view->count = 0;
5397 4deef56f 2021-09-02 naddy break;
5398 4deef56f 2021-09-02 naddy case 'G':
5399 4deef56f 2021-09-02 naddy case KEY_END:
5400 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5401 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5402 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5403 4deef56f 2021-09-02 naddy } else {
5404 9b058f45 2022-06-30 mark s->selected_line = eos;
5405 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5406 4deef56f 2021-09-02 naddy }
5407 640cd7ff 2022-06-22 mark view->count = 0;
5408 1e37a5c2 2019-05-12 jcs break;
5409 1e37a5c2 2019-05-12 jcs case 'k':
5410 1e37a5c2 2019-05-12 jcs case KEY_UP:
5411 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5412 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5413 1e37a5c2 2019-05-12 jcs s->selected_line--;
5414 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5415 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5416 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5417 640cd7ff 2022-06-22 mark else
5418 640cd7ff 2022-06-22 mark view->count = 0;
5419 1e37a5c2 2019-05-12 jcs break;
5420 83cc4199 2022-06-13 stsp case CTRL('u'):
5421 33c3719a 2022-06-15 stsp case 'u':
5422 83cc4199 2022-06-13 stsp nscroll /= 2;
5423 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5424 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5425 ea025d1d 2020-02-22 naddy case CTRL('b'):
5426 61417565 2022-06-20 mark case 'b':
5427 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5428 640cd7ff 2022-06-22 mark if (view->count > 1)
5429 640cd7ff 2022-06-22 mark nscroll += nscroll;
5430 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5431 640cd7ff 2022-06-22 mark view->count = 0;
5432 e5a0f69f 2018-08-18 stsp break;
5433 1e37a5c2 2019-05-12 jcs }
5434 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5435 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5436 1e37a5c2 2019-05-12 jcs else
5437 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5438 1e37a5c2 2019-05-12 jcs break;
5439 1e37a5c2 2019-05-12 jcs case 'j':
5440 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5441 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5442 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5443 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5444 1e37a5c2 2019-05-12 jcs s->selected_line++;
5445 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5446 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5447 640cd7ff 2022-06-22 mark else
5448 640cd7ff 2022-06-22 mark view->count = 0;
5449 1e37a5c2 2019-05-12 jcs break;
5450 61417565 2022-06-20 mark case 'c':
5451 1e37a5c2 2019-05-12 jcs case 'p': {
5452 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5453 640cd7ff 2022-06-22 mark
5454 640cd7ff 2022-06-22 mark view->count = 0;
5455 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5456 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5457 1e37a5c2 2019-05-12 jcs if (id == NULL)
5458 e5a0f69f 2018-08-18 stsp break;
5459 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5460 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5461 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5462 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5463 1e37a5c2 2019-05-12 jcs int obj_type;
5464 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5465 1e37a5c2 2019-05-12 jcs s->repo, id);
5466 e5a0f69f 2018-08-18 stsp if (err)
5467 e5a0f69f 2018-08-18 stsp break;
5468 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5469 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5470 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5471 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5472 e5a0f69f 2018-08-18 stsp break;
5473 e5a0f69f 2018-08-18 stsp }
5474 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5475 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5476 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5477 a44927cc 2022-04-07 stsp if (err)
5478 a44927cc 2022-04-07 stsp break;
5479 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5480 a44927cc 2022-04-07 stsp pcommit, s->path);
5481 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5482 e5a0f69f 2018-08-18 stsp if (err) {
5483 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5484 1e37a5c2 2019-05-12 jcs err = NULL;
5485 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5486 e5a0f69f 2018-08-18 stsp break;
5487 e5a0f69f 2018-08-18 stsp }
5488 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5489 1e37a5c2 2019-05-12 jcs blob_id);
5490 1e37a5c2 2019-05-12 jcs free(blob_id);
5491 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5492 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5493 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5494 e5a0f69f 2018-08-18 stsp break;
5495 1e37a5c2 2019-05-12 jcs }
5496 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5497 d7b5a0e8 2022-04-20 stsp &pid->id);
5498 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5499 1e37a5c2 2019-05-12 jcs } else {
5500 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5501 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5502 1e37a5c2 2019-05-12 jcs break;
5503 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5504 1e37a5c2 2019-05-12 jcs id);
5505 1e37a5c2 2019-05-12 jcs }
5506 1e37a5c2 2019-05-12 jcs if (err)
5507 e5a0f69f 2018-08-18 stsp break;
5508 1e37a5c2 2019-05-12 jcs s->done = 1;
5509 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5510 1e37a5c2 2019-05-12 jcs s->done = 0;
5511 1e37a5c2 2019-05-12 jcs if (thread_err)
5512 1e37a5c2 2019-05-12 jcs break;
5513 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5514 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5515 a5388363 2020-12-01 naddy err = run_blame(view);
5516 1e37a5c2 2019-05-12 jcs if (err)
5517 1e37a5c2 2019-05-12 jcs break;
5518 1e37a5c2 2019-05-12 jcs break;
5519 1e37a5c2 2019-05-12 jcs }
5520 61417565 2022-06-20 mark case 'C': {
5521 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5522 640cd7ff 2022-06-22 mark
5523 640cd7ff 2022-06-22 mark view->count = 0;
5524 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5525 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5526 1e37a5c2 2019-05-12 jcs break;
5527 1e37a5c2 2019-05-12 jcs s->done = 1;
5528 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5529 1e37a5c2 2019-05-12 jcs s->done = 0;
5530 1e37a5c2 2019-05-12 jcs if (thread_err)
5531 1e37a5c2 2019-05-12 jcs break;
5532 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5533 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5534 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5535 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5536 a5388363 2020-12-01 naddy err = run_blame(view);
5537 1e37a5c2 2019-05-12 jcs if (err)
5538 1e37a5c2 2019-05-12 jcs break;
5539 1e37a5c2 2019-05-12 jcs break;
5540 1e37a5c2 2019-05-12 jcs }
5541 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5542 1e37a5c2 2019-05-12 jcs case '\r': {
5543 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5544 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5545 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5546 640cd7ff 2022-06-22 mark
5547 640cd7ff 2022-06-22 mark view->count = 0;
5548 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5549 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5550 1e37a5c2 2019-05-12 jcs if (id == NULL)
5551 1e37a5c2 2019-05-12 jcs break;
5552 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5553 1e37a5c2 2019-05-12 jcs if (err)
5554 1e37a5c2 2019-05-12 jcs break;
5555 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5556 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5557 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5558 9b058f45 2022-06-30 mark
5559 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5560 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5561 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5562 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5563 1e37a5c2 2019-05-12 jcs break;
5564 15a94983 2018-12-23 stsp }
5565 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5566 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5567 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5568 1e37a5c2 2019-05-12 jcs if (err) {
5569 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5570 1e37a5c2 2019-05-12 jcs break;
5571 1e37a5c2 2019-05-12 jcs }
5572 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5573 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5574 9b058f45 2022-06-30 mark if (err)
5575 9b058f45 2022-06-30 mark break;
5576 9b058f45 2022-06-30 mark }
5577 9b058f45 2022-06-30 mark
5578 e78dc838 2020-12-04 stsp view->focussed = 0;
5579 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5580 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5581 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5582 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5583 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5584 1e37a5c2 2019-05-12 jcs if (err)
5585 34bc9ec9 2019-02-22 stsp break;
5586 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5587 0dbbbe90 2022-06-17 op if (err)
5588 0dbbbe90 2022-06-17 op break;
5589 e78dc838 2020-12-04 stsp view->focus_child = 1;
5590 1e37a5c2 2019-05-12 jcs } else
5591 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5592 1e37a5c2 2019-05-12 jcs if (err)
5593 e5a0f69f 2018-08-18 stsp break;
5594 1e37a5c2 2019-05-12 jcs break;
5595 1e37a5c2 2019-05-12 jcs }
5596 83cc4199 2022-06-13 stsp case CTRL('d'):
5597 33c3719a 2022-06-15 stsp case 'd':
5598 83cc4199 2022-06-13 stsp nscroll /= 2;
5599 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5600 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5601 ea025d1d 2020-02-22 naddy case CTRL('f'):
5602 61417565 2022-06-20 mark case 'f':
5603 1e37a5c2 2019-05-12 jcs case ' ':
5604 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5605 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5606 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5607 640cd7ff 2022-06-22 mark view->count = 0;
5608 e5a0f69f 2018-08-18 stsp break;
5609 1e37a5c2 2019-05-12 jcs }
5610 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5611 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5612 83cc4199 2022-06-13 stsp s->selected_line +=
5613 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5614 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5615 1e37a5c2 2019-05-12 jcs }
5616 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5617 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5618 1e37a5c2 2019-05-12 jcs else
5619 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5620 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5621 1e37a5c2 2019-05-12 jcs break;
5622 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5623 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5624 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5625 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5626 1e37a5c2 2019-05-12 jcs }
5627 1e37a5c2 2019-05-12 jcs break;
5628 1e37a5c2 2019-05-12 jcs default:
5629 640cd7ff 2022-06-22 mark view->count = 0;
5630 1e37a5c2 2019-05-12 jcs break;
5631 a70480e0 2018-06-23 stsp }
5632 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5633 a70480e0 2018-06-23 stsp }
5634 a70480e0 2018-06-23 stsp
5635 a70480e0 2018-06-23 stsp static const struct got_error *
5636 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5637 9f7d7167 2018-04-29 stsp {
5638 a70480e0 2018-06-23 stsp const struct got_error *error;
5639 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5640 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5641 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5642 0587e10c 2020-07-23 stsp char *link_target = NULL;
5643 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5644 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5645 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5646 a70480e0 2018-06-23 stsp int ch;
5647 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5648 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5649 a70480e0 2018-06-23 stsp
5650 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5651 a70480e0 2018-06-23 stsp switch (ch) {
5652 a70480e0 2018-06-23 stsp case 'c':
5653 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5654 a70480e0 2018-06-23 stsp break;
5655 69069811 2018-08-02 stsp case 'r':
5656 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5657 69069811 2018-08-02 stsp if (repo_path == NULL)
5658 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5659 9ba1d308 2019-10-21 stsp optarg);
5660 69069811 2018-08-02 stsp break;
5661 a70480e0 2018-06-23 stsp default:
5662 17020d27 2019-03-07 stsp usage_blame();
5663 a70480e0 2018-06-23 stsp /* NOTREACHED */
5664 a70480e0 2018-06-23 stsp }
5665 a70480e0 2018-06-23 stsp }
5666 a70480e0 2018-06-23 stsp
5667 a70480e0 2018-06-23 stsp argc -= optind;
5668 a70480e0 2018-06-23 stsp argv += optind;
5669 a70480e0 2018-06-23 stsp
5670 f135c941 2020-02-20 stsp if (argc != 1)
5671 a70480e0 2018-06-23 stsp usage_blame();
5672 6962eb72 2020-02-20 stsp
5673 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5674 0ae84acc 2022-06-15 tracey if (error != NULL)
5675 0ae84acc 2022-06-15 tracey goto done;
5676 0ae84acc 2022-06-15 tracey
5677 69069811 2018-08-02 stsp if (repo_path == NULL) {
5678 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5679 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5680 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5681 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5682 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5683 c156c7a4 2020-12-18 stsp goto done;
5684 f135c941 2020-02-20 stsp if (worktree)
5685 eb41ed75 2019-02-05 stsp repo_path =
5686 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5687 f135c941 2020-02-20 stsp else
5688 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5689 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5690 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5691 c156c7a4 2020-12-18 stsp goto done;
5692 c156c7a4 2020-12-18 stsp }
5693 f135c941 2020-02-20 stsp }
5694 a915003a 2019-02-05 stsp
5695 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5696 c02c541e 2019-03-29 stsp if (error != NULL)
5697 8e94dd5b 2019-01-04 stsp goto done;
5698 69069811 2018-08-02 stsp
5699 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5700 f135c941 2020-02-20 stsp worktree);
5701 c02c541e 2019-03-29 stsp if (error)
5702 92205607 2019-01-04 stsp goto done;
5703 69069811 2018-08-02 stsp
5704 f135c941 2020-02-20 stsp init_curses();
5705 f135c941 2020-02-20 stsp
5706 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5707 51a10b52 2020-12-26 stsp if (error)
5708 51a10b52 2020-12-26 stsp goto done;
5709 51a10b52 2020-12-26 stsp
5710 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5711 eb41ed75 2019-02-05 stsp if (error)
5712 69069811 2018-08-02 stsp goto done;
5713 a70480e0 2018-06-23 stsp
5714 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5715 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5716 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5717 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5718 a70480e0 2018-06-23 stsp if (error != NULL)
5719 66b4983c 2018-06-23 stsp goto done;
5720 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5721 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5722 a70480e0 2018-06-23 stsp } else {
5723 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5724 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5725 a70480e0 2018-06-23 stsp }
5726 a19e88aa 2018-06-23 stsp if (error != NULL)
5727 8b473291 2019-02-21 stsp goto done;
5728 8b473291 2019-02-21 stsp
5729 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5730 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5731 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5732 e1cd8fed 2018-08-01 stsp goto done;
5733 e1cd8fed 2018-08-01 stsp }
5734 0587e10c 2020-07-23 stsp
5735 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5736 a44927cc 2022-04-07 stsp if (error)
5737 a44927cc 2022-04-07 stsp goto done;
5738 a44927cc 2022-04-07 stsp
5739 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5740 a44927cc 2022-04-07 stsp commit, repo);
5741 7cbe629d 2018-08-04 stsp if (error)
5742 7cbe629d 2018-08-04 stsp goto done;
5743 0587e10c 2020-07-23 stsp
5744 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5745 78756c87 2020-11-24 stsp commit_id, repo);
5746 0587e10c 2020-07-23 stsp if (error)
5747 0587e10c 2020-07-23 stsp goto done;
5748 12314ad4 2019-08-31 stsp if (worktree) {
5749 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5750 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5751 12314ad4 2019-08-31 stsp worktree = NULL;
5752 12314ad4 2019-08-31 stsp }
5753 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5754 a70480e0 2018-06-23 stsp done:
5755 69069811 2018-08-02 stsp free(repo_path);
5756 f135c941 2020-02-20 stsp free(in_repo_path);
5757 0587e10c 2020-07-23 stsp free(link_target);
5758 69069811 2018-08-02 stsp free(cwd);
5759 a70480e0 2018-06-23 stsp free(commit_id);
5760 a44927cc 2022-04-07 stsp if (commit)
5761 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5762 eb41ed75 2019-02-05 stsp if (worktree)
5763 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5764 1d0f4054 2021-06-17 stsp if (repo) {
5765 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5766 1d0f4054 2021-06-17 stsp if (error == NULL)
5767 1d0f4054 2021-06-17 stsp error = close_err;
5768 1d0f4054 2021-06-17 stsp }
5769 0ae84acc 2022-06-15 tracey if (pack_fds) {
5770 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5771 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5772 0ae84acc 2022-06-15 tracey if (error == NULL)
5773 0ae84acc 2022-06-15 tracey error = pack_err;
5774 0ae84acc 2022-06-15 tracey }
5775 51a10b52 2020-12-26 stsp tog_free_refs();
5776 a70480e0 2018-06-23 stsp return error;
5777 ffd1d5e5 2018-06-23 stsp }
5778 ffd1d5e5 2018-06-23 stsp
5779 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5780 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5781 ffd1d5e5 2018-06-23 stsp {
5782 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5783 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5784 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5785 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5786 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5787 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5788 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5789 ffd1d5e5 2018-06-23 stsp
5790 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5791 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5792 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5793 9b058f45 2022-06-30 mark --limit; /* border */
5794 ffd1d5e5 2018-06-23 stsp
5795 f7d12f7e 2018-08-01 stsp werase(view->window);
5796 ffd1d5e5 2018-06-23 stsp
5797 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5798 ffd1d5e5 2018-06-23 stsp return NULL;
5799 ffd1d5e5 2018-06-23 stsp
5800 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5801 ccda2f4d 2022-06-16 stsp 0, 0);
5802 ffd1d5e5 2018-06-23 stsp if (err)
5803 ffd1d5e5 2018-06-23 stsp return err;
5804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5805 a3404814 2018-09-02 stsp wstandout(view->window);
5806 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5807 11b20872 2019-11-08 stsp if (tc)
5808 11b20872 2019-11-08 stsp wattr_on(view->window,
5809 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5810 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5811 11b20872 2019-11-08 stsp if (tc)
5812 11b20872 2019-11-08 stsp wattr_off(view->window,
5813 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5814 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5815 a3404814 2018-09-02 stsp wstandend(view->window);
5816 2550e4c3 2018-07-13 stsp free(wline);
5817 2550e4c3 2018-07-13 stsp wline = NULL;
5818 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5819 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5820 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5821 ffd1d5e5 2018-06-23 stsp return NULL;
5822 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5823 ccda2f4d 2022-06-16 stsp 0, 0);
5824 ce52c690 2018-06-23 stsp if (err)
5825 ce52c690 2018-06-23 stsp return err;
5826 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5827 2550e4c3 2018-07-13 stsp free(wline);
5828 2550e4c3 2018-07-13 stsp wline = NULL;
5829 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5830 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5831 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5832 ffd1d5e5 2018-06-23 stsp return NULL;
5833 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5834 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5835 a1eca9bb 2018-06-23 stsp return NULL;
5836 ffd1d5e5 2018-06-23 stsp
5837 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5838 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5839 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5840 0cf4efb1 2018-09-29 stsp if (view->focussed)
5841 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5842 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5843 ffd1d5e5 2018-06-23 stsp }
5844 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5845 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5846 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5847 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5848 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5849 ffd1d5e5 2018-06-23 stsp return NULL;
5850 ffd1d5e5 2018-06-23 stsp n = 1;
5851 ffd1d5e5 2018-06-23 stsp } else {
5852 ffd1d5e5 2018-06-23 stsp n = 0;
5853 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5854 ffd1d5e5 2018-06-23 stsp }
5855 ffd1d5e5 2018-06-23 stsp
5856 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5857 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5858 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5859 848d6979 2019-08-12 stsp const char *modestr = "";
5860 56e0773d 2019-11-28 stsp mode_t mode;
5861 1d13200f 2018-07-12 stsp
5862 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5863 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5864 56e0773d 2019-11-28 stsp
5865 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5866 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5867 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5868 1d13200f 2018-07-12 stsp if (err)
5869 638f9024 2019-05-13 stsp return got_error_from_errno(
5870 230a42bd 2019-05-11 jcs "got_object_id_str");
5871 1d13200f 2018-07-12 stsp }
5872 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5873 63c5ca5d 2019-08-24 stsp modestr = "$";
5874 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5875 0d6c6ee3 2020-05-20 stsp int i;
5876 0d6c6ee3 2020-05-20 stsp
5877 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5878 d86d3b18 2020-12-01 naddy te, s->repo);
5879 0d6c6ee3 2020-05-20 stsp if (err) {
5880 0d6c6ee3 2020-05-20 stsp free(id_str);
5881 0d6c6ee3 2020-05-20 stsp return err;
5882 0d6c6ee3 2020-05-20 stsp }
5883 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5884 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5885 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5886 0d6c6ee3 2020-05-20 stsp }
5887 848d6979 2019-08-12 stsp modestr = "@";
5888 0d6c6ee3 2020-05-20 stsp }
5889 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5890 848d6979 2019-08-12 stsp modestr = "/";
5891 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5892 848d6979 2019-08-12 stsp modestr = "*";
5893 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5894 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5895 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5896 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5897 1d13200f 2018-07-12 stsp free(id_str);
5898 0d6c6ee3 2020-05-20 stsp free(link_target);
5899 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5900 1d13200f 2018-07-12 stsp }
5901 1d13200f 2018-07-12 stsp free(id_str);
5902 0d6c6ee3 2020-05-20 stsp free(link_target);
5903 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5904 ccda2f4d 2022-06-16 stsp 0, 0);
5905 ffd1d5e5 2018-06-23 stsp if (err) {
5906 ffd1d5e5 2018-06-23 stsp free(line);
5907 ffd1d5e5 2018-06-23 stsp break;
5908 ffd1d5e5 2018-06-23 stsp }
5909 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5910 0cf4efb1 2018-09-29 stsp if (view->focussed)
5911 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5912 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5913 ffd1d5e5 2018-06-23 stsp }
5914 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5915 f26dddb7 2019-11-08 stsp if (tc)
5916 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5917 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5918 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5919 f26dddb7 2019-11-08 stsp if (tc)
5920 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5921 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5922 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5923 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5924 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5925 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5926 ffd1d5e5 2018-06-23 stsp free(line);
5927 2550e4c3 2018-07-13 stsp free(wline);
5928 2550e4c3 2018-07-13 stsp wline = NULL;
5929 ffd1d5e5 2018-06-23 stsp n++;
5930 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5931 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5932 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5933 ffd1d5e5 2018-06-23 stsp break;
5934 ffd1d5e5 2018-06-23 stsp }
5935 ffd1d5e5 2018-06-23 stsp
5936 ffd1d5e5 2018-06-23 stsp return err;
5937 ffd1d5e5 2018-06-23 stsp }
5938 ffd1d5e5 2018-06-23 stsp
5939 ffd1d5e5 2018-06-23 stsp static void
5940 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5941 ffd1d5e5 2018-06-23 stsp {
5942 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5943 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5944 fa86c4bf 2020-11-29 stsp int i = 0;
5945 ffd1d5e5 2018-06-23 stsp
5946 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5947 ffd1d5e5 2018-06-23 stsp return;
5948 ffd1d5e5 2018-06-23 stsp
5949 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5950 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5951 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5952 fa86c4bf 2020-11-29 stsp if (!isroot)
5953 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5954 fa86c4bf 2020-11-29 stsp break;
5955 fa86c4bf 2020-11-29 stsp }
5956 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5957 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5958 ffd1d5e5 2018-06-23 stsp }
5959 ffd1d5e5 2018-06-23 stsp }
5960 ffd1d5e5 2018-06-23 stsp
5961 9b058f45 2022-06-30 mark static const struct got_error *
5962 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
5963 ffd1d5e5 2018-06-23 stsp {
5964 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
5965 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5966 ffd1d5e5 2018-06-23 stsp int n = 0;
5967 ffd1d5e5 2018-06-23 stsp
5968 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5969 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5970 694d3271 2020-12-01 naddy s->first_displayed_entry);
5971 694d3271 2020-12-01 naddy else
5972 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5973 56e0773d 2019-11-28 stsp
5974 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5975 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
5976 9b058f45 2022-06-30 mark if (last)
5977 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
5978 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
5979 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5980 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5981 768394f3 2019-01-24 stsp }
5982 ffd1d5e5 2018-06-23 stsp }
5983 9b058f45 2022-06-30 mark
5984 9b058f45 2022-06-30 mark return NULL;
5985 ffd1d5e5 2018-06-23 stsp }
5986 ffd1d5e5 2018-06-23 stsp
5987 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5988 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5989 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5990 ffd1d5e5 2018-06-23 stsp {
5991 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5992 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5993 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5994 ffd1d5e5 2018-06-23 stsp
5995 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5996 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5997 56e0773d 2019-11-28 stsp + 1 /* slash */;
5998 ce52c690 2018-06-23 stsp if (te)
5999 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6000 ce52c690 2018-06-23 stsp
6001 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6002 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6003 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6004 ffd1d5e5 2018-06-23 stsp
6005 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6006 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6007 d9765a41 2018-06-23 stsp while (pt) {
6008 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6009 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6010 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6011 cb2ebc8a 2018-06-23 stsp goto done;
6012 cb2ebc8a 2018-06-23 stsp }
6013 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6014 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6015 cb2ebc8a 2018-06-23 stsp goto done;
6016 cb2ebc8a 2018-06-23 stsp }
6017 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6018 ffd1d5e5 2018-06-23 stsp }
6019 ce52c690 2018-06-23 stsp if (te) {
6020 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6021 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6022 ce52c690 2018-06-23 stsp goto done;
6023 ce52c690 2018-06-23 stsp }
6024 cb2ebc8a 2018-06-23 stsp }
6025 ce52c690 2018-06-23 stsp done:
6026 ce52c690 2018-06-23 stsp if (err) {
6027 ce52c690 2018-06-23 stsp free(*path);
6028 ce52c690 2018-06-23 stsp *path = NULL;
6029 ce52c690 2018-06-23 stsp }
6030 ce52c690 2018-06-23 stsp return err;
6031 ce52c690 2018-06-23 stsp }
6032 ce52c690 2018-06-23 stsp
6033 ce52c690 2018-06-23 stsp static const struct got_error *
6034 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6035 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6036 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6037 ce52c690 2018-06-23 stsp {
6038 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6039 ce52c690 2018-06-23 stsp char *path;
6040 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6041 a0de39f3 2019-08-09 stsp
6042 a0de39f3 2019-08-09 stsp *new_view = NULL;
6043 69efd4c4 2018-07-18 stsp
6044 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6045 ce52c690 2018-06-23 stsp if (err)
6046 ce52c690 2018-06-23 stsp return err;
6047 ffd1d5e5 2018-06-23 stsp
6048 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6049 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6050 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6051 83ce39e3 2019-08-12 stsp goto done;
6052 83ce39e3 2019-08-12 stsp }
6053 cdf1ee82 2018-08-01 stsp
6054 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6055 e5a0f69f 2018-08-18 stsp if (err) {
6056 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6057 fc06ba56 2019-08-22 stsp err = NULL;
6058 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6059 e5a0f69f 2018-08-18 stsp } else
6060 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6061 83ce39e3 2019-08-12 stsp done:
6062 83ce39e3 2019-08-12 stsp free(path);
6063 69efd4c4 2018-07-18 stsp return err;
6064 69efd4c4 2018-07-18 stsp }
6065 69efd4c4 2018-07-18 stsp
6066 69efd4c4 2018-07-18 stsp static const struct got_error *
6067 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6068 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6069 69efd4c4 2018-07-18 stsp {
6070 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6071 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6072 69efd4c4 2018-07-18 stsp char *path;
6073 a0de39f3 2019-08-09 stsp
6074 a0de39f3 2019-08-09 stsp *new_view = NULL;
6075 69efd4c4 2018-07-18 stsp
6076 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6077 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6078 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6079 e5a0f69f 2018-08-18 stsp
6080 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6081 69efd4c4 2018-07-18 stsp if (err)
6082 69efd4c4 2018-07-18 stsp return err;
6083 69efd4c4 2018-07-18 stsp
6084 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6085 4e97c21c 2020-12-06 stsp path, 0);
6086 ba4f502b 2018-08-04 stsp if (err)
6087 e5a0f69f 2018-08-18 stsp view_close(log_view);
6088 e5a0f69f 2018-08-18 stsp else
6089 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6090 cb2ebc8a 2018-06-23 stsp free(path);
6091 cb2ebc8a 2018-06-23 stsp return err;
6092 ffd1d5e5 2018-06-23 stsp }
6093 ffd1d5e5 2018-06-23 stsp
6094 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6095 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6096 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6097 ffd1d5e5 2018-06-23 stsp {
6098 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6099 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6100 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6101 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6102 ffd1d5e5 2018-06-23 stsp
6103 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6104 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6105 bc573f3b 2021-07-10 stsp
6106 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6107 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6108 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6109 ffd1d5e5 2018-06-23 stsp
6110 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6111 bc573f3b 2021-07-10 stsp if (err)
6112 bc573f3b 2021-07-10 stsp goto done;
6113 bc573f3b 2021-07-10 stsp
6114 bc573f3b 2021-07-10 stsp /*
6115 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6116 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6117 bc573f3b 2021-07-10 stsp * closed on demand.
6118 bc573f3b 2021-07-10 stsp */
6119 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6120 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6121 bc573f3b 2021-07-10 stsp if (err)
6122 bc573f3b 2021-07-10 stsp goto done;
6123 bc573f3b 2021-07-10 stsp s->tree = s->root;
6124 bc573f3b 2021-07-10 stsp
6125 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6126 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6127 ffd1d5e5 2018-06-23 stsp goto done;
6128 ffd1d5e5 2018-06-23 stsp
6129 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6130 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6131 ffd1d5e5 2018-06-23 stsp goto done;
6132 ffd1d5e5 2018-06-23 stsp }
6133 ffd1d5e5 2018-06-23 stsp
6134 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6135 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6136 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6137 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6138 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6139 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6140 9cd7cbd1 2020-12-07 stsp goto done;
6141 9cd7cbd1 2020-12-07 stsp }
6142 9cd7cbd1 2020-12-07 stsp }
6143 fb2756b9 2018-08-04 stsp s->repo = repo;
6144 c0b01bdb 2019-11-08 stsp
6145 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6146 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6147 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6148 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6149 c0b01bdb 2019-11-08 stsp if (err)
6150 c0b01bdb 2019-11-08 stsp goto done;
6151 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6152 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6153 bc573f3b 2021-07-10 stsp if (err)
6154 c0b01bdb 2019-11-08 stsp goto done;
6155 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6156 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6157 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6158 bc573f3b 2021-07-10 stsp if (err)
6159 c0b01bdb 2019-11-08 stsp goto done;
6160 e5a0f69f 2018-08-18 stsp
6161 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6162 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6163 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6164 bc573f3b 2021-07-10 stsp if (err)
6165 11b20872 2019-11-08 stsp goto done;
6166 11b20872 2019-11-08 stsp
6167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6168 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6169 bc573f3b 2021-07-10 stsp if (err)
6170 c0b01bdb 2019-11-08 stsp goto done;
6171 c0b01bdb 2019-11-08 stsp }
6172 c0b01bdb 2019-11-08 stsp
6173 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6174 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6175 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6176 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6177 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6178 ad80ab7b 2018-08-04 stsp done:
6179 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6180 bc573f3b 2021-07-10 stsp if (commit)
6181 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6182 bc573f3b 2021-07-10 stsp if (err)
6183 bc573f3b 2021-07-10 stsp close_tree_view(view);
6184 ad80ab7b 2018-08-04 stsp return err;
6185 ad80ab7b 2018-08-04 stsp }
6186 ad80ab7b 2018-08-04 stsp
6187 e5a0f69f 2018-08-18 stsp static const struct got_error *
6188 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6189 ad80ab7b 2018-08-04 stsp {
6190 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6191 ad80ab7b 2018-08-04 stsp
6192 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6193 fb2756b9 2018-08-04 stsp free(s->tree_label);
6194 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6195 6484ec90 2018-09-29 stsp free(s->commit_id);
6196 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6197 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6198 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6199 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6200 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6201 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6202 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6203 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6204 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6205 ad80ab7b 2018-08-04 stsp free(parent);
6206 ad80ab7b 2018-08-04 stsp
6207 ad80ab7b 2018-08-04 stsp }
6208 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6209 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6210 bc573f3b 2021-07-10 stsp if (s->root)
6211 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6212 7c32bd05 2019-06-22 stsp return NULL;
6213 7c32bd05 2019-06-22 stsp }
6214 7c32bd05 2019-06-22 stsp
6215 7c32bd05 2019-06-22 stsp static const struct got_error *
6216 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6217 7c32bd05 2019-06-22 stsp {
6218 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6219 7c32bd05 2019-06-22 stsp
6220 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6221 7c32bd05 2019-06-22 stsp return NULL;
6222 7c32bd05 2019-06-22 stsp }
6223 7c32bd05 2019-06-22 stsp
6224 7c32bd05 2019-06-22 stsp static int
6225 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6226 7c32bd05 2019-06-22 stsp {
6227 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6228 7c32bd05 2019-06-22 stsp
6229 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6230 56e0773d 2019-11-28 stsp 0) == 0;
6231 7c32bd05 2019-06-22 stsp }
6232 7c32bd05 2019-06-22 stsp
6233 7c32bd05 2019-06-22 stsp static const struct got_error *
6234 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6235 7c32bd05 2019-06-22 stsp {
6236 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6237 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6238 7c32bd05 2019-06-22 stsp
6239 7c32bd05 2019-06-22 stsp if (!view->searching) {
6240 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6241 7c32bd05 2019-06-22 stsp return NULL;
6242 7c32bd05 2019-06-22 stsp }
6243 7c32bd05 2019-06-22 stsp
6244 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6245 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6246 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6247 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6248 56e0773d 2019-11-28 stsp s->selected_entry);
6249 7c32bd05 2019-06-22 stsp else
6250 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6251 56e0773d 2019-11-28 stsp } else {
6252 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6253 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6254 56e0773d 2019-11-28 stsp else
6255 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6256 56e0773d 2019-11-28 stsp s->selected_entry);
6257 7c32bd05 2019-06-22 stsp }
6258 7c32bd05 2019-06-22 stsp } else {
6259 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6260 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6261 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6262 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6263 56e0773d 2019-11-28 stsp else
6264 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6265 7c32bd05 2019-06-22 stsp }
6266 7c32bd05 2019-06-22 stsp
6267 7c32bd05 2019-06-22 stsp while (1) {
6268 56e0773d 2019-11-28 stsp if (te == NULL) {
6269 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6271 ac66afb8 2019-06-24 stsp return NULL;
6272 ac66afb8 2019-06-24 stsp }
6273 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6274 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6275 56e0773d 2019-11-28 stsp else
6276 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6277 7c32bd05 2019-06-22 stsp }
6278 7c32bd05 2019-06-22 stsp
6279 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6280 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6281 56e0773d 2019-11-28 stsp s->matched_entry = te;
6282 7c32bd05 2019-06-22 stsp break;
6283 7c32bd05 2019-06-22 stsp }
6284 7c32bd05 2019-06-22 stsp
6285 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6286 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6287 56e0773d 2019-11-28 stsp else
6288 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6289 7c32bd05 2019-06-22 stsp }
6290 e5a0f69f 2018-08-18 stsp
6291 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6292 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6293 7c32bd05 2019-06-22 stsp s->selected = 0;
6294 7c32bd05 2019-06-22 stsp }
6295 7c32bd05 2019-06-22 stsp
6296 e5a0f69f 2018-08-18 stsp return NULL;
6297 ad80ab7b 2018-08-04 stsp }
6298 ad80ab7b 2018-08-04 stsp
6299 ad80ab7b 2018-08-04 stsp static const struct got_error *
6300 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6301 ad80ab7b 2018-08-04 stsp {
6302 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6303 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6304 e5a0f69f 2018-08-18 stsp char *parent_path;
6305 ad80ab7b 2018-08-04 stsp
6306 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6307 e5a0f69f 2018-08-18 stsp if (err)
6308 e5a0f69f 2018-08-18 stsp return err;
6309 ffd1d5e5 2018-06-23 stsp
6310 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6311 e5a0f69f 2018-08-18 stsp free(parent_path);
6312 669b5ffa 2018-10-07 stsp
6313 9b058f45 2022-06-30 mark view_border(view);
6314 e5a0f69f 2018-08-18 stsp return err;
6315 e5a0f69f 2018-08-18 stsp }
6316 ce52c690 2018-06-23 stsp
6317 e5a0f69f 2018-08-18 stsp static const struct got_error *
6318 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6319 e5a0f69f 2018-08-18 stsp {
6320 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6321 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6322 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6323 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6324 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6325 ffd1d5e5 2018-06-23 stsp
6326 e5a0f69f 2018-08-18 stsp switch (ch) {
6327 1e37a5c2 2019-05-12 jcs case 'i':
6328 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6329 640cd7ff 2022-06-22 mark view->count = 0;
6330 1e37a5c2 2019-05-12 jcs break;
6331 1e37a5c2 2019-05-12 jcs case 'l':
6332 640cd7ff 2022-06-22 mark view->count = 0;
6333 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6334 ffd1d5e5 2018-06-23 stsp break;
6335 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6336 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6337 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6338 e78dc838 2020-12-04 stsp view->focussed = 0;
6339 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6340 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6341 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6342 1e37a5c2 2019-05-12 jcs if (err)
6343 1e37a5c2 2019-05-12 jcs return err;
6344 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6345 0dbbbe90 2022-06-17 op if (err)
6346 0dbbbe90 2022-06-17 op return err;
6347 e78dc838 2020-12-04 stsp view->focus_child = 1;
6348 1e37a5c2 2019-05-12 jcs } else
6349 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6350 152c1c93 2020-11-29 stsp break;
6351 152c1c93 2020-11-29 stsp case 'r':
6352 640cd7ff 2022-06-22 mark view->count = 0;
6353 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6354 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6355 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6356 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6357 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6358 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6359 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6360 152c1c93 2020-11-29 stsp if (err) {
6361 152c1c93 2020-11-29 stsp view_close(ref_view);
6362 152c1c93 2020-11-29 stsp return err;
6363 152c1c93 2020-11-29 stsp }
6364 e78dc838 2020-12-04 stsp view->focussed = 0;
6365 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6366 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6367 152c1c93 2020-11-29 stsp err = view_close_child(view);
6368 152c1c93 2020-11-29 stsp if (err)
6369 152c1c93 2020-11-29 stsp return err;
6370 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6371 0dbbbe90 2022-06-17 op if (err)
6372 0dbbbe90 2022-06-17 op return err;
6373 e78dc838 2020-12-04 stsp view->focus_child = 1;
6374 152c1c93 2020-11-29 stsp } else
6375 152c1c93 2020-11-29 stsp *new_view = ref_view;
6376 e4526bf5 2021-09-03 naddy break;
6377 e4526bf5 2021-09-03 naddy case 'g':
6378 e4526bf5 2021-09-03 naddy case KEY_HOME:
6379 e4526bf5 2021-09-03 naddy s->selected = 0;
6380 640cd7ff 2022-06-22 mark view->count = 0;
6381 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6382 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6383 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6384 e4526bf5 2021-09-03 naddy else
6385 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6386 1e37a5c2 2019-05-12 jcs break;
6387 e4526bf5 2021-09-03 naddy case 'G':
6388 9b058f45 2022-06-30 mark case KEY_END: {
6389 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6390 9b058f45 2022-06-30 mark
6391 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6392 9b058f45 2022-06-30 mark --eos; /* border */
6393 e4526bf5 2021-09-03 naddy s->selected = 0;
6394 640cd7ff 2022-06-22 mark view->count = 0;
6395 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6396 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6397 e4526bf5 2021-09-03 naddy if (te == NULL) {
6398 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6399 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6400 e4526bf5 2021-09-03 naddy n++;
6401 e4526bf5 2021-09-03 naddy }
6402 e4526bf5 2021-09-03 naddy break;
6403 e4526bf5 2021-09-03 naddy }
6404 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6405 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6406 e4526bf5 2021-09-03 naddy }
6407 e4526bf5 2021-09-03 naddy if (n > 0)
6408 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6409 e4526bf5 2021-09-03 naddy break;
6410 9b058f45 2022-06-30 mark }
6411 1e37a5c2 2019-05-12 jcs case 'k':
6412 1e37a5c2 2019-05-12 jcs case KEY_UP:
6413 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6414 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6415 1e37a5c2 2019-05-12 jcs s->selected--;
6416 fa86c4bf 2020-11-29 stsp break;
6417 1e37a5c2 2019-05-12 jcs }
6418 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6419 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6420 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6421 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6422 640cd7ff 2022-06-22 mark view->count = 0;
6423 1e37a5c2 2019-05-12 jcs break;
6424 83cc4199 2022-06-13 stsp case CTRL('u'):
6425 33c3719a 2022-06-15 stsp case 'u':
6426 83cc4199 2022-06-13 stsp nscroll /= 2;
6427 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6428 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6429 ea025d1d 2020-02-22 naddy case CTRL('b'):
6430 61417565 2022-06-20 mark case 'b':
6431 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6432 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6433 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6434 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6435 fa86c4bf 2020-11-29 stsp } else {
6436 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6437 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6438 fa86c4bf 2020-11-29 stsp }
6439 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6440 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6441 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6442 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6443 640cd7ff 2022-06-22 mark view->count = 0;
6444 1e37a5c2 2019-05-12 jcs break;
6445 1e37a5c2 2019-05-12 jcs case 'j':
6446 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6447 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6448 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6449 1e37a5c2 2019-05-12 jcs s->selected++;
6450 1e37a5c2 2019-05-12 jcs break;
6451 1e37a5c2 2019-05-12 jcs }
6452 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6453 640cd7ff 2022-06-22 mark == NULL) {
6454 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6455 640cd7ff 2022-06-22 mark view->count = 0;
6456 1e37a5c2 2019-05-12 jcs break;
6457 640cd7ff 2022-06-22 mark }
6458 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6459 1e37a5c2 2019-05-12 jcs break;
6460 83cc4199 2022-06-13 stsp case CTRL('d'):
6461 33c3719a 2022-06-15 stsp case 'd':
6462 83cc4199 2022-06-13 stsp nscroll /= 2;
6463 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6464 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6465 ea025d1d 2020-02-22 naddy case CTRL('f'):
6466 61417565 2022-06-20 mark case 'f':
6467 48bb96f0 2022-06-20 naddy case ' ':
6468 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6469 1e37a5c2 2019-05-12 jcs == NULL) {
6470 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6471 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6472 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6473 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6474 640cd7ff 2022-06-22 mark else
6475 640cd7ff 2022-06-22 mark view->count = 0;
6476 1e37a5c2 2019-05-12 jcs break;
6477 1e37a5c2 2019-05-12 jcs }
6478 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6479 1e37a5c2 2019-05-12 jcs break;
6480 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6481 1e37a5c2 2019-05-12 jcs case '\r':
6482 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6483 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6484 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6485 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6486 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6487 640cd7ff 2022-06-22 mark view->count = 0;
6488 1e37a5c2 2019-05-12 jcs break;
6489 640cd7ff 2022-06-22 mark }
6490 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6491 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6492 1e37a5c2 2019-05-12 jcs entry);
6493 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6494 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6495 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6496 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6497 1e37a5c2 2019-05-12 jcs s->selected_entry =
6498 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6499 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6500 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6501 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6502 9b058f45 2022-06-30 mark if (err)
6503 9b058f45 2022-06-30 mark break;
6504 9b058f45 2022-06-30 mark }
6505 1e37a5c2 2019-05-12 jcs free(parent);
6506 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6507 56e0773d 2019-11-28 stsp s->selected_entry))) {
6508 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6509 640cd7ff 2022-06-22 mark view->count = 0;
6510 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6511 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6512 1e37a5c2 2019-05-12 jcs if (err)
6513 1e37a5c2 2019-05-12 jcs break;
6514 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6515 941e9f74 2019-05-21 stsp if (err) {
6516 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6517 1e37a5c2 2019-05-12 jcs break;
6518 1e37a5c2 2019-05-12 jcs }
6519 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6520 56e0773d 2019-11-28 stsp s->selected_entry))) {
6521 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6522 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6523 1e37a5c2 2019-05-12 jcs
6524 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6525 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6526 9b058f45 2022-06-30 mark
6527 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6528 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6529 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6530 1e37a5c2 2019-05-12 jcs if (err)
6531 1e37a5c2 2019-05-12 jcs break;
6532 9b058f45 2022-06-30 mark
6533 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6534 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6535 9b058f45 2022-06-30 mark if (err)
6536 9b058f45 2022-06-30 mark break;
6537 9b058f45 2022-06-30 mark }
6538 9b058f45 2022-06-30 mark
6539 640cd7ff 2022-06-22 mark view->count = 0;
6540 e78dc838 2020-12-04 stsp view->focussed = 0;
6541 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6542 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6543 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6544 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6545 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6546 669b5ffa 2018-10-07 stsp if (err)
6547 669b5ffa 2018-10-07 stsp return err;
6548 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6549 0dbbbe90 2022-06-17 op if (err)
6550 0dbbbe90 2022-06-17 op return err;
6551 e78dc838 2020-12-04 stsp view->focus_child = 1;
6552 669b5ffa 2018-10-07 stsp } else
6553 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6554 1e37a5c2 2019-05-12 jcs }
6555 1e37a5c2 2019-05-12 jcs break;
6556 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6557 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6558 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6559 640cd7ff 2022-06-22 mark view->count = 0;
6560 1e37a5c2 2019-05-12 jcs break;
6561 1e37a5c2 2019-05-12 jcs default:
6562 640cd7ff 2022-06-22 mark view->count = 0;
6563 1e37a5c2 2019-05-12 jcs break;
6564 ffd1d5e5 2018-06-23 stsp }
6565 e5a0f69f 2018-08-18 stsp
6566 ffd1d5e5 2018-06-23 stsp return err;
6567 9f7d7167 2018-04-29 stsp }
6568 9f7d7167 2018-04-29 stsp
6569 ffd1d5e5 2018-06-23 stsp __dead static void
6570 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6571 ffd1d5e5 2018-06-23 stsp {
6572 ffd1d5e5 2018-06-23 stsp endwin();
6573 87411fa9 2022-06-16 stsp fprintf(stderr,
6574 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6575 ffd1d5e5 2018-06-23 stsp getprogname());
6576 ffd1d5e5 2018-06-23 stsp exit(1);
6577 ffd1d5e5 2018-06-23 stsp }
6578 ffd1d5e5 2018-06-23 stsp
6579 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6580 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6581 ffd1d5e5 2018-06-23 stsp {
6582 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6583 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6584 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6585 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6586 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6587 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6588 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6589 4e97c21c 2020-12-06 stsp char *label = NULL;
6590 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6591 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6592 ffd1d5e5 2018-06-23 stsp int ch;
6593 5221c383 2018-08-01 stsp struct tog_view *view;
6594 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6595 70ac5f84 2019-03-28 stsp
6596 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6597 ffd1d5e5 2018-06-23 stsp switch (ch) {
6598 ffd1d5e5 2018-06-23 stsp case 'c':
6599 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6600 74283ab8 2020-02-07 stsp break;
6601 74283ab8 2020-02-07 stsp case 'r':
6602 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6603 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6604 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6605 74283ab8 2020-02-07 stsp optarg);
6606 ffd1d5e5 2018-06-23 stsp break;
6607 ffd1d5e5 2018-06-23 stsp default:
6608 e99e2d15 2020-11-24 naddy usage_tree();
6609 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6610 ffd1d5e5 2018-06-23 stsp }
6611 ffd1d5e5 2018-06-23 stsp }
6612 ffd1d5e5 2018-06-23 stsp
6613 ffd1d5e5 2018-06-23 stsp argc -= optind;
6614 ffd1d5e5 2018-06-23 stsp argv += optind;
6615 ffd1d5e5 2018-06-23 stsp
6616 55cccc34 2020-02-20 stsp if (argc > 1)
6617 e99e2d15 2020-11-24 naddy usage_tree();
6618 0ae84acc 2022-06-15 tracey
6619 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6620 0ae84acc 2022-06-15 tracey if (error != NULL)
6621 0ae84acc 2022-06-15 tracey goto done;
6622 74283ab8 2020-02-07 stsp
6623 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6624 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6625 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6626 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6627 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6628 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6629 c156c7a4 2020-12-18 stsp goto done;
6630 55cccc34 2020-02-20 stsp if (worktree)
6631 52185f70 2019-02-05 stsp repo_path =
6632 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6633 55cccc34 2020-02-20 stsp else
6634 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6635 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6636 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6637 c156c7a4 2020-12-18 stsp goto done;
6638 c156c7a4 2020-12-18 stsp }
6639 55cccc34 2020-02-20 stsp }
6640 a915003a 2019-02-05 stsp
6641 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 c02c541e 2019-03-29 stsp if (error != NULL)
6643 52185f70 2019-02-05 stsp goto done;
6644 d188b9a6 2019-01-04 stsp
6645 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6646 55cccc34 2020-02-20 stsp repo, worktree);
6647 55cccc34 2020-02-20 stsp if (error)
6648 55cccc34 2020-02-20 stsp goto done;
6649 55cccc34 2020-02-20 stsp
6650 55cccc34 2020-02-20 stsp init_curses();
6651 55cccc34 2020-02-20 stsp
6652 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6653 c02c541e 2019-03-29 stsp if (error)
6654 52185f70 2019-02-05 stsp goto done;
6655 ffd1d5e5 2018-06-23 stsp
6656 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6657 51a10b52 2020-12-26 stsp if (error)
6658 51a10b52 2020-12-26 stsp goto done;
6659 51a10b52 2020-12-26 stsp
6660 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6661 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6662 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6663 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6664 4e97c21c 2020-12-06 stsp if (error)
6665 4e97c21c 2020-12-06 stsp goto done;
6666 4e97c21c 2020-12-06 stsp head_ref_name = label;
6667 4e97c21c 2020-12-06 stsp } else {
6668 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6669 4e97c21c 2020-12-06 stsp if (error == NULL)
6670 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6671 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6672 4e97c21c 2020-12-06 stsp goto done;
6673 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6674 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6675 4e97c21c 2020-12-06 stsp if (error)
6676 4e97c21c 2020-12-06 stsp goto done;
6677 4e97c21c 2020-12-06 stsp }
6678 ffd1d5e5 2018-06-23 stsp
6679 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6680 a44927cc 2022-04-07 stsp if (error)
6681 a44927cc 2022-04-07 stsp goto done;
6682 a44927cc 2022-04-07 stsp
6683 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6684 5221c383 2018-08-01 stsp if (view == NULL) {
6685 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6686 5221c383 2018-08-01 stsp goto done;
6687 5221c383 2018-08-01 stsp }
6688 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6689 ad80ab7b 2018-08-04 stsp if (error)
6690 ad80ab7b 2018-08-04 stsp goto done;
6691 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6692 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6693 d91faf3b 2020-12-01 naddy in_repo_path);
6694 55cccc34 2020-02-20 stsp if (error)
6695 55cccc34 2020-02-20 stsp goto done;
6696 55cccc34 2020-02-20 stsp }
6697 55cccc34 2020-02-20 stsp
6698 55cccc34 2020-02-20 stsp if (worktree) {
6699 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6700 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6701 55cccc34 2020-02-20 stsp worktree = NULL;
6702 55cccc34 2020-02-20 stsp }
6703 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6704 ffd1d5e5 2018-06-23 stsp done:
6705 52185f70 2019-02-05 stsp free(repo_path);
6706 e4a0e26d 2020-02-20 stsp free(cwd);
6707 ffd1d5e5 2018-06-23 stsp free(commit_id);
6708 4e97c21c 2020-12-06 stsp free(label);
6709 486cd271 2020-12-06 stsp if (ref)
6710 486cd271 2020-12-06 stsp got_ref_close(ref);
6711 1d0f4054 2021-06-17 stsp if (repo) {
6712 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6713 1d0f4054 2021-06-17 stsp if (error == NULL)
6714 1d0f4054 2021-06-17 stsp error = close_err;
6715 1d0f4054 2021-06-17 stsp }
6716 0ae84acc 2022-06-15 tracey if (pack_fds) {
6717 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6718 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6719 0ae84acc 2022-06-15 tracey if (error == NULL)
6720 0ae84acc 2022-06-15 tracey error = pack_err;
6721 0ae84acc 2022-06-15 tracey }
6722 51a10b52 2020-12-26 stsp tog_free_refs();
6723 ffd1d5e5 2018-06-23 stsp return error;
6724 6458efa5 2020-11-24 stsp }
6725 6458efa5 2020-11-24 stsp
6726 6458efa5 2020-11-24 stsp static const struct got_error *
6727 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6728 6458efa5 2020-11-24 stsp {
6729 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6730 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6731 6458efa5 2020-11-24 stsp
6732 6458efa5 2020-11-24 stsp s->nrefs = 0;
6733 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6734 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6735 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6736 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6737 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6738 6458efa5 2020-11-24 stsp continue;
6739 6458efa5 2020-11-24 stsp
6740 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6741 6458efa5 2020-11-24 stsp if (re == NULL)
6742 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6743 6458efa5 2020-11-24 stsp
6744 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6745 8924d611 2020-12-26 stsp if (re->ref == NULL)
6746 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6747 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6748 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6749 6458efa5 2020-11-24 stsp }
6750 6458efa5 2020-11-24 stsp
6751 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6752 6458efa5 2020-11-24 stsp return NULL;
6753 6458efa5 2020-11-24 stsp }
6754 6458efa5 2020-11-24 stsp
6755 336075a4 2022-06-25 op static void
6756 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6757 6458efa5 2020-11-24 stsp {
6758 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6759 6458efa5 2020-11-24 stsp
6760 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6761 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6762 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6763 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6764 6458efa5 2020-11-24 stsp free(re);
6765 6458efa5 2020-11-24 stsp }
6766 6458efa5 2020-11-24 stsp }
6767 6458efa5 2020-11-24 stsp
6768 6458efa5 2020-11-24 stsp static const struct got_error *
6769 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6770 6458efa5 2020-11-24 stsp {
6771 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6772 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6773 6458efa5 2020-11-24 stsp
6774 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6775 6458efa5 2020-11-24 stsp s->repo = repo;
6776 6458efa5 2020-11-24 stsp
6777 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6778 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6779 6458efa5 2020-11-24 stsp
6780 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6781 6458efa5 2020-11-24 stsp if (err)
6782 6458efa5 2020-11-24 stsp return err;
6783 34ba6917 2020-11-30 stsp
6784 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6785 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6786 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6787 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6788 6458efa5 2020-11-24 stsp if (err)
6789 6458efa5 2020-11-24 stsp goto done;
6790 6458efa5 2020-11-24 stsp
6791 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6792 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6793 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6794 6458efa5 2020-11-24 stsp if (err)
6795 6458efa5 2020-11-24 stsp goto done;
6796 6458efa5 2020-11-24 stsp
6797 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6798 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6799 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6800 cc488aa7 2022-01-23 stsp if (err)
6801 cc488aa7 2022-01-23 stsp goto done;
6802 cc488aa7 2022-01-23 stsp
6803 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6804 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6805 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6806 6458efa5 2020-11-24 stsp if (err)
6807 6458efa5 2020-11-24 stsp goto done;
6808 6458efa5 2020-11-24 stsp }
6809 6458efa5 2020-11-24 stsp
6810 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6811 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6812 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6813 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6814 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6815 6458efa5 2020-11-24 stsp done:
6816 6458efa5 2020-11-24 stsp if (err)
6817 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6818 6458efa5 2020-11-24 stsp return err;
6819 6458efa5 2020-11-24 stsp }
6820 6458efa5 2020-11-24 stsp
6821 6458efa5 2020-11-24 stsp static const struct got_error *
6822 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6823 6458efa5 2020-11-24 stsp {
6824 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6825 6458efa5 2020-11-24 stsp
6826 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6827 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6828 6458efa5 2020-11-24 stsp
6829 6458efa5 2020-11-24 stsp return NULL;
6830 9f7d7167 2018-04-29 stsp }
6831 ce5b7c56 2019-07-09 stsp
6832 6458efa5 2020-11-24 stsp static const struct got_error *
6833 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6834 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6835 6458efa5 2020-11-24 stsp {
6836 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6837 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6838 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6839 6458efa5 2020-11-24 stsp int obj_type;
6840 6458efa5 2020-11-24 stsp
6841 c42c9805 2020-11-24 stsp *commit_id = NULL;
6842 6458efa5 2020-11-24 stsp
6843 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6844 6458efa5 2020-11-24 stsp if (err)
6845 6458efa5 2020-11-24 stsp return err;
6846 6458efa5 2020-11-24 stsp
6847 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6848 6458efa5 2020-11-24 stsp if (err)
6849 6458efa5 2020-11-24 stsp goto done;
6850 6458efa5 2020-11-24 stsp
6851 6458efa5 2020-11-24 stsp switch (obj_type) {
6852 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6853 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6854 6458efa5 2020-11-24 stsp break;
6855 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6856 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6857 6458efa5 2020-11-24 stsp if (err)
6858 6458efa5 2020-11-24 stsp goto done;
6859 c42c9805 2020-11-24 stsp free(obj_id);
6860 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6861 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6862 c42c9805 2020-11-24 stsp if (err)
6863 6458efa5 2020-11-24 stsp goto done;
6864 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6865 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6866 c42c9805 2020-11-24 stsp goto done;
6867 c42c9805 2020-11-24 stsp }
6868 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6869 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6870 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6871 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6872 c42c9805 2020-11-24 stsp goto done;
6873 c42c9805 2020-11-24 stsp }
6874 6458efa5 2020-11-24 stsp break;
6875 6458efa5 2020-11-24 stsp default:
6876 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6877 c42c9805 2020-11-24 stsp break;
6878 6458efa5 2020-11-24 stsp }
6879 6458efa5 2020-11-24 stsp
6880 c42c9805 2020-11-24 stsp done:
6881 c42c9805 2020-11-24 stsp if (tag)
6882 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6883 c42c9805 2020-11-24 stsp if (err) {
6884 c42c9805 2020-11-24 stsp free(*commit_id);
6885 c42c9805 2020-11-24 stsp *commit_id = NULL;
6886 c42c9805 2020-11-24 stsp }
6887 c42c9805 2020-11-24 stsp return err;
6888 c42c9805 2020-11-24 stsp }
6889 c42c9805 2020-11-24 stsp
6890 c42c9805 2020-11-24 stsp static const struct got_error *
6891 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6892 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6893 c42c9805 2020-11-24 stsp {
6894 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6895 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6896 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6897 c42c9805 2020-11-24 stsp
6898 c42c9805 2020-11-24 stsp *new_view = NULL;
6899 c42c9805 2020-11-24 stsp
6900 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6901 c42c9805 2020-11-24 stsp if (err) {
6902 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6903 c42c9805 2020-11-24 stsp return err;
6904 c42c9805 2020-11-24 stsp else
6905 c42c9805 2020-11-24 stsp return NULL;
6906 c42c9805 2020-11-24 stsp }
6907 c42c9805 2020-11-24 stsp
6908 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6909 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6910 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6911 6458efa5 2020-11-24 stsp goto done;
6912 6458efa5 2020-11-24 stsp }
6913 6458efa5 2020-11-24 stsp
6914 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6915 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6916 6458efa5 2020-11-24 stsp done:
6917 6458efa5 2020-11-24 stsp if (err)
6918 6458efa5 2020-11-24 stsp view_close(log_view);
6919 6458efa5 2020-11-24 stsp else
6920 6458efa5 2020-11-24 stsp *new_view = log_view;
6921 c42c9805 2020-11-24 stsp free(commit_id);
6922 6458efa5 2020-11-24 stsp return err;
6923 6458efa5 2020-11-24 stsp }
6924 6458efa5 2020-11-24 stsp
6925 ce5b7c56 2019-07-09 stsp static void
6926 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6927 6458efa5 2020-11-24 stsp {
6928 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6929 34ba6917 2020-11-30 stsp int i = 0;
6930 6458efa5 2020-11-24 stsp
6931 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6932 6458efa5 2020-11-24 stsp return;
6933 6458efa5 2020-11-24 stsp
6934 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6935 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6936 34ba6917 2020-11-30 stsp if (re == NULL)
6937 34ba6917 2020-11-30 stsp break;
6938 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6939 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6940 6458efa5 2020-11-24 stsp }
6941 6458efa5 2020-11-24 stsp }
6942 6458efa5 2020-11-24 stsp
6943 9b058f45 2022-06-30 mark static const struct got_error *
6944 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6945 6458efa5 2020-11-24 stsp {
6946 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6947 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6948 6458efa5 2020-11-24 stsp int n = 0;
6949 6458efa5 2020-11-24 stsp
6950 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6951 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6952 6458efa5 2020-11-24 stsp else
6953 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6954 6458efa5 2020-11-24 stsp
6955 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6956 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6957 9b058f45 2022-06-30 mark if (last)
6958 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
6959 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
6960 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6961 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6962 6458efa5 2020-11-24 stsp }
6963 6458efa5 2020-11-24 stsp }
6964 9b058f45 2022-06-30 mark
6965 9b058f45 2022-06-30 mark return NULL;
6966 6458efa5 2020-11-24 stsp }
6967 6458efa5 2020-11-24 stsp
6968 6458efa5 2020-11-24 stsp static const struct got_error *
6969 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6970 6458efa5 2020-11-24 stsp {
6971 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6972 6458efa5 2020-11-24 stsp
6973 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6974 6458efa5 2020-11-24 stsp return NULL;
6975 6458efa5 2020-11-24 stsp }
6976 6458efa5 2020-11-24 stsp
6977 6458efa5 2020-11-24 stsp static int
6978 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6979 6458efa5 2020-11-24 stsp {
6980 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6981 6458efa5 2020-11-24 stsp
6982 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6983 6458efa5 2020-11-24 stsp 0) == 0;
6984 6458efa5 2020-11-24 stsp }
6985 6458efa5 2020-11-24 stsp
6986 6458efa5 2020-11-24 stsp static const struct got_error *
6987 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6988 6458efa5 2020-11-24 stsp {
6989 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6990 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6991 6458efa5 2020-11-24 stsp
6992 6458efa5 2020-11-24 stsp if (!view->searching) {
6993 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6994 6458efa5 2020-11-24 stsp return NULL;
6995 6458efa5 2020-11-24 stsp }
6996 6458efa5 2020-11-24 stsp
6997 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6998 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6999 6458efa5 2020-11-24 stsp if (s->selected_entry)
7000 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7001 6458efa5 2020-11-24 stsp else
7002 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7003 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7004 6458efa5 2020-11-24 stsp } else {
7005 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7006 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7007 6458efa5 2020-11-24 stsp else
7008 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7009 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7010 6458efa5 2020-11-24 stsp }
7011 6458efa5 2020-11-24 stsp } else {
7012 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7013 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7014 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7015 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7016 6458efa5 2020-11-24 stsp else
7017 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7018 6458efa5 2020-11-24 stsp }
7019 6458efa5 2020-11-24 stsp
7020 6458efa5 2020-11-24 stsp while (1) {
7021 6458efa5 2020-11-24 stsp if (re == NULL) {
7022 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7023 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7024 6458efa5 2020-11-24 stsp return NULL;
7025 6458efa5 2020-11-24 stsp }
7026 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7027 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7028 6458efa5 2020-11-24 stsp else
7029 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7030 6458efa5 2020-11-24 stsp }
7031 6458efa5 2020-11-24 stsp
7032 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7033 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7034 6458efa5 2020-11-24 stsp s->matched_entry = re;
7035 6458efa5 2020-11-24 stsp break;
7036 6458efa5 2020-11-24 stsp }
7037 6458efa5 2020-11-24 stsp
7038 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7039 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7040 6458efa5 2020-11-24 stsp else
7041 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7042 6458efa5 2020-11-24 stsp }
7043 6458efa5 2020-11-24 stsp
7044 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7045 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7046 6458efa5 2020-11-24 stsp s->selected = 0;
7047 6458efa5 2020-11-24 stsp }
7048 6458efa5 2020-11-24 stsp
7049 6458efa5 2020-11-24 stsp return NULL;
7050 6458efa5 2020-11-24 stsp }
7051 6458efa5 2020-11-24 stsp
7052 6458efa5 2020-11-24 stsp static const struct got_error *
7053 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7054 6458efa5 2020-11-24 stsp {
7055 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7056 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7057 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7058 6458efa5 2020-11-24 stsp char *line = NULL;
7059 6458efa5 2020-11-24 stsp wchar_t *wline;
7060 6458efa5 2020-11-24 stsp struct tog_color *tc;
7061 6458efa5 2020-11-24 stsp int width, n;
7062 6458efa5 2020-11-24 stsp int limit = view->nlines;
7063 6458efa5 2020-11-24 stsp
7064 6458efa5 2020-11-24 stsp werase(view->window);
7065 6458efa5 2020-11-24 stsp
7066 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7067 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7068 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7069 9b058f45 2022-06-30 mark --limit; /* border */
7070 6458efa5 2020-11-24 stsp
7071 6458efa5 2020-11-24 stsp if (limit == 0)
7072 6458efa5 2020-11-24 stsp return NULL;
7073 6458efa5 2020-11-24 stsp
7074 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7075 6458efa5 2020-11-24 stsp
7076 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7077 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7078 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7079 6458efa5 2020-11-24 stsp
7080 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7081 6458efa5 2020-11-24 stsp if (err) {
7082 6458efa5 2020-11-24 stsp free(line);
7083 6458efa5 2020-11-24 stsp return err;
7084 6458efa5 2020-11-24 stsp }
7085 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7086 6458efa5 2020-11-24 stsp wstandout(view->window);
7087 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7088 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7089 6458efa5 2020-11-24 stsp wstandend(view->window);
7090 6458efa5 2020-11-24 stsp free(wline);
7091 6458efa5 2020-11-24 stsp wline = NULL;
7092 6458efa5 2020-11-24 stsp free(line);
7093 6458efa5 2020-11-24 stsp line = NULL;
7094 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7095 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7096 6458efa5 2020-11-24 stsp if (--limit <= 0)
7097 6458efa5 2020-11-24 stsp return NULL;
7098 6458efa5 2020-11-24 stsp
7099 6458efa5 2020-11-24 stsp n = 0;
7100 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7101 6458efa5 2020-11-24 stsp char *line = NULL;
7102 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7103 6458efa5 2020-11-24 stsp
7104 b4996bee 2022-06-16 stsp if (s->show_date) {
7105 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7106 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7107 b4996bee 2022-06-16 stsp struct got_object_id *id;
7108 b4996bee 2022-06-16 stsp struct tm tm;
7109 b4996bee 2022-06-16 stsp time_t t;
7110 b4996bee 2022-06-16 stsp
7111 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7112 b4996bee 2022-06-16 stsp if (err)
7113 b4996bee 2022-06-16 stsp return err;
7114 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7115 b4996bee 2022-06-16 stsp if (err) {
7116 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7117 b4996bee 2022-06-16 stsp free(id);
7118 b4996bee 2022-06-16 stsp return err;
7119 b4996bee 2022-06-16 stsp }
7120 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7121 b4996bee 2022-06-16 stsp id);
7122 b4996bee 2022-06-16 stsp if (err) {
7123 b4996bee 2022-06-16 stsp free(id);
7124 b4996bee 2022-06-16 stsp return err;
7125 b4996bee 2022-06-16 stsp }
7126 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7127 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7128 b4996bee 2022-06-16 stsp } else {
7129 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7130 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7131 b4996bee 2022-06-16 stsp }
7132 b4996bee 2022-06-16 stsp free(id);
7133 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7134 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7135 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7136 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7137 b4996bee 2022-06-16 stsp }
7138 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7139 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7140 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7141 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7142 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7143 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7144 6458efa5 2020-11-24 stsp struct got_object_id *id;
7145 6458efa5 2020-11-24 stsp char *id_str;
7146 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7147 6458efa5 2020-11-24 stsp if (err)
7148 6458efa5 2020-11-24 stsp return err;
7149 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7150 6458efa5 2020-11-24 stsp if (err) {
7151 6458efa5 2020-11-24 stsp free(id);
7152 6458efa5 2020-11-24 stsp return err;
7153 6458efa5 2020-11-24 stsp }
7154 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7155 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7156 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7157 6458efa5 2020-11-24 stsp free(id);
7158 6458efa5 2020-11-24 stsp free(id_str);
7159 6458efa5 2020-11-24 stsp return err;
7160 6458efa5 2020-11-24 stsp }
7161 6458efa5 2020-11-24 stsp free(id);
7162 6458efa5 2020-11-24 stsp free(id_str);
7163 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7164 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7165 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7166 6458efa5 2020-11-24 stsp
7167 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7168 ccda2f4d 2022-06-16 stsp 0, 0);
7169 6458efa5 2020-11-24 stsp if (err) {
7170 6458efa5 2020-11-24 stsp free(line);
7171 6458efa5 2020-11-24 stsp return err;
7172 6458efa5 2020-11-24 stsp }
7173 6458efa5 2020-11-24 stsp if (n == s->selected) {
7174 6458efa5 2020-11-24 stsp if (view->focussed)
7175 6458efa5 2020-11-24 stsp wstandout(view->window);
7176 6458efa5 2020-11-24 stsp s->selected_entry = re;
7177 6458efa5 2020-11-24 stsp }
7178 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7179 6458efa5 2020-11-24 stsp if (tc)
7180 6458efa5 2020-11-24 stsp wattr_on(view->window,
7181 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7182 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7183 6458efa5 2020-11-24 stsp if (tc)
7184 6458efa5 2020-11-24 stsp wattr_off(view->window,
7185 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7186 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7187 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7188 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7189 6458efa5 2020-11-24 stsp wstandend(view->window);
7190 6458efa5 2020-11-24 stsp free(line);
7191 6458efa5 2020-11-24 stsp free(wline);
7192 6458efa5 2020-11-24 stsp wline = NULL;
7193 6458efa5 2020-11-24 stsp n++;
7194 6458efa5 2020-11-24 stsp s->ndisplayed++;
7195 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7196 6458efa5 2020-11-24 stsp
7197 6458efa5 2020-11-24 stsp limit--;
7198 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7199 6458efa5 2020-11-24 stsp }
7200 6458efa5 2020-11-24 stsp
7201 9b058f45 2022-06-30 mark view_border(view);
7202 6458efa5 2020-11-24 stsp return err;
7203 6458efa5 2020-11-24 stsp }
7204 6458efa5 2020-11-24 stsp
7205 6458efa5 2020-11-24 stsp static const struct got_error *
7206 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7207 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7208 c42c9805 2020-11-24 stsp {
7209 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7210 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7211 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7212 c42c9805 2020-11-24 stsp
7213 c42c9805 2020-11-24 stsp *new_view = NULL;
7214 c42c9805 2020-11-24 stsp
7215 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7216 c42c9805 2020-11-24 stsp if (err) {
7217 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7218 c42c9805 2020-11-24 stsp return err;
7219 c42c9805 2020-11-24 stsp else
7220 c42c9805 2020-11-24 stsp return NULL;
7221 c42c9805 2020-11-24 stsp }
7222 c42c9805 2020-11-24 stsp
7223 c42c9805 2020-11-24 stsp
7224 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7225 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7226 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7227 c42c9805 2020-11-24 stsp goto done;
7228 c42c9805 2020-11-24 stsp }
7229 c42c9805 2020-11-24 stsp
7230 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7231 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7232 c42c9805 2020-11-24 stsp if (err)
7233 c42c9805 2020-11-24 stsp goto done;
7234 c42c9805 2020-11-24 stsp
7235 c42c9805 2020-11-24 stsp *new_view = tree_view;
7236 c42c9805 2020-11-24 stsp done:
7237 c42c9805 2020-11-24 stsp free(commit_id);
7238 c42c9805 2020-11-24 stsp return err;
7239 c42c9805 2020-11-24 stsp }
7240 c42c9805 2020-11-24 stsp static const struct got_error *
7241 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7242 6458efa5 2020-11-24 stsp {
7243 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7244 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7245 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7246 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7247 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7248 6458efa5 2020-11-24 stsp
7249 6458efa5 2020-11-24 stsp switch (ch) {
7250 6458efa5 2020-11-24 stsp case 'i':
7251 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7252 640cd7ff 2022-06-22 mark view->count = 0;
7253 7f66531d 2021-11-16 stsp break;
7254 b4996bee 2022-06-16 stsp case 'm':
7255 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7256 640cd7ff 2022-06-22 mark view->count = 0;
7257 b4996bee 2022-06-16 stsp break;
7258 07a065fe 2021-11-20 stsp case 'o':
7259 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7260 640cd7ff 2022-06-22 mark view->count = 0;
7261 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7262 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7263 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7264 50617b77 2021-11-20 stsp if (err)
7265 50617b77 2021-11-20 stsp break;
7266 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7267 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7268 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7269 7f66531d 2021-11-16 stsp if (err)
7270 7f66531d 2021-11-16 stsp break;
7271 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7272 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7273 6458efa5 2020-11-24 stsp break;
7274 6458efa5 2020-11-24 stsp case KEY_ENTER:
7275 6458efa5 2020-11-24 stsp case '\r':
7276 640cd7ff 2022-06-22 mark view->count = 0;
7277 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7278 6458efa5 2020-11-24 stsp break;
7279 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7280 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7281 9b058f45 2022-06-30 mark
7282 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7283 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7284 9b058f45 2022-06-30 mark if (err)
7285 9b058f45 2022-06-30 mark break;
7286 9b058f45 2022-06-30 mark
7287 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7288 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7289 9b058f45 2022-06-30 mark if (err)
7290 9b058f45 2022-06-30 mark break;
7291 9b058f45 2022-06-30 mark }
7292 9b058f45 2022-06-30 mark
7293 e78dc838 2020-12-04 stsp view->focussed = 0;
7294 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7295 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7296 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7297 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7298 6458efa5 2020-11-24 stsp err = view_close_child(view);
7299 6458efa5 2020-11-24 stsp if (err)
7300 6458efa5 2020-11-24 stsp return err;
7301 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7302 0dbbbe90 2022-06-17 op if (err)
7303 0dbbbe90 2022-06-17 op return err;
7304 e78dc838 2020-12-04 stsp view->focus_child = 1;
7305 6458efa5 2020-11-24 stsp } else
7306 6458efa5 2020-11-24 stsp *new_view = log_view;
7307 6458efa5 2020-11-24 stsp break;
7308 c42c9805 2020-11-24 stsp case 't':
7309 640cd7ff 2022-06-22 mark view->count = 0;
7310 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7311 c42c9805 2020-11-24 stsp break;
7312 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7313 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7314 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7315 c42c9805 2020-11-24 stsp s->repo);
7316 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7317 c42c9805 2020-11-24 stsp break;
7318 e78dc838 2020-12-04 stsp view->focussed = 0;
7319 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7320 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7321 c42c9805 2020-11-24 stsp err = view_close_child(view);
7322 c42c9805 2020-11-24 stsp if (err)
7323 c42c9805 2020-11-24 stsp return err;
7324 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7325 0dbbbe90 2022-06-17 op if (err)
7326 0dbbbe90 2022-06-17 op return err;
7327 e78dc838 2020-12-04 stsp view->focus_child = 1;
7328 c42c9805 2020-11-24 stsp } else
7329 c42c9805 2020-11-24 stsp *new_view = tree_view;
7330 c42c9805 2020-11-24 stsp break;
7331 e4526bf5 2021-09-03 naddy case 'g':
7332 e4526bf5 2021-09-03 naddy case KEY_HOME:
7333 e4526bf5 2021-09-03 naddy s->selected = 0;
7334 640cd7ff 2022-06-22 mark view->count = 0;
7335 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7336 e4526bf5 2021-09-03 naddy break;
7337 e4526bf5 2021-09-03 naddy case 'G':
7338 9b058f45 2022-06-30 mark case KEY_END: {
7339 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7340 9b058f45 2022-06-30 mark
7341 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7342 9b058f45 2022-06-30 mark --eos; /* border */
7343 e4526bf5 2021-09-03 naddy s->selected = 0;
7344 640cd7ff 2022-06-22 mark view->count = 0;
7345 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7346 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7347 e4526bf5 2021-09-03 naddy if (re == NULL)
7348 e4526bf5 2021-09-03 naddy break;
7349 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7350 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7351 e4526bf5 2021-09-03 naddy }
7352 e4526bf5 2021-09-03 naddy if (n > 0)
7353 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7354 e4526bf5 2021-09-03 naddy break;
7355 9b058f45 2022-06-30 mark }
7356 6458efa5 2020-11-24 stsp case 'k':
7357 6458efa5 2020-11-24 stsp case KEY_UP:
7358 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7359 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7360 6458efa5 2020-11-24 stsp s->selected--;
7361 6458efa5 2020-11-24 stsp break;
7362 34ba6917 2020-11-30 stsp }
7363 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7364 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7365 640cd7ff 2022-06-22 mark view->count = 0;
7366 6458efa5 2020-11-24 stsp break;
7367 83cc4199 2022-06-13 stsp case CTRL('u'):
7368 33c3719a 2022-06-15 stsp case 'u':
7369 83cc4199 2022-06-13 stsp nscroll /= 2;
7370 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7371 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7372 6458efa5 2020-11-24 stsp case CTRL('b'):
7373 61417565 2022-06-20 mark case 'b':
7374 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7375 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7376 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7377 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7378 640cd7ff 2022-06-22 mark view->count = 0;
7379 6458efa5 2020-11-24 stsp break;
7380 6458efa5 2020-11-24 stsp case 'j':
7381 6458efa5 2020-11-24 stsp case KEY_DOWN:
7382 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7383 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7384 6458efa5 2020-11-24 stsp s->selected++;
7385 6458efa5 2020-11-24 stsp break;
7386 6458efa5 2020-11-24 stsp }
7387 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7388 6458efa5 2020-11-24 stsp /* can't scroll any further */
7389 640cd7ff 2022-06-22 mark view->count = 0;
7390 6458efa5 2020-11-24 stsp break;
7391 640cd7ff 2022-06-22 mark }
7392 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7393 6458efa5 2020-11-24 stsp break;
7394 83cc4199 2022-06-13 stsp case CTRL('d'):
7395 33c3719a 2022-06-15 stsp case 'd':
7396 83cc4199 2022-06-13 stsp nscroll /= 2;
7397 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7398 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7399 6458efa5 2020-11-24 stsp case CTRL('f'):
7400 61417565 2022-06-20 mark case 'f':
7401 48bb96f0 2022-06-20 naddy case ' ':
7402 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7403 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7404 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7405 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7406 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7407 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7408 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7409 640cd7ff 2022-06-22 mark view->count = 0;
7410 6458efa5 2020-11-24 stsp break;
7411 6458efa5 2020-11-24 stsp }
7412 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7413 6458efa5 2020-11-24 stsp break;
7414 6458efa5 2020-11-24 stsp case CTRL('l'):
7415 640cd7ff 2022-06-22 mark view->count = 0;
7416 8924d611 2020-12-26 stsp tog_free_refs();
7417 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7418 8924d611 2020-12-26 stsp if (err)
7419 8924d611 2020-12-26 stsp break;
7420 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7421 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7422 6458efa5 2020-11-24 stsp break;
7423 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7424 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7425 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7426 6458efa5 2020-11-24 stsp break;
7427 6458efa5 2020-11-24 stsp default:
7428 640cd7ff 2022-06-22 mark view->count = 0;
7429 6458efa5 2020-11-24 stsp break;
7430 6458efa5 2020-11-24 stsp }
7431 6458efa5 2020-11-24 stsp
7432 6458efa5 2020-11-24 stsp return err;
7433 6458efa5 2020-11-24 stsp }
7434 6458efa5 2020-11-24 stsp
7435 6458efa5 2020-11-24 stsp __dead static void
7436 6458efa5 2020-11-24 stsp usage_ref(void)
7437 6458efa5 2020-11-24 stsp {
7438 6458efa5 2020-11-24 stsp endwin();
7439 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7440 6458efa5 2020-11-24 stsp getprogname());
7441 6458efa5 2020-11-24 stsp exit(1);
7442 6458efa5 2020-11-24 stsp }
7443 6458efa5 2020-11-24 stsp
7444 6458efa5 2020-11-24 stsp static const struct got_error *
7445 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7446 6458efa5 2020-11-24 stsp {
7447 6458efa5 2020-11-24 stsp const struct got_error *error;
7448 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7449 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7450 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7451 6458efa5 2020-11-24 stsp int ch;
7452 6458efa5 2020-11-24 stsp struct tog_view *view;
7453 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7454 6458efa5 2020-11-24 stsp
7455 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7456 6458efa5 2020-11-24 stsp switch (ch) {
7457 6458efa5 2020-11-24 stsp case 'r':
7458 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7459 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7460 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7461 6458efa5 2020-11-24 stsp optarg);
7462 6458efa5 2020-11-24 stsp break;
7463 6458efa5 2020-11-24 stsp default:
7464 e99e2d15 2020-11-24 naddy usage_ref();
7465 6458efa5 2020-11-24 stsp /* NOTREACHED */
7466 6458efa5 2020-11-24 stsp }
7467 6458efa5 2020-11-24 stsp }
7468 6458efa5 2020-11-24 stsp
7469 6458efa5 2020-11-24 stsp argc -= optind;
7470 6458efa5 2020-11-24 stsp argv += optind;
7471 6458efa5 2020-11-24 stsp
7472 6458efa5 2020-11-24 stsp if (argc > 1)
7473 e99e2d15 2020-11-24 naddy usage_ref();
7474 0ae84acc 2022-06-15 tracey
7475 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7476 0ae84acc 2022-06-15 tracey if (error != NULL)
7477 0ae84acc 2022-06-15 tracey goto done;
7478 6458efa5 2020-11-24 stsp
7479 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7480 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7481 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7482 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7483 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7484 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7485 c156c7a4 2020-12-18 stsp goto done;
7486 6458efa5 2020-11-24 stsp if (worktree)
7487 6458efa5 2020-11-24 stsp repo_path =
7488 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7489 6458efa5 2020-11-24 stsp else
7490 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7491 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7492 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7493 c156c7a4 2020-12-18 stsp goto done;
7494 c156c7a4 2020-12-18 stsp }
7495 6458efa5 2020-11-24 stsp }
7496 6458efa5 2020-11-24 stsp
7497 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7498 6458efa5 2020-11-24 stsp if (error != NULL)
7499 6458efa5 2020-11-24 stsp goto done;
7500 6458efa5 2020-11-24 stsp
7501 6458efa5 2020-11-24 stsp init_curses();
7502 6458efa5 2020-11-24 stsp
7503 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7504 51a10b52 2020-12-26 stsp if (error)
7505 51a10b52 2020-12-26 stsp goto done;
7506 51a10b52 2020-12-26 stsp
7507 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7508 6458efa5 2020-11-24 stsp if (error)
7509 6458efa5 2020-11-24 stsp goto done;
7510 6458efa5 2020-11-24 stsp
7511 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7512 6458efa5 2020-11-24 stsp if (view == NULL) {
7513 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7514 6458efa5 2020-11-24 stsp goto done;
7515 6458efa5 2020-11-24 stsp }
7516 6458efa5 2020-11-24 stsp
7517 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7518 6458efa5 2020-11-24 stsp if (error)
7519 6458efa5 2020-11-24 stsp goto done;
7520 6458efa5 2020-11-24 stsp
7521 6458efa5 2020-11-24 stsp if (worktree) {
7522 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7523 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7524 6458efa5 2020-11-24 stsp worktree = NULL;
7525 6458efa5 2020-11-24 stsp }
7526 6458efa5 2020-11-24 stsp error = view_loop(view);
7527 6458efa5 2020-11-24 stsp done:
7528 6458efa5 2020-11-24 stsp free(repo_path);
7529 6458efa5 2020-11-24 stsp free(cwd);
7530 1d0f4054 2021-06-17 stsp if (repo) {
7531 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7532 1d0f4054 2021-06-17 stsp if (close_err)
7533 1d0f4054 2021-06-17 stsp error = close_err;
7534 1d0f4054 2021-06-17 stsp }
7535 0ae84acc 2022-06-15 tracey if (pack_fds) {
7536 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7537 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7538 0ae84acc 2022-06-15 tracey if (error == NULL)
7539 0ae84acc 2022-06-15 tracey error = pack_err;
7540 0ae84acc 2022-06-15 tracey }
7541 51a10b52 2020-12-26 stsp tog_free_refs();
7542 6458efa5 2020-11-24 stsp return error;
7543 6458efa5 2020-11-24 stsp }
7544 6458efa5 2020-11-24 stsp
7545 9b058f45 2022-06-30 mark /*
7546 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7547 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7548 9b058f45 2022-06-30 mark */
7549 6458efa5 2020-11-24 stsp static void
7550 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7551 9b058f45 2022-06-30 mark {
7552 9b058f45 2022-06-30 mark switch (view->type) {
7553 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7554 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7555 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7556 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7557 9b058f45 2022-06-30 mark 1);
7558 9b058f45 2022-06-30 mark break;
7559 9b058f45 2022-06-30 mark }
7560 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7561 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7562 9b058f45 2022-06-30 mark else
7563 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7564 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7565 9b058f45 2022-06-30 mark break;
7566 9b058f45 2022-06-30 mark }
7567 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7568 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7569 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7570 9b058f45 2022-06-30 mark break;
7571 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7572 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7573 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7574 9b058f45 2022-06-30 mark break;
7575 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7576 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7577 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7578 9b058f45 2022-06-30 mark break;
7579 9b058f45 2022-06-30 mark default:
7580 9b058f45 2022-06-30 mark break;
7581 9b058f45 2022-06-30 mark }
7582 9b058f45 2022-06-30 mark
7583 9b058f45 2022-06-30 mark view->offset = 0;
7584 9b058f45 2022-06-30 mark }
7585 9b058f45 2022-06-30 mark
7586 9b058f45 2022-06-30 mark /*
7587 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7588 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7589 9b058f45 2022-06-30 mark */
7590 9b058f45 2022-06-30 mark static const struct got_error *
7591 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7592 9b058f45 2022-06-30 mark {
7593 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7594 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7595 9b058f45 2022-06-30 mark int *selected = NULL;
7596 9b058f45 2022-06-30 mark int header, offset;
7597 9b058f45 2022-06-30 mark
7598 9b058f45 2022-06-30 mark switch (view->type) {
7599 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7600 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7601 9b058f45 2022-06-30 mark header = 3;
7602 9b058f45 2022-06-30 mark scrolld = NULL;
7603 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7604 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7605 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7606 9b058f45 2022-06-30 mark s->selected_line -= offset;
7607 9b058f45 2022-06-30 mark view->offset = offset;
7608 9b058f45 2022-06-30 mark }
7609 9b058f45 2022-06-30 mark break;
7610 9b058f45 2022-06-30 mark }
7611 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7612 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7613 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7614 9b058f45 2022-06-30 mark header = 3;
7615 9b058f45 2022-06-30 mark selected = &s->selected;
7616 9b058f45 2022-06-30 mark break;
7617 9b058f45 2022-06-30 mark }
7618 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7619 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7620 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7621 9b058f45 2022-06-30 mark header = 3;
7622 9b058f45 2022-06-30 mark selected = &s->selected;
7623 9b058f45 2022-06-30 mark break;
7624 9b058f45 2022-06-30 mark }
7625 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7626 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7627 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7628 9b058f45 2022-06-30 mark header = 5;
7629 9b058f45 2022-06-30 mark selected = &s->selected;
7630 9b058f45 2022-06-30 mark break;
7631 9b058f45 2022-06-30 mark }
7632 9b058f45 2022-06-30 mark default:
7633 9b058f45 2022-06-30 mark selected = NULL;
7634 9b058f45 2022-06-30 mark scrolld = NULL;
7635 9b058f45 2022-06-30 mark header = 0;
7636 9b058f45 2022-06-30 mark break;
7637 9b058f45 2022-06-30 mark }
7638 9b058f45 2022-06-30 mark
7639 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7640 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7641 9b058f45 2022-06-30 mark view->offset = offset;
7642 9b058f45 2022-06-30 mark if (scrolld && offset) {
7643 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7644 9b058f45 2022-06-30 mark *selected -= offset;
7645 9b058f45 2022-06-30 mark }
7646 9b058f45 2022-06-30 mark }
7647 9b058f45 2022-06-30 mark
7648 9b058f45 2022-06-30 mark return err;
7649 9b058f45 2022-06-30 mark }
7650 9b058f45 2022-06-30 mark
7651 9b058f45 2022-06-30 mark static void
7652 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7653 ce5b7c56 2019-07-09 stsp {
7654 6059809a 2020-12-17 stsp size_t i;
7655 9f7d7167 2018-04-29 stsp
7656 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7657 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7658 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7659 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7660 ce5b7c56 2019-07-09 stsp }
7661 6879ba42 2020-10-01 naddy fputc('\n', fp);
7662 ce5b7c56 2019-07-09 stsp }
7663 ce5b7c56 2019-07-09 stsp
7664 4ed7e80c 2018-05-20 stsp __dead static void
7665 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7666 9f7d7167 2018-04-29 stsp {
7667 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7668 6879ba42 2020-10-01 naddy
7669 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7670 6879ba42 2020-10-01 naddy getprogname());
7671 6879ba42 2020-10-01 naddy if (hflag) {
7672 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7673 6879ba42 2020-10-01 naddy list_commands(fp);
7674 ee85c5e8 2020-02-29 stsp }
7675 6879ba42 2020-10-01 naddy exit(status);
7676 9f7d7167 2018-04-29 stsp }
7677 9f7d7167 2018-04-29 stsp
7678 c2301be8 2018-04-30 stsp static char **
7679 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7680 c2301be8 2018-04-30 stsp {
7681 ee85c5e8 2020-02-29 stsp va_list ap;
7682 c2301be8 2018-04-30 stsp char **argv;
7683 ee85c5e8 2020-02-29 stsp int i;
7684 c2301be8 2018-04-30 stsp
7685 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7686 ee85c5e8 2020-02-29 stsp
7687 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7688 c2301be8 2018-04-30 stsp if (argv == NULL)
7689 c2301be8 2018-04-30 stsp err(1, "calloc");
7690 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7691 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7692 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7693 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7694 c2301be8 2018-04-30 stsp }
7695 c2301be8 2018-04-30 stsp
7696 ee85c5e8 2020-02-29 stsp va_end(ap);
7697 c2301be8 2018-04-30 stsp return argv;
7698 ee85c5e8 2020-02-29 stsp }
7699 ee85c5e8 2020-02-29 stsp
7700 ee85c5e8 2020-02-29 stsp /*
7701 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7702 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7703 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7704 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7705 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7706 ee85c5e8 2020-02-29 stsp */
7707 ee85c5e8 2020-02-29 stsp static const struct got_error *
7708 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7709 ee85c5e8 2020-02-29 stsp {
7710 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7711 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7712 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7713 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7714 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7715 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7716 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7717 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7718 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7719 84de9106 2020-12-26 stsp
7720 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7721 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7722 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7723 ee85c5e8 2020-02-29 stsp
7724 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7725 0ae84acc 2022-06-15 tracey if (error != NULL)
7726 0ae84acc 2022-06-15 tracey goto done;
7727 0ae84acc 2022-06-15 tracey
7728 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7729 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7730 ee85c5e8 2020-02-29 stsp goto done;
7731 ee85c5e8 2020-02-29 stsp
7732 ee85c5e8 2020-02-29 stsp if (worktree)
7733 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7734 ee85c5e8 2020-02-29 stsp else
7735 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7736 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7737 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7738 ee85c5e8 2020-02-29 stsp goto done;
7739 ee85c5e8 2020-02-29 stsp }
7740 ee85c5e8 2020-02-29 stsp
7741 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7742 ee85c5e8 2020-02-29 stsp if (error != NULL)
7743 ee85c5e8 2020-02-29 stsp goto done;
7744 ee85c5e8 2020-02-29 stsp
7745 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7746 ee85c5e8 2020-02-29 stsp repo, worktree);
7747 ee85c5e8 2020-02-29 stsp if (error)
7748 ee85c5e8 2020-02-29 stsp goto done;
7749 ee85c5e8 2020-02-29 stsp
7750 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7751 84de9106 2020-12-26 stsp if (error)
7752 84de9106 2020-12-26 stsp goto done;
7753 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7754 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7755 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7756 ee85c5e8 2020-02-29 stsp if (error)
7757 ee85c5e8 2020-02-29 stsp goto done;
7758 ee85c5e8 2020-02-29 stsp
7759 ee85c5e8 2020-02-29 stsp if (worktree) {
7760 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7761 ee85c5e8 2020-02-29 stsp worktree = NULL;
7762 ee85c5e8 2020-02-29 stsp }
7763 ee85c5e8 2020-02-29 stsp
7764 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7765 a44927cc 2022-04-07 stsp if (error)
7766 a44927cc 2022-04-07 stsp goto done;
7767 a44927cc 2022-04-07 stsp
7768 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7769 ee85c5e8 2020-02-29 stsp if (error) {
7770 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7771 ee85c5e8 2020-02-29 stsp goto done;
7772 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7773 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7774 6879ba42 2020-10-01 naddy usage(1, 1);
7775 ee85c5e8 2020-02-29 stsp /* not reached */
7776 ee85c5e8 2020-02-29 stsp }
7777 ee85c5e8 2020-02-29 stsp
7778 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7779 1d0f4054 2021-06-17 stsp if (error == NULL)
7780 1d0f4054 2021-06-17 stsp error = close_err;
7781 ee85c5e8 2020-02-29 stsp repo = NULL;
7782 ee85c5e8 2020-02-29 stsp
7783 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7784 ee85c5e8 2020-02-29 stsp if (error)
7785 ee85c5e8 2020-02-29 stsp goto done;
7786 ee85c5e8 2020-02-29 stsp
7787 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7788 ee85c5e8 2020-02-29 stsp argc = 4;
7789 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7790 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7791 ee85c5e8 2020-02-29 stsp done:
7792 1d0f4054 2021-06-17 stsp if (repo) {
7793 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7794 1d0f4054 2021-06-17 stsp if (error == NULL)
7795 1d0f4054 2021-06-17 stsp error = close_err;
7796 1d0f4054 2021-06-17 stsp }
7797 a44927cc 2022-04-07 stsp if (commit)
7798 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7799 ee85c5e8 2020-02-29 stsp if (worktree)
7800 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7801 0ae84acc 2022-06-15 tracey if (pack_fds) {
7802 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7803 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7804 0ae84acc 2022-06-15 tracey if (error == NULL)
7805 0ae84acc 2022-06-15 tracey error = pack_err;
7806 0ae84acc 2022-06-15 tracey }
7807 ee85c5e8 2020-02-29 stsp free(id);
7808 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7809 ee85c5e8 2020-02-29 stsp free(commit_id);
7810 ee85c5e8 2020-02-29 stsp free(cwd);
7811 ee85c5e8 2020-02-29 stsp free(repo_path);
7812 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7813 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7814 ee85c5e8 2020-02-29 stsp int i;
7815 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7816 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7817 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7818 ee85c5e8 2020-02-29 stsp }
7819 87670572 2020-12-26 naddy tog_free_refs();
7820 ee85c5e8 2020-02-29 stsp return error;
7821 c2301be8 2018-04-30 stsp }
7822 c2301be8 2018-04-30 stsp
7823 9f7d7167 2018-04-29 stsp int
7824 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7825 9f7d7167 2018-04-29 stsp {
7826 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7827 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7828 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7829 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7830 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7831 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7832 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7833 83cd27f8 2020-01-13 stsp };
7834 9f7d7167 2018-04-29 stsp
7835 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7836 9f7d7167 2018-04-29 stsp
7837 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7838 9f7d7167 2018-04-29 stsp switch (ch) {
7839 9f7d7167 2018-04-29 stsp case 'h':
7840 9f7d7167 2018-04-29 stsp hflag = 1;
7841 9f7d7167 2018-04-29 stsp break;
7842 53ccebc2 2019-07-30 stsp case 'V':
7843 53ccebc2 2019-07-30 stsp Vflag = 1;
7844 53ccebc2 2019-07-30 stsp break;
7845 9f7d7167 2018-04-29 stsp default:
7846 6879ba42 2020-10-01 naddy usage(hflag, 1);
7847 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7848 9f7d7167 2018-04-29 stsp }
7849 9f7d7167 2018-04-29 stsp }
7850 9f7d7167 2018-04-29 stsp
7851 9f7d7167 2018-04-29 stsp argc -= optind;
7852 9f7d7167 2018-04-29 stsp argv += optind;
7853 9814e6a3 2020-09-27 naddy optind = 1;
7854 c2301be8 2018-04-30 stsp optreset = 1;
7855 9f7d7167 2018-04-29 stsp
7856 53ccebc2 2019-07-30 stsp if (Vflag) {
7857 53ccebc2 2019-07-30 stsp got_version_print_str();
7858 6879ba42 2020-10-01 naddy return 0;
7859 53ccebc2 2019-07-30 stsp }
7860 4010e238 2020-12-04 stsp
7861 4010e238 2020-12-04 stsp #ifndef PROFILE
7862 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7863 4010e238 2020-12-04 stsp NULL) == -1)
7864 4010e238 2020-12-04 stsp err(1, "pledge");
7865 4010e238 2020-12-04 stsp #endif
7866 53ccebc2 2019-07-30 stsp
7867 c2301be8 2018-04-30 stsp if (argc == 0) {
7868 f29d3e89 2018-06-23 stsp if (hflag)
7869 6879ba42 2020-10-01 naddy usage(hflag, 0);
7870 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7871 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7872 c2301be8 2018-04-30 stsp argc = 1;
7873 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7874 c2301be8 2018-04-30 stsp } else {
7875 6059809a 2020-12-17 stsp size_t i;
7876 9f7d7167 2018-04-29 stsp
7877 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7878 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7879 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7880 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7881 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7882 9f7d7167 2018-04-29 stsp break;
7883 9f7d7167 2018-04-29 stsp }
7884 9f7d7167 2018-04-29 stsp }
7885 ee85c5e8 2020-02-29 stsp }
7886 3642c4c6 2019-07-09 stsp
7887 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7888 ee85c5e8 2020-02-29 stsp if (argc != 1)
7889 6879ba42 2020-10-01 naddy usage(0, 1);
7890 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7891 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7892 ee85c5e8 2020-02-29 stsp } else {
7893 ee85c5e8 2020-02-29 stsp if (hflag)
7894 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7895 ee85c5e8 2020-02-29 stsp else
7896 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7897 9f7d7167 2018-04-29 stsp }
7898 9f7d7167 2018-04-29 stsp
7899 9f7d7167 2018-04-29 stsp endwin();
7900 b46c1e04 2020-09-20 naddy putchar('\n');
7901 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7902 a2f4a359 2020-02-28 stsp int i;
7903 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7904 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7905 a2f4a359 2020-02-28 stsp free(cmd_argv);
7906 a2f4a359 2020-02-28 stsp }
7907 a2f4a359 2020-02-28 stsp
7908 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7909 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7910 9f7d7167 2018-04-29 stsp return 0;
7911 9f7d7167 2018-04-29 stsp }