Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
112 9b058f45 2022-06-30 mark };
113 9b058f45 2022-06-30 mark
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
115 9b058f45 2022-06-30 mark
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 d6b05b5b 2018-08-04 stsp
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
122 1a76625f 2018-10-22 stsp int idx;
123 ba4f502b 2018-08-04 stsp };
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
128 6d17833f 2019-11-08 stsp };
129 6d17833f 2019-11-08 stsp
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
134 15a087fe 2019-02-21 stsp };
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 11b20872 2019-11-08 stsp
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
140 cc488aa7 2022-01-23 stsp
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp {
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp *cmp = -1;
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp *cmp = 1;
157 cc488aa7 2022-01-23 stsp return NULL;
158 cc488aa7 2022-01-23 stsp }
159 cc488aa7 2022-01-23 stsp
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
162 cc488aa7 2022-01-23 stsp }
163 51a10b52 2020-12-26 stsp
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp {
167 51a10b52 2020-12-26 stsp const struct got_error *err;
168 51a10b52 2020-12-26 stsp
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 7f66531d 2021-11-16 stsp repo);
172 51a10b52 2020-12-26 stsp if (err)
173 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 51a10b52 2020-12-26 stsp repo);
177 51a10b52 2020-12-26 stsp }
178 51a10b52 2020-12-26 stsp
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp {
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp }
188 51a10b52 2020-12-26 stsp
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp {
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp free(tc);
216 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp }
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp }
222 11b20872 2019-11-08 stsp
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp {
226 11b20872 2019-11-08 stsp struct tog_color *tc;
227 11b20872 2019-11-08 stsp
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
232 11b20872 2019-11-08 stsp free(tc);
233 11b20872 2019-11-08 stsp }
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
243 11b20872 2019-11-08 stsp return tc;
244 11b20872 2019-11-08 stsp }
245 11b20872 2019-11-08 stsp
246 11b20872 2019-11-08 stsp return NULL;
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 11b20872 2019-11-08 stsp static int
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
282 11b20872 2019-11-08 stsp
283 11b20872 2019-11-08 stsp return -1;
284 11b20872 2019-11-08 stsp }
285 11b20872 2019-11-08 stsp
286 11b20872 2019-11-08 stsp static int
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp {
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 15a087fe 2019-02-21 stsp
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return -1;
312 11b20872 2019-11-08 stsp
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
314 11b20872 2019-11-08 stsp }
315 11b20872 2019-11-08 stsp
316 11b20872 2019-11-08 stsp
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int eof;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp
335 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
336 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
337 b01e7d3b 2018-08-04 stsp };
338 b01e7d3b 2018-08-04 stsp
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 1a76625f 2018-10-22 stsp
341 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
342 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
343 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
344 1a76625f 2018-10-22 stsp int commits_needed;
345 fb280deb 2021-08-30 stsp int load_all;
346 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
347 1a76625f 2018-10-22 stsp struct commit_queue *commits;
348 1a76625f 2018-10-22 stsp const char *in_repo_path;
349 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
350 1a76625f 2018-10-22 stsp struct got_repository *repo;
351 74467cc8 2022-06-15 stsp int *pack_fds;
352 1a76625f 2018-10-22 stsp int log_complete;
353 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
354 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
356 13add988 2019-10-15 stsp int *searching;
357 13add988 2019-10-15 stsp int *search_next_done;
358 13add988 2019-10-15 stsp regex_t *regex;
359 1a76625f 2018-10-22 stsp };
360 1a76625f 2018-10-22 stsp
361 1a76625f 2018-10-22 stsp struct tog_log_view_state {
362 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
363 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
366 b01e7d3b 2018-08-04 stsp int selected;
367 b01e7d3b 2018-08-04 stsp char *in_repo_path;
368 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
369 b672a97a 2020-01-27 stsp int log_branches;
370 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
371 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
372 1a76625f 2018-10-22 stsp sig_atomic_t quit;
373 1a76625f 2018-10-22 stsp pthread_t thread;
374 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
375 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
376 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
377 11b20872 2019-11-08 stsp struct tog_colors colors;
378 ba4f502b 2018-08-04 stsp };
379 11b20872 2019-11-08 stsp
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
381 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
385 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
388 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
389 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
390 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
391 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
392 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
394 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
395 ba4f502b 2018-08-04 stsp
396 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
397 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
398 e9424729 2018-08-04 stsp int nlines;
399 e9424729 2018-08-04 stsp
400 e9424729 2018-08-04 stsp struct tog_view *view;
401 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
402 e9424729 2018-08-04 stsp int *quit;
403 e9424729 2018-08-04 stsp };
404 e9424729 2018-08-04 stsp
405 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
406 e9424729 2018-08-04 stsp const char *path;
407 e9424729 2018-08-04 stsp struct got_repository *repo;
408 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
409 e9424729 2018-08-04 stsp int *complete;
410 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
411 fc06ba56 2019-08-22 stsp void *cancel_arg;
412 e9424729 2018-08-04 stsp };
413 e9424729 2018-08-04 stsp
414 e9424729 2018-08-04 stsp struct tog_blame {
415 e9424729 2018-08-04 stsp FILE *f;
416 be659d10 2020-11-18 stsp off_t filesize;
417 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
418 6fcac457 2018-11-19 stsp int nlines;
419 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
420 e9424729 2018-08-04 stsp pthread_t thread;
421 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
423 e9424729 2018-08-04 stsp const char *path;
424 0ae84acc 2022-06-15 tracey int *pack_fds;
425 e9424729 2018-08-04 stsp };
426 e9424729 2018-08-04 stsp
427 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
428 7cbe629d 2018-08-04 stsp int first_displayed_line;
429 7cbe629d 2018-08-04 stsp int last_displayed_line;
430 7cbe629d 2018-08-04 stsp int selected_line;
431 7cbe629d 2018-08-04 stsp int blame_complete;
432 e5a0f69f 2018-08-18 stsp int eof;
433 e5a0f69f 2018-08-18 stsp int done;
434 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
435 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
436 e5a0f69f 2018-08-18 stsp char *path;
437 7cbe629d 2018-08-04 stsp struct got_repository *repo;
438 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
439 e9424729 2018-08-04 stsp struct tog_blame blame;
440 6c4c42e0 2019-06-24 stsp int matched_line;
441 11b20872 2019-11-08 stsp struct tog_colors colors;
442 ad80ab7b 2018-08-04 stsp };
443 ad80ab7b 2018-08-04 stsp
444 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
445 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
446 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
447 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
448 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
449 ad80ab7b 2018-08-04 stsp int selected;
450 ad80ab7b 2018-08-04 stsp };
451 ad80ab7b 2018-08-04 stsp
452 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
453 ad80ab7b 2018-08-04 stsp
454 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
455 ad80ab7b 2018-08-04 stsp char *tree_label;
456 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
457 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
458 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
459 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
460 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
461 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
462 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
463 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
464 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
465 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
466 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
467 6458efa5 2020-11-24 stsp struct tog_colors colors;
468 6458efa5 2020-11-24 stsp };
469 6458efa5 2020-11-24 stsp
470 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
471 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
472 6458efa5 2020-11-24 stsp struct got_reference *ref;
473 6458efa5 2020-11-24 stsp int idx;
474 6458efa5 2020-11-24 stsp };
475 6458efa5 2020-11-24 stsp
476 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
477 6458efa5 2020-11-24 stsp
478 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
479 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
482 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
483 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
484 6458efa5 2020-11-24 stsp struct got_repository *repo;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
486 bddb1296 2019-11-08 stsp struct tog_colors colors;
487 7cbe629d 2018-08-04 stsp };
488 7cbe629d 2018-08-04 stsp
489 669b5ffa 2018-10-07 stsp /*
490 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
491 669b5ffa 2018-10-07 stsp *
492 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
493 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
494 669b5ffa 2018-10-07 stsp * there is enough screen estate.
495 669b5ffa 2018-10-07 stsp *
496 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
497 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
498 669b5ffa 2018-10-07 stsp *
499 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
500 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
501 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
502 669b5ffa 2018-10-07 stsp *
503 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
504 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
505 669b5ffa 2018-10-07 stsp */
506 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
507 669b5ffa 2018-10-07 stsp
508 cc3c9aac 2018-08-01 stsp struct tog_view {
509 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
510 26ed57b2 2018-05-19 stsp WINDOW *window;
511 26ed57b2 2018-05-19 stsp PANEL *panel;
512 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
513 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
514 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
515 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
516 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
517 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
518 9970f7fc 2020-12-03 stsp int dying;
519 669b5ffa 2018-10-07 stsp struct tog_view *parent;
520 669b5ffa 2018-10-07 stsp struct tog_view *child;
521 5dc9f4bc 2018-08-04 stsp
522 e78dc838 2020-12-04 stsp /*
523 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
524 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
525 e78dc838 2020-12-04 stsp * between parent and child.
526 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
527 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
528 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
529 e78dc838 2020-12-04 stsp * situations.
530 e78dc838 2020-12-04 stsp */
531 e78dc838 2020-12-04 stsp int focus_child;
532 e78dc838 2020-12-04 stsp
533 9b058f45 2022-06-30 mark enum tog_view_mode mode;
534 5dc9f4bc 2018-08-04 stsp /* type-specific state */
535 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
536 5dc9f4bc 2018-08-04 stsp union {
537 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
538 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
539 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
540 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
541 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
542 5dc9f4bc 2018-08-04 stsp } state;
543 e5a0f69f 2018-08-18 stsp
544 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
545 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
546 e78dc838 2020-12-04 stsp struct tog_view *, int);
547 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
548 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
549 60493ae3 2019-06-20 stsp
550 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
551 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
552 c0c4acc8 2021-01-24 stsp int search_started;
553 60493ae3 2019-06-20 stsp int searching;
554 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
555 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
556 60493ae3 2019-06-20 stsp int search_next_done;
557 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
558 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
559 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
560 1803e47f 2019-06-22 stsp regex_t regex;
561 41605754 2020-11-12 stsp regmatch_t regmatch;
562 cc3c9aac 2018-08-01 stsp };
563 cd0acaa7 2018-05-20 stsp
564 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
565 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
566 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
567 78756c87 2020-11-24 stsp struct got_repository *);
568 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
572 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
573 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
574 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp
576 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
577 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
578 78756c87 2020-11-24 stsp const char *, const char *, int);
579 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
581 e78dc838 2020-12-04 stsp struct tog_view *, int);
582 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
583 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
584 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
585 e5a0f69f 2018-08-18 stsp
586 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
587 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
588 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
593 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
594 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
595 e5a0f69f 2018-08-18 stsp
596 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
597 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
598 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
600 e78dc838 2020-12-04 stsp struct tog_view *, int);
601 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
602 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
603 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
604 6458efa5 2020-11-24 stsp
605 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
606 6458efa5 2020-11-24 stsp struct got_repository *);
607 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
608 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
609 e78dc838 2020-12-04 stsp struct tog_view *, int);
610 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
611 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
613 25791caa 2018-10-24 stsp
614 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
615 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
616 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
617 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
618 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
619 25791caa 2018-10-24 stsp
620 25791caa 2018-10-24 stsp static void
621 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
622 25791caa 2018-10-24 stsp {
623 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
624 83baff54 2019-08-12 stsp }
625 83baff54 2019-08-12 stsp
626 83baff54 2019-08-12 stsp static void
627 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
628 83baff54 2019-08-12 stsp {
629 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
630 25791caa 2018-10-24 stsp }
631 26ed57b2 2018-05-19 stsp
632 61266923 2020-01-14 stsp static void
633 61266923 2020-01-14 stsp tog_sigcont(int signo)
634 61266923 2020-01-14 stsp {
635 61266923 2020-01-14 stsp tog_sigcont_received = 1;
636 61266923 2020-01-14 stsp }
637 61266923 2020-01-14 stsp
638 2497f032 2022-05-31 stsp static void
639 2497f032 2022-05-31 stsp tog_sigint(int signo)
640 2497f032 2022-05-31 stsp {
641 2497f032 2022-05-31 stsp tog_sigint_received = 1;
642 2497f032 2022-05-31 stsp }
643 2497f032 2022-05-31 stsp
644 2497f032 2022-05-31 stsp static void
645 2497f032 2022-05-31 stsp tog_sigterm(int signo)
646 2497f032 2022-05-31 stsp {
647 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
648 2497f032 2022-05-31 stsp }
649 2497f032 2022-05-31 stsp
650 2497f032 2022-05-31 stsp static int
651 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
652 2497f032 2022-05-31 stsp {
653 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
654 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
655 2497f032 2022-05-31 stsp }
656 2497f032 2022-05-31 stsp
657 e5a0f69f 2018-08-18 stsp static const struct got_error *
658 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
659 ea5e7bb5 2018-08-01 stsp {
660 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
661 e5a0f69f 2018-08-18 stsp
662 669b5ffa 2018-10-07 stsp if (view->child) {
663 669b5ffa 2018-10-07 stsp view_close(view->child);
664 669b5ffa 2018-10-07 stsp view->child = NULL;
665 669b5ffa 2018-10-07 stsp }
666 e5a0f69f 2018-08-18 stsp if (view->close)
667 e5a0f69f 2018-08-18 stsp err = view->close(view);
668 ea5e7bb5 2018-08-01 stsp if (view->panel)
669 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
670 ea5e7bb5 2018-08-01 stsp if (view->window)
671 ea5e7bb5 2018-08-01 stsp delwin(view->window);
672 ea5e7bb5 2018-08-01 stsp free(view);
673 e5a0f69f 2018-08-18 stsp return err;
674 ea5e7bb5 2018-08-01 stsp }
675 ea5e7bb5 2018-08-01 stsp
676 ea5e7bb5 2018-08-01 stsp static struct tog_view *
677 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
678 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
679 ea5e7bb5 2018-08-01 stsp {
680 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
681 ea5e7bb5 2018-08-01 stsp
682 ea5e7bb5 2018-08-01 stsp if (view == NULL)
683 ea5e7bb5 2018-08-01 stsp return NULL;
684 ea5e7bb5 2018-08-01 stsp
685 d6b05b5b 2018-08-04 stsp view->type = type;
686 f7d12f7e 2018-08-01 stsp view->lines = LINES;
687 f7d12f7e 2018-08-01 stsp view->cols = COLS;
688 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
689 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
690 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
691 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
692 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
693 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
694 96a765a8 2018-08-04 stsp view_close(view);
695 ea5e7bb5 2018-08-01 stsp return NULL;
696 ea5e7bb5 2018-08-01 stsp }
697 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
698 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
699 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
700 96a765a8 2018-08-04 stsp view_close(view);
701 ea5e7bb5 2018-08-01 stsp return NULL;
702 ea5e7bb5 2018-08-01 stsp }
703 ea5e7bb5 2018-08-01 stsp
704 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
705 ea5e7bb5 2018-08-01 stsp return view;
706 cdf1ee82 2018-08-01 stsp }
707 cdf1ee82 2018-08-01 stsp
708 0cf4efb1 2018-09-29 stsp static int
709 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
710 0cf4efb1 2018-09-29 stsp {
711 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
712 0cf4efb1 2018-09-29 stsp return 0;
713 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
714 5c60c32a 2018-10-18 stsp }
715 5c60c32a 2018-10-18 stsp
716 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
717 9b058f45 2022-06-30 mark static int
718 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
719 9b058f45 2022-06-30 mark {
720 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
721 9b058f45 2022-06-30 mark }
722 9b058f45 2022-06-30 mark
723 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
724 5c60c32a 2018-10-18 stsp
725 5c60c32a 2018-10-18 stsp static const struct got_error *
726 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
727 5c60c32a 2018-10-18 stsp {
728 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
729 5c60c32a 2018-10-18 stsp
730 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
731 9b058f45 2022-06-30 mark view->begin_y = view_split_begin_y(view->nlines);
732 9b058f45 2022-06-30 mark view->begin_x = 0;
733 9b058f45 2022-06-30 mark } else {
734 9b058f45 2022-06-30 mark view->begin_x = view_split_begin_x(0);
735 9b058f45 2022-06-30 mark view->begin_y = 0;
736 9b058f45 2022-06-30 mark }
737 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
738 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
739 5c60c32a 2018-10-18 stsp view->lines = LINES;
740 5c60c32a 2018-10-18 stsp view->cols = COLS;
741 5c60c32a 2018-10-18 stsp err = view_resize(view);
742 5c60c32a 2018-10-18 stsp if (err)
743 5c60c32a 2018-10-18 stsp return err;
744 5c60c32a 2018-10-18 stsp
745 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
746 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
747 9b058f45 2022-06-30 mark
748 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
749 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
750 5c60c32a 2018-10-18 stsp
751 5c60c32a 2018-10-18 stsp return NULL;
752 5c60c32a 2018-10-18 stsp }
753 5c60c32a 2018-10-18 stsp
754 5c60c32a 2018-10-18 stsp static const struct got_error *
755 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
756 5c60c32a 2018-10-18 stsp {
757 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
758 5c60c32a 2018-10-18 stsp
759 5c60c32a 2018-10-18 stsp view->begin_x = 0;
760 5c60c32a 2018-10-18 stsp view->begin_y = 0;
761 5c60c32a 2018-10-18 stsp view->nlines = LINES;
762 5c60c32a 2018-10-18 stsp view->ncols = COLS;
763 5c60c32a 2018-10-18 stsp view->lines = LINES;
764 5c60c32a 2018-10-18 stsp view->cols = COLS;
765 5c60c32a 2018-10-18 stsp err = view_resize(view);
766 5c60c32a 2018-10-18 stsp if (err)
767 5c60c32a 2018-10-18 stsp return err;
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
770 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
771 5c60c32a 2018-10-18 stsp
772 5c60c32a 2018-10-18 stsp return NULL;
773 0cf4efb1 2018-09-29 stsp }
774 0cf4efb1 2018-09-29 stsp
775 5c60c32a 2018-10-18 stsp static int
776 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
777 5c60c32a 2018-10-18 stsp {
778 5c60c32a 2018-10-18 stsp return view->parent == NULL;
779 5c60c32a 2018-10-18 stsp }
780 5c60c32a 2018-10-18 stsp
781 6131ff18 2022-06-20 mark static int
782 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
783 6131ff18 2022-06-20 mark {
784 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
785 24b9cfdc 2022-06-30 stsp }
786 24b9cfdc 2022-06-30 stsp
787 24b9cfdc 2022-06-30 stsp static int
788 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
789 24b9cfdc 2022-06-30 stsp {
790 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
791 6131ff18 2022-06-20 mark }
792 6131ff18 2022-06-20 mark
793 9b058f45 2022-06-30 mark static void
794 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
795 9b058f45 2022-06-30 mark {
796 9b058f45 2022-06-30 mark PANEL *panel;
797 9b058f45 2022-06-30 mark const struct tog_view *view_above;
798 6131ff18 2022-06-20 mark
799 9b058f45 2022-06-30 mark if (view->parent)
800 9b058f45 2022-06-30 mark return view_border(view->parent);
801 9b058f45 2022-06-30 mark
802 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
803 9b058f45 2022-06-30 mark if (panel == NULL)
804 9b058f45 2022-06-30 mark return;
805 9b058f45 2022-06-30 mark
806 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
807 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
808 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
809 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
810 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
811 9b058f45 2022-06-30 mark else
812 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
813 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
814 9b058f45 2022-06-30 mark }
815 9b058f45 2022-06-30 mark
816 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
817 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
818 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
819 9b058f45 2022-06-30 mark
820 4d8c2215 2018-08-19 stsp static const struct got_error *
821 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
822 f7d12f7e 2018-08-01 stsp {
823 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
824 9b058f45 2022-06-30 mark int dif, nlines, ncols;
825 f7d12f7e 2018-08-01 stsp
826 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
827 9b058f45 2022-06-30 mark
828 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
829 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
830 0cf4efb1 2018-09-29 stsp else
831 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
832 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
833 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
834 0cf4efb1 2018-09-29 stsp else
835 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
836 6d0fee91 2018-08-01 stsp
837 4dd27a72 2022-06-29 stsp if (view->child) {
838 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
839 9b058f45 2022-06-30 mark
840 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
841 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
842 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
843 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
844 0dbbbe90 2022-06-17 op ncols = COLS;
845 0dbbbe90 2022-06-17 op
846 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
847 5c60c32a 2018-10-18 stsp if (view->child->focussed)
848 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
849 5c60c32a 2018-10-18 stsp else
850 5c60c32a 2018-10-18 stsp show_panel(view->panel);
851 5c60c32a 2018-10-18 stsp } else {
852 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
853 0dbbbe90 2022-06-17 op
854 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
855 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
856 5c60c32a 2018-10-18 stsp }
857 9b058f45 2022-06-30 mark /*
858 9b058f45 2022-06-30 mark * Request commits if terminal height was increased in a log
859 9b058f45 2022-06-30 mark * view so we have enough commits loaded to populate the view.
860 9b058f45 2022-06-30 mark */
861 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG && dif > 0) {
862 9b058f45 2022-06-30 mark struct tog_log_view_state *ts = &view->state.log;
863 9b058f45 2022-06-30 mark
864 9b058f45 2022-06-30 mark if (ts->commits.ncommits < ts->selected_entry->idx +
865 9b058f45 2022-06-30 mark view->lines - ts->selected) {
866 9b058f45 2022-06-30 mark view->nscrolled = ts->selected_entry->idx +
867 9b058f45 2022-06-30 mark view->lines - ts->selected -
868 9b058f45 2022-06-30 mark ts->commits.ncommits + dif;
869 9b058f45 2022-06-30 mark err = request_log_commits(view);
870 9b058f45 2022-06-30 mark if (err)
871 9b058f45 2022-06-30 mark return err;
872 9b058f45 2022-06-30 mark }
873 9b058f45 2022-06-30 mark }
874 9b058f45 2022-06-30 mark
875 9b058f45 2022-06-30 mark /*
876 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
877 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
878 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
879 9b058f45 2022-06-30 mark */
880 9b058f45 2022-06-30 mark if (hs) {
881 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
882 9b058f45 2022-06-30 mark if (err)
883 9b058f45 2022-06-30 mark return err;
884 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
885 9b058f45 2022-06-30 mark err = offset_selection_down(view);
886 9b058f45 2022-06-30 mark if (err)
887 9b058f45 2022-06-30 mark return err;
888 9b058f45 2022-06-30 mark }
889 9b058f45 2022-06-30 mark view_border(view);
890 9b058f45 2022-06-30 mark update_panels();
891 9b058f45 2022-06-30 mark doupdate();
892 9b058f45 2022-06-30 mark show_panel(view->child->panel);
893 9b058f45 2022-06-30 mark nlines = view->nlines;
894 9b058f45 2022-06-30 mark }
895 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
896 0dbbbe90 2022-06-17 op ncols = COLS;
897 669b5ffa 2018-10-07 stsp
898 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
899 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
900 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
901 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
902 0dbbbe90 2022-06-17 op wclear(view->window);
903 0dbbbe90 2022-06-17 op
904 0dbbbe90 2022-06-17 op view->nlines = nlines;
905 0dbbbe90 2022-06-17 op view->ncols = ncols;
906 0dbbbe90 2022-06-17 op view->lines = LINES;
907 0dbbbe90 2022-06-17 op view->cols = COLS;
908 0dbbbe90 2022-06-17 op
909 5c60c32a 2018-10-18 stsp return NULL;
910 669b5ffa 2018-10-07 stsp }
911 669b5ffa 2018-10-07 stsp
912 669b5ffa 2018-10-07 stsp static const struct got_error *
913 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
914 669b5ffa 2018-10-07 stsp {
915 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
916 669b5ffa 2018-10-07 stsp
917 669b5ffa 2018-10-07 stsp if (view->child == NULL)
918 669b5ffa 2018-10-07 stsp return NULL;
919 669b5ffa 2018-10-07 stsp
920 669b5ffa 2018-10-07 stsp err = view_close(view->child);
921 669b5ffa 2018-10-07 stsp view->child = NULL;
922 669b5ffa 2018-10-07 stsp return err;
923 669b5ffa 2018-10-07 stsp }
924 669b5ffa 2018-10-07 stsp
925 0dbbbe90 2022-06-17 op static const struct got_error *
926 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
927 669b5ffa 2018-10-07 stsp {
928 669b5ffa 2018-10-07 stsp view->child = child;
929 669b5ffa 2018-10-07 stsp child->parent = view;
930 0dbbbe90 2022-06-17 op
931 0dbbbe90 2022-06-17 op return view_resize(view);
932 bfddd0d9 2018-09-29 stsp }
933 bfddd0d9 2018-09-29 stsp
934 34bc9ec9 2019-02-22 stsp static void
935 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
936 25791caa 2018-10-24 stsp {
937 25791caa 2018-10-24 stsp int cols, lines;
938 25791caa 2018-10-24 stsp struct winsize size;
939 25791caa 2018-10-24 stsp
940 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
941 25791caa 2018-10-24 stsp cols = 80; /* Default */
942 25791caa 2018-10-24 stsp lines = 24;
943 25791caa 2018-10-24 stsp } else {
944 25791caa 2018-10-24 stsp cols = size.ws_col;
945 25791caa 2018-10-24 stsp lines = size.ws_row;
946 25791caa 2018-10-24 stsp }
947 25791caa 2018-10-24 stsp resize_term(lines, cols);
948 2b49a8ae 2019-06-22 stsp }
949 2b49a8ae 2019-06-22 stsp
950 2b49a8ae 2019-06-22 stsp static const struct got_error *
951 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
952 2b49a8ae 2019-06-22 stsp {
953 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
954 9b058f45 2022-06-30 mark struct tog_view *v = view;
955 2b49a8ae 2019-06-22 stsp char pattern[1024];
956 2b49a8ae 2019-06-22 stsp int ret;
957 c0c4acc8 2021-01-24 stsp
958 c0c4acc8 2021-01-24 stsp if (view->search_started) {
959 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
960 c0c4acc8 2021-01-24 stsp view->searching = 0;
961 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
962 c0c4acc8 2021-01-24 stsp }
963 c0c4acc8 2021-01-24 stsp view->search_started = 0;
964 2b49a8ae 2019-06-22 stsp
965 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
966 2b49a8ae 2019-06-22 stsp return NULL;
967 2b49a8ae 2019-06-22 stsp
968 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
969 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
970 9b058f45 2022-06-30 mark v = view->child;
971 2b49a8ae 2019-06-22 stsp
972 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
973 9b058f45 2022-06-30 mark wclrtoeol(v->window);
974 9b058f45 2022-06-30 mark
975 2b49a8ae 2019-06-22 stsp nocbreak();
976 2b49a8ae 2019-06-22 stsp echo();
977 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
978 9b058f45 2022-06-30 mark wrefresh(v->window);
979 2b49a8ae 2019-06-22 stsp cbreak();
980 2b49a8ae 2019-06-22 stsp noecho();
981 2b49a8ae 2019-06-22 stsp if (ret == ERR)
982 2b49a8ae 2019-06-22 stsp return NULL;
983 2b49a8ae 2019-06-22 stsp
984 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
985 7c32bd05 2019-06-22 stsp err = view->search_start(view);
986 7c32bd05 2019-06-22 stsp if (err) {
987 7c32bd05 2019-06-22 stsp regfree(&view->regex);
988 7c32bd05 2019-06-22 stsp return err;
989 7c32bd05 2019-06-22 stsp }
990 c0c4acc8 2021-01-24 stsp view->search_started = 1;
991 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
992 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
993 2b49a8ae 2019-06-22 stsp view->search_next(view);
994 2b49a8ae 2019-06-22 stsp }
995 2b49a8ae 2019-06-22 stsp
996 2b49a8ae 2019-06-22 stsp return NULL;
997 0cf4efb1 2018-09-29 stsp }
998 6d0fee91 2018-08-01 stsp
999 640cd7ff 2022-06-22 mark /*
1000 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1001 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1002 640cd7ff 2022-06-22 mark */
1003 640cd7ff 2022-06-22 mark static int
1004 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1005 640cd7ff 2022-06-22 mark {
1006 9b058f45 2022-06-30 mark struct tog_view *v = view;
1007 9b058f45 2022-06-30 mark int x, n = 0;
1008 640cd7ff 2022-06-22 mark
1009 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
1010 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
1011 9b058f45 2022-06-30 mark v = view->child;
1012 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1013 9b058f45 2022-06-30 mark v = view->parent;
1014 9b058f45 2022-06-30 mark
1015 640cd7ff 2022-06-22 mark view->count = 0;
1016 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
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 640cd7ff 2022-06-22 mark return c;
1042 640cd7ff 2022-06-22 mark }
1043 640cd7ff 2022-06-22 mark
1044 0cf4efb1 2018-09-29 stsp static const struct got_error *
1045 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1046 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1047 e5a0f69f 2018-08-18 stsp {
1048 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1049 669b5ffa 2018-10-07 stsp struct tog_view *v;
1050 1a76625f 2018-10-22 stsp int ch, errcode;
1051 e5a0f69f 2018-08-18 stsp
1052 e5a0f69f 2018-08-18 stsp *new = NULL;
1053 8f4ed634 2020-03-26 stsp
1054 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1055 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1056 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1057 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1058 640cd7ff 2022-06-22 mark view->count = 0;
1059 640cd7ff 2022-06-22 mark }
1060 e5a0f69f 2018-08-18 stsp
1061 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1062 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1063 82954512 2020-02-03 stsp if (errcode)
1064 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1065 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1066 3da8ef85 2021-09-21 stsp sched_yield();
1067 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1068 82954512 2020-02-03 stsp if (errcode)
1069 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1070 82954512 2020-02-03 stsp "pthread_mutex_lock");
1071 60493ae3 2019-06-20 stsp view->search_next(view);
1072 60493ae3 2019-06-20 stsp return NULL;
1073 60493ae3 2019-06-20 stsp }
1074 60493ae3 2019-06-20 stsp
1075 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
1076 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1078 1a76625f 2018-10-22 stsp if (errcode)
1079 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1080 640cd7ff 2022-06-22 mark /* If we have an unfinished count, don't get a new key map. */
1081 640cd7ff 2022-06-22 mark ch = view->ch;
1082 640cd7ff 2022-06-22 mark if ((view->count && --view->count == 0) || !view->count) {
1083 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1084 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1085 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1086 640cd7ff 2022-06-22 mark }
1087 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1088 1a76625f 2018-10-22 stsp if (errcode)
1089 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1090 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
1091 25791caa 2018-10-24 stsp
1092 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1093 25791caa 2018-10-24 stsp tog_resizeterm();
1094 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1095 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1096 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1097 25791caa 2018-10-24 stsp err = view_resize(v);
1098 25791caa 2018-10-24 stsp if (err)
1099 25791caa 2018-10-24 stsp return err;
1100 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1101 25791caa 2018-10-24 stsp if (err)
1102 25791caa 2018-10-24 stsp return err;
1103 cdfcfb03 2020-12-06 stsp if (v->child) {
1104 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1105 cdfcfb03 2020-12-06 stsp if (err)
1106 cdfcfb03 2020-12-06 stsp return err;
1107 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1108 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1109 cdfcfb03 2020-12-06 stsp if (err)
1110 cdfcfb03 2020-12-06 stsp return err;
1111 cdfcfb03 2020-12-06 stsp }
1112 25791caa 2018-10-24 stsp }
1113 25791caa 2018-10-24 stsp }
1114 25791caa 2018-10-24 stsp
1115 e5a0f69f 2018-08-18 stsp switch (ch) {
1116 1e37a5c2 2019-05-12 jcs case '\t':
1117 640cd7ff 2022-06-22 mark view->count = 0;
1118 1e37a5c2 2019-05-12 jcs if (view->child) {
1119 e78dc838 2020-12-04 stsp view->focussed = 0;
1120 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1121 e78dc838 2020-12-04 stsp view->focus_child = 1;
1122 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1123 e78dc838 2020-12-04 stsp view->focussed = 0;
1124 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1125 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1126 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1127 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1128 9b058f45 2022-06-30 mark view->parent->type == TOG_VIEW_LOG) {
1129 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1130 9b058f45 2022-06-30 mark if (err)
1131 9b058f45 2022-06-30 mark return err;
1132 9b058f45 2022-06-30 mark }
1133 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1134 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1135 9b058f45 2022-06-30 mark if (err)
1136 9b058f45 2022-06-30 mark return err;
1137 9b058f45 2022-06-30 mark }
1138 1e37a5c2 2019-05-12 jcs }
1139 1e37a5c2 2019-05-12 jcs break;
1140 1e37a5c2 2019-05-12 jcs case 'q':
1141 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1142 9b058f45 2022-06-30 mark if (view->parent->type == TOG_VIEW_LOG) {
1143 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1144 9b058f45 2022-06-30 mark err = request_log_commits(view->parent);
1145 9b058f45 2022-06-30 mark if (err)
1146 9b058f45 2022-06-30 mark break;
1147 9b058f45 2022-06-30 mark }
1148 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1149 9b058f45 2022-06-30 mark view->parent->mode = TOG_VIEW_SPLIT_NONE;
1150 9b058f45 2022-06-30 mark }
1151 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1152 9970f7fc 2020-12-03 stsp view->dying = 1;
1153 1e37a5c2 2019-05-12 jcs break;
1154 1e37a5c2 2019-05-12 jcs case 'Q':
1155 1e37a5c2 2019-05-12 jcs *done = 1;
1156 1e37a5c2 2019-05-12 jcs break;
1157 61417565 2022-06-20 mark case 'F':
1158 640cd7ff 2022-06-22 mark view->count = 0;
1159 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1160 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1161 1e37a5c2 2019-05-12 jcs break;
1162 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1163 e78dc838 2020-12-04 stsp view->focussed = 0;
1164 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1165 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1166 1e37a5c2 2019-05-12 jcs } else
1167 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1168 1e37a5c2 2019-05-12 jcs if (err)
1169 1e37a5c2 2019-05-12 jcs break;
1170 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1171 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1172 1e37a5c2 2019-05-12 jcs } else {
1173 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1174 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1175 e78dc838 2020-12-04 stsp view->focussed = 1;
1176 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1177 1e37a5c2 2019-05-12 jcs } else {
1178 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1179 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1180 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1181 669b5ffa 2018-10-07 stsp }
1182 1e37a5c2 2019-05-12 jcs if (err)
1183 1e37a5c2 2019-05-12 jcs break;
1184 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1185 1e37a5c2 2019-05-12 jcs }
1186 9b058f45 2022-06-30 mark if (err)
1187 9b058f45 2022-06-30 mark break;
1188 9b058f45 2022-06-30 mark if (view->type == TOG_VIEW_LOG) {
1189 9b058f45 2022-06-30 mark err = request_log_commits(view);
1190 9b058f45 2022-06-30 mark if (err)
1191 9b058f45 2022-06-30 mark break;
1192 9b058f45 2022-06-30 mark }
1193 9b058f45 2022-06-30 mark if (view->parent)
1194 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1195 9b058f45 2022-06-30 mark if (!err)
1196 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1197 1e37a5c2 2019-05-12 jcs break;
1198 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1199 60493ae3 2019-06-20 stsp break;
1200 60493ae3 2019-06-20 stsp case '/':
1201 640cd7ff 2022-06-22 mark view->count = 0;
1202 60493ae3 2019-06-20 stsp if (view->search_start)
1203 2b49a8ae 2019-06-22 stsp view_search_start(view);
1204 60493ae3 2019-06-20 stsp else
1205 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1206 1e37a5c2 2019-05-12 jcs break;
1207 b1bf1435 2019-06-21 stsp case 'N':
1208 60493ae3 2019-06-20 stsp case 'n':
1209 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1210 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1211 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1212 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1213 60493ae3 2019-06-20 stsp view->search_next(view);
1214 60493ae3 2019-06-20 stsp } else
1215 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1216 917d79a7 2022-07-01 stsp break;
1217 917d79a7 2022-07-01 stsp case 'A':
1218 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1219 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1220 917d79a7 2022-07-01 stsp else
1221 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1222 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1223 917d79a7 2022-07-01 stsp if (v->reset) {
1224 917d79a7 2022-07-01 stsp err = v->reset(v);
1225 917d79a7 2022-07-01 stsp if (err)
1226 917d79a7 2022-07-01 stsp return err;
1227 917d79a7 2022-07-01 stsp }
1228 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1229 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1230 917d79a7 2022-07-01 stsp if (err)
1231 917d79a7 2022-07-01 stsp return err;
1232 917d79a7 2022-07-01 stsp }
1233 917d79a7 2022-07-01 stsp }
1234 60493ae3 2019-06-20 stsp break;
1235 1e37a5c2 2019-05-12 jcs default:
1236 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1237 1e37a5c2 2019-05-12 jcs break;
1238 e5a0f69f 2018-08-18 stsp }
1239 e5a0f69f 2018-08-18 stsp
1240 e5a0f69f 2018-08-18 stsp return err;
1241 e5a0f69f 2018-08-18 stsp }
1242 e5a0f69f 2018-08-18 stsp
1243 336075a4 2022-06-25 op static int
1244 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1245 a3404814 2018-09-02 stsp {
1246 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1247 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1248 669b5ffa 2018-10-07 stsp return 0;
1249 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1250 669b5ffa 2018-10-07 stsp return 0;
1251 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1252 a3404814 2018-09-02 stsp return 0;
1253 a3404814 2018-09-02 stsp
1254 669b5ffa 2018-10-07 stsp return view->focussed;
1255 a3404814 2018-09-02 stsp }
1256 a3404814 2018-09-02 stsp
1257 bcbd79e2 2018-08-19 stsp static const struct got_error *
1258 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1259 e5a0f69f 2018-08-18 stsp {
1260 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1261 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1262 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1263 fd823528 2018-10-22 stsp int fast_refresh = 10;
1264 1a76625f 2018-10-22 stsp int done = 0, errcode;
1265 e5a0f69f 2018-08-18 stsp
1266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1267 1a76625f 2018-10-22 stsp if (errcode)
1268 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1269 1a76625f 2018-10-22 stsp
1270 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1271 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1272 e5a0f69f 2018-08-18 stsp
1273 1004088d 2018-09-29 stsp view->focussed = 1;
1274 878940b7 2018-09-29 stsp err = view->show(view);
1275 0cf4efb1 2018-09-29 stsp if (err)
1276 0cf4efb1 2018-09-29 stsp return err;
1277 0cf4efb1 2018-09-29 stsp update_panels();
1278 0cf4efb1 2018-09-29 stsp doupdate();
1279 2497f032 2022-05-31 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1280 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1281 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1282 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1283 fd823528 2018-10-22 stsp
1284 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1285 e5a0f69f 2018-08-18 stsp if (err)
1286 e5a0f69f 2018-08-18 stsp break;
1287 9970f7fc 2020-12-03 stsp if (view->dying) {
1288 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1289 669b5ffa 2018-10-07 stsp
1290 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1291 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1292 9970f7fc 2020-12-03 stsp entry);
1293 e78dc838 2020-12-04 stsp else if (view->parent)
1294 669b5ffa 2018-10-07 stsp prev = view->parent;
1295 669b5ffa 2018-10-07 stsp
1296 e78dc838 2020-12-04 stsp if (view->parent) {
1297 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1298 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1299 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1300 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1301 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1302 0dbbbe90 2022-06-17 op if (err)
1303 0dbbbe90 2022-06-17 op break;
1304 e78dc838 2020-12-04 stsp } else
1305 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1306 669b5ffa 2018-10-07 stsp
1307 9970f7fc 2020-12-03 stsp err = view_close(view);
1308 fb59748f 2020-12-05 stsp if (err)
1309 e5a0f69f 2018-08-18 stsp goto done;
1310 669b5ffa 2018-10-07 stsp
1311 e78dc838 2020-12-04 stsp view = NULL;
1312 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1313 e78dc838 2020-12-04 stsp if (v->focussed)
1314 e78dc838 2020-12-04 stsp break;
1315 0cf4efb1 2018-09-29 stsp }
1316 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1317 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1318 e78dc838 2020-12-04 stsp if (prev)
1319 e78dc838 2020-12-04 stsp view = prev;
1320 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1321 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1322 e78dc838 2020-12-04 stsp tog_view_list_head);
1323 e78dc838 2020-12-04 stsp }
1324 e78dc838 2020-12-04 stsp if (view) {
1325 e78dc838 2020-12-04 stsp if (view->focus_child) {
1326 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1327 e78dc838 2020-12-04 stsp view = view->child;
1328 e78dc838 2020-12-04 stsp } else
1329 e78dc838 2020-12-04 stsp view->focussed = 1;
1330 e78dc838 2020-12-04 stsp }
1331 e78dc838 2020-12-04 stsp }
1332 e5a0f69f 2018-08-18 stsp }
1333 bcbd79e2 2018-08-19 stsp if (new_view) {
1334 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1335 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1336 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1337 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1338 86c66b02 2018-10-18 stsp continue;
1339 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1340 86c66b02 2018-10-18 stsp err = view_close(v);
1341 86c66b02 2018-10-18 stsp if (err)
1342 86c66b02 2018-10-18 stsp goto done;
1343 86c66b02 2018-10-18 stsp break;
1344 86c66b02 2018-10-18 stsp }
1345 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1346 fed7eaa8 2018-10-24 stsp view = new_view;
1347 0ae84acc 2022-06-15 tracey }
1348 669b5ffa 2018-10-07 stsp if (view) {
1349 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1350 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1351 e78dc838 2020-12-04 stsp view = view->child;
1352 e78dc838 2020-12-04 stsp } else {
1353 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1354 e78dc838 2020-12-04 stsp view = view->parent;
1355 1a76625f 2018-10-22 stsp }
1356 e78dc838 2020-12-04 stsp show_panel(view->panel);
1357 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1358 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1359 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1360 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1361 669b5ffa 2018-10-07 stsp if (err)
1362 1a76625f 2018-10-22 stsp goto done;
1363 669b5ffa 2018-10-07 stsp }
1364 669b5ffa 2018-10-07 stsp err = view->show(view);
1365 0cf4efb1 2018-09-29 stsp if (err)
1366 1a76625f 2018-10-22 stsp goto done;
1367 669b5ffa 2018-10-07 stsp if (view->child) {
1368 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1369 669b5ffa 2018-10-07 stsp if (err)
1370 1a76625f 2018-10-22 stsp goto done;
1371 669b5ffa 2018-10-07 stsp }
1372 1a76625f 2018-10-22 stsp update_panels();
1373 1a76625f 2018-10-22 stsp doupdate();
1374 0cf4efb1 2018-09-29 stsp }
1375 e5a0f69f 2018-08-18 stsp }
1376 e5a0f69f 2018-08-18 stsp done:
1377 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1378 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1379 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1380 e5a0f69f 2018-08-18 stsp view_close(view);
1381 e5a0f69f 2018-08-18 stsp }
1382 1a76625f 2018-10-22 stsp
1383 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1384 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1385 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1386 1a76625f 2018-10-22 stsp
1387 e5a0f69f 2018-08-18 stsp return err;
1388 ea5e7bb5 2018-08-01 stsp }
1389 ea5e7bb5 2018-08-01 stsp
1390 4ed7e80c 2018-05-20 stsp __dead static void
1391 9f7d7167 2018-04-29 stsp usage_log(void)
1392 9f7d7167 2018-04-29 stsp {
1393 80ddbec8 2018-04-29 stsp endwin();
1394 c70c5802 2018-08-01 stsp fprintf(stderr,
1395 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1396 9f7d7167 2018-04-29 stsp getprogname());
1397 9f7d7167 2018-04-29 stsp exit(1);
1398 80ddbec8 2018-04-29 stsp }
1399 80ddbec8 2018-04-29 stsp
1400 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1401 80ddbec8 2018-04-29 stsp static const struct got_error *
1402 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1403 963b370f 2018-05-20 stsp {
1404 00dfcb92 2018-06-11 stsp char *vis = NULL;
1405 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1406 963b370f 2018-05-20 stsp
1407 963b370f 2018-05-20 stsp *ws = NULL;
1408 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1409 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1410 00dfcb92 2018-06-11 stsp int vislen;
1411 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1412 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1413 00dfcb92 2018-06-11 stsp
1414 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1415 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1416 00dfcb92 2018-06-11 stsp if (err)
1417 00dfcb92 2018-06-11 stsp return err;
1418 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1419 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1420 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1421 a7f50699 2018-06-11 stsp goto done;
1422 a7f50699 2018-06-11 stsp }
1423 00dfcb92 2018-06-11 stsp }
1424 963b370f 2018-05-20 stsp
1425 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1426 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1427 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1428 a7f50699 2018-06-11 stsp goto done;
1429 a7f50699 2018-06-11 stsp }
1430 963b370f 2018-05-20 stsp
1431 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1433 a7f50699 2018-06-11 stsp done:
1434 00dfcb92 2018-06-11 stsp free(vis);
1435 963b370f 2018-05-20 stsp if (err) {
1436 963b370f 2018-05-20 stsp free(*ws);
1437 963b370f 2018-05-20 stsp *ws = NULL;
1438 963b370f 2018-05-20 stsp *wlen = 0;
1439 963b370f 2018-05-20 stsp }
1440 963b370f 2018-05-20 stsp return err;
1441 145b6838 2022-06-16 stsp }
1442 145b6838 2022-06-16 stsp
1443 145b6838 2022-06-16 stsp static const struct got_error *
1444 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1445 145b6838 2022-06-16 stsp {
1446 145b6838 2022-06-16 stsp char *dst;
1447 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1448 145b6838 2022-06-16 stsp
1449 145b6838 2022-06-16 stsp *ptr = NULL;
1450 145b6838 2022-06-16 stsp n = len = strlen(src);
1451 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1452 145b6838 2022-06-16 stsp if (dst == NULL)
1453 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1454 145b6838 2022-06-16 stsp
1455 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1456 145b6838 2022-06-16 stsp const char c = src[idx];
1457 145b6838 2022-06-16 stsp
1458 145b6838 2022-06-16 stsp if (c == '\t') {
1459 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1460 367ddf28 2022-06-16 mark char *p;
1461 367ddf28 2022-06-16 mark
1462 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1463 6e1c41ad 2022-06-16 mark if (p == NULL) {
1464 6e1c41ad 2022-06-16 mark free(dst);
1465 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1466 6e1c41ad 2022-06-16 mark
1467 6e1c41ad 2022-06-16 mark }
1468 6e1c41ad 2022-06-16 mark dst = p;
1469 145b6838 2022-06-16 stsp n += nb;
1470 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1471 145b6838 2022-06-16 stsp sz += nb;
1472 145b6838 2022-06-16 stsp } else
1473 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1474 145b6838 2022-06-16 stsp ++idx;
1475 145b6838 2022-06-16 stsp }
1476 145b6838 2022-06-16 stsp
1477 145b6838 2022-06-16 stsp dst[sz] = '\0';
1478 145b6838 2022-06-16 stsp *ptr = dst;
1479 145b6838 2022-06-16 stsp return NULL;
1480 963b370f 2018-05-20 stsp }
1481 963b370f 2018-05-20 stsp
1482 4e4a9ac8 2022-06-17 op /*
1483 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1484 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1485 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1486 4e4a9ac8 2022-06-17 op * *rcol.
1487 ccda2f4d 2022-06-16 stsp */
1488 4e4a9ac8 2022-06-17 op static int
1489 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1490 4e4a9ac8 2022-06-17 op {
1491 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1492 ccda2f4d 2022-06-16 stsp
1493 4e4a9ac8 2022-06-17 op if (n == 0) {
1494 4e4a9ac8 2022-06-17 op *rcol = cols;
1495 4e4a9ac8 2022-06-17 op return off;
1496 4e4a9ac8 2022-06-17 op }
1497 ccda2f4d 2022-06-16 stsp
1498 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1499 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1500 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1501 4e4a9ac8 2022-06-17 op else
1502 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1503 ccda2f4d 2022-06-16 stsp
1504 4e4a9ac8 2022-06-17 op if (width == -1) {
1505 4e4a9ac8 2022-06-17 op width = 1;
1506 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1507 ccda2f4d 2022-06-16 stsp }
1508 ccda2f4d 2022-06-16 stsp
1509 4e4a9ac8 2022-06-17 op if (cols + width > n)
1510 4e4a9ac8 2022-06-17 op break;
1511 4e4a9ac8 2022-06-17 op cols += width;
1512 ccda2f4d 2022-06-16 stsp }
1513 ccda2f4d 2022-06-16 stsp
1514 4e4a9ac8 2022-06-17 op *rcol = cols;
1515 4e4a9ac8 2022-06-17 op return i;
1516 ccda2f4d 2022-06-16 stsp }
1517 ccda2f4d 2022-06-16 stsp
1518 ccda2f4d 2022-06-16 stsp /*
1519 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1520 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1521 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1522 ccda2f4d 2022-06-16 stsp */
1523 ccda2f4d 2022-06-16 stsp static const struct got_error *
1524 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1525 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1526 ccda2f4d 2022-06-16 stsp {
1527 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1528 4e4a9ac8 2022-06-17 op int cols;
1529 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1530 145b6838 2022-06-16 stsp char *exstr = NULL;
1531 963b370f 2018-05-20 stsp size_t wlen;
1532 4e4a9ac8 2022-06-17 op int i, scrollx;
1533 963b370f 2018-05-20 stsp
1534 963b370f 2018-05-20 stsp *wlinep = NULL;
1535 b700b5d6 2018-07-10 stsp *widthp = 0;
1536 963b370f 2018-05-20 stsp
1537 145b6838 2022-06-16 stsp if (expand) {
1538 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1539 145b6838 2022-06-16 stsp if (err)
1540 145b6838 2022-06-16 stsp return err;
1541 145b6838 2022-06-16 stsp }
1542 145b6838 2022-06-16 stsp
1543 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1544 145b6838 2022-06-16 stsp free(exstr);
1545 963b370f 2018-05-20 stsp if (err)
1546 963b370f 2018-05-20 stsp return err;
1547 963b370f 2018-05-20 stsp
1548 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1549 ccda2f4d 2022-06-16 stsp
1550 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1551 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1552 3f670bfb 2020-12-10 stsp wlen--;
1553 3f670bfb 2020-12-10 stsp }
1554 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1555 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1556 3f670bfb 2020-12-10 stsp wlen--;
1557 3f670bfb 2020-12-10 stsp }
1558 3f670bfb 2020-12-10 stsp
1559 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1560 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1561 27a741e5 2019-09-11 stsp
1562 b700b5d6 2018-07-10 stsp if (widthp)
1563 b700b5d6 2018-07-10 stsp *widthp = cols;
1564 ccda2f4d 2022-06-16 stsp if (scrollxp)
1565 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1566 963b370f 2018-05-20 stsp if (err)
1567 963b370f 2018-05-20 stsp free(wline);
1568 963b370f 2018-05-20 stsp else
1569 963b370f 2018-05-20 stsp *wlinep = wline;
1570 963b370f 2018-05-20 stsp return err;
1571 963b370f 2018-05-20 stsp }
1572 963b370f 2018-05-20 stsp
1573 8b473291 2019-02-21 stsp static const struct got_error*
1574 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1575 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1576 8b473291 2019-02-21 stsp {
1577 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1578 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1579 8b473291 2019-02-21 stsp char *s;
1580 8b473291 2019-02-21 stsp const char *name;
1581 8b473291 2019-02-21 stsp
1582 8b473291 2019-02-21 stsp *refs_str = NULL;
1583 8b473291 2019-02-21 stsp
1584 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1585 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1586 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1587 52b5abe1 2019-08-13 stsp int cmp;
1588 52b5abe1 2019-08-13 stsp
1589 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1590 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1591 8b473291 2019-02-21 stsp continue;
1592 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1593 8b473291 2019-02-21 stsp name += 5;
1594 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1595 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1596 7143d404 2019-03-12 stsp continue;
1597 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1598 8b473291 2019-02-21 stsp name += 6;
1599 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1600 8b473291 2019-02-21 stsp name += 8;
1601 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1602 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1603 79cc719f 2020-04-24 stsp continue;
1604 79cc719f 2020-04-24 stsp }
1605 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1606 48cae60d 2020-09-22 stsp if (err)
1607 48cae60d 2020-09-22 stsp break;
1608 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1609 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1610 5d844a1e 2019-08-13 stsp if (err) {
1611 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1612 48cae60d 2020-09-22 stsp free(ref_id);
1613 5d844a1e 2019-08-13 stsp break;
1614 48cae60d 2020-09-22 stsp }
1615 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1616 5d844a1e 2019-08-13 stsp err = NULL;
1617 5d844a1e 2019-08-13 stsp tag = NULL;
1618 5d844a1e 2019-08-13 stsp }
1619 52b5abe1 2019-08-13 stsp }
1620 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1621 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1622 48cae60d 2020-09-22 stsp free(ref_id);
1623 52b5abe1 2019-08-13 stsp if (tag)
1624 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1625 52b5abe1 2019-08-13 stsp if (cmp != 0)
1626 52b5abe1 2019-08-13 stsp continue;
1627 8b473291 2019-02-21 stsp s = *refs_str;
1628 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1629 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1630 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1631 8b473291 2019-02-21 stsp free(s);
1632 8b473291 2019-02-21 stsp *refs_str = NULL;
1633 8b473291 2019-02-21 stsp break;
1634 8b473291 2019-02-21 stsp }
1635 8b473291 2019-02-21 stsp free(s);
1636 8b473291 2019-02-21 stsp }
1637 8b473291 2019-02-21 stsp
1638 8b473291 2019-02-21 stsp return err;
1639 8b473291 2019-02-21 stsp }
1640 8b473291 2019-02-21 stsp
1641 963b370f 2018-05-20 stsp static const struct got_error *
1642 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1643 27a741e5 2019-09-11 stsp int col_tab_align)
1644 5813d178 2019-03-09 stsp {
1645 e6b8b890 2020-12-29 naddy char *smallerthan;
1646 5813d178 2019-03-09 stsp
1647 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1648 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1649 5813d178 2019-03-09 stsp author = smallerthan + 1;
1650 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1651 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1652 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1653 5813d178 2019-03-09 stsp }
1654 5813d178 2019-03-09 stsp
1655 5813d178 2019-03-09 stsp static const struct got_error *
1656 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1657 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1658 8fdc79fe 2020-12-01 naddy int author_display_cols)
1659 80ddbec8 2018-04-29 stsp {
1660 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1661 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1662 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1663 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1664 5813d178 2019-03-09 stsp char *author = NULL;
1665 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1666 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1667 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1668 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1669 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1670 ccb26ccd 2018-11-05 stsp struct tm tm;
1671 45d799e2 2018-12-23 stsp time_t committer_time;
1672 11b20872 2019-11-08 stsp struct tog_color *tc;
1673 80ddbec8 2018-04-29 stsp
1674 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1675 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1676 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1677 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1678 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1679 b39d25c7 2018-07-10 stsp
1680 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1681 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1682 b39d25c7 2018-07-10 stsp else
1683 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1684 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1685 11b20872 2019-11-08 stsp if (tc)
1686 11b20872 2019-11-08 stsp wattr_on(view->window,
1687 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1688 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1689 11b20872 2019-11-08 stsp if (tc)
1690 11b20872 2019-11-08 stsp wattr_off(view->window,
1691 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1692 27a741e5 2019-09-11 stsp col = limit;
1693 b39d25c7 2018-07-10 stsp if (col > avail)
1694 b39d25c7 2018-07-10 stsp goto done;
1695 6570a66d 2019-11-08 stsp
1696 6570a66d 2019-11-08 stsp if (avail >= 120) {
1697 6570a66d 2019-11-08 stsp char *id_str;
1698 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1699 6570a66d 2019-11-08 stsp if (err)
1700 6570a66d 2019-11-08 stsp goto done;
1701 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1702 11b20872 2019-11-08 stsp if (tc)
1703 11b20872 2019-11-08 stsp wattr_on(view->window,
1704 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1705 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1706 11b20872 2019-11-08 stsp if (tc)
1707 11b20872 2019-11-08 stsp wattr_off(view->window,
1708 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1709 6570a66d 2019-11-08 stsp free(id_str);
1710 6570a66d 2019-11-08 stsp col += 9;
1711 6570a66d 2019-11-08 stsp if (col > avail)
1712 6570a66d 2019-11-08 stsp goto done;
1713 6570a66d 2019-11-08 stsp }
1714 b39d25c7 2018-07-10 stsp
1715 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1716 5813d178 2019-03-09 stsp if (author == NULL) {
1717 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1718 80ddbec8 2018-04-29 stsp goto done;
1719 80ddbec8 2018-04-29 stsp }
1720 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1721 bb737323 2018-05-20 stsp if (err)
1722 bb737323 2018-05-20 stsp goto done;
1723 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1724 11b20872 2019-11-08 stsp if (tc)
1725 11b20872 2019-11-08 stsp wattr_on(view->window,
1726 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1727 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1728 11b20872 2019-11-08 stsp if (tc)
1729 11b20872 2019-11-08 stsp wattr_off(view->window,
1730 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1731 bb737323 2018-05-20 stsp col += author_width;
1732 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1733 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1734 bb737323 2018-05-20 stsp col++;
1735 bb737323 2018-05-20 stsp author_width++;
1736 bb737323 2018-05-20 stsp }
1737 9c2eaf34 2018-05-20 stsp if (col > avail)
1738 9c2eaf34 2018-05-20 stsp goto done;
1739 80ddbec8 2018-04-29 stsp
1740 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1741 5943eee2 2019-08-13 stsp if (err)
1742 6d9fbc00 2018-04-29 stsp goto done;
1743 bb737323 2018-05-20 stsp logmsg = logmsg0;
1744 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1745 bb737323 2018-05-20 stsp logmsg++;
1746 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1747 bb737323 2018-05-20 stsp if (newline)
1748 bb737323 2018-05-20 stsp *newline = '\0';
1749 ccda2f4d 2022-06-16 stsp limit = avail - col;
1750 63ba1a3a 2022-06-21 op if (view->child && view_is_splitscreen(view->child) && limit > 0)
1751 4d1f6af3 2022-06-17 op limit--; /* for the border */
1752 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1753 ccda2f4d 2022-06-16 stsp limit, col, 1);
1754 29688b02 2022-06-16 stsp if (err)
1755 29688b02 2022-06-16 stsp goto done;
1756 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
1757 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
1758 27a741e5 2019-09-11 stsp while (col < avail) {
1759 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1760 bb737323 2018-05-20 stsp col++;
1761 881b2d3e 2018-04-30 stsp }
1762 80ddbec8 2018-04-29 stsp done:
1763 80ddbec8 2018-04-29 stsp free(logmsg0);
1764 bb737323 2018-05-20 stsp free(wlogmsg);
1765 5813d178 2019-03-09 stsp free(author);
1766 bb737323 2018-05-20 stsp free(wauthor);
1767 80ddbec8 2018-04-29 stsp free(line);
1768 80ddbec8 2018-04-29 stsp return err;
1769 80ddbec8 2018-04-29 stsp }
1770 26ed57b2 2018-05-19 stsp
1771 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1772 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1773 899d86c2 2018-05-10 stsp struct got_object_id *id)
1774 80ddbec8 2018-04-29 stsp {
1775 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1776 80ddbec8 2018-04-29 stsp
1777 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1778 80ddbec8 2018-04-29 stsp if (entry == NULL)
1779 899d86c2 2018-05-10 stsp return NULL;
1780 99db9666 2018-05-07 stsp
1781 899d86c2 2018-05-10 stsp entry->id = id;
1782 99db9666 2018-05-07 stsp entry->commit = commit;
1783 899d86c2 2018-05-10 stsp return entry;
1784 99db9666 2018-05-07 stsp }
1785 80ddbec8 2018-04-29 stsp
1786 99db9666 2018-05-07 stsp static void
1787 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1788 99db9666 2018-05-07 stsp {
1789 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1790 99db9666 2018-05-07 stsp
1791 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1792 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1793 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1794 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1795 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1796 99db9666 2018-05-07 stsp free(entry);
1797 99db9666 2018-05-07 stsp }
1798 99db9666 2018-05-07 stsp
1799 99db9666 2018-05-07 stsp static void
1800 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1801 99db9666 2018-05-07 stsp {
1802 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1803 99db9666 2018-05-07 stsp pop_commit(commits);
1804 c4972b91 2018-05-07 stsp }
1805 c4972b91 2018-05-07 stsp
1806 c4972b91 2018-05-07 stsp static const struct got_error *
1807 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1808 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1809 13add988 2019-10-15 stsp {
1810 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1811 13add988 2019-10-15 stsp regmatch_t regmatch;
1812 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1813 13add988 2019-10-15 stsp
1814 13add988 2019-10-15 stsp *have_match = 0;
1815 13add988 2019-10-15 stsp
1816 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1817 13add988 2019-10-15 stsp if (err)
1818 13add988 2019-10-15 stsp return err;
1819 13add988 2019-10-15 stsp
1820 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1821 13add988 2019-10-15 stsp if (err)
1822 13add988 2019-10-15 stsp goto done;
1823 13add988 2019-10-15 stsp
1824 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1825 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1826 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1827 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
1828 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1829 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1830 13add988 2019-10-15 stsp *have_match = 1;
1831 13add988 2019-10-15 stsp done:
1832 13add988 2019-10-15 stsp free(id_str);
1833 13add988 2019-10-15 stsp free(logmsg);
1834 13add988 2019-10-15 stsp return err;
1835 13add988 2019-10-15 stsp }
1836 13add988 2019-10-15 stsp
1837 13add988 2019-10-15 stsp static const struct got_error *
1838 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1839 c4972b91 2018-05-07 stsp {
1840 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1841 9ba79e04 2018-06-11 stsp
1842 1a76625f 2018-10-22 stsp /*
1843 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1844 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1845 1a76625f 2018-10-22 stsp * while updating the display.
1846 1a76625f 2018-10-22 stsp */
1847 4e0d2870 2020-12-07 naddy do {
1848 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1849 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1850 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1851 1a76625f 2018-10-22 stsp int errcode;
1852 899d86c2 2018-05-10 stsp
1853 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1854 4e0d2870 2020-12-07 naddy NULL, NULL);
1855 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1856 ecb28ae0 2018-07-16 stsp break;
1857 899d86c2 2018-05-10 stsp
1858 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1859 9ba79e04 2018-06-11 stsp if (err)
1860 9ba79e04 2018-06-11 stsp break;
1861 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1862 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1864 9ba79e04 2018-06-11 stsp break;
1865 9ba79e04 2018-06-11 stsp }
1866 93e45b7c 2018-09-24 stsp
1867 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1868 1a76625f 2018-10-22 stsp if (errcode) {
1869 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1870 13add988 2019-10-15 stsp "pthread_mutex_lock");
1871 1a76625f 2018-10-22 stsp break;
1872 1a76625f 2018-10-22 stsp }
1873 1a76625f 2018-10-22 stsp
1874 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1875 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1876 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1877 1a76625f 2018-10-22 stsp
1878 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1879 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1880 7c1452c1 2020-03-26 stsp int have_match;
1881 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1882 7c1452c1 2020-03-26 stsp if (err)
1883 7c1452c1 2020-03-26 stsp break;
1884 7c1452c1 2020-03-26 stsp if (have_match)
1885 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1886 13add988 2019-10-15 stsp }
1887 13add988 2019-10-15 stsp
1888 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1889 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1890 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1891 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1892 7c1452c1 2020-03-26 stsp if (err)
1893 13add988 2019-10-15 stsp break;
1894 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1895 899d86c2 2018-05-10 stsp
1896 9ba79e04 2018-06-11 stsp return err;
1897 0553a4e3 2018-04-30 stsp }
1898 0553a4e3 2018-04-30 stsp
1899 2b779855 2020-12-05 naddy static void
1900 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1901 2b779855 2020-12-05 naddy {
1902 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1903 2b779855 2020-12-05 naddy int ncommits = 0;
1904 2b779855 2020-12-05 naddy
1905 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1906 2b779855 2020-12-05 naddy while (entry) {
1907 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1908 2b779855 2020-12-05 naddy s->selected_entry = entry;
1909 2b779855 2020-12-05 naddy break;
1910 2b779855 2020-12-05 naddy }
1911 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1912 2b779855 2020-12-05 naddy ncommits++;
1913 2b779855 2020-12-05 naddy }
1914 2b779855 2020-12-05 naddy }
1915 2b779855 2020-12-05 naddy
1916 0553a4e3 2018-04-30 stsp static const struct got_error *
1917 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1918 0553a4e3 2018-04-30 stsp {
1919 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1920 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1921 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1922 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1923 60493ae3 2019-06-20 stsp int width;
1924 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1925 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1926 8b473291 2019-02-21 stsp char *refs_str = NULL;
1927 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1928 11b20872 2019-11-08 stsp struct tog_color *tc;
1929 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1930 0553a4e3 2018-04-30 stsp
1931 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1932 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1933 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1934 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1935 1a76625f 2018-10-22 stsp if (err)
1936 ecb28ae0 2018-07-16 stsp return err;
1937 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1938 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1939 d2075bf3 2020-12-25 stsp if (refs) {
1940 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1941 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1942 d2075bf3 2020-12-25 stsp if (err)
1943 d2075bf3 2020-12-25 stsp goto done;
1944 d2075bf3 2020-12-25 stsp }
1945 867c6645 2018-07-10 stsp }
1946 359bfafd 2019-02-22 stsp
1947 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1948 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1949 1a76625f 2018-10-22 stsp
1950 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1951 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1952 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1953 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1954 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1955 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1956 8f4ed634 2020-03-26 stsp goto done;
1957 8f4ed634 2020-03-26 stsp }
1958 8f4ed634 2020-03-26 stsp } else {
1959 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1960 f9686aa5 2020-03-27 stsp
1961 f9686aa5 2020-03-27 stsp if (view->searching) {
1962 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1963 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1964 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1965 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1966 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1967 f9686aa5 2020-03-27 stsp search_str = "searching...";
1968 f9686aa5 2020-03-27 stsp }
1969 f9686aa5 2020-03-27 stsp
1970 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1971 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1972 f9686aa5 2020-03-27 stsp search_str ? search_str :
1973 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1974 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1975 8f4ed634 2020-03-26 stsp goto done;
1976 8f4ed634 2020-03-26 stsp }
1977 8b473291 2019-02-21 stsp }
1978 1a76625f 2018-10-22 stsp
1979 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1980 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1981 87411fa9 2022-06-16 stsp "........................................",
1982 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1983 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1984 1a76625f 2018-10-22 stsp header = NULL;
1985 1a76625f 2018-10-22 stsp goto done;
1986 1a76625f 2018-10-22 stsp }
1987 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1988 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1989 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1990 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1991 1a76625f 2018-10-22 stsp header = NULL;
1992 1a76625f 2018-10-22 stsp goto done;
1993 ecb28ae0 2018-07-16 stsp }
1994 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1995 1a76625f 2018-10-22 stsp if (err)
1996 1a76625f 2018-10-22 stsp goto done;
1997 867c6645 2018-07-10 stsp
1998 2814baeb 2018-08-01 stsp werase(view->window);
1999 867c6645 2018-07-10 stsp
2000 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2001 a3404814 2018-09-02 stsp wstandout(view->window);
2002 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2003 11b20872 2019-11-08 stsp if (tc)
2004 11b20872 2019-11-08 stsp wattr_on(view->window,
2005 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2006 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2007 11b20872 2019-11-08 stsp if (tc)
2008 11b20872 2019-11-08 stsp wattr_off(view->window,
2009 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2010 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2011 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2012 1a76625f 2018-10-22 stsp width++;
2013 1a76625f 2018-10-22 stsp }
2014 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2015 a3404814 2018-09-02 stsp wstandend(view->window);
2016 ecb28ae0 2018-07-16 stsp free(wline);
2017 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2018 1a76625f 2018-10-22 stsp goto done;
2019 0553a4e3 2018-04-30 stsp
2020 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2021 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2022 5813d178 2019-03-09 stsp ncommits = 0;
2023 145b6838 2022-06-16 stsp view->maxx = 0;
2024 5813d178 2019-03-09 stsp while (entry) {
2025 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2026 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2027 5813d178 2019-03-09 stsp int width;
2028 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2029 5813d178 2019-03-09 stsp break;
2030 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2031 5813d178 2019-03-09 stsp if (author == NULL) {
2032 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2033 5813d178 2019-03-09 stsp goto done;
2034 5813d178 2019-03-09 stsp }
2035 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2036 27a741e5 2019-09-11 stsp date_display_cols);
2037 5813d178 2019-03-09 stsp if (author_cols < width)
2038 5813d178 2019-03-09 stsp author_cols = width;
2039 5813d178 2019-03-09 stsp free(wauthor);
2040 5813d178 2019-03-09 stsp free(author);
2041 145b6838 2022-06-16 stsp err = got_object_commit_get_logmsg(&msg0, entry->commit);
2042 145b6838 2022-06-16 stsp if (err)
2043 145b6838 2022-06-16 stsp goto done;
2044 145b6838 2022-06-16 stsp msg = msg0;
2045 145b6838 2022-06-16 stsp while (*msg == '\n')
2046 145b6838 2022-06-16 stsp ++msg;
2047 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2048 29688b02 2022-06-16 stsp *eol = '\0';
2049 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2050 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2051 29688b02 2022-06-16 stsp if (err)
2052 29688b02 2022-06-16 stsp goto done;
2053 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2054 145b6838 2022-06-16 stsp free(msg0);
2055 29688b02 2022-06-16 stsp free(wmsg);
2056 7ca04879 2019-10-19 stsp ncommits++;
2057 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2058 5813d178 2019-03-09 stsp }
2059 5813d178 2019-03-09 stsp
2060 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2061 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2062 867c6645 2018-07-10 stsp ncommits = 0;
2063 899d86c2 2018-05-10 stsp while (entry) {
2064 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2065 899d86c2 2018-05-10 stsp break;
2066 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2067 2814baeb 2018-08-01 stsp wstandout(view->window);
2068 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2069 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2070 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2071 2814baeb 2018-08-01 stsp wstandend(view->window);
2072 0553a4e3 2018-04-30 stsp if (err)
2073 60493ae3 2019-06-20 stsp goto done;
2074 0553a4e3 2018-04-30 stsp ncommits++;
2075 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2076 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2077 80ddbec8 2018-04-29 stsp }
2078 80ddbec8 2018-04-29 stsp
2079 9b058f45 2022-06-30 mark view_border(view);
2080 1a76625f 2018-10-22 stsp done:
2081 1a76625f 2018-10-22 stsp free(id_str);
2082 8b473291 2019-02-21 stsp free(refs_str);
2083 1a76625f 2018-10-22 stsp free(ncommits_str);
2084 1a76625f 2018-10-22 stsp free(header);
2085 80ddbec8 2018-04-29 stsp return err;
2086 9f7d7167 2018-04-29 stsp }
2087 07b55e75 2018-05-10 stsp
2088 07b55e75 2018-05-10 stsp static void
2089 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2090 07b55e75 2018-05-10 stsp {
2091 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2092 07b55e75 2018-05-10 stsp int nscrolled = 0;
2093 07b55e75 2018-05-10 stsp
2094 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2095 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2096 07b55e75 2018-05-10 stsp return;
2097 9f7d7167 2018-04-29 stsp
2098 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2099 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2100 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2101 07b55e75 2018-05-10 stsp if (entry) {
2102 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2103 07b55e75 2018-05-10 stsp nscrolled++;
2104 07b55e75 2018-05-10 stsp }
2105 07b55e75 2018-05-10 stsp }
2106 aa075928 2018-05-10 stsp }
2107 aa075928 2018-05-10 stsp
2108 aa075928 2018-05-10 stsp static const struct got_error *
2109 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2110 aa075928 2018-05-10 stsp {
2111 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2112 5e224a3e 2019-02-22 stsp int errcode;
2113 8a42fca8 2019-02-22 stsp
2114 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2115 aa075928 2018-05-10 stsp
2116 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2117 ffe38506 2020-12-01 naddy if (ta->log_complete)
2118 5e224a3e 2019-02-22 stsp break;
2119 b295e71b 2019-02-22 stsp
2120 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2121 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2122 7aafa0d1 2019-02-22 stsp if (errcode)
2123 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2124 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2125 7c1452c1 2020-03-26 stsp
2126 7c1452c1 2020-03-26 stsp /*
2127 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2128 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2129 7c1452c1 2020-03-26 stsp */
2130 7c1452c1 2020-03-26 stsp if (!wait)
2131 7c1452c1 2020-03-26 stsp break;
2132 7c1452c1 2020-03-26 stsp
2133 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2134 ffe38506 2020-12-01 naddy show_log_view(view);
2135 7c1452c1 2020-03-26 stsp update_panels();
2136 7c1452c1 2020-03-26 stsp doupdate();
2137 7c1452c1 2020-03-26 stsp
2138 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2139 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2140 82954512 2020-02-03 stsp if (errcode)
2141 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2142 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2143 82954512 2020-02-03 stsp
2144 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2145 ffe38506 2020-12-01 naddy show_log_view(view);
2146 7c1452c1 2020-03-26 stsp update_panels();
2147 7c1452c1 2020-03-26 stsp doupdate();
2148 5e224a3e 2019-02-22 stsp }
2149 5e224a3e 2019-02-22 stsp
2150 5e224a3e 2019-02-22 stsp return NULL;
2151 5e224a3e 2019-02-22 stsp }
2152 5e224a3e 2019-02-22 stsp
2153 5e224a3e 2019-02-22 stsp static const struct got_error *
2154 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2155 9b058f45 2022-06-30 mark {
2156 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2157 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2158 9b058f45 2022-06-30 mark
2159 9b058f45 2022-06-30 mark state->thread_args.commits_needed = view->nscrolled;
2160 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2161 9b058f45 2022-06-30 mark view->nscrolled = 0;
2162 9b058f45 2022-06-30 mark
2163 9b058f45 2022-06-30 mark return err;
2164 9b058f45 2022-06-30 mark }
2165 9b058f45 2022-06-30 mark
2166 9b058f45 2022-06-30 mark static const struct got_error *
2167 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2168 5e224a3e 2019-02-22 stsp {
2169 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2170 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2171 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2172 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2173 5e224a3e 2019-02-22 stsp
2174 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2175 5e224a3e 2019-02-22 stsp return NULL;
2176 5e224a3e 2019-02-22 stsp
2177 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2178 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2179 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2180 08ebd0a9 2019-02-22 stsp /*
2181 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2182 08ebd0a9 2019-02-22 stsp */
2183 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2184 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2185 5e224a3e 2019-02-22 stsp if (err)
2186 5e224a3e 2019-02-22 stsp return err;
2187 7aafa0d1 2019-02-22 stsp }
2188 b295e71b 2019-02-22 stsp
2189 7aafa0d1 2019-02-22 stsp do {
2190 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2191 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2192 88048b54 2019-02-21 stsp break;
2193 88048b54 2019-02-21 stsp
2194 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2195 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2196 aa075928 2018-05-10 stsp
2197 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2198 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2199 dd0a52c1 2018-05-20 stsp break;
2200 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2201 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2202 aa075928 2018-05-10 stsp
2203 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
2204 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2205 9b058f45 2022-06-30 mark else
2206 9b058f45 2022-06-30 mark view->nscrolled = 0;
2207 9b058f45 2022-06-30 mark
2208 dd0a52c1 2018-05-20 stsp return err;
2209 07b55e75 2018-05-10 stsp }
2210 4a7f7875 2018-05-10 stsp
2211 cd0acaa7 2018-05-20 stsp static const struct got_error *
2212 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2213 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2214 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2215 cd0acaa7 2018-05-20 stsp {
2216 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2217 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2218 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2219 cd0acaa7 2018-05-20 stsp
2220 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2221 15a94983 2018-12-23 stsp if (diff_view == NULL)
2222 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2223 ea5e7bb5 2018-08-01 stsp
2224 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2225 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2226 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2227 e5a0f69f 2018-08-18 stsp if (err == NULL)
2228 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2229 cd0acaa7 2018-05-20 stsp return err;
2230 4a7f7875 2018-05-10 stsp }
2231 4a7f7875 2018-05-10 stsp
2232 80ddbec8 2018-04-29 stsp static const struct got_error *
2233 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2234 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2235 9343a5fb 2018-06-23 stsp {
2236 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2237 941e9f74 2019-05-21 stsp
2238 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2239 941e9f74 2019-05-21 stsp if (parent == NULL)
2240 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2241 941e9f74 2019-05-21 stsp
2242 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2243 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2244 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2245 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2246 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2247 941e9f74 2019-05-21 stsp s->tree = subtree;
2248 941e9f74 2019-05-21 stsp s->selected = 0;
2249 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2250 941e9f74 2019-05-21 stsp return NULL;
2251 941e9f74 2019-05-21 stsp }
2252 941e9f74 2019-05-21 stsp
2253 941e9f74 2019-05-21 stsp static const struct got_error *
2254 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2255 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2256 941e9f74 2019-05-21 stsp {
2257 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2258 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2259 941e9f74 2019-05-21 stsp const char *p;
2260 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2261 9343a5fb 2018-06-23 stsp
2262 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2263 941e9f74 2019-05-21 stsp p = path;
2264 941e9f74 2019-05-21 stsp while (*p) {
2265 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2266 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2267 56e0773d 2019-11-28 stsp char *te_name;
2268 33cbf02b 2020-01-12 stsp
2269 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2270 33cbf02b 2020-01-12 stsp p++;
2271 941e9f74 2019-05-21 stsp
2272 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2273 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2274 941e9f74 2019-05-21 stsp if (slash == NULL)
2275 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2276 33cbf02b 2020-01-12 stsp else
2277 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2278 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2279 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2280 56e0773d 2019-11-28 stsp break;
2281 941e9f74 2019-05-21 stsp }
2282 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2283 56e0773d 2019-11-28 stsp if (te == NULL) {
2284 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2285 56e0773d 2019-11-28 stsp free(te_name);
2286 941e9f74 2019-05-21 stsp break;
2287 941e9f74 2019-05-21 stsp }
2288 56e0773d 2019-11-28 stsp free(te_name);
2289 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2290 941e9f74 2019-05-21 stsp
2291 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2292 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2293 b03c880f 2019-05-21 stsp
2294 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2295 941e9f74 2019-05-21 stsp if (slash)
2296 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2297 941e9f74 2019-05-21 stsp else
2298 941e9f74 2019-05-21 stsp subpath = strdup(path);
2299 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2300 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2301 941e9f74 2019-05-21 stsp break;
2302 941e9f74 2019-05-21 stsp }
2303 941e9f74 2019-05-21 stsp
2304 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2305 941e9f74 2019-05-21 stsp subpath);
2306 941e9f74 2019-05-21 stsp if (err)
2307 941e9f74 2019-05-21 stsp break;
2308 941e9f74 2019-05-21 stsp
2309 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2310 941e9f74 2019-05-21 stsp free(tree_id);
2311 941e9f74 2019-05-21 stsp if (err)
2312 941e9f74 2019-05-21 stsp break;
2313 941e9f74 2019-05-21 stsp
2314 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2315 941e9f74 2019-05-21 stsp if (err) {
2316 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2317 941e9f74 2019-05-21 stsp break;
2318 941e9f74 2019-05-21 stsp }
2319 941e9f74 2019-05-21 stsp if (slash == NULL)
2320 941e9f74 2019-05-21 stsp break;
2321 941e9f74 2019-05-21 stsp free(subpath);
2322 941e9f74 2019-05-21 stsp subpath = NULL;
2323 941e9f74 2019-05-21 stsp p = slash;
2324 941e9f74 2019-05-21 stsp }
2325 941e9f74 2019-05-21 stsp
2326 941e9f74 2019-05-21 stsp free(subpath);
2327 1a76625f 2018-10-22 stsp return err;
2328 61266923 2020-01-14 stsp }
2329 61266923 2020-01-14 stsp
2330 61266923 2020-01-14 stsp static const struct got_error *
2331 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2332 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2333 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2334 55cccc34 2020-02-20 stsp {
2335 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2336 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2337 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2338 55cccc34 2020-02-20 stsp
2339 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2340 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2341 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2342 55cccc34 2020-02-20 stsp
2343 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2344 bc573f3b 2021-07-10 stsp if (err)
2345 55cccc34 2020-02-20 stsp return err;
2346 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2347 55cccc34 2020-02-20 stsp
2348 55cccc34 2020-02-20 stsp *new_view = tree_view;
2349 55cccc34 2020-02-20 stsp
2350 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2351 55cccc34 2020-02-20 stsp return NULL;
2352 55cccc34 2020-02-20 stsp
2353 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2354 55cccc34 2020-02-20 stsp }
2355 55cccc34 2020-02-20 stsp
2356 55cccc34 2020-02-20 stsp static const struct got_error *
2357 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2358 61266923 2020-01-14 stsp {
2359 61266923 2020-01-14 stsp sigset_t sigset;
2360 61266923 2020-01-14 stsp int errcode;
2361 61266923 2020-01-14 stsp
2362 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2363 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2364 61266923 2020-01-14 stsp
2365 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2366 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2367 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2368 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2369 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2370 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2371 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2372 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2373 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2374 61266923 2020-01-14 stsp
2375 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2376 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2377 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2378 61266923 2020-01-14 stsp
2379 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2380 61266923 2020-01-14 stsp if (errcode)
2381 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2382 61266923 2020-01-14 stsp
2383 61266923 2020-01-14 stsp return NULL;
2384 1a76625f 2018-10-22 stsp }
2385 1a76625f 2018-10-22 stsp
2386 1a76625f 2018-10-22 stsp static void *
2387 1a76625f 2018-10-22 stsp log_thread(void *arg)
2388 1a76625f 2018-10-22 stsp {
2389 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2390 1a76625f 2018-10-22 stsp int errcode = 0;
2391 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2392 1a76625f 2018-10-22 stsp int done = 0;
2393 61266923 2020-01-14 stsp
2394 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2395 61266923 2020-01-14 stsp if (err)
2396 61266923 2020-01-14 stsp return (void *)err;
2397 1a76625f 2018-10-22 stsp
2398 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2399 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2400 1a76625f 2018-10-22 stsp if (err) {
2401 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2402 1a76625f 2018-10-22 stsp return (void *)err;
2403 1a76625f 2018-10-22 stsp err = NULL;
2404 1a76625f 2018-10-22 stsp done = 1;
2405 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2406 1a76625f 2018-10-22 stsp a->commits_needed--;
2407 1a76625f 2018-10-22 stsp
2408 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2409 3abe8080 2019-04-10 stsp if (errcode) {
2410 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2411 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2412 3abe8080 2019-04-10 stsp break;
2413 3abe8080 2019-04-10 stsp } else if (*a->quit)
2414 1a76625f 2018-10-22 stsp done = 1;
2415 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2416 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2417 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2418 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2419 1a76625f 2018-10-22 stsp }
2420 1a76625f 2018-10-22 stsp
2421 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2422 7c1452c1 2020-03-26 stsp if (errcode) {
2423 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2424 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2425 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2426 7c1452c1 2020-03-26 stsp break;
2427 7c1452c1 2020-03-26 stsp }
2428 7c1452c1 2020-03-26 stsp
2429 1a76625f 2018-10-22 stsp if (done)
2430 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2431 7c1452c1 2020-03-26 stsp else {
2432 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2433 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2434 7c1452c1 2020-03-26 stsp &tog_mutex);
2435 7c1452c1 2020-03-26 stsp if (errcode)
2436 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2437 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2438 21355643 2020-12-06 stsp if (*a->quit)
2439 21355643 2020-12-06 stsp done = 1;
2440 7c1452c1 2020-03-26 stsp }
2441 1a76625f 2018-10-22 stsp }
2442 1a76625f 2018-10-22 stsp
2443 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2444 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2445 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2446 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2447 1a76625f 2018-10-22 stsp }
2448 3abe8080 2019-04-10 stsp a->log_complete = 1;
2449 1a76625f 2018-10-22 stsp return (void *)err;
2450 1a76625f 2018-10-22 stsp }
2451 1a76625f 2018-10-22 stsp
2452 1a76625f 2018-10-22 stsp static const struct got_error *
2453 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2454 1a76625f 2018-10-22 stsp {
2455 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2456 1a76625f 2018-10-22 stsp int errcode;
2457 1a76625f 2018-10-22 stsp
2458 1a76625f 2018-10-22 stsp if (s->thread) {
2459 1a76625f 2018-10-22 stsp s->quit = 1;
2460 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2461 1a76625f 2018-10-22 stsp if (errcode)
2462 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2463 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2464 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2465 1a76625f 2018-10-22 stsp if (errcode)
2466 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2467 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2468 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2469 1a76625f 2018-10-22 stsp if (errcode)
2470 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2471 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2472 1a76625f 2018-10-22 stsp if (errcode)
2473 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2474 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2475 1a76625f 2018-10-22 stsp s->thread = NULL;
2476 1a76625f 2018-10-22 stsp }
2477 1a76625f 2018-10-22 stsp
2478 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2479 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2480 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2481 74467cc8 2022-06-15 stsp }
2482 74467cc8 2022-06-15 stsp
2483 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2484 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2485 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2486 74467cc8 2022-06-15 stsp if (err == NULL)
2487 74467cc8 2022-06-15 stsp err = pack_err;
2488 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2489 1a76625f 2018-10-22 stsp }
2490 1a76625f 2018-10-22 stsp
2491 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2492 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2493 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2494 1a76625f 2018-10-22 stsp }
2495 1a76625f 2018-10-22 stsp
2496 9343a5fb 2018-06-23 stsp return err;
2497 9343a5fb 2018-06-23 stsp }
2498 9343a5fb 2018-06-23 stsp
2499 9343a5fb 2018-06-23 stsp static const struct got_error *
2500 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2501 1a76625f 2018-10-22 stsp {
2502 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2503 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2504 276b94a1 2020-11-13 naddy int errcode;
2505 1a76625f 2018-10-22 stsp
2506 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2507 276b94a1 2020-11-13 naddy
2508 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2509 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2510 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2511 276b94a1 2020-11-13 naddy
2512 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2513 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2514 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2515 276b94a1 2020-11-13 naddy
2516 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2517 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2518 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2519 1a76625f 2018-10-22 stsp free(s->start_id);
2520 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2521 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2522 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2523 1a76625f 2018-10-22 stsp return err;
2524 1a76625f 2018-10-22 stsp }
2525 1a76625f 2018-10-22 stsp
2526 1a76625f 2018-10-22 stsp static const struct got_error *
2527 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2528 60493ae3 2019-06-20 stsp {
2529 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2530 60493ae3 2019-06-20 stsp
2531 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2532 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2533 60493ae3 2019-06-20 stsp return NULL;
2534 60493ae3 2019-06-20 stsp }
2535 60493ae3 2019-06-20 stsp
2536 60493ae3 2019-06-20 stsp static const struct got_error *
2537 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2538 60493ae3 2019-06-20 stsp {
2539 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2540 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2541 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2542 60493ae3 2019-06-20 stsp
2543 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2544 f9686aa5 2020-03-27 stsp show_log_view(view);
2545 f9686aa5 2020-03-27 stsp update_panels();
2546 f9686aa5 2020-03-27 stsp doupdate();
2547 f9686aa5 2020-03-27 stsp
2548 96e2b566 2019-07-08 stsp if (s->search_entry) {
2549 13add988 2019-10-15 stsp int errcode, ch;
2550 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2551 13add988 2019-10-15 stsp if (errcode)
2552 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2553 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2554 13add988 2019-10-15 stsp ch = wgetch(view->window);
2555 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2556 13add988 2019-10-15 stsp if (errcode)
2557 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2558 13add988 2019-10-15 stsp "pthread_mutex_lock");
2559 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2560 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2561 678cbce5 2019-07-28 stsp return NULL;
2562 678cbce5 2019-07-28 stsp }
2563 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2564 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2565 96e2b566 2019-07-08 stsp else
2566 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2567 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2568 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2569 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2570 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2571 364ac6fd 2022-06-18 stsp
2572 364ac6fd 2022-06-18 stsp /*
2573 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2574 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2575 f704b35c 2022-06-18 stsp * might have changed.
2576 364ac6fd 2022-06-18 stsp */
2577 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2578 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2579 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2580 364ac6fd 2022-06-18 stsp else
2581 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2582 4bfe9f0a 2022-06-18 stsp } else {
2583 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2584 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2585 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2586 364ac6fd 2022-06-18 stsp else
2587 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2588 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2589 4bfe9f0a 2022-06-18 stsp }
2590 20be8d96 2019-06-21 stsp } else {
2591 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2592 20be8d96 2019-06-21 stsp }
2593 60493ae3 2019-06-20 stsp
2594 60493ae3 2019-06-20 stsp while (1) {
2595 13add988 2019-10-15 stsp int have_match = 0;
2596 13add988 2019-10-15 stsp
2597 60493ae3 2019-06-20 stsp if (entry == NULL) {
2598 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2599 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2600 f9967bca 2020-03-27 stsp view->search_next_done =
2601 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2602 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2603 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2604 f801134a 2019-06-25 stsp return NULL;
2605 60493ae3 2019-06-20 stsp }
2606 96e2b566 2019-07-08 stsp /*
2607 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2608 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2609 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2610 96e2b566 2019-07-08 stsp */
2611 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2612 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2613 60493ae3 2019-06-20 stsp }
2614 60493ae3 2019-06-20 stsp
2615 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2616 13add988 2019-10-15 stsp &view->regex);
2617 5943eee2 2019-08-13 stsp if (err)
2618 13add988 2019-10-15 stsp break;
2619 13add988 2019-10-15 stsp if (have_match) {
2620 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2621 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2622 60493ae3 2019-06-20 stsp break;
2623 60493ae3 2019-06-20 stsp }
2624 13add988 2019-10-15 stsp
2625 96e2b566 2019-07-08 stsp s->search_entry = entry;
2626 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2627 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2628 b1bf1435 2019-06-21 stsp else
2629 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2630 60493ae3 2019-06-20 stsp }
2631 60493ae3 2019-06-20 stsp
2632 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2633 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2634 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2635 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2636 60493ae3 2019-06-20 stsp if (err)
2637 60493ae3 2019-06-20 stsp return err;
2638 ead14cbe 2019-06-21 stsp cur++;
2639 ead14cbe 2019-06-21 stsp }
2640 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2641 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2642 60493ae3 2019-06-20 stsp if (err)
2643 60493ae3 2019-06-20 stsp return err;
2644 ead14cbe 2019-06-21 stsp cur--;
2645 60493ae3 2019-06-20 stsp }
2646 60493ae3 2019-06-20 stsp }
2647 60493ae3 2019-06-20 stsp
2648 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2649 96e2b566 2019-07-08 stsp
2650 60493ae3 2019-06-20 stsp return NULL;
2651 60493ae3 2019-06-20 stsp }
2652 60493ae3 2019-06-20 stsp
2653 60493ae3 2019-06-20 stsp static const struct got_error *
2654 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2655 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2656 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2657 80ddbec8 2018-04-29 stsp {
2658 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2659 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2660 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2661 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2662 1a76625f 2018-10-22 stsp int errcode;
2663 80ddbec8 2018-04-29 stsp
2664 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2665 f135c941 2020-02-20 stsp free(s->in_repo_path);
2666 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2667 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2668 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2669 f135c941 2020-02-20 stsp }
2670 ecb28ae0 2018-07-16 stsp
2671 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2672 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2673 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2674 78756c87 2020-11-24 stsp
2675 fb2756b9 2018-08-04 stsp s->repo = repo;
2676 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2677 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2678 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2679 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2680 9cd7cbd1 2020-12-07 stsp goto done;
2681 9cd7cbd1 2020-12-07 stsp }
2682 9cd7cbd1 2020-12-07 stsp }
2683 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2684 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2685 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2686 5036bf37 2018-09-24 stsp goto done;
2687 5036bf37 2018-09-24 stsp }
2688 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2689 e5a0f69f 2018-08-18 stsp
2690 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2691 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2692 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2693 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2694 11b20872 2019-11-08 stsp if (err)
2695 11b20872 2019-11-08 stsp goto done;
2696 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2697 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2698 11b20872 2019-11-08 stsp if (err) {
2699 11b20872 2019-11-08 stsp free_colors(&s->colors);
2700 11b20872 2019-11-08 stsp goto done;
2701 11b20872 2019-11-08 stsp }
2702 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2703 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2704 11b20872 2019-11-08 stsp if (err) {
2705 11b20872 2019-11-08 stsp free_colors(&s->colors);
2706 11b20872 2019-11-08 stsp goto done;
2707 11b20872 2019-11-08 stsp }
2708 11b20872 2019-11-08 stsp }
2709 11b20872 2019-11-08 stsp
2710 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2711 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2712 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2713 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2714 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2715 1a76625f 2018-10-22 stsp
2716 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
2717 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2718 74467cc8 2022-06-15 stsp if (err)
2719 74467cc8 2022-06-15 stsp goto done;
2720 74467cc8 2022-06-15 stsp }
2721 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2722 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
2723 0ae84acc 2022-06-15 tracey if (err)
2724 0ae84acc 2022-06-15 tracey goto done;
2725 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2726 b672a97a 2020-01-27 stsp !s->log_branches);
2727 1a76625f 2018-10-22 stsp if (err)
2728 1a76625f 2018-10-22 stsp goto done;
2729 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2730 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2731 c5b78334 2020-01-12 stsp if (err)
2732 c5b78334 2020-01-12 stsp goto done;
2733 1a76625f 2018-10-22 stsp
2734 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2735 1a76625f 2018-10-22 stsp if (errcode) {
2736 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2737 1a76625f 2018-10-22 stsp goto done;
2738 1a76625f 2018-10-22 stsp }
2739 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2740 7c1452c1 2020-03-26 stsp if (errcode) {
2741 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2742 7c1452c1 2020-03-26 stsp goto done;
2743 7c1452c1 2020-03-26 stsp }
2744 1a76625f 2018-10-22 stsp
2745 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2746 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2747 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2748 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2749 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2750 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2751 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2752 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2753 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2754 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2755 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2756 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2757 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2758 ba4f502b 2018-08-04 stsp done:
2759 1a76625f 2018-10-22 stsp if (err)
2760 1a76625f 2018-10-22 stsp close_log_view(view);
2761 ba4f502b 2018-08-04 stsp return err;
2762 ba4f502b 2018-08-04 stsp }
2763 ba4f502b 2018-08-04 stsp
2764 e5a0f69f 2018-08-18 stsp static const struct got_error *
2765 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2766 ba4f502b 2018-08-04 stsp {
2767 f2f6d207 2020-11-24 stsp const struct got_error *err;
2768 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2769 ba4f502b 2018-08-04 stsp
2770 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2771 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2772 2b380cc8 2018-10-24 stsp &s->thread_args);
2773 2b380cc8 2018-10-24 stsp if (errcode)
2774 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2775 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2776 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2777 f2f6d207 2020-11-24 stsp if (err)
2778 f2f6d207 2020-11-24 stsp return err;
2779 f2f6d207 2020-11-24 stsp }
2780 2b380cc8 2018-10-24 stsp }
2781 2b380cc8 2018-10-24 stsp
2782 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2783 b880cc75 2022-06-30 mark }
2784 b880cc75 2022-06-30 mark
2785 b880cc75 2022-06-30 mark static void
2786 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
2787 b880cc75 2022-06-30 mark {
2788 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2789 b880cc75 2022-06-30 mark
2790 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
2791 b880cc75 2022-06-30 mark view->count = 0;
2792 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
2793 b880cc75 2022-06-30 mark return;
2794 b880cc75 2022-06-30 mark
2795 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2796 b880cc75 2022-06-30 mark || home)
2797 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2798 b880cc75 2022-06-30 mark
2799 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
2800 b880cc75 2022-06-30 mark --s->selected;
2801 b880cc75 2022-06-30 mark else
2802 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2803 b880cc75 2022-06-30 mark
2804 b880cc75 2022-06-30 mark select_commit(s);
2805 b880cc75 2022-06-30 mark return;
2806 b880cc75 2022-06-30 mark }
2807 b880cc75 2022-06-30 mark
2808 b880cc75 2022-06-30 mark static const struct got_error *
2809 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
2810 b880cc75 2022-06-30 mark {
2811 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
2812 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
2813 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
2814 b880cc75 2022-06-30 mark
2815 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
2816 b880cc75 2022-06-30 mark if (first == NULL) {
2817 b880cc75 2022-06-30 mark view->count = 0;
2818 b880cc75 2022-06-30 mark return NULL;
2819 b880cc75 2022-06-30 mark }
2820 b880cc75 2022-06-30 mark
2821 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2822 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
2823 b880cc75 2022-06-30 mark return NULL;
2824 b880cc75 2022-06-30 mark
2825 b880cc75 2022-06-30 mark if (!page) {
2826 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
2827 b880cc75 2022-06-30 mark
2828 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2829 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
2830 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
2831 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
2832 b880cc75 2022-06-30 mark ++s->selected;
2833 b880cc75 2022-06-30 mark else
2834 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
2835 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
2836 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2837 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
2838 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
2839 b880cc75 2022-06-30 mark else
2840 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
2841 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
2842 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2843 b880cc75 2022-06-30 mark } else {
2844 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
2845 b880cc75 2022-06-30 mark if (err)
2846 b880cc75 2022-06-30 mark return err;
2847 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
2848 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2849 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
2850 b880cc75 2022-06-30 mark }
2851 b880cc75 2022-06-30 mark }
2852 b880cc75 2022-06-30 mark if (err)
2853 b880cc75 2022-06-30 mark return err;
2854 b880cc75 2022-06-30 mark
2855 9b058f45 2022-06-30 mark /*
2856 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
2857 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
2858 9b058f45 2022-06-30 mark */
2859 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
2860 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2861 9b058f45 2022-06-30 mark
2862 b880cc75 2022-06-30 mark select_commit(s);
2863 b880cc75 2022-06-30 mark
2864 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
2865 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
2866 b880cc75 2022-06-30 mark view->count = 0;
2867 b880cc75 2022-06-30 mark
2868 b880cc75 2022-06-30 mark return NULL;
2869 e5a0f69f 2018-08-18 stsp }
2870 04cc582a 2018-08-01 stsp
2871 9b058f45 2022-06-30 mark /*
2872 9b058f45 2022-06-30 mark * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2873 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2874 9b058f45 2022-06-30 mark * TOG_VIEW_SPLIT_HRZN horizontal split
2875 9b058f45 2022-06-30 mark * Assign start column and line of the new split to *x and *y, respectively,
2876 9b058f45 2022-06-30 mark * and assign view mode to view->mode.
2877 9b058f45 2022-06-30 mark */
2878 9b058f45 2022-06-30 mark static void
2879 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
2880 9b058f45 2022-06-30 mark {
2881 9b058f45 2022-06-30 mark char *mode;
2882 9b058f45 2022-06-30 mark
2883 76364b2d 2022-07-02 mark *x = 0;
2884 76364b2d 2022-07-02 mark *y = 0;
2885 76364b2d 2022-07-02 mark
2886 9b058f45 2022-06-30 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
2887 9b058f45 2022-06-30 mark
2888 9b058f45 2022-06-30 mark if (!mode || mode[0] != 'h') {
2889 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_VERT;
2890 9b058f45 2022-06-30 mark *x = view_split_begin_x(view->begin_x);
2891 9b058f45 2022-06-30 mark } else if (mode && mode[0] == 'h') {
2892 9b058f45 2022-06-30 mark view->mode = TOG_VIEW_SPLIT_HRZN;
2893 9b058f45 2022-06-30 mark *y = view_split_begin_y(view->lines);
2894 9b058f45 2022-06-30 mark }
2895 9b058f45 2022-06-30 mark }
2896 9b058f45 2022-06-30 mark
2897 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
2898 e5a0f69f 2018-08-18 stsp static const struct got_error *
2899 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
2900 9b058f45 2022-06-30 mark {
2901 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2902 9b058f45 2022-06-30 mark
2903 9b058f45 2022-06-30 mark view->nlines = y;
2904 9b058f45 2022-06-30 mark err = view_resize(view);
2905 9b058f45 2022-06-30 mark if (err)
2906 9b058f45 2022-06-30 mark return err;
2907 9b058f45 2022-06-30 mark
2908 9b058f45 2022-06-30 mark err = offset_selection_down(view);
2909 9b058f45 2022-06-30 mark
2910 9b058f45 2022-06-30 mark return err;
2911 9b058f45 2022-06-30 mark }
2912 9b058f45 2022-06-30 mark
2913 9b058f45 2022-06-30 mark static const struct got_error *
2914 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2915 e5a0f69f 2018-08-18 stsp {
2916 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2917 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2918 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2919 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2920 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2921 0dca135e 2022-06-30 mark int begin_x = 0, begin_y = 0, eos, n, nscroll;
2922 80ddbec8 2018-04-29 stsp
2923 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2924 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2925 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2926 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2927 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
2928 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
2929 fb280deb 2021-08-30 stsp }
2930 b880cc75 2022-06-30 mark return err;
2931 528dedf3 2021-08-30 stsp }
2932 0dca135e 2022-06-30 mark
2933 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
2934 0dca135e 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
2935 0dca135e 2022-06-30 mark view_is_splitscreen(view->child))
2936 0dca135e 2022-06-30 mark --eos; /* border */
2937 0dca135e 2022-06-30 mark
2938 528dedf3 2021-08-30 stsp
2939 528dedf3 2021-08-30 stsp switch (ch) {
2940 1e37a5c2 2019-05-12 jcs case 'q':
2941 1e37a5c2 2019-05-12 jcs s->quit = 1;
2942 145b6838 2022-06-16 stsp break;
2943 145b6838 2022-06-16 stsp case '0':
2944 145b6838 2022-06-16 stsp view->x = 0;
2945 145b6838 2022-06-16 stsp break;
2946 145b6838 2022-06-16 stsp case '$':
2947 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
2948 640cd7ff 2022-06-22 mark view->count = 0;
2949 145b6838 2022-06-16 stsp break;
2950 145b6838 2022-06-16 stsp case KEY_RIGHT:
2951 145b6838 2022-06-16 stsp case 'l':
2952 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
2953 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
2954 640cd7ff 2022-06-22 mark else
2955 640cd7ff 2022-06-22 mark view->count = 0;
2956 1e37a5c2 2019-05-12 jcs break;
2957 145b6838 2022-06-16 stsp case KEY_LEFT:
2958 145b6838 2022-06-16 stsp case 'h':
2959 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
2960 640cd7ff 2022-06-22 mark if (view->x <= 0)
2961 640cd7ff 2022-06-22 mark view->count = 0;
2962 145b6838 2022-06-16 stsp break;
2963 1e37a5c2 2019-05-12 jcs case 'k':
2964 1e37a5c2 2019-05-12 jcs case KEY_UP:
2965 1e37a5c2 2019-05-12 jcs case '<':
2966 1e37a5c2 2019-05-12 jcs case ',':
2967 02ffd0d5 2021-10-17 stsp case CTRL('p'):
2968 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
2969 912a3f79 2021-08-30 j break;
2970 912a3f79 2021-08-30 j case 'g':
2971 912a3f79 2021-08-30 j case KEY_HOME:
2972 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
2973 640cd7ff 2022-06-22 mark view->count = 0;
2974 1e37a5c2 2019-05-12 jcs break;
2975 83cc4199 2022-06-13 stsp case CTRL('u'):
2976 33c3719a 2022-06-15 stsp case 'u':
2977 83cc4199 2022-06-13 stsp nscroll /= 2;
2978 83cc4199 2022-06-13 stsp /* FALL THROUGH */
2979 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2980 a4292ac5 2019-05-12 jcs case CTRL('b'):
2981 61417565 2022-06-20 mark case 'b':
2982 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
2983 1e37a5c2 2019-05-12 jcs break;
2984 1e37a5c2 2019-05-12 jcs case 'j':
2985 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2986 1e37a5c2 2019-05-12 jcs case '>':
2987 1e37a5c2 2019-05-12 jcs case '.':
2988 02ffd0d5 2021-10-17 stsp case CTRL('n'):
2989 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
2990 912a3f79 2021-08-30 j break;
2991 912a3f79 2021-08-30 j case 'G':
2992 912a3f79 2021-08-30 j case KEY_END: {
2993 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2994 912a3f79 2021-08-30 j * traverse them all. */
2995 640cd7ff 2022-06-22 mark view->count = 0;
2996 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2997 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2998 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2999 80ddbec8 2018-04-29 stsp }
3000 912a3f79 2021-08-30 j
3001 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3002 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3003 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3004 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3005 f3bc9f1d 2021-09-05 naddy break;
3006 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3007 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3008 f3bc9f1d 2021-09-05 naddy }
3009 f3bc9f1d 2021-09-05 naddy if (n > 0)
3010 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3011 2b779855 2020-12-05 naddy select_commit(s);
3012 1e37a5c2 2019-05-12 jcs break;
3013 912a3f79 2021-08-30 j }
3014 80b7e8da 2022-06-11 stsp case CTRL('d'):
3015 33c3719a 2022-06-15 stsp case 'd':
3016 83cc4199 2022-06-13 stsp nscroll /= 2;
3017 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3018 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3019 61417565 2022-06-20 mark case CTRL('f'):
3020 48bb96f0 2022-06-20 naddy case 'f':
3021 b880cc75 2022-06-30 mark case ' ':
3022 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3023 1e37a5c2 2019-05-12 jcs break;
3024 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3025 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3026 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3027 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3028 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3029 2b779855 2020-12-05 naddy select_commit(s);
3030 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3031 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3032 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3033 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3034 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3035 0bf7f153 2020-12-02 naddy }
3036 1e37a5c2 2019-05-12 jcs break;
3037 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3038 9b058f45 2022-06-30 mark case '\r': {
3039 640cd7ff 2022-06-22 mark view->count = 0;
3040 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3041 e5a0f69f 2018-08-18 stsp break;
3042 9b058f45 2022-06-30 mark
3043 9b058f45 2022-06-30 mark /* get dimensions--don't split till initialisation succeeds */
3044 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3045 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
3046 9b058f45 2022-06-30 mark
3047 9b058f45 2022-06-30 mark err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3048 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3049 78756c87 2020-11-24 stsp view, s->repo);
3050 1e37a5c2 2019-05-12 jcs if (err)
3051 1e37a5c2 2019-05-12 jcs break;
3052 9b058f45 2022-06-30 mark
3053 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3054 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
3055 9b058f45 2022-06-30 mark if (err)
3056 9b058f45 2022-06-30 mark break;
3057 9b058f45 2022-06-30 mark }
3058 9b058f45 2022-06-30 mark
3059 e78dc838 2020-12-04 stsp view->focussed = 0;
3060 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3061 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
3062 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
3063 9b058f45 2022-06-30 mark
3064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3065 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3066 f7013a22 2018-10-24 stsp if (err)
3067 1e37a5c2 2019-05-12 jcs return err;
3068 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_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 = diff_view;
3074 1e37a5c2 2019-05-12 jcs break;
3075 9b058f45 2022-06-30 mark }
3076 1e37a5c2 2019-05-12 jcs case 't':
3077 640cd7ff 2022-06-22 mark view->count = 0;
3078 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3079 5036bf37 2018-09-24 stsp break;
3080 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3081 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3082 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
3083 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3084 4e97c21c 2020-12-06 stsp s->repo);
3085 1e37a5c2 2019-05-12 jcs if (err)
3086 e5a0f69f 2018-08-18 stsp break;
3087 e78dc838 2020-12-04 stsp view->focussed = 0;
3088 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3089 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3090 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3091 1e37a5c2 2019-05-12 jcs if (err)
3092 1e37a5c2 2019-05-12 jcs return err;
3093 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
3094 0dbbbe90 2022-06-17 op if (err)
3095 0dbbbe90 2022-06-17 op return err;
3096 e78dc838 2020-12-04 stsp view->focus_child = 1;
3097 1e37a5c2 2019-05-12 jcs } else
3098 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3099 1e37a5c2 2019-05-12 jcs break;
3100 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3101 21355643 2020-12-06 stsp case CTRL('l'):
3102 21355643 2020-12-06 stsp case 'B':
3103 640cd7ff 2022-06-22 mark view->count = 0;
3104 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3105 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3106 1e37a5c2 2019-05-12 jcs break;
3107 21355643 2020-12-06 stsp err = stop_log_thread(s);
3108 74cfe85e 2020-10-20 stsp if (err)
3109 74cfe85e 2020-10-20 stsp return err;
3110 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3111 21355643 2020-12-06 stsp char *parent_path;
3112 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3113 21355643 2020-12-06 stsp if (err)
3114 21355643 2020-12-06 stsp return err;
3115 21355643 2020-12-06 stsp free(s->in_repo_path);
3116 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3117 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3118 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3119 21355643 2020-12-06 stsp struct got_object_id *start_id;
3120 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3121 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3122 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3123 21355643 2020-12-06 stsp if (err)
3124 21355643 2020-12-06 stsp return err;
3125 21355643 2020-12-06 stsp free(s->start_id);
3126 21355643 2020-12-06 stsp s->start_id = start_id;
3127 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3128 21355643 2020-12-06 stsp } else /* 'B' */
3129 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3130 21355643 2020-12-06 stsp
3131 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3132 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3133 b0dd8d27 2022-06-16 stsp if (err)
3134 b0dd8d27 2022-06-16 stsp return err;
3135 b0dd8d27 2022-06-16 stsp }
3136 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3137 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3138 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3139 74cfe85e 2020-10-20 stsp if (err)
3140 21355643 2020-12-06 stsp return err;
3141 51a10b52 2020-12-26 stsp tog_free_refs();
3142 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3143 ca51c541 2020-12-07 stsp if (err)
3144 ca51c541 2020-12-07 stsp return err;
3145 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3146 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3147 d01904d4 2019-06-25 stsp if (err)
3148 d01904d4 2019-06-25 stsp return err;
3149 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3150 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3151 b672a97a 2020-01-27 stsp if (err)
3152 b672a97a 2020-01-27 stsp return err;
3153 21355643 2020-12-06 stsp free_commits(&s->commits);
3154 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3155 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3156 21355643 2020-12-06 stsp s->selected_entry = NULL;
3157 21355643 2020-12-06 stsp s->selected = 0;
3158 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3159 21355643 2020-12-06 stsp s->quit = 0;
3160 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3161 dfee752e 2022-06-18 op s->matched_entry = NULL;
3162 dfee752e 2022-06-18 op s->search_entry = NULL;
3163 d01904d4 2019-06-25 stsp break;
3164 6458efa5 2020-11-24 stsp case 'r':
3165 640cd7ff 2022-06-22 mark view->count = 0;
3166 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3167 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
3168 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
3169 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
3170 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3171 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3172 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3173 6458efa5 2020-11-24 stsp if (err) {
3174 6458efa5 2020-11-24 stsp view_close(ref_view);
3175 6458efa5 2020-11-24 stsp return err;
3176 6458efa5 2020-11-24 stsp }
3177 e78dc838 2020-12-04 stsp view->focussed = 0;
3178 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3179 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3180 6458efa5 2020-11-24 stsp err = view_close_child(view);
3181 6458efa5 2020-11-24 stsp if (err)
3182 6458efa5 2020-11-24 stsp return err;
3183 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
3184 0dbbbe90 2022-06-17 op if (err)
3185 0dbbbe90 2022-06-17 op return err;
3186 e78dc838 2020-12-04 stsp view->focus_child = 1;
3187 6458efa5 2020-11-24 stsp } else
3188 6458efa5 2020-11-24 stsp *new_view = ref_view;
3189 6458efa5 2020-11-24 stsp break;
3190 1e37a5c2 2019-05-12 jcs default:
3191 640cd7ff 2022-06-22 mark view->count = 0;
3192 1e37a5c2 2019-05-12 jcs break;
3193 899d86c2 2018-05-10 stsp }
3194 e5a0f69f 2018-08-18 stsp
3195 80ddbec8 2018-04-29 stsp return err;
3196 80ddbec8 2018-04-29 stsp }
3197 80ddbec8 2018-04-29 stsp
3198 4ed7e80c 2018-05-20 stsp static const struct got_error *
3199 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3200 c2db6724 2019-01-04 stsp {
3201 c2db6724 2019-01-04 stsp const struct got_error *error;
3202 c2db6724 2019-01-04 stsp
3203 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3204 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3205 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3206 37c06ea4 2019-07-15 stsp #endif
3207 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3208 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3209 c2db6724 2019-01-04 stsp
3210 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3211 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3212 c2db6724 2019-01-04 stsp
3213 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3214 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3215 c2db6724 2019-01-04 stsp
3216 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3217 c2db6724 2019-01-04 stsp if (error != NULL)
3218 c2db6724 2019-01-04 stsp return error;
3219 c2db6724 2019-01-04 stsp
3220 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3221 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3222 c2db6724 2019-01-04 stsp
3223 c2db6724 2019-01-04 stsp return NULL;
3224 c2db6724 2019-01-04 stsp }
3225 c2db6724 2019-01-04 stsp
3226 a915003a 2019-02-05 stsp static void
3227 a915003a 2019-02-05 stsp init_curses(void)
3228 a915003a 2019-02-05 stsp {
3229 2497f032 2022-05-31 stsp /*
3230 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3231 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3232 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3233 2497f032 2022-05-31 stsp */
3234 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3235 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3236 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3237 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3238 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3239 2497f032 2022-05-31 stsp
3240 a915003a 2019-02-05 stsp initscr();
3241 a915003a 2019-02-05 stsp cbreak();
3242 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3243 a915003a 2019-02-05 stsp noecho();
3244 a915003a 2019-02-05 stsp nonl();
3245 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3246 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3247 a915003a 2019-02-05 stsp curs_set(0);
3248 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3249 6d17833f 2019-11-08 stsp start_color();
3250 6d17833f 2019-11-08 stsp use_default_colors();
3251 6d17833f 2019-11-08 stsp }
3252 a915003a 2019-02-05 stsp }
3253 a915003a 2019-02-05 stsp
3254 c2db6724 2019-01-04 stsp static const struct got_error *
3255 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3256 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3257 f135c941 2020-02-20 stsp {
3258 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3259 f135c941 2020-02-20 stsp
3260 f135c941 2020-02-20 stsp if (argc == 0) {
3261 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3262 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3263 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3264 f135c941 2020-02-20 stsp return NULL;
3265 f135c941 2020-02-20 stsp }
3266 f135c941 2020-02-20 stsp
3267 f135c941 2020-02-20 stsp if (worktree) {
3268 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3269 bfd61697 2020-11-14 stsp char *p;
3270 f135c941 2020-02-20 stsp
3271 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3272 f135c941 2020-02-20 stsp if (err)
3273 f135c941 2020-02-20 stsp return err;
3274 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3275 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3276 bfd61697 2020-11-14 stsp p) == -1) {
3277 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3278 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3279 f135c941 2020-02-20 stsp }
3280 f135c941 2020-02-20 stsp free(p);
3281 f135c941 2020-02-20 stsp } else
3282 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3283 f135c941 2020-02-20 stsp
3284 f135c941 2020-02-20 stsp return err;
3285 f135c941 2020-02-20 stsp }
3286 f135c941 2020-02-20 stsp
3287 f135c941 2020-02-20 stsp static const struct got_error *
3288 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3289 9f7d7167 2018-04-29 stsp {
3290 80ddbec8 2018-04-29 stsp const struct got_error *error;
3291 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3292 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3293 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3294 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3295 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3296 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3297 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3298 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3299 04cc582a 2018-08-01 stsp struct tog_view *view;
3300 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3301 80ddbec8 2018-04-29 stsp
3302 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3303 80ddbec8 2018-04-29 stsp switch (ch) {
3304 b672a97a 2020-01-27 stsp case 'b':
3305 b672a97a 2020-01-27 stsp log_branches = 1;
3306 b672a97a 2020-01-27 stsp break;
3307 80ddbec8 2018-04-29 stsp case 'c':
3308 80ddbec8 2018-04-29 stsp start_commit = optarg;
3309 80ddbec8 2018-04-29 stsp break;
3310 ecb28ae0 2018-07-16 stsp case 'r':
3311 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3312 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3313 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3314 9ba1d308 2019-10-21 stsp optarg);
3315 ecb28ae0 2018-07-16 stsp break;
3316 80ddbec8 2018-04-29 stsp default:
3317 17020d27 2019-03-07 stsp usage_log();
3318 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3319 80ddbec8 2018-04-29 stsp }
3320 80ddbec8 2018-04-29 stsp }
3321 80ddbec8 2018-04-29 stsp
3322 80ddbec8 2018-04-29 stsp argc -= optind;
3323 80ddbec8 2018-04-29 stsp argv += optind;
3324 80ddbec8 2018-04-29 stsp
3325 f135c941 2020-02-20 stsp if (argc > 1)
3326 f135c941 2020-02-20 stsp usage_log();
3327 963f97a1 2019-03-18 stsp
3328 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3329 0ae84acc 2022-06-15 tracey if (error != NULL)
3330 0ae84acc 2022-06-15 tracey goto done;
3331 0ae84acc 2022-06-15 tracey
3332 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3333 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3334 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3335 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3336 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3337 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3338 c156c7a4 2020-12-18 stsp goto done;
3339 a1fbf39a 2019-08-11 stsp if (worktree)
3340 6962eb72 2020-02-20 stsp repo_path =
3341 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3342 a1fbf39a 2019-08-11 stsp else
3343 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3344 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3345 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3346 c156c7a4 2020-12-18 stsp goto done;
3347 c156c7a4 2020-12-18 stsp }
3348 ecb28ae0 2018-07-16 stsp }
3349 ecb28ae0 2018-07-16 stsp
3350 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3351 80ddbec8 2018-04-29 stsp if (error != NULL)
3352 ecb28ae0 2018-07-16 stsp goto done;
3353 80ddbec8 2018-04-29 stsp
3354 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3355 f135c941 2020-02-20 stsp repo, worktree);
3356 f135c941 2020-02-20 stsp if (error)
3357 f135c941 2020-02-20 stsp goto done;
3358 f135c941 2020-02-20 stsp
3359 f135c941 2020-02-20 stsp init_curses();
3360 f135c941 2020-02-20 stsp
3361 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3362 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3363 c02c541e 2019-03-29 stsp if (error)
3364 c02c541e 2019-03-29 stsp goto done;
3365 c02c541e 2019-03-29 stsp
3366 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3367 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3368 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3369 87670572 2020-12-26 naddy if (error)
3370 87670572 2020-12-26 naddy goto done;
3371 87670572 2020-12-26 naddy }
3372 51a10b52 2020-12-26 stsp
3373 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3374 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3375 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3376 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3377 d8f38dc4 2020-12-05 stsp if (error)
3378 d8f38dc4 2020-12-05 stsp goto done;
3379 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3380 d8f38dc4 2020-12-05 stsp } else {
3381 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3382 d8f38dc4 2020-12-05 stsp if (error == NULL)
3383 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3384 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3385 d8f38dc4 2020-12-05 stsp goto done;
3386 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3387 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3388 d8f38dc4 2020-12-05 stsp if (error)
3389 d8f38dc4 2020-12-05 stsp goto done;
3390 d8f38dc4 2020-12-05 stsp }
3391 8b473291 2019-02-21 stsp
3392 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3393 04cc582a 2018-08-01 stsp if (view == NULL) {
3394 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3395 04cc582a 2018-08-01 stsp goto done;
3396 04cc582a 2018-08-01 stsp }
3397 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3398 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3399 ba4f502b 2018-08-04 stsp if (error)
3400 ba4f502b 2018-08-04 stsp goto done;
3401 2fc00ff4 2019-08-31 stsp if (worktree) {
3402 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3403 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3404 2fc00ff4 2019-08-31 stsp worktree = NULL;
3405 2fc00ff4 2019-08-31 stsp }
3406 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3407 ecb28ae0 2018-07-16 stsp done:
3408 f135c941 2020-02-20 stsp free(in_repo_path);
3409 ecb28ae0 2018-07-16 stsp free(repo_path);
3410 ecb28ae0 2018-07-16 stsp free(cwd);
3411 899d86c2 2018-05-10 stsp free(start_id);
3412 d8f38dc4 2020-12-05 stsp free(label);
3413 d8f38dc4 2020-12-05 stsp if (ref)
3414 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3415 1d0f4054 2021-06-17 stsp if (repo) {
3416 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3417 1d0f4054 2021-06-17 stsp if (error == NULL)
3418 1d0f4054 2021-06-17 stsp error = close_err;
3419 1d0f4054 2021-06-17 stsp }
3420 ec142235 2019-03-07 stsp if (worktree)
3421 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3422 0ae84acc 2022-06-15 tracey if (pack_fds) {
3423 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3424 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3425 0ae84acc 2022-06-15 tracey if (error == NULL)
3426 0ae84acc 2022-06-15 tracey error = pack_err;
3427 0ae84acc 2022-06-15 tracey }
3428 51a10b52 2020-12-26 stsp tog_free_refs();
3429 80ddbec8 2018-04-29 stsp return error;
3430 9f7d7167 2018-04-29 stsp }
3431 9f7d7167 2018-04-29 stsp
3432 4ed7e80c 2018-05-20 stsp __dead static void
3433 9f7d7167 2018-04-29 stsp usage_diff(void)
3434 9f7d7167 2018-04-29 stsp {
3435 80ddbec8 2018-04-29 stsp endwin();
3436 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3437 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3438 9f7d7167 2018-04-29 stsp exit(1);
3439 b304db33 2018-05-20 stsp }
3440 b304db33 2018-05-20 stsp
3441 6d17833f 2019-11-08 stsp static int
3442 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3443 41605754 2020-11-12 stsp regmatch_t *regmatch)
3444 6d17833f 2019-11-08 stsp {
3445 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3446 6d17833f 2019-11-08 stsp }
3447 6d17833f 2019-11-08 stsp
3448 336075a4 2022-06-25 op static struct tog_color *
3449 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3450 6d17833f 2019-11-08 stsp {
3451 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3452 6d17833f 2019-11-08 stsp
3453 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3454 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3455 6d17833f 2019-11-08 stsp return tc;
3456 6d17833f 2019-11-08 stsp }
3457 6d17833f 2019-11-08 stsp
3458 6d17833f 2019-11-08 stsp return NULL;
3459 6d17833f 2019-11-08 stsp }
3460 6d17833f 2019-11-08 stsp
3461 4ed7e80c 2018-05-20 stsp static const struct got_error *
3462 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3463 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3464 41605754 2020-11-12 stsp {
3465 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3466 44a87665 2022-06-16 stsp char *exstr = NULL;
3467 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3468 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3469 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3470 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3471 41605754 2020-11-12 stsp
3472 41605754 2020-11-12 stsp *wtotal = 0;
3473 1853e0f4 2022-06-16 stsp
3474 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3475 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3476 41605754 2020-11-12 stsp
3477 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3478 44a87665 2022-06-16 stsp if (err)
3479 44a87665 2022-06-16 stsp return err;
3480 44a87665 2022-06-16 stsp
3481 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3482 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3483 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3484 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3485 44a87665 2022-06-16 stsp goto done;
3486 44a87665 2022-06-16 stsp }
3487 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3488 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3489 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3490 1853e0f4 2022-06-16 stsp goto done;
3491 1853e0f4 2022-06-16 stsp }
3492 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3493 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3494 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3495 1853e0f4 2022-06-16 stsp goto done;
3496 1853e0f4 2022-06-16 stsp }
3497 145b6838 2022-06-16 stsp
3498 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3499 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3500 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3501 1853e0f4 2022-06-16 stsp if (err)
3502 1853e0f4 2022-06-16 stsp goto done;
3503 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3504 145b6838 2022-06-16 stsp if (n) {
3505 1853e0f4 2022-06-16 stsp free(wline);
3506 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3507 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3508 1853e0f4 2022-06-16 stsp if (err)
3509 1853e0f4 2022-06-16 stsp goto done;
3510 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3511 1853e0f4 2022-06-16 stsp wlimit -= width;
3512 1853e0f4 2022-06-16 stsp *wtotal += width;
3513 41605754 2020-11-12 stsp }
3514 41605754 2020-11-12 stsp
3515 41605754 2020-11-12 stsp if (wlimit > 0) {
3516 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3517 1853e0f4 2022-06-16 stsp size_t wlen;
3518 1853e0f4 2022-06-16 stsp
3519 1853e0f4 2022-06-16 stsp free(wline);
3520 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3521 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3522 1853e0f4 2022-06-16 stsp if (err)
3523 1853e0f4 2022-06-16 stsp goto done;
3524 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3525 1853e0f4 2022-06-16 stsp while (i < wlen) {
3526 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3527 1853e0f4 2022-06-16 stsp if (width == -1) {
3528 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3529 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3530 1853e0f4 2022-06-16 stsp goto done;
3531 1853e0f4 2022-06-16 stsp }
3532 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3533 1853e0f4 2022-06-16 stsp break;
3534 61417565 2022-06-20 mark w += width;
3535 1853e0f4 2022-06-16 stsp i++;
3536 41605754 2020-11-12 stsp }
3537 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3538 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3539 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3540 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3541 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3542 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3543 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3544 41605754 2020-11-12 stsp }
3545 41605754 2020-11-12 stsp }
3546 41605754 2020-11-12 stsp
3547 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3548 1853e0f4 2022-06-16 stsp free(wline);
3549 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3550 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3551 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3552 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3553 1853e0f4 2022-06-16 stsp if (err)
3554 1853e0f4 2022-06-16 stsp goto done;
3555 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3556 1853e0f4 2022-06-16 stsp } else {
3557 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3558 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3559 1853e0f4 2022-06-16 stsp if (err)
3560 1853e0f4 2022-06-16 stsp goto done;
3561 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3562 1853e0f4 2022-06-16 stsp }
3563 1853e0f4 2022-06-16 stsp *wtotal += width2;
3564 41605754 2020-11-12 stsp }
3565 1853e0f4 2022-06-16 stsp done:
3566 145b6838 2022-06-16 stsp free(wline);
3567 44a87665 2022-06-16 stsp free(exstr);
3568 1853e0f4 2022-06-16 stsp free(seg0);
3569 1853e0f4 2022-06-16 stsp free(seg1);
3570 1853e0f4 2022-06-16 stsp free(seg2);
3571 1853e0f4 2022-06-16 stsp return err;
3572 41605754 2020-11-12 stsp }
3573 41605754 2020-11-12 stsp
3574 41605754 2020-11-12 stsp static const struct got_error *
3575 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3576 26ed57b2 2018-05-19 stsp {
3577 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3578 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3579 61e69b96 2018-05-20 stsp const struct got_error *err;
3580 fe621944 2020-11-10 stsp int nprinted = 0;
3581 b304db33 2018-05-20 stsp char *line;
3582 826082fe 2020-12-10 stsp size_t linesize = 0;
3583 826082fe 2020-12-10 stsp ssize_t linelen;
3584 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3585 61e69b96 2018-05-20 stsp wchar_t *wline;
3586 e0b650dd 2018-05-20 stsp int width;
3587 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3588 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3589 fe621944 2020-11-10 stsp off_t line_offset;
3590 26ed57b2 2018-05-19 stsp
3591 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3592 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3593 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3594 fe621944 2020-11-10 stsp
3595 f7d12f7e 2018-08-01 stsp werase(view->window);
3596 a3404814 2018-09-02 stsp
3597 a3404814 2018-09-02 stsp if (header) {
3598 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3599 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3600 135a2da0 2020-11-11 stsp header) == -1)
3601 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3602 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3603 ccda2f4d 2022-06-16 stsp 0, 0);
3604 135a2da0 2020-11-11 stsp free(line);
3605 135a2da0 2020-11-11 stsp if (err)
3606 a3404814 2018-09-02 stsp return err;
3607 a3404814 2018-09-02 stsp
3608 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3609 a3404814 2018-09-02 stsp wstandout(view->window);
3610 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3611 e54cc94a 2020-11-11 stsp free(wline);
3612 e54cc94a 2020-11-11 stsp wline = NULL;
3613 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3614 a3404814 2018-09-02 stsp wstandend(view->window);
3615 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3616 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3617 26ed57b2 2018-05-19 stsp
3618 a3404814 2018-09-02 stsp if (max_lines <= 1)
3619 a3404814 2018-09-02 stsp return NULL;
3620 a3404814 2018-09-02 stsp max_lines--;
3621 a3404814 2018-09-02 stsp }
3622 a3404814 2018-09-02 stsp
3623 89f1a395 2020-12-01 naddy s->eof = 0;
3624 145b6838 2022-06-16 stsp view->maxx = 0;
3625 826082fe 2020-12-10 stsp line = NULL;
3626 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3627 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3628 826082fe 2020-12-10 stsp if (linelen == -1) {
3629 826082fe 2020-12-10 stsp if (feof(s->f)) {
3630 826082fe 2020-12-10 stsp s->eof = 1;
3631 826082fe 2020-12-10 stsp break;
3632 826082fe 2020-12-10 stsp }
3633 826082fe 2020-12-10 stsp free(line);
3634 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3635 61e69b96 2018-05-20 stsp }
3636 145b6838 2022-06-16 stsp
3637 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3638 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3639 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3640 1853e0f4 2022-06-16 stsp if (err) {
3641 1853e0f4 2022-06-16 stsp free(line);
3642 1853e0f4 2022-06-16 stsp return err;
3643 1853e0f4 2022-06-16 stsp }
3644 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3645 1853e0f4 2022-06-16 stsp free(wline);
3646 1853e0f4 2022-06-16 stsp wline = NULL;
3647 1853e0f4 2022-06-16 stsp
3648 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3649 f26dddb7 2019-11-08 stsp if (tc)
3650 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3651 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3652 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3653 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3654 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3655 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3656 41605754 2020-11-12 stsp if (err) {
3657 41605754 2020-11-12 stsp free(line);
3658 41605754 2020-11-12 stsp return err;
3659 41605754 2020-11-12 stsp }
3660 41605754 2020-11-12 stsp } else {
3661 969c159c 2022-06-16 stsp int skip;
3662 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3663 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3664 969c159c 2022-06-16 stsp if (err) {
3665 969c159c 2022-06-16 stsp free(line);
3666 969c159c 2022-06-16 stsp return err;
3667 969c159c 2022-06-16 stsp }
3668 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3669 969c159c 2022-06-16 stsp free(wline);
3670 41605754 2020-11-12 stsp wline = NULL;
3671 41605754 2020-11-12 stsp }
3672 f26dddb7 2019-11-08 stsp if (tc)
3673 6d17833f 2019-11-08 stsp wattr_off(view->window,
3674 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3675 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3676 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3677 fe621944 2020-11-10 stsp nprinted++;
3678 826082fe 2020-12-10 stsp }
3679 826082fe 2020-12-10 stsp free(line);
3680 fe621944 2020-11-10 stsp if (nprinted >= 1)
3681 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3682 89f1a395 2020-12-01 naddy (nprinted - 1);
3683 fe621944 2020-11-10 stsp else
3684 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3685 26ed57b2 2018-05-19 stsp
3686 9b058f45 2022-06-30 mark view_border(view);
3687 c3e9aa98 2019-05-13 jcs
3688 89f1a395 2020-12-01 naddy if (s->eof) {
3689 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3690 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3691 c3e9aa98 2019-05-13 jcs nprinted++;
3692 c3e9aa98 2019-05-13 jcs }
3693 c3e9aa98 2019-05-13 jcs
3694 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3695 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3696 c3e9aa98 2019-05-13 jcs if (err) {
3697 c3e9aa98 2019-05-13 jcs return err;
3698 c3e9aa98 2019-05-13 jcs }
3699 26ed57b2 2018-05-19 stsp
3700 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3701 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3702 e54cc94a 2020-11-11 stsp free(wline);
3703 e54cc94a 2020-11-11 stsp wline = NULL;
3704 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3705 c3e9aa98 2019-05-13 jcs }
3706 c3e9aa98 2019-05-13 jcs
3707 26ed57b2 2018-05-19 stsp return NULL;
3708 abd2672a 2018-12-23 stsp }
3709 abd2672a 2018-12-23 stsp
3710 abd2672a 2018-12-23 stsp static char *
3711 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3712 abd2672a 2018-12-23 stsp {
3713 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3714 09867e48 2019-08-13 stsp char *p, *s;
3715 09867e48 2019-08-13 stsp
3716 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3717 09867e48 2019-08-13 stsp if (tm == NULL)
3718 09867e48 2019-08-13 stsp return NULL;
3719 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3720 09867e48 2019-08-13 stsp if (s == NULL)
3721 09867e48 2019-08-13 stsp return NULL;
3722 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3723 abd2672a 2018-12-23 stsp if (p)
3724 abd2672a 2018-12-23 stsp *p = '\0';
3725 abd2672a 2018-12-23 stsp return s;
3726 9f7d7167 2018-04-29 stsp }
3727 9f7d7167 2018-04-29 stsp
3728 4ed7e80c 2018-05-20 stsp static const struct got_error *
3729 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3730 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3731 0208f208 2020-05-05 stsp {
3732 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3733 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3734 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3735 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3736 0208f208 2020-05-05 stsp
3737 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3738 0208f208 2020-05-05 stsp if (qid != NULL) {
3739 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3740 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3741 d7b5a0e8 2022-04-20 stsp &qid->id);
3742 0208f208 2020-05-05 stsp if (err)
3743 0208f208 2020-05-05 stsp return err;
3744 0208f208 2020-05-05 stsp
3745 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3746 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3747 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3748 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3749 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3750 aa8b5dd0 2021-08-01 stsp }
3751 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3752 0208f208 2020-05-05 stsp
3753 0208f208 2020-05-05 stsp }
3754 0208f208 2020-05-05 stsp
3755 0208f208 2020-05-05 stsp if (tree_id1) {
3756 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3757 0208f208 2020-05-05 stsp if (err)
3758 0208f208 2020-05-05 stsp goto done;
3759 0208f208 2020-05-05 stsp }
3760 0208f208 2020-05-05 stsp
3761 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3762 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3763 0208f208 2020-05-05 stsp if (err)
3764 0208f208 2020-05-05 stsp goto done;
3765 0208f208 2020-05-05 stsp
3766 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3767 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3768 0208f208 2020-05-05 stsp done:
3769 0208f208 2020-05-05 stsp if (tree1)
3770 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3771 0208f208 2020-05-05 stsp if (tree2)
3772 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3773 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3774 0208f208 2020-05-05 stsp return err;
3775 0208f208 2020-05-05 stsp }
3776 0208f208 2020-05-05 stsp
3777 0208f208 2020-05-05 stsp static const struct got_error *
3778 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3779 abd2672a 2018-12-23 stsp {
3780 fe621944 2020-11-10 stsp off_t *p;
3781 fe621944 2020-11-10 stsp
3782 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3783 fe621944 2020-11-10 stsp if (p == NULL)
3784 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3785 fe621944 2020-11-10 stsp *line_offsets = p;
3786 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3787 fe621944 2020-11-10 stsp (*nlines)++;
3788 fe621944 2020-11-10 stsp return NULL;
3789 fe621944 2020-11-10 stsp }
3790 fe621944 2020-11-10 stsp
3791 fe621944 2020-11-10 stsp static const struct got_error *
3792 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3793 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3794 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3795 fe621944 2020-11-10 stsp {
3796 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3797 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3798 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3799 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3800 45d799e2 2018-12-23 stsp time_t committer_time;
3801 45d799e2 2018-12-23 stsp const char *author, *committer;
3802 8b473291 2019-02-21 stsp char *refs_str = NULL;
3803 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3804 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3805 fe621944 2020-11-10 stsp off_t outoff = 0;
3806 fe621944 2020-11-10 stsp int n;
3807 abd2672a 2018-12-23 stsp
3808 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3809 0208f208 2020-05-05 stsp
3810 8b473291 2019-02-21 stsp if (refs) {
3811 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3812 8b473291 2019-02-21 stsp if (err)
3813 8b473291 2019-02-21 stsp return err;
3814 8b473291 2019-02-21 stsp }
3815 8b473291 2019-02-21 stsp
3816 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3817 abd2672a 2018-12-23 stsp if (err)
3818 abd2672a 2018-12-23 stsp return err;
3819 abd2672a 2018-12-23 stsp
3820 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3821 15a94983 2018-12-23 stsp if (err) {
3822 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3823 15a94983 2018-12-23 stsp goto done;
3824 15a94983 2018-12-23 stsp }
3825 abd2672a 2018-12-23 stsp
3826 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3827 fe621944 2020-11-10 stsp if (err)
3828 fe621944 2020-11-10 stsp goto done;
3829 fe621944 2020-11-10 stsp
3830 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3831 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3832 fe621944 2020-11-10 stsp if (n < 0) {
3833 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3834 abd2672a 2018-12-23 stsp goto done;
3835 abd2672a 2018-12-23 stsp }
3836 fe621944 2020-11-10 stsp outoff += n;
3837 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3838 fe621944 2020-11-10 stsp if (err)
3839 fe621944 2020-11-10 stsp goto done;
3840 fe621944 2020-11-10 stsp
3841 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3842 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3843 fe621944 2020-11-10 stsp if (n < 0) {
3844 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3845 abd2672a 2018-12-23 stsp goto done;
3846 abd2672a 2018-12-23 stsp }
3847 fe621944 2020-11-10 stsp outoff += n;
3848 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3849 fe621944 2020-11-10 stsp if (err)
3850 fe621944 2020-11-10 stsp goto done;
3851 fe621944 2020-11-10 stsp
3852 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3853 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3854 fe621944 2020-11-10 stsp if (datestr) {
3855 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3856 fe621944 2020-11-10 stsp if (n < 0) {
3857 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3858 fe621944 2020-11-10 stsp goto done;
3859 fe621944 2020-11-10 stsp }
3860 fe621944 2020-11-10 stsp outoff += n;
3861 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3862 fe621944 2020-11-10 stsp if (err)
3863 fe621944 2020-11-10 stsp goto done;
3864 abd2672a 2018-12-23 stsp }
3865 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3866 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3867 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3868 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3869 fe621944 2020-11-10 stsp if (n < 0) {
3870 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3871 fe621944 2020-11-10 stsp goto done;
3872 fe621944 2020-11-10 stsp }
3873 fe621944 2020-11-10 stsp outoff += n;
3874 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3875 fe621944 2020-11-10 stsp if (err)
3876 fe621944 2020-11-10 stsp goto done;
3877 abd2672a 2018-12-23 stsp }
3878 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
3879 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
3880 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
3881 9f98ca05 2021-09-24 stsp int pn = 1;
3882 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3883 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
3884 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
3885 9f98ca05 2021-09-24 stsp if (err)
3886 9f98ca05 2021-09-24 stsp goto done;
3887 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3888 9f98ca05 2021-09-24 stsp if (n < 0) {
3889 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
3890 9f98ca05 2021-09-24 stsp goto done;
3891 9f98ca05 2021-09-24 stsp }
3892 9f98ca05 2021-09-24 stsp outoff += n;
3893 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
3894 9f98ca05 2021-09-24 stsp if (err)
3895 9f98ca05 2021-09-24 stsp goto done;
3896 9f98ca05 2021-09-24 stsp free(id_str);
3897 9f98ca05 2021-09-24 stsp id_str = NULL;
3898 9f98ca05 2021-09-24 stsp }
3899 9f98ca05 2021-09-24 stsp }
3900 9f98ca05 2021-09-24 stsp
3901 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3902 5943eee2 2019-08-13 stsp if (err)
3903 5943eee2 2019-08-13 stsp goto done;
3904 fe621944 2020-11-10 stsp s = logmsg;
3905 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3906 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3907 fe621944 2020-11-10 stsp if (n < 0) {
3908 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3909 fe621944 2020-11-10 stsp goto done;
3910 fe621944 2020-11-10 stsp }
3911 fe621944 2020-11-10 stsp outoff += n;
3912 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3913 fe621944 2020-11-10 stsp if (err)
3914 fe621944 2020-11-10 stsp goto done;
3915 abd2672a 2018-12-23 stsp }
3916 fe621944 2020-11-10 stsp
3917 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3918 0208f208 2020-05-05 stsp if (err)
3919 0208f208 2020-05-05 stsp goto done;
3920 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3921 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3922 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3923 fe621944 2020-11-10 stsp if (n < 0) {
3924 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3925 fe621944 2020-11-10 stsp goto done;
3926 fe621944 2020-11-10 stsp }
3927 fe621944 2020-11-10 stsp outoff += n;
3928 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3929 fe621944 2020-11-10 stsp if (err)
3930 fe621944 2020-11-10 stsp goto done;
3931 0208f208 2020-05-05 stsp free((char *)pe->path);
3932 0208f208 2020-05-05 stsp free(pe->data);
3933 0208f208 2020-05-05 stsp }
3934 fe621944 2020-11-10 stsp
3935 0208f208 2020-05-05 stsp fputc('\n', outfile);
3936 fe621944 2020-11-10 stsp outoff++;
3937 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3938 abd2672a 2018-12-23 stsp done:
3939 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3940 abd2672a 2018-12-23 stsp free(id_str);
3941 5943eee2 2019-08-13 stsp free(logmsg);
3942 8b473291 2019-02-21 stsp free(refs_str);
3943 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3944 7510f233 2020-08-09 stsp if (err) {
3945 ae6a6978 2020-08-09 stsp free(*line_offsets);
3946 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3947 ae6a6978 2020-08-09 stsp *nlines = 0;
3948 7510f233 2020-08-09 stsp }
3949 fe621944 2020-11-10 stsp return err;
3950 abd2672a 2018-12-23 stsp }
3951 abd2672a 2018-12-23 stsp
3952 abd2672a 2018-12-23 stsp static const struct got_error *
3953 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3954 26ed57b2 2018-05-19 stsp {
3955 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3956 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3957 15a94983 2018-12-23 stsp int obj_type;
3958 fe621944 2020-11-10 stsp
3959 fe621944 2020-11-10 stsp free(s->line_offsets);
3960 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3961 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3962 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3963 fe621944 2020-11-10 stsp s->nlines = 0;
3964 26ed57b2 2018-05-19 stsp
3965 511a516b 2018-05-19 stsp f = got_opentemp();
3966 48ae06ee 2018-10-18 stsp if (f == NULL) {
3967 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3968 48ae06ee 2018-10-18 stsp goto done;
3969 48ae06ee 2018-10-18 stsp }
3970 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3971 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3972 fb43ecf1 2019-02-11 stsp goto done;
3973 fb43ecf1 2019-02-11 stsp }
3974 48ae06ee 2018-10-18 stsp s->f = f;
3975 26ed57b2 2018-05-19 stsp
3976 15a94983 2018-12-23 stsp if (s->id1)
3977 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3978 15a94983 2018-12-23 stsp else
3979 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3980 15a94983 2018-12-23 stsp if (err)
3981 15a94983 2018-12-23 stsp goto done;
3982 15a94983 2018-12-23 stsp
3983 15a94983 2018-12-23 stsp switch (obj_type) {
3984 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3985 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3986 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
3987 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
3988 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3989 26ed57b2 2018-05-19 stsp break;
3990 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3991 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3992 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
3993 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
3994 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
3995 26ed57b2 2018-05-19 stsp break;
3996 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3997 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3998 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3999 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4000 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4001 abd2672a 2018-12-23 stsp
4002 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4003 abd2672a 2018-12-23 stsp if (err)
4004 3ffacbe1 2020-02-02 tracey goto done;
4005 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4006 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4007 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4008 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4009 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4010 f44b1f58 2020-02-02 tracey if (err)
4011 f44b1f58 2020-02-02 tracey goto done;
4012 f44b1f58 2020-02-02 tracey } else {
4013 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4014 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4015 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4016 fe621944 2020-11-10 stsp err = write_commit_info(
4017 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4018 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4019 f44b1f58 2020-02-02 tracey if (err)
4020 f44b1f58 2020-02-02 tracey goto done;
4021 f5404e4e 2020-02-02 tracey break;
4022 15a087fe 2019-02-21 stsp }
4023 abd2672a 2018-12-23 stsp }
4024 abd2672a 2018-12-23 stsp }
4025 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4026 abd2672a 2018-12-23 stsp
4027 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4028 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4029 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4030 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4031 26ed57b2 2018-05-19 stsp break;
4032 abd2672a 2018-12-23 stsp }
4033 26ed57b2 2018-05-19 stsp default:
4034 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4035 48ae06ee 2018-10-18 stsp break;
4036 26ed57b2 2018-05-19 stsp }
4037 f44b1f58 2020-02-02 tracey if (err)
4038 f44b1f58 2020-02-02 tracey goto done;
4039 48ae06ee 2018-10-18 stsp done:
4040 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4041 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4042 48ae06ee 2018-10-18 stsp return err;
4043 48ae06ee 2018-10-18 stsp }
4044 26ed57b2 2018-05-19 stsp
4045 f5215bb9 2019-02-22 stsp static void
4046 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4047 f5215bb9 2019-02-22 stsp {
4048 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4049 f5215bb9 2019-02-22 stsp update_panels();
4050 f5215bb9 2019-02-22 stsp doupdate();
4051 f44b1f58 2020-02-02 tracey }
4052 f44b1f58 2020-02-02 tracey
4053 f44b1f58 2020-02-02 tracey static const struct got_error *
4054 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4055 f44b1f58 2020-02-02 tracey {
4056 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4057 f44b1f58 2020-02-02 tracey
4058 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4059 f44b1f58 2020-02-02 tracey return NULL;
4060 f44b1f58 2020-02-02 tracey }
4061 f44b1f58 2020-02-02 tracey
4062 f44b1f58 2020-02-02 tracey static const struct got_error *
4063 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4064 f44b1f58 2020-02-02 tracey {
4065 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4066 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4067 f44b1f58 2020-02-02 tracey int lineno;
4068 cb713507 2022-06-16 stsp char *line = NULL;
4069 826082fe 2020-12-10 stsp size_t linesize = 0;
4070 826082fe 2020-12-10 stsp ssize_t linelen;
4071 f44b1f58 2020-02-02 tracey
4072 f44b1f58 2020-02-02 tracey if (!view->searching) {
4073 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4074 f44b1f58 2020-02-02 tracey return NULL;
4075 f44b1f58 2020-02-02 tracey }
4076 f44b1f58 2020-02-02 tracey
4077 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4078 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4079 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4080 f44b1f58 2020-02-02 tracey else
4081 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4082 487cd7d2 2021-12-17 stsp } else
4083 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4084 f44b1f58 2020-02-02 tracey
4085 f44b1f58 2020-02-02 tracey while (1) {
4086 f44b1f58 2020-02-02 tracey off_t offset;
4087 f44b1f58 2020-02-02 tracey
4088 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4089 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4090 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4091 f44b1f58 2020-02-02 tracey break;
4092 f44b1f58 2020-02-02 tracey }
4093 f44b1f58 2020-02-02 tracey
4094 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4095 f44b1f58 2020-02-02 tracey lineno = 1;
4096 f44b1f58 2020-02-02 tracey else
4097 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4098 f44b1f58 2020-02-02 tracey }
4099 f44b1f58 2020-02-02 tracey
4100 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4101 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4102 f44b1f58 2020-02-02 tracey free(line);
4103 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4104 f44b1f58 2020-02-02 tracey }
4105 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4106 cb713507 2022-06-16 stsp if (linelen != -1) {
4107 cb713507 2022-06-16 stsp char *exstr;
4108 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4109 cb713507 2022-06-16 stsp if (err)
4110 cb713507 2022-06-16 stsp break;
4111 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4112 cb713507 2022-06-16 stsp &view->regmatch)) {
4113 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4114 cb713507 2022-06-16 stsp s->matched_line = lineno;
4115 cb713507 2022-06-16 stsp free(exstr);
4116 cb713507 2022-06-16 stsp break;
4117 cb713507 2022-06-16 stsp }
4118 cb713507 2022-06-16 stsp free(exstr);
4119 f44b1f58 2020-02-02 tracey }
4120 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4121 f44b1f58 2020-02-02 tracey lineno++;
4122 f44b1f58 2020-02-02 tracey else
4123 f44b1f58 2020-02-02 tracey lineno--;
4124 f44b1f58 2020-02-02 tracey }
4125 826082fe 2020-12-10 stsp free(line);
4126 f44b1f58 2020-02-02 tracey
4127 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4128 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4129 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4130 f44b1f58 2020-02-02 tracey }
4131 f44b1f58 2020-02-02 tracey
4132 145b6838 2022-06-16 stsp return err;
4133 f5215bb9 2019-02-22 stsp }
4134 f5215bb9 2019-02-22 stsp
4135 48ae06ee 2018-10-18 stsp static const struct got_error *
4136 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4137 b72706c3 2022-06-01 stsp {
4138 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4139 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4140 b72706c3 2022-06-01 stsp
4141 b72706c3 2022-06-01 stsp free(s->id1);
4142 b72706c3 2022-06-01 stsp s->id1 = NULL;
4143 b72706c3 2022-06-01 stsp free(s->id2);
4144 b72706c3 2022-06-01 stsp s->id2 = NULL;
4145 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4146 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4147 b72706c3 2022-06-01 stsp s->f = NULL;
4148 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4149 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4150 b72706c3 2022-06-01 stsp s->f1 = NULL;
4151 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4152 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4153 b72706c3 2022-06-01 stsp s->f2 = NULL;
4154 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4155 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4156 f9d37699 2022-06-28 stsp s->fd1 = -1;
4157 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4158 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4159 f9d37699 2022-06-28 stsp s->fd2 = -1;
4160 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4161 b72706c3 2022-06-01 stsp free(s->line_offsets);
4162 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4163 b72706c3 2022-06-01 stsp s->nlines = 0;
4164 b72706c3 2022-06-01 stsp return err;
4165 b72706c3 2022-06-01 stsp }
4166 b72706c3 2022-06-01 stsp
4167 b72706c3 2022-06-01 stsp static const struct got_error *
4168 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4169 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4170 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4171 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4172 48ae06ee 2018-10-18 stsp {
4173 48ae06ee 2018-10-18 stsp const struct got_error *err;
4174 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4175 5dc9f4bc 2018-08-04 stsp
4176 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4177 f9d37699 2022-06-28 stsp s->fd1 = -1;
4178 f9d37699 2022-06-28 stsp s->fd2 = -1;
4179 b72706c3 2022-06-01 stsp
4180 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4181 15a94983 2018-12-23 stsp int type1, type2;
4182 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4183 15a94983 2018-12-23 stsp if (err)
4184 15a94983 2018-12-23 stsp return err;
4185 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4186 15a94983 2018-12-23 stsp if (err)
4187 15a94983 2018-12-23 stsp return err;
4188 15a94983 2018-12-23 stsp
4189 15a94983 2018-12-23 stsp if (type1 != type2)
4190 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4191 15a94983 2018-12-23 stsp }
4192 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4193 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4194 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4195 f44b1f58 2020-02-02 tracey s->repo = repo;
4196 f44b1f58 2020-02-02 tracey s->id1 = id1;
4197 f44b1f58 2020-02-02 tracey s->id2 = id2;
4198 3dbaef42 2020-11-24 stsp s->label1 = label1;
4199 3dbaef42 2020-11-24 stsp s->label2 = label2;
4200 48ae06ee 2018-10-18 stsp
4201 15a94983 2018-12-23 stsp if (id1) {
4202 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4203 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4204 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4205 48ae06ee 2018-10-18 stsp } else
4206 5465d566 2020-02-01 tracey s->id1 = NULL;
4207 48ae06ee 2018-10-18 stsp
4208 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4209 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4210 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4211 b72706c3 2022-06-01 stsp goto done;
4212 48ae06ee 2018-10-18 stsp }
4213 b72706c3 2022-06-01 stsp
4214 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4215 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4216 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4217 a00719e9 2022-06-17 stsp goto done;
4218 a00719e9 2022-06-17 stsp }
4219 a00719e9 2022-06-17 stsp
4220 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4221 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4222 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4223 b72706c3 2022-06-01 stsp goto done;
4224 b72706c3 2022-06-01 stsp }
4225 b72706c3 2022-06-01 stsp
4226 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4227 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4228 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4229 f9d37699 2022-06-28 stsp goto done;
4230 f9d37699 2022-06-28 stsp }
4231 f9d37699 2022-06-28 stsp
4232 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4233 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4234 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4235 f9d37699 2022-06-28 stsp goto done;
4236 f9d37699 2022-06-28 stsp }
4237 f9d37699 2022-06-28 stsp
4238 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4239 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4240 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4241 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4242 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4243 5465d566 2020-02-01 tracey s->log_view = log_view;
4244 5465d566 2020-02-01 tracey s->repo = repo;
4245 6d17833f 2019-11-08 stsp
4246 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4247 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4248 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4249 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4250 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4251 6d17833f 2019-11-08 stsp if (err)
4252 b72706c3 2022-06-01 stsp goto done;
4253 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4254 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4255 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4256 b72706c3 2022-06-01 stsp if (err)
4257 b72706c3 2022-06-01 stsp goto done;
4258 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4259 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4260 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4261 b72706c3 2022-06-01 stsp if (err)
4262 b72706c3 2022-06-01 stsp goto done;
4263 6d17833f 2019-11-08 stsp
4264 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4265 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4266 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4267 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4268 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4269 b72706c3 2022-06-01 stsp if (err)
4270 b72706c3 2022-06-01 stsp goto done;
4271 11b20872 2019-11-08 stsp
4272 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4273 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4274 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4275 b72706c3 2022-06-01 stsp if (err)
4276 b72706c3 2022-06-01 stsp goto done;
4277 11b20872 2019-11-08 stsp
4278 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4279 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4280 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4281 b72706c3 2022-06-01 stsp if (err)
4282 b72706c3 2022-06-01 stsp goto done;
4283 6d17833f 2019-11-08 stsp }
4284 5dc9f4bc 2018-08-04 stsp
4285 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4286 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4287 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4288 f5215bb9 2019-02-22 stsp
4289 5465d566 2020-02-01 tracey err = create_diff(s);
4290 48ae06ee 2018-10-18 stsp
4291 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4292 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4293 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4294 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4295 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4296 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4297 b72706c3 2022-06-01 stsp done:
4298 b72706c3 2022-06-01 stsp if (err)
4299 b72706c3 2022-06-01 stsp close_diff_view(view);
4300 e5a0f69f 2018-08-18 stsp return err;
4301 5dc9f4bc 2018-08-04 stsp }
4302 5dc9f4bc 2018-08-04 stsp
4303 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4304 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4305 5dc9f4bc 2018-08-04 stsp {
4306 a3404814 2018-09-02 stsp const struct got_error *err;
4307 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4308 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4309 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4310 a3404814 2018-09-02 stsp
4311 a3404814 2018-09-02 stsp if (s->id1) {
4312 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4313 a3404814 2018-09-02 stsp if (err)
4314 a3404814 2018-09-02 stsp return err;
4315 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4316 3dbaef42 2020-11-24 stsp } else
4317 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4318 3dbaef42 2020-11-24 stsp
4319 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4320 a3404814 2018-09-02 stsp if (err)
4321 a3404814 2018-09-02 stsp return err;
4322 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4323 26ed57b2 2018-05-19 stsp
4324 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4325 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4326 a3404814 2018-09-02 stsp free(id_str1);
4327 a3404814 2018-09-02 stsp free(id_str2);
4328 a3404814 2018-09-02 stsp return err;
4329 a3404814 2018-09-02 stsp }
4330 a3404814 2018-09-02 stsp free(id_str1);
4331 a3404814 2018-09-02 stsp free(id_str2);
4332 a3404814 2018-09-02 stsp
4333 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4334 267bb3b8 2021-08-01 stsp free(header);
4335 267bb3b8 2021-08-01 stsp return err;
4336 15a087fe 2019-02-21 stsp }
4337 15a087fe 2019-02-21 stsp
4338 15a087fe 2019-02-21 stsp static const struct got_error *
4339 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4340 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4341 15a087fe 2019-02-21 stsp {
4342 d7a04538 2019-02-21 stsp const struct got_error *err;
4343 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4344 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4345 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4346 15a087fe 2019-02-21 stsp
4347 15a087fe 2019-02-21 stsp free(s->id2);
4348 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4349 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4350 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4351 15a087fe 2019-02-21 stsp
4352 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4353 d7a04538 2019-02-21 stsp if (err)
4354 d7a04538 2019-02-21 stsp return err;
4355 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4356 15a087fe 2019-02-21 stsp free(s->id1);
4357 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4358 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4359 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4360 15a087fe 2019-02-21 stsp return NULL;
4361 0cf4efb1 2018-09-29 stsp }
4362 0cf4efb1 2018-09-29 stsp
4363 0cf4efb1 2018-09-29 stsp static const struct got_error *
4364 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4365 917d79a7 2022-07-01 stsp {
4366 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4367 917d79a7 2022-07-01 stsp
4368 917d79a7 2022-07-01 stsp view->count = 0;
4369 917d79a7 2022-07-01 stsp wclear(view->window);
4370 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4371 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4372 917d79a7 2022-07-01 stsp s->matched_line = 0;
4373 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4374 917d79a7 2022-07-01 stsp return create_diff(s);
4375 917d79a7 2022-07-01 stsp }
4376 917d79a7 2022-07-01 stsp
4377 917d79a7 2022-07-01 stsp static const struct got_error *
4378 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4379 e5a0f69f 2018-08-18 stsp {
4380 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4381 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4382 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4383 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4384 826082fe 2020-12-10 stsp char *line = NULL;
4385 826082fe 2020-12-10 stsp size_t linesize = 0;
4386 826082fe 2020-12-10 stsp ssize_t linelen;
4387 83cc4199 2022-06-13 stsp int i, nscroll = view->nlines - 1;
4388 e5a0f69f 2018-08-18 stsp
4389 e5a0f69f 2018-08-18 stsp switch (ch) {
4390 145b6838 2022-06-16 stsp case '0':
4391 145b6838 2022-06-16 stsp view->x = 0;
4392 145b6838 2022-06-16 stsp break;
4393 145b6838 2022-06-16 stsp case '$':
4394 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4395 640cd7ff 2022-06-22 mark view->count = 0;
4396 145b6838 2022-06-16 stsp break;
4397 145b6838 2022-06-16 stsp case KEY_RIGHT:
4398 145b6838 2022-06-16 stsp case 'l':
4399 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4400 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4401 640cd7ff 2022-06-22 mark else
4402 640cd7ff 2022-06-22 mark view->count = 0;
4403 145b6838 2022-06-16 stsp break;
4404 145b6838 2022-06-16 stsp case KEY_LEFT:
4405 145b6838 2022-06-16 stsp case 'h':
4406 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4407 640cd7ff 2022-06-22 mark if (view->x <= 0)
4408 640cd7ff 2022-06-22 mark view->count = 0;
4409 145b6838 2022-06-16 stsp break;
4410 64453f7e 2020-11-21 stsp case 'a':
4411 3dbaef42 2020-11-24 stsp case 'w':
4412 3dbaef42 2020-11-24 stsp if (ch == 'a')
4413 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4414 3dbaef42 2020-11-24 stsp if (ch == 'w')
4415 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4416 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4417 912a3f79 2021-08-30 j break;
4418 912a3f79 2021-08-30 j case 'g':
4419 912a3f79 2021-08-30 j case KEY_HOME:
4420 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4421 640cd7ff 2022-06-22 mark view->count = 0;
4422 64453f7e 2020-11-21 stsp break;
4423 912a3f79 2021-08-30 j case 'G':
4424 912a3f79 2021-08-30 j case KEY_END:
4425 640cd7ff 2022-06-22 mark view->count = 0;
4426 912a3f79 2021-08-30 j if (s->eof)
4427 912a3f79 2021-08-30 j break;
4428 912a3f79 2021-08-30 j
4429 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4430 912a3f79 2021-08-30 j s->eof = 1;
4431 912a3f79 2021-08-30 j break;
4432 1e37a5c2 2019-05-12 jcs case 'k':
4433 1e37a5c2 2019-05-12 jcs case KEY_UP:
4434 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4435 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4436 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4437 640cd7ff 2022-06-22 mark else
4438 640cd7ff 2022-06-22 mark view->count = 0;
4439 1e37a5c2 2019-05-12 jcs break;
4440 83cc4199 2022-06-13 stsp case CTRL('u'):
4441 33c3719a 2022-06-15 stsp case 'u':
4442 83cc4199 2022-06-13 stsp nscroll /= 2;
4443 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4444 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4445 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4446 61417565 2022-06-20 mark case 'b':
4447 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4448 640cd7ff 2022-06-22 mark view->count = 0;
4449 26ed57b2 2018-05-19 stsp break;
4450 640cd7ff 2022-06-22 mark }
4451 1e37a5c2 2019-05-12 jcs i = 0;
4452 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4453 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4454 1e37a5c2 2019-05-12 jcs break;
4455 1e37a5c2 2019-05-12 jcs case 'j':
4456 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4457 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4458 1e37a5c2 2019-05-12 jcs if (!s->eof)
4459 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4460 640cd7ff 2022-06-22 mark else
4461 640cd7ff 2022-06-22 mark view->count = 0;
4462 1e37a5c2 2019-05-12 jcs break;
4463 83cc4199 2022-06-13 stsp case CTRL('d'):
4464 33c3719a 2022-06-15 stsp case 'd':
4465 83cc4199 2022-06-13 stsp nscroll /= 2;
4466 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4467 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4468 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4469 61417565 2022-06-20 mark case 'f':
4470 1e37a5c2 2019-05-12 jcs case ' ':
4471 640cd7ff 2022-06-22 mark if (s->eof) {
4472 640cd7ff 2022-06-22 mark view->count = 0;
4473 1e37a5c2 2019-05-12 jcs break;
4474 640cd7ff 2022-06-22 mark }
4475 1e37a5c2 2019-05-12 jcs i = 0;
4476 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4477 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4478 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4479 826082fe 2020-12-10 stsp if (linelen == -1) {
4480 826082fe 2020-12-10 stsp if (feof(s->f)) {
4481 826082fe 2020-12-10 stsp s->eof = 1;
4482 826082fe 2020-12-10 stsp } else
4483 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4484 34bc9ec9 2019-02-22 stsp break;
4485 826082fe 2020-12-10 stsp }
4486 1e37a5c2 2019-05-12 jcs }
4487 826082fe 2020-12-10 stsp free(line);
4488 1e37a5c2 2019-05-12 jcs break;
4489 1e37a5c2 2019-05-12 jcs case '[':
4490 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4491 1e37a5c2 2019-05-12 jcs s->diff_context--;
4492 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4493 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4494 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4495 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4496 27829c9e 2020-11-21 stsp s->nlines) {
4497 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4498 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4499 27829c9e 2020-11-21 stsp }
4500 640cd7ff 2022-06-22 mark } else
4501 640cd7ff 2022-06-22 mark view->count = 0;
4502 1e37a5c2 2019-05-12 jcs break;
4503 1e37a5c2 2019-05-12 jcs case ']':
4504 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4505 1e37a5c2 2019-05-12 jcs s->diff_context++;
4506 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4507 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4508 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4509 640cd7ff 2022-06-22 mark } else
4510 640cd7ff 2022-06-22 mark view->count = 0;
4511 1e37a5c2 2019-05-12 jcs break;
4512 1e37a5c2 2019-05-12 jcs case '<':
4513 1e37a5c2 2019-05-12 jcs case ',':
4514 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4515 640cd7ff 2022-06-22 mark view->count = 0;
4516 48ae06ee 2018-10-18 stsp break;
4517 640cd7ff 2022-06-22 mark }
4518 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4519 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4520 6524637e 2019-02-21 stsp
4521 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4522 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4523 1e37a5c2 2019-05-12 jcs if (err)
4524 1e37a5c2 2019-05-12 jcs break;
4525 15a087fe 2019-02-21 stsp
4526 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4527 5a8b5076 2020-12-05 stsp break;
4528 5a8b5076 2020-12-05 stsp
4529 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4530 1e37a5c2 2019-05-12 jcs if (err)
4531 1e37a5c2 2019-05-12 jcs break;
4532 15a087fe 2019-02-21 stsp
4533 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4534 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4535 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4536 145b6838 2022-06-16 stsp view->x = 0;
4537 15a087fe 2019-02-21 stsp
4538 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4539 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4540 1e37a5c2 2019-05-12 jcs break;
4541 1e37a5c2 2019-05-12 jcs case '>':
4542 1e37a5c2 2019-05-12 jcs case '.':
4543 640cd7ff 2022-06-22 mark if (s->log_view == NULL) {
4544 640cd7ff 2022-06-22 mark view->count = 0;
4545 15a087fe 2019-02-21 stsp break;
4546 640cd7ff 2022-06-22 mark }
4547 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4548 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4549 5e224a3e 2019-02-22 stsp
4550 640cd7ff 2022-06-22 mark /* view->count handled in input_log_view() */
4551 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4552 1e37a5c2 2019-05-12 jcs if (err)
4553 5a8b5076 2020-12-05 stsp break;
4554 5a8b5076 2020-12-05 stsp
4555 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4556 1e37a5c2 2019-05-12 jcs break;
4557 15a087fe 2019-02-21 stsp
4558 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4559 1e37a5c2 2019-05-12 jcs if (err)
4560 1e37a5c2 2019-05-12 jcs break;
4561 15a087fe 2019-02-21 stsp
4562 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4563 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4564 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4565 145b6838 2022-06-16 stsp view->x = 0;
4566 1e37a5c2 2019-05-12 jcs
4567 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4568 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4569 1e37a5c2 2019-05-12 jcs break;
4570 1e37a5c2 2019-05-12 jcs default:
4571 640cd7ff 2022-06-22 mark view->count = 0;
4572 1e37a5c2 2019-05-12 jcs break;
4573 26ed57b2 2018-05-19 stsp }
4574 e5a0f69f 2018-08-18 stsp
4575 bcbd79e2 2018-08-19 stsp return err;
4576 26ed57b2 2018-05-19 stsp }
4577 26ed57b2 2018-05-19 stsp
4578 4ed7e80c 2018-05-20 stsp static const struct got_error *
4579 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4580 9f7d7167 2018-04-29 stsp {
4581 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4582 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4583 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4584 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4585 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4586 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4587 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4588 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4589 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4590 3dbaef42 2020-11-24 stsp const char *errstr;
4591 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4592 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4593 70ac5f84 2019-03-28 stsp
4594 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4595 26ed57b2 2018-05-19 stsp switch (ch) {
4596 64453f7e 2020-11-21 stsp case 'a':
4597 64453f7e 2020-11-21 stsp force_text_diff = 1;
4598 3dbaef42 2020-11-24 stsp break;
4599 3dbaef42 2020-11-24 stsp case 'C':
4600 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4601 3dbaef42 2020-11-24 stsp &errstr);
4602 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4603 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4604 5a20d08d 2022-02-09 op errstr, errstr);
4605 64453f7e 2020-11-21 stsp break;
4606 09b5bff8 2020-02-23 naddy case 'r':
4607 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4608 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4609 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4610 09b5bff8 2020-02-23 naddy optarg);
4611 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4612 09b5bff8 2020-02-23 naddy break;
4613 3dbaef42 2020-11-24 stsp case 'w':
4614 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4615 3dbaef42 2020-11-24 stsp break;
4616 26ed57b2 2018-05-19 stsp default:
4617 17020d27 2019-03-07 stsp usage_diff();
4618 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4619 26ed57b2 2018-05-19 stsp }
4620 26ed57b2 2018-05-19 stsp }
4621 26ed57b2 2018-05-19 stsp
4622 26ed57b2 2018-05-19 stsp argc -= optind;
4623 26ed57b2 2018-05-19 stsp argv += optind;
4624 26ed57b2 2018-05-19 stsp
4625 26ed57b2 2018-05-19 stsp if (argc == 0) {
4626 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4627 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4628 15a94983 2018-12-23 stsp id_str1 = argv[0];
4629 15a94983 2018-12-23 stsp id_str2 = argv[1];
4630 26ed57b2 2018-05-19 stsp } else
4631 26ed57b2 2018-05-19 stsp usage_diff();
4632 eb6600df 2019-01-04 stsp
4633 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4634 0ae84acc 2022-06-15 tracey if (error)
4635 0ae84acc 2022-06-15 tracey goto done;
4636 0ae84acc 2022-06-15 tracey
4637 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4638 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4639 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4640 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4641 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4642 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4643 c156c7a4 2020-12-18 stsp goto done;
4644 a273ac94 2020-02-23 naddy if (worktree)
4645 a273ac94 2020-02-23 naddy repo_path =
4646 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4647 a273ac94 2020-02-23 naddy else
4648 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4649 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4650 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4651 c156c7a4 2020-12-18 stsp goto done;
4652 c156c7a4 2020-12-18 stsp }
4653 a273ac94 2020-02-23 naddy }
4654 a273ac94 2020-02-23 naddy
4655 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4656 eb6600df 2019-01-04 stsp if (error)
4657 eb6600df 2019-01-04 stsp goto done;
4658 26ed57b2 2018-05-19 stsp
4659 a273ac94 2020-02-23 naddy init_curses();
4660 a273ac94 2020-02-23 naddy
4661 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4662 51a10b52 2020-12-26 stsp if (error)
4663 51a10b52 2020-12-26 stsp goto done;
4664 51a10b52 2020-12-26 stsp
4665 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4666 26ed57b2 2018-05-19 stsp if (error)
4667 26ed57b2 2018-05-19 stsp goto done;
4668 26ed57b2 2018-05-19 stsp
4669 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4670 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4671 26ed57b2 2018-05-19 stsp if (error)
4672 26ed57b2 2018-05-19 stsp goto done;
4673 26ed57b2 2018-05-19 stsp
4674 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4675 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4676 26ed57b2 2018-05-19 stsp if (error)
4677 26ed57b2 2018-05-19 stsp goto done;
4678 26ed57b2 2018-05-19 stsp
4679 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4680 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4681 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4682 ea5e7bb5 2018-08-01 stsp goto done;
4683 ea5e7bb5 2018-08-01 stsp }
4684 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4685 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4686 5dc9f4bc 2018-08-04 stsp if (error)
4687 5dc9f4bc 2018-08-04 stsp goto done;
4688 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4689 26ed57b2 2018-05-19 stsp done:
4690 3dbaef42 2020-11-24 stsp free(label1);
4691 3dbaef42 2020-11-24 stsp free(label2);
4692 c02c541e 2019-03-29 stsp free(repo_path);
4693 a273ac94 2020-02-23 naddy free(cwd);
4694 1d0f4054 2021-06-17 stsp if (repo) {
4695 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4696 1d0f4054 2021-06-17 stsp if (error == NULL)
4697 1d0f4054 2021-06-17 stsp error = close_err;
4698 1d0f4054 2021-06-17 stsp }
4699 a273ac94 2020-02-23 naddy if (worktree)
4700 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4701 0ae84acc 2022-06-15 tracey if (pack_fds) {
4702 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4703 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4704 0ae84acc 2022-06-15 tracey if (error == NULL)
4705 0ae84acc 2022-06-15 tracey error = pack_err;
4706 0ae84acc 2022-06-15 tracey }
4707 51a10b52 2020-12-26 stsp tog_free_refs();
4708 26ed57b2 2018-05-19 stsp return error;
4709 9f7d7167 2018-04-29 stsp }
4710 9f7d7167 2018-04-29 stsp
4711 4ed7e80c 2018-05-20 stsp __dead static void
4712 9f7d7167 2018-04-29 stsp usage_blame(void)
4713 9f7d7167 2018-04-29 stsp {
4714 80ddbec8 2018-04-29 stsp endwin();
4715 87411fa9 2022-06-16 stsp fprintf(stderr,
4716 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4717 9f7d7167 2018-04-29 stsp getprogname());
4718 9f7d7167 2018-04-29 stsp exit(1);
4719 9f7d7167 2018-04-29 stsp }
4720 84451b3e 2018-07-10 stsp
4721 84451b3e 2018-07-10 stsp struct tog_blame_line {
4722 84451b3e 2018-07-10 stsp int annotated;
4723 84451b3e 2018-07-10 stsp struct got_object_id *id;
4724 84451b3e 2018-07-10 stsp };
4725 9f7d7167 2018-04-29 stsp
4726 4ed7e80c 2018-05-20 stsp static const struct got_error *
4727 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4728 84451b3e 2018-07-10 stsp {
4729 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4730 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4731 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4732 84451b3e 2018-07-10 stsp const struct got_error *err;
4733 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
4734 826082fe 2020-12-10 stsp char *line = NULL;
4735 826082fe 2020-12-10 stsp size_t linesize = 0;
4736 826082fe 2020-12-10 stsp ssize_t linelen;
4737 84451b3e 2018-07-10 stsp wchar_t *wline;
4738 27a741e5 2019-09-11 stsp int width;
4739 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4740 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4741 ab089a2a 2018-07-12 stsp char *id_str;
4742 11b20872 2019-11-08 stsp struct tog_color *tc;
4743 ab089a2a 2018-07-12 stsp
4744 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
4745 ab089a2a 2018-07-12 stsp if (err)
4746 ab089a2a 2018-07-12 stsp return err;
4747 84451b3e 2018-07-10 stsp
4748 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4749 f7d12f7e 2018-08-01 stsp werase(view->window);
4750 84451b3e 2018-07-10 stsp
4751 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4752 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4753 ab089a2a 2018-07-12 stsp free(id_str);
4754 ab089a2a 2018-07-12 stsp return err;
4755 ab089a2a 2018-07-12 stsp }
4756 ab089a2a 2018-07-12 stsp
4757 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4758 ab089a2a 2018-07-12 stsp free(line);
4759 2550e4c3 2018-07-13 stsp line = NULL;
4760 1cae65b4 2019-09-22 stsp if (err)
4761 1cae65b4 2019-09-22 stsp return err;
4762 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4763 a3404814 2018-09-02 stsp wstandout(view->window);
4764 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4765 11b20872 2019-11-08 stsp if (tc)
4766 11b20872 2019-11-08 stsp wattr_on(view->window,
4767 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4768 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4769 11b20872 2019-11-08 stsp if (tc)
4770 11b20872 2019-11-08 stsp wattr_off(view->window,
4771 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4772 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4773 a3404814 2018-09-02 stsp wstandend(view->window);
4774 2550e4c3 2018-07-13 stsp free(wline);
4775 2550e4c3 2018-07-13 stsp wline = NULL;
4776 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4777 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4778 ab089a2a 2018-07-12 stsp
4779 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4780 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4781 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4782 ab089a2a 2018-07-12 stsp free(id_str);
4783 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4784 ab089a2a 2018-07-12 stsp }
4785 ab089a2a 2018-07-12 stsp free(id_str);
4786 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4787 3f60a8ef 2018-07-10 stsp free(line);
4788 2550e4c3 2018-07-13 stsp line = NULL;
4789 3f60a8ef 2018-07-10 stsp if (err)
4790 3f60a8ef 2018-07-10 stsp return err;
4791 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4792 2550e4c3 2018-07-13 stsp free(wline);
4793 2550e4c3 2018-07-13 stsp wline = NULL;
4794 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4795 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4796 3f60a8ef 2018-07-10 stsp
4797 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4798 145b6838 2022-06-16 stsp view->maxx = 0;
4799 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4800 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4801 826082fe 2020-12-10 stsp if (linelen == -1) {
4802 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4803 826082fe 2020-12-10 stsp s->eof = 1;
4804 826082fe 2020-12-10 stsp break;
4805 826082fe 2020-12-10 stsp }
4806 84451b3e 2018-07-10 stsp free(line);
4807 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4808 84451b3e 2018-07-10 stsp }
4809 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4810 826082fe 2020-12-10 stsp continue;
4811 1853e0f4 2022-06-16 stsp
4812 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
4813 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4814 1853e0f4 2022-06-16 stsp if (err) {
4815 1853e0f4 2022-06-16 stsp free(line);
4816 1853e0f4 2022-06-16 stsp return err;
4817 1853e0f4 2022-06-16 stsp }
4818 1853e0f4 2022-06-16 stsp free(wline);
4819 1853e0f4 2022-06-16 stsp wline = NULL;
4820 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
4821 84451b3e 2018-07-10 stsp
4822 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4823 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4824 b700b5d6 2018-07-10 stsp
4825 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4826 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4827 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4828 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4829 27a741e5 2019-09-11 stsp !(view->focussed &&
4830 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4831 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4832 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4833 8d0fe45a 2019-08-12 stsp char *id_str;
4834 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
4835 87411fa9 2022-06-16 stsp blame_line->id);
4836 8d0fe45a 2019-08-12 stsp if (err) {
4837 8d0fe45a 2019-08-12 stsp free(line);
4838 8d0fe45a 2019-08-12 stsp return err;
4839 8d0fe45a 2019-08-12 stsp }
4840 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4841 11b20872 2019-11-08 stsp if (tc)
4842 11b20872 2019-11-08 stsp wattr_on(view->window,
4843 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4844 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4845 11b20872 2019-11-08 stsp if (tc)
4846 11b20872 2019-11-08 stsp wattr_off(view->window,
4847 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4848 8d0fe45a 2019-08-12 stsp free(id_str);
4849 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4850 8d0fe45a 2019-08-12 stsp } else {
4851 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4852 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4853 84451b3e 2018-07-10 stsp }
4854 ee41ec32 2018-07-10 stsp } else {
4855 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4856 ee41ec32 2018-07-10 stsp prev_id = NULL;
4857 ee41ec32 2018-07-10 stsp }
4858 84451b3e 2018-07-10 stsp
4859 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4860 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4861 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4862 27a741e5 2019-09-11 stsp
4863 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4864 41605754 2020-11-12 stsp width = 9;
4865 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4866 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4867 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4868 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4869 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
4870 41605754 2020-11-12 stsp if (err) {
4871 41605754 2020-11-12 stsp free(line);
4872 41605754 2020-11-12 stsp return err;
4873 41605754 2020-11-12 stsp }
4874 41605754 2020-11-12 stsp width += 9;
4875 41605754 2020-11-12 stsp } else {
4876 4cb9d869 2022-06-16 stsp int skip;
4877 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
4878 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
4879 4cb9d869 2022-06-16 stsp if (err) {
4880 4cb9d869 2022-06-16 stsp free(line);
4881 4cb9d869 2022-06-16 stsp return err;
4882 145b6838 2022-06-16 stsp }
4883 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
4884 a75b3e2d 2022-06-16 stsp width += 9;
4885 41605754 2020-11-12 stsp free(wline);
4886 41605754 2020-11-12 stsp wline = NULL;
4887 41605754 2020-11-12 stsp }
4888 41605754 2020-11-12 stsp
4889 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4890 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4891 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4892 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4893 84451b3e 2018-07-10 stsp }
4894 826082fe 2020-12-10 stsp free(line);
4895 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4896 84451b3e 2018-07-10 stsp
4897 9b058f45 2022-06-30 mark view_border(view);
4898 84451b3e 2018-07-10 stsp
4899 84451b3e 2018-07-10 stsp return NULL;
4900 84451b3e 2018-07-10 stsp }
4901 84451b3e 2018-07-10 stsp
4902 84451b3e 2018-07-10 stsp static const struct got_error *
4903 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
4904 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
4905 84451b3e 2018-07-10 stsp {
4906 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4907 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4908 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4909 1a76625f 2018-10-22 stsp int errcode;
4910 84451b3e 2018-07-10 stsp
4911 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4912 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4913 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4914 84451b3e 2018-07-10 stsp
4915 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4916 1a76625f 2018-10-22 stsp if (errcode)
4917 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4918 84451b3e 2018-07-10 stsp
4919 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4920 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4921 d68a0a7d 2018-07-10 stsp goto done;
4922 d68a0a7d 2018-07-10 stsp }
4923 d68a0a7d 2018-07-10 stsp
4924 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4925 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4926 d68a0a7d 2018-07-10 stsp
4927 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4928 d68a0a7d 2018-07-10 stsp if (line->annotated)
4929 d68a0a7d 2018-07-10 stsp goto done;
4930 d68a0a7d 2018-07-10 stsp
4931 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4932 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4933 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4934 84451b3e 2018-07-10 stsp goto done;
4935 84451b3e 2018-07-10 stsp }
4936 84451b3e 2018-07-10 stsp line->annotated = 1;
4937 84451b3e 2018-07-10 stsp done:
4938 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4939 1a76625f 2018-10-22 stsp if (errcode)
4940 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4941 84451b3e 2018-07-10 stsp return err;
4942 84451b3e 2018-07-10 stsp }
4943 84451b3e 2018-07-10 stsp
4944 84451b3e 2018-07-10 stsp static void *
4945 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4946 84451b3e 2018-07-10 stsp {
4947 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4948 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4949 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4950 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
4951 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
4952 1b484788 2022-06-28 tracey
4953 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
4954 e6e73e55 2022-06-30 tracey if (fd1 == -1)
4955 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
4956 e6e73e55 2022-06-30 tracey
4957 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
4958 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
4959 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
4960 e6e73e55 2022-06-30 tracey goto done;
4961 e6e73e55 2022-06-30 tracey }
4962 18430de3 2018-07-10 stsp
4963 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
4964 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
4965 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4966 e6e73e55 2022-06-30 tracey goto done;
4967 e6e73e55 2022-06-30 tracey }
4968 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
4969 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
4970 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
4971 e6e73e55 2022-06-30 tracey goto done;
4972 e6e73e55 2022-06-30 tracey }
4973 e6e73e55 2022-06-30 tracey
4974 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4975 61266923 2020-01-14 stsp if (err)
4976 e6e73e55 2022-06-30 tracey goto done;
4977 61266923 2020-01-14 stsp
4978 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4979 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
4980 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
4981 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4982 fc06ba56 2019-08-22 stsp err = NULL;
4983 18430de3 2018-07-10 stsp
4984 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4985 e6e73e55 2022-06-30 tracey if (errcode) {
4986 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
4987 e6e73e55 2022-06-30 tracey goto done;
4988 e6e73e55 2022-06-30 tracey }
4989 18430de3 2018-07-10 stsp
4990 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4991 1d0f4054 2021-06-17 stsp if (err == NULL)
4992 1d0f4054 2021-06-17 stsp err = close_err;
4993 c9beca56 2018-07-22 stsp ta->repo = NULL;
4994 c9beca56 2018-07-22 stsp *ta->complete = 1;
4995 18430de3 2018-07-10 stsp
4996 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4997 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4998 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4999 18430de3 2018-07-10 stsp
5000 e6e73e55 2022-06-30 tracey done:
5001 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5002 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5003 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5004 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5005 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5006 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5007 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5008 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5009 1b484788 2022-06-28 tracey
5010 18430de3 2018-07-10 stsp return (void *)err;
5011 84451b3e 2018-07-10 stsp }
5012 84451b3e 2018-07-10 stsp
5013 245d91c1 2018-07-12 stsp static struct got_object_id *
5014 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5015 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5016 245d91c1 2018-07-12 stsp {
5017 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5018 8d0fe45a 2019-08-12 stsp
5019 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5020 8d0fe45a 2019-08-12 stsp return NULL;
5021 b880a918 2018-07-10 stsp
5022 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5023 245d91c1 2018-07-12 stsp if (!line->annotated)
5024 245d91c1 2018-07-12 stsp return NULL;
5025 245d91c1 2018-07-12 stsp
5026 245d91c1 2018-07-12 stsp return line->id;
5027 b880a918 2018-07-10 stsp }
5028 245d91c1 2018-07-12 stsp
5029 b880a918 2018-07-10 stsp static const struct got_error *
5030 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5031 a70480e0 2018-06-23 stsp {
5032 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5033 245d91c1 2018-07-12 stsp int i;
5034 245d91c1 2018-07-12 stsp
5035 245d91c1 2018-07-12 stsp if (blame->thread) {
5036 1a76625f 2018-10-22 stsp int errcode;
5037 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5038 1a76625f 2018-10-22 stsp if (errcode)
5039 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5040 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5041 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5042 1a76625f 2018-10-22 stsp if (errcode)
5043 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5044 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5045 1a76625f 2018-10-22 stsp if (errcode)
5046 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5047 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5048 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5049 245d91c1 2018-07-12 stsp err = NULL;
5050 245d91c1 2018-07-12 stsp blame->thread = NULL;
5051 245d91c1 2018-07-12 stsp }
5052 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5053 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5054 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5055 1d0f4054 2021-06-17 stsp if (err == NULL)
5056 1d0f4054 2021-06-17 stsp err = close_err;
5057 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5058 245d91c1 2018-07-12 stsp }
5059 245d91c1 2018-07-12 stsp if (blame->f) {
5060 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5061 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5062 245d91c1 2018-07-12 stsp blame->f = NULL;
5063 245d91c1 2018-07-12 stsp }
5064 57670559 2018-12-23 stsp if (blame->lines) {
5065 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5066 57670559 2018-12-23 stsp free(blame->lines[i].id);
5067 57670559 2018-12-23 stsp free(blame->lines);
5068 57670559 2018-12-23 stsp blame->lines = NULL;
5069 57670559 2018-12-23 stsp }
5070 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5071 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5072 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5073 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5074 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5075 0ae84acc 2022-06-15 tracey if (err == NULL)
5076 0ae84acc 2022-06-15 tracey err = pack_err;
5077 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5078 0ae84acc 2022-06-15 tracey }
5079 245d91c1 2018-07-12 stsp return err;
5080 245d91c1 2018-07-12 stsp }
5081 245d91c1 2018-07-12 stsp
5082 245d91c1 2018-07-12 stsp static const struct got_error *
5083 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5084 fc06ba56 2019-08-22 stsp {
5085 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5086 fc06ba56 2019-08-22 stsp int *done = arg;
5087 fc06ba56 2019-08-22 stsp int errcode;
5088 fc06ba56 2019-08-22 stsp
5089 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5090 fc06ba56 2019-08-22 stsp if (errcode)
5091 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5092 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5093 fc06ba56 2019-08-22 stsp
5094 fc06ba56 2019-08-22 stsp if (*done)
5095 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5096 fc06ba56 2019-08-22 stsp
5097 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5098 fc06ba56 2019-08-22 stsp if (errcode)
5099 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5100 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5101 fc06ba56 2019-08-22 stsp
5102 fc06ba56 2019-08-22 stsp return err;
5103 fc06ba56 2019-08-22 stsp }
5104 fc06ba56 2019-08-22 stsp
5105 fc06ba56 2019-08-22 stsp static const struct got_error *
5106 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5107 245d91c1 2018-07-12 stsp {
5108 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5109 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5110 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5111 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5112 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5113 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5114 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5115 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5116 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5117 a70480e0 2018-06-23 stsp
5118 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5119 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5120 27d434c2 2018-09-15 stsp if (err)
5121 15a94983 2018-12-23 stsp return err;
5122 eb81bc23 2022-06-28 tracey
5123 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5124 eb81bc23 2022-06-28 tracey if (fd == -1) {
5125 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5126 eb81bc23 2022-06-28 tracey goto done;
5127 eb81bc23 2022-06-28 tracey }
5128 a44927cc 2022-04-07 stsp
5129 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5130 a44927cc 2022-04-07 stsp if (err)
5131 a44927cc 2022-04-07 stsp goto done;
5132 27d434c2 2018-09-15 stsp
5133 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5134 84451b3e 2018-07-10 stsp if (err)
5135 84451b3e 2018-07-10 stsp goto done;
5136 27d434c2 2018-09-15 stsp
5137 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5138 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5139 84451b3e 2018-07-10 stsp goto done;
5140 84451b3e 2018-07-10 stsp }
5141 a70480e0 2018-06-23 stsp
5142 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5143 a70480e0 2018-06-23 stsp if (err)
5144 a70480e0 2018-06-23 stsp goto done;
5145 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5146 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5147 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5148 84451b3e 2018-07-10 stsp goto done;
5149 84451b3e 2018-07-10 stsp }
5150 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5151 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5152 1fddf795 2021-01-20 stsp if (err)
5153 1fddf795 2021-01-20 stsp goto done;
5154 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5155 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5156 84451b3e 2018-07-10 stsp goto done;
5157 1fddf795 2021-01-20 stsp }
5158 b02560ec 2019-08-19 stsp
5159 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5160 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5161 b02560ec 2019-08-19 stsp blame->nlines--;
5162 a70480e0 2018-06-23 stsp
5163 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5164 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5165 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5166 84451b3e 2018-07-10 stsp goto done;
5167 84451b3e 2018-07-10 stsp }
5168 a70480e0 2018-06-23 stsp
5169 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5170 bd24772e 2018-07-11 stsp if (err)
5171 bd24772e 2018-07-11 stsp goto done;
5172 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5173 0ae84acc 2022-06-15 tracey pack_fds);
5174 0ae84acc 2022-06-15 tracey if (err)
5175 0ae84acc 2022-06-15 tracey goto done;
5176 bd24772e 2018-07-11 stsp
5177 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5178 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5179 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5180 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5181 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5182 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5183 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5184 245d91c1 2018-07-12 stsp goto done;
5185 245d91c1 2018-07-12 stsp }
5186 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5187 245d91c1 2018-07-12 stsp
5188 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5189 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5190 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5191 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5192 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5193 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5194 a5388363 2020-12-01 naddy s->blame_complete = 0;
5195 f5a09613 2020-12-13 naddy
5196 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5197 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5198 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5199 f5a09613 2020-12-13 naddy s->selected_line = 1;
5200 f5a09613 2020-12-13 naddy }
5201 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5202 245d91c1 2018-07-12 stsp
5203 245d91c1 2018-07-12 stsp done:
5204 a44927cc 2022-04-07 stsp if (commit)
5205 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5206 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5207 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5208 245d91c1 2018-07-12 stsp if (blob)
5209 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5210 27d434c2 2018-09-15 stsp free(obj_id);
5211 245d91c1 2018-07-12 stsp if (err)
5212 245d91c1 2018-07-12 stsp stop_blame(blame);
5213 245d91c1 2018-07-12 stsp return err;
5214 245d91c1 2018-07-12 stsp }
5215 245d91c1 2018-07-12 stsp
5216 245d91c1 2018-07-12 stsp static const struct got_error *
5217 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5218 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5219 245d91c1 2018-07-12 stsp {
5220 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5221 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5222 dbc6a6b6 2018-07-12 stsp
5223 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5224 245d91c1 2018-07-12 stsp
5225 c4843652 2019-08-12 stsp s->path = strdup(path);
5226 c4843652 2019-08-12 stsp if (s->path == NULL)
5227 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5228 c4843652 2019-08-12 stsp
5229 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5230 c4843652 2019-08-12 stsp if (err) {
5231 c4843652 2019-08-12 stsp free(s->path);
5232 7cbe629d 2018-08-04 stsp return err;
5233 c4843652 2019-08-12 stsp }
5234 245d91c1 2018-07-12 stsp
5235 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5236 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5237 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5238 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5239 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5240 fb2756b9 2018-08-04 stsp s->repo = repo;
5241 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5242 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5243 7cbe629d 2018-08-04 stsp
5244 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5245 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5246 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5247 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5248 11b20872 2019-11-08 stsp if (err)
5249 11b20872 2019-11-08 stsp return err;
5250 11b20872 2019-11-08 stsp }
5251 11b20872 2019-11-08 stsp
5252 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5253 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5254 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5255 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5256 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5257 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5258 e5a0f69f 2018-08-18 stsp
5259 a5388363 2020-12-01 naddy return run_blame(view);
5260 7cbe629d 2018-08-04 stsp }
5261 7cbe629d 2018-08-04 stsp
5262 e5a0f69f 2018-08-18 stsp static const struct got_error *
5263 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5264 7cbe629d 2018-08-04 stsp {
5265 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5266 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5267 7cbe629d 2018-08-04 stsp
5268 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5269 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5270 e5a0f69f 2018-08-18 stsp
5271 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5272 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5273 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5274 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5275 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5276 7cbe629d 2018-08-04 stsp }
5277 e5a0f69f 2018-08-18 stsp
5278 e5a0f69f 2018-08-18 stsp free(s->path);
5279 11b20872 2019-11-08 stsp free_colors(&s->colors);
5280 e5a0f69f 2018-08-18 stsp return err;
5281 7cbe629d 2018-08-04 stsp }
5282 7cbe629d 2018-08-04 stsp
5283 7cbe629d 2018-08-04 stsp static const struct got_error *
5284 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5285 6c4c42e0 2019-06-24 stsp {
5286 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5287 6c4c42e0 2019-06-24 stsp
5288 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5289 6c4c42e0 2019-06-24 stsp return NULL;
5290 6c4c42e0 2019-06-24 stsp }
5291 6c4c42e0 2019-06-24 stsp
5292 6c4c42e0 2019-06-24 stsp static const struct got_error *
5293 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5294 6c4c42e0 2019-06-24 stsp {
5295 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5296 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5297 6c4c42e0 2019-06-24 stsp int lineno;
5298 cb713507 2022-06-16 stsp char *line = NULL;
5299 826082fe 2020-12-10 stsp size_t linesize = 0;
5300 826082fe 2020-12-10 stsp ssize_t linelen;
5301 6c4c42e0 2019-06-24 stsp
5302 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5303 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5304 6c4c42e0 2019-06-24 stsp return NULL;
5305 6c4c42e0 2019-06-24 stsp }
5306 6c4c42e0 2019-06-24 stsp
5307 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5308 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5309 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5310 6c4c42e0 2019-06-24 stsp else
5311 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5312 487cd7d2 2021-12-17 stsp } else
5313 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5314 6c4c42e0 2019-06-24 stsp
5315 6c4c42e0 2019-06-24 stsp while (1) {
5316 6c4c42e0 2019-06-24 stsp off_t offset;
5317 6c4c42e0 2019-06-24 stsp
5318 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5319 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5320 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5321 6c4c42e0 2019-06-24 stsp break;
5322 6c4c42e0 2019-06-24 stsp }
5323 2246482e 2019-06-25 stsp
5324 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5325 6c4c42e0 2019-06-24 stsp lineno = 1;
5326 6c4c42e0 2019-06-24 stsp else
5327 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5328 6c4c42e0 2019-06-24 stsp }
5329 6c4c42e0 2019-06-24 stsp
5330 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5331 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5332 6c4c42e0 2019-06-24 stsp free(line);
5333 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5334 6c4c42e0 2019-06-24 stsp }
5335 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5336 cb713507 2022-06-16 stsp if (linelen != -1) {
5337 cb713507 2022-06-16 stsp char *exstr;
5338 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5339 cb713507 2022-06-16 stsp if (err)
5340 cb713507 2022-06-16 stsp break;
5341 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5342 cb713507 2022-06-16 stsp &view->regmatch)) {
5343 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5344 cb713507 2022-06-16 stsp s->matched_line = lineno;
5345 cb713507 2022-06-16 stsp free(exstr);
5346 cb713507 2022-06-16 stsp break;
5347 cb713507 2022-06-16 stsp }
5348 cb713507 2022-06-16 stsp free(exstr);
5349 6c4c42e0 2019-06-24 stsp }
5350 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5351 6c4c42e0 2019-06-24 stsp lineno++;
5352 6c4c42e0 2019-06-24 stsp else
5353 6c4c42e0 2019-06-24 stsp lineno--;
5354 6c4c42e0 2019-06-24 stsp }
5355 826082fe 2020-12-10 stsp free(line);
5356 6c4c42e0 2019-06-24 stsp
5357 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5358 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5359 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5360 6c4c42e0 2019-06-24 stsp }
5361 6c4c42e0 2019-06-24 stsp
5362 145b6838 2022-06-16 stsp return err;
5363 6c4c42e0 2019-06-24 stsp }
5364 6c4c42e0 2019-06-24 stsp
5365 6c4c42e0 2019-06-24 stsp static const struct got_error *
5366 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5367 7cbe629d 2018-08-04 stsp {
5368 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5369 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5370 2b380cc8 2018-10-24 stsp int errcode;
5371 2b380cc8 2018-10-24 stsp
5372 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5373 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5374 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5375 2b380cc8 2018-10-24 stsp if (errcode)
5376 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5377 51fe7530 2019-08-19 stsp
5378 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5379 2b380cc8 2018-10-24 stsp }
5380 e5a0f69f 2018-08-18 stsp
5381 51fe7530 2019-08-19 stsp if (s->blame_complete)
5382 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5383 51fe7530 2019-08-19 stsp
5384 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5385 e5a0f69f 2018-08-18 stsp
5386 9b058f45 2022-06-30 mark view_border(view);
5387 e5a0f69f 2018-08-18 stsp return err;
5388 e5a0f69f 2018-08-18 stsp }
5389 e5a0f69f 2018-08-18 stsp
5390 e5a0f69f 2018-08-18 stsp static const struct got_error *
5391 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5392 e5a0f69f 2018-08-18 stsp {
5393 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5394 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5395 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5396 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5397 9b058f45 2022-06-30 mark
5398 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5399 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5400 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5401 9b058f45 2022-06-30 mark --eos; /* border */
5402 7cbe629d 2018-08-04 stsp
5403 e5a0f69f 2018-08-18 stsp switch (ch) {
5404 145b6838 2022-06-16 stsp case '0':
5405 145b6838 2022-06-16 stsp view->x = 0;
5406 145b6838 2022-06-16 stsp break;
5407 145b6838 2022-06-16 stsp case '$':
5408 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5409 640cd7ff 2022-06-22 mark view->count = 0;
5410 145b6838 2022-06-16 stsp break;
5411 145b6838 2022-06-16 stsp case KEY_RIGHT:
5412 145b6838 2022-06-16 stsp case 'l':
5413 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5414 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5415 640cd7ff 2022-06-22 mark else
5416 640cd7ff 2022-06-22 mark view->count = 0;
5417 145b6838 2022-06-16 stsp break;
5418 145b6838 2022-06-16 stsp case KEY_LEFT:
5419 145b6838 2022-06-16 stsp case 'h':
5420 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5421 640cd7ff 2022-06-22 mark if (view->x <= 0)
5422 640cd7ff 2022-06-22 mark view->count = 0;
5423 145b6838 2022-06-16 stsp break;
5424 1e37a5c2 2019-05-12 jcs case 'q':
5425 1e37a5c2 2019-05-12 jcs s->done = 1;
5426 4deef56f 2021-09-02 naddy break;
5427 4deef56f 2021-09-02 naddy case 'g':
5428 4deef56f 2021-09-02 naddy case KEY_HOME:
5429 4deef56f 2021-09-02 naddy s->selected_line = 1;
5430 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5431 640cd7ff 2022-06-22 mark view->count = 0;
5432 4deef56f 2021-09-02 naddy break;
5433 4deef56f 2021-09-02 naddy case 'G':
5434 4deef56f 2021-09-02 naddy case KEY_END:
5435 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5436 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5437 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5438 4deef56f 2021-09-02 naddy } else {
5439 9b058f45 2022-06-30 mark s->selected_line = eos;
5440 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5441 4deef56f 2021-09-02 naddy }
5442 640cd7ff 2022-06-22 mark view->count = 0;
5443 1e37a5c2 2019-05-12 jcs break;
5444 1e37a5c2 2019-05-12 jcs case 'k':
5445 1e37a5c2 2019-05-12 jcs case KEY_UP:
5446 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5447 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5448 1e37a5c2 2019-05-12 jcs s->selected_line--;
5449 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5450 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5451 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5452 640cd7ff 2022-06-22 mark else
5453 640cd7ff 2022-06-22 mark view->count = 0;
5454 1e37a5c2 2019-05-12 jcs break;
5455 83cc4199 2022-06-13 stsp case CTRL('u'):
5456 33c3719a 2022-06-15 stsp case 'u':
5457 83cc4199 2022-06-13 stsp nscroll /= 2;
5458 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5459 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5460 ea025d1d 2020-02-22 naddy case CTRL('b'):
5461 61417565 2022-06-20 mark case 'b':
5462 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5463 640cd7ff 2022-06-22 mark if (view->count > 1)
5464 640cd7ff 2022-06-22 mark nscroll += nscroll;
5465 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5466 640cd7ff 2022-06-22 mark view->count = 0;
5467 e5a0f69f 2018-08-18 stsp break;
5468 1e37a5c2 2019-05-12 jcs }
5469 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5470 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5471 1e37a5c2 2019-05-12 jcs else
5472 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5473 1e37a5c2 2019-05-12 jcs break;
5474 1e37a5c2 2019-05-12 jcs case 'j':
5475 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5476 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5477 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5478 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5479 1e37a5c2 2019-05-12 jcs s->selected_line++;
5480 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5481 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5482 640cd7ff 2022-06-22 mark else
5483 640cd7ff 2022-06-22 mark view->count = 0;
5484 1e37a5c2 2019-05-12 jcs break;
5485 61417565 2022-06-20 mark case 'c':
5486 1e37a5c2 2019-05-12 jcs case 'p': {
5487 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5488 640cd7ff 2022-06-22 mark
5489 640cd7ff 2022-06-22 mark view->count = 0;
5490 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5491 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5492 1e37a5c2 2019-05-12 jcs if (id == NULL)
5493 e5a0f69f 2018-08-18 stsp break;
5494 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5495 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5496 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5497 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5498 1e37a5c2 2019-05-12 jcs int obj_type;
5499 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5500 1e37a5c2 2019-05-12 jcs s->repo, id);
5501 e5a0f69f 2018-08-18 stsp if (err)
5502 e5a0f69f 2018-08-18 stsp break;
5503 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5504 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5505 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5506 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5507 e5a0f69f 2018-08-18 stsp break;
5508 e5a0f69f 2018-08-18 stsp }
5509 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5510 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5511 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5512 a44927cc 2022-04-07 stsp if (err)
5513 a44927cc 2022-04-07 stsp break;
5514 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5515 a44927cc 2022-04-07 stsp pcommit, s->path);
5516 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5517 e5a0f69f 2018-08-18 stsp if (err) {
5518 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5519 1e37a5c2 2019-05-12 jcs err = NULL;
5520 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5521 e5a0f69f 2018-08-18 stsp break;
5522 e5a0f69f 2018-08-18 stsp }
5523 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5524 1e37a5c2 2019-05-12 jcs blob_id);
5525 1e37a5c2 2019-05-12 jcs free(blob_id);
5526 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5527 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5528 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5529 e5a0f69f 2018-08-18 stsp break;
5530 1e37a5c2 2019-05-12 jcs }
5531 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5532 d7b5a0e8 2022-04-20 stsp &pid->id);
5533 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5534 1e37a5c2 2019-05-12 jcs } else {
5535 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5536 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5537 1e37a5c2 2019-05-12 jcs break;
5538 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5539 1e37a5c2 2019-05-12 jcs id);
5540 1e37a5c2 2019-05-12 jcs }
5541 1e37a5c2 2019-05-12 jcs if (err)
5542 e5a0f69f 2018-08-18 stsp break;
5543 1e37a5c2 2019-05-12 jcs s->done = 1;
5544 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5545 1e37a5c2 2019-05-12 jcs s->done = 0;
5546 1e37a5c2 2019-05-12 jcs if (thread_err)
5547 1e37a5c2 2019-05-12 jcs break;
5548 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5549 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5550 a5388363 2020-12-01 naddy err = run_blame(view);
5551 1e37a5c2 2019-05-12 jcs if (err)
5552 1e37a5c2 2019-05-12 jcs break;
5553 1e37a5c2 2019-05-12 jcs break;
5554 1e37a5c2 2019-05-12 jcs }
5555 61417565 2022-06-20 mark case 'C': {
5556 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5557 640cd7ff 2022-06-22 mark
5558 640cd7ff 2022-06-22 mark view->count = 0;
5559 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5560 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5561 1e37a5c2 2019-05-12 jcs break;
5562 1e37a5c2 2019-05-12 jcs s->done = 1;
5563 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5564 1e37a5c2 2019-05-12 jcs s->done = 0;
5565 1e37a5c2 2019-05-12 jcs if (thread_err)
5566 1e37a5c2 2019-05-12 jcs break;
5567 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5568 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5569 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5570 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5571 a5388363 2020-12-01 naddy err = run_blame(view);
5572 1e37a5c2 2019-05-12 jcs if (err)
5573 1e37a5c2 2019-05-12 jcs break;
5574 1e37a5c2 2019-05-12 jcs break;
5575 1e37a5c2 2019-05-12 jcs }
5576 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5577 1e37a5c2 2019-05-12 jcs case '\r': {
5578 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5579 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5580 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5581 640cd7ff 2022-06-22 mark
5582 640cd7ff 2022-06-22 mark view->count = 0;
5583 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5584 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5585 1e37a5c2 2019-05-12 jcs if (id == NULL)
5586 1e37a5c2 2019-05-12 jcs break;
5587 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5588 1e37a5c2 2019-05-12 jcs if (err)
5589 1e37a5c2 2019-05-12 jcs break;
5590 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5591 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5592 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
5593 9b058f45 2022-06-30 mark
5594 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5595 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5596 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5597 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5598 1e37a5c2 2019-05-12 jcs break;
5599 15a94983 2018-12-23 stsp }
5600 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5601 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5602 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5603 1e37a5c2 2019-05-12 jcs if (err) {
5604 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5605 1e37a5c2 2019-05-12 jcs break;
5606 1e37a5c2 2019-05-12 jcs }
5607 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
5608 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5609 9b058f45 2022-06-30 mark if (err)
5610 9b058f45 2022-06-30 mark break;
5611 9b058f45 2022-06-30 mark }
5612 9b058f45 2022-06-30 mark
5613 e78dc838 2020-12-04 stsp view->focussed = 0;
5614 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5615 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5616 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5617 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5618 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5619 1e37a5c2 2019-05-12 jcs if (err)
5620 34bc9ec9 2019-02-22 stsp break;
5621 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5622 0dbbbe90 2022-06-17 op if (err)
5623 0dbbbe90 2022-06-17 op break;
5624 e78dc838 2020-12-04 stsp view->focus_child = 1;
5625 1e37a5c2 2019-05-12 jcs } else
5626 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5627 1e37a5c2 2019-05-12 jcs if (err)
5628 e5a0f69f 2018-08-18 stsp break;
5629 1e37a5c2 2019-05-12 jcs break;
5630 1e37a5c2 2019-05-12 jcs }
5631 83cc4199 2022-06-13 stsp case CTRL('d'):
5632 33c3719a 2022-06-15 stsp case 'd':
5633 83cc4199 2022-06-13 stsp nscroll /= 2;
5634 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5635 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5636 ea025d1d 2020-02-22 naddy case CTRL('f'):
5637 61417565 2022-06-20 mark case 'f':
5638 1e37a5c2 2019-05-12 jcs case ' ':
5639 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5640 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5641 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5642 640cd7ff 2022-06-22 mark view->count = 0;
5643 e5a0f69f 2018-08-18 stsp break;
5644 1e37a5c2 2019-05-12 jcs }
5645 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5646 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5647 83cc4199 2022-06-13 stsp s->selected_line +=
5648 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5649 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5650 1e37a5c2 2019-05-12 jcs }
5651 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5652 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5653 1e37a5c2 2019-05-12 jcs else
5654 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5655 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5656 1e37a5c2 2019-05-12 jcs break;
5657 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5658 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5659 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5660 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5661 1e37a5c2 2019-05-12 jcs }
5662 1e37a5c2 2019-05-12 jcs break;
5663 1e37a5c2 2019-05-12 jcs default:
5664 640cd7ff 2022-06-22 mark view->count = 0;
5665 1e37a5c2 2019-05-12 jcs break;
5666 a70480e0 2018-06-23 stsp }
5667 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5668 917d79a7 2022-07-01 stsp }
5669 917d79a7 2022-07-01 stsp
5670 917d79a7 2022-07-01 stsp static const struct got_error *
5671 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
5672 917d79a7 2022-07-01 stsp {
5673 917d79a7 2022-07-01 stsp const struct got_error *err;
5674 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
5675 917d79a7 2022-07-01 stsp
5676 917d79a7 2022-07-01 stsp view->count = 0;
5677 917d79a7 2022-07-01 stsp s->done = 1;
5678 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
5679 917d79a7 2022-07-01 stsp s->done = 0;
5680 917d79a7 2022-07-01 stsp if (err)
5681 917d79a7 2022-07-01 stsp return err;
5682 917d79a7 2022-07-01 stsp return run_blame(view);
5683 a70480e0 2018-06-23 stsp }
5684 a70480e0 2018-06-23 stsp
5685 a70480e0 2018-06-23 stsp static const struct got_error *
5686 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5687 9f7d7167 2018-04-29 stsp {
5688 a70480e0 2018-06-23 stsp const struct got_error *error;
5689 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5690 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5691 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 0587e10c 2020-07-23 stsp char *link_target = NULL;
5693 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5694 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5695 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5696 a70480e0 2018-06-23 stsp int ch;
5697 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5698 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5699 a70480e0 2018-06-23 stsp
5700 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5701 a70480e0 2018-06-23 stsp switch (ch) {
5702 a70480e0 2018-06-23 stsp case 'c':
5703 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5704 a70480e0 2018-06-23 stsp break;
5705 69069811 2018-08-02 stsp case 'r':
5706 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5707 69069811 2018-08-02 stsp if (repo_path == NULL)
5708 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5709 9ba1d308 2019-10-21 stsp optarg);
5710 69069811 2018-08-02 stsp break;
5711 a70480e0 2018-06-23 stsp default:
5712 17020d27 2019-03-07 stsp usage_blame();
5713 a70480e0 2018-06-23 stsp /* NOTREACHED */
5714 a70480e0 2018-06-23 stsp }
5715 a70480e0 2018-06-23 stsp }
5716 a70480e0 2018-06-23 stsp
5717 a70480e0 2018-06-23 stsp argc -= optind;
5718 a70480e0 2018-06-23 stsp argv += optind;
5719 a70480e0 2018-06-23 stsp
5720 f135c941 2020-02-20 stsp if (argc != 1)
5721 a70480e0 2018-06-23 stsp usage_blame();
5722 6962eb72 2020-02-20 stsp
5723 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
5724 0ae84acc 2022-06-15 tracey if (error != NULL)
5725 0ae84acc 2022-06-15 tracey goto done;
5726 0ae84acc 2022-06-15 tracey
5727 69069811 2018-08-02 stsp if (repo_path == NULL) {
5728 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5729 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5730 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5731 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5732 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5733 c156c7a4 2020-12-18 stsp goto done;
5734 f135c941 2020-02-20 stsp if (worktree)
5735 eb41ed75 2019-02-05 stsp repo_path =
5736 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5737 f135c941 2020-02-20 stsp else
5738 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5739 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5740 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5741 c156c7a4 2020-12-18 stsp goto done;
5742 c156c7a4 2020-12-18 stsp }
5743 f135c941 2020-02-20 stsp }
5744 a915003a 2019-02-05 stsp
5745 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5746 c02c541e 2019-03-29 stsp if (error != NULL)
5747 8e94dd5b 2019-01-04 stsp goto done;
5748 69069811 2018-08-02 stsp
5749 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5750 f135c941 2020-02-20 stsp worktree);
5751 c02c541e 2019-03-29 stsp if (error)
5752 92205607 2019-01-04 stsp goto done;
5753 69069811 2018-08-02 stsp
5754 f135c941 2020-02-20 stsp init_curses();
5755 f135c941 2020-02-20 stsp
5756 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5757 51a10b52 2020-12-26 stsp if (error)
5758 51a10b52 2020-12-26 stsp goto done;
5759 51a10b52 2020-12-26 stsp
5760 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
5761 eb41ed75 2019-02-05 stsp if (error)
5762 69069811 2018-08-02 stsp goto done;
5763 a70480e0 2018-06-23 stsp
5764 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5765 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5766 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5767 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5768 a70480e0 2018-06-23 stsp if (error != NULL)
5769 66b4983c 2018-06-23 stsp goto done;
5770 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5771 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5772 a70480e0 2018-06-23 stsp } else {
5773 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5774 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5775 a70480e0 2018-06-23 stsp }
5776 a19e88aa 2018-06-23 stsp if (error != NULL)
5777 8b473291 2019-02-21 stsp goto done;
5778 8b473291 2019-02-21 stsp
5779 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5780 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5781 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5782 e1cd8fed 2018-08-01 stsp goto done;
5783 e1cd8fed 2018-08-01 stsp }
5784 0587e10c 2020-07-23 stsp
5785 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5786 a44927cc 2022-04-07 stsp if (error)
5787 a44927cc 2022-04-07 stsp goto done;
5788 a44927cc 2022-04-07 stsp
5789 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5790 a44927cc 2022-04-07 stsp commit, repo);
5791 7cbe629d 2018-08-04 stsp if (error)
5792 7cbe629d 2018-08-04 stsp goto done;
5793 0587e10c 2020-07-23 stsp
5794 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5795 78756c87 2020-11-24 stsp commit_id, repo);
5796 0587e10c 2020-07-23 stsp if (error)
5797 0587e10c 2020-07-23 stsp goto done;
5798 12314ad4 2019-08-31 stsp if (worktree) {
5799 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5800 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5801 12314ad4 2019-08-31 stsp worktree = NULL;
5802 12314ad4 2019-08-31 stsp }
5803 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5804 a70480e0 2018-06-23 stsp done:
5805 69069811 2018-08-02 stsp free(repo_path);
5806 f135c941 2020-02-20 stsp free(in_repo_path);
5807 0587e10c 2020-07-23 stsp free(link_target);
5808 69069811 2018-08-02 stsp free(cwd);
5809 a70480e0 2018-06-23 stsp free(commit_id);
5810 a44927cc 2022-04-07 stsp if (commit)
5811 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5812 eb41ed75 2019-02-05 stsp if (worktree)
5813 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5814 1d0f4054 2021-06-17 stsp if (repo) {
5815 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5816 1d0f4054 2021-06-17 stsp if (error == NULL)
5817 1d0f4054 2021-06-17 stsp error = close_err;
5818 1d0f4054 2021-06-17 stsp }
5819 0ae84acc 2022-06-15 tracey if (pack_fds) {
5820 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5821 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
5822 0ae84acc 2022-06-15 tracey if (error == NULL)
5823 0ae84acc 2022-06-15 tracey error = pack_err;
5824 0ae84acc 2022-06-15 tracey }
5825 51a10b52 2020-12-26 stsp tog_free_refs();
5826 a70480e0 2018-06-23 stsp return error;
5827 ffd1d5e5 2018-06-23 stsp }
5828 ffd1d5e5 2018-06-23 stsp
5829 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5830 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5831 ffd1d5e5 2018-06-23 stsp {
5832 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5833 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5834 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5835 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5836 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5837 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5838 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5839 ffd1d5e5 2018-06-23 stsp
5840 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5841 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
5842 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
5843 9b058f45 2022-06-30 mark --limit; /* border */
5844 ffd1d5e5 2018-06-23 stsp
5845 f7d12f7e 2018-08-01 stsp werase(view->window);
5846 ffd1d5e5 2018-06-23 stsp
5847 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5848 ffd1d5e5 2018-06-23 stsp return NULL;
5849 ffd1d5e5 2018-06-23 stsp
5850 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5851 ccda2f4d 2022-06-16 stsp 0, 0);
5852 ffd1d5e5 2018-06-23 stsp if (err)
5853 ffd1d5e5 2018-06-23 stsp return err;
5854 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5855 a3404814 2018-09-02 stsp wstandout(view->window);
5856 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5857 11b20872 2019-11-08 stsp if (tc)
5858 11b20872 2019-11-08 stsp wattr_on(view->window,
5859 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5860 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5861 11b20872 2019-11-08 stsp if (tc)
5862 11b20872 2019-11-08 stsp wattr_off(view->window,
5863 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5864 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5865 a3404814 2018-09-02 stsp wstandend(view->window);
5866 2550e4c3 2018-07-13 stsp free(wline);
5867 2550e4c3 2018-07-13 stsp wline = NULL;
5868 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5869 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5870 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5871 ffd1d5e5 2018-06-23 stsp return NULL;
5872 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5873 ccda2f4d 2022-06-16 stsp 0, 0);
5874 ce52c690 2018-06-23 stsp if (err)
5875 ce52c690 2018-06-23 stsp return err;
5876 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5877 2550e4c3 2018-07-13 stsp free(wline);
5878 2550e4c3 2018-07-13 stsp wline = NULL;
5879 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5880 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5881 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5882 ffd1d5e5 2018-06-23 stsp return NULL;
5883 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5884 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5885 a1eca9bb 2018-06-23 stsp return NULL;
5886 ffd1d5e5 2018-06-23 stsp
5887 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5888 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5889 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5890 0cf4efb1 2018-09-29 stsp if (view->focussed)
5891 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5892 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5893 ffd1d5e5 2018-06-23 stsp }
5894 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5895 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5896 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5897 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5898 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5899 ffd1d5e5 2018-06-23 stsp return NULL;
5900 ffd1d5e5 2018-06-23 stsp n = 1;
5901 ffd1d5e5 2018-06-23 stsp } else {
5902 ffd1d5e5 2018-06-23 stsp n = 0;
5903 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5904 ffd1d5e5 2018-06-23 stsp }
5905 ffd1d5e5 2018-06-23 stsp
5906 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5907 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5908 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5909 848d6979 2019-08-12 stsp const char *modestr = "";
5910 56e0773d 2019-11-28 stsp mode_t mode;
5911 1d13200f 2018-07-12 stsp
5912 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5913 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5914 56e0773d 2019-11-28 stsp
5915 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5916 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5917 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5918 1d13200f 2018-07-12 stsp if (err)
5919 638f9024 2019-05-13 stsp return got_error_from_errno(
5920 230a42bd 2019-05-11 jcs "got_object_id_str");
5921 1d13200f 2018-07-12 stsp }
5922 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5923 63c5ca5d 2019-08-24 stsp modestr = "$";
5924 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5925 0d6c6ee3 2020-05-20 stsp int i;
5926 0d6c6ee3 2020-05-20 stsp
5927 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5928 d86d3b18 2020-12-01 naddy te, s->repo);
5929 0d6c6ee3 2020-05-20 stsp if (err) {
5930 0d6c6ee3 2020-05-20 stsp free(id_str);
5931 0d6c6ee3 2020-05-20 stsp return err;
5932 0d6c6ee3 2020-05-20 stsp }
5933 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5934 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5935 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5936 0d6c6ee3 2020-05-20 stsp }
5937 848d6979 2019-08-12 stsp modestr = "@";
5938 0d6c6ee3 2020-05-20 stsp }
5939 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5940 848d6979 2019-08-12 stsp modestr = "/";
5941 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5942 848d6979 2019-08-12 stsp modestr = "*";
5943 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5944 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5945 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5946 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5947 1d13200f 2018-07-12 stsp free(id_str);
5948 0d6c6ee3 2020-05-20 stsp free(link_target);
5949 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5950 1d13200f 2018-07-12 stsp }
5951 1d13200f 2018-07-12 stsp free(id_str);
5952 0d6c6ee3 2020-05-20 stsp free(link_target);
5953 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5954 ccda2f4d 2022-06-16 stsp 0, 0);
5955 ffd1d5e5 2018-06-23 stsp if (err) {
5956 ffd1d5e5 2018-06-23 stsp free(line);
5957 ffd1d5e5 2018-06-23 stsp break;
5958 ffd1d5e5 2018-06-23 stsp }
5959 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5960 0cf4efb1 2018-09-29 stsp if (view->focussed)
5961 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5962 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5963 ffd1d5e5 2018-06-23 stsp }
5964 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5965 f26dddb7 2019-11-08 stsp if (tc)
5966 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5967 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5968 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5969 f26dddb7 2019-11-08 stsp if (tc)
5970 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5971 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5972 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5973 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5974 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5975 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5976 ffd1d5e5 2018-06-23 stsp free(line);
5977 2550e4c3 2018-07-13 stsp free(wline);
5978 2550e4c3 2018-07-13 stsp wline = NULL;
5979 ffd1d5e5 2018-06-23 stsp n++;
5980 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5981 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5982 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5983 ffd1d5e5 2018-06-23 stsp break;
5984 ffd1d5e5 2018-06-23 stsp }
5985 ffd1d5e5 2018-06-23 stsp
5986 ffd1d5e5 2018-06-23 stsp return err;
5987 ffd1d5e5 2018-06-23 stsp }
5988 ffd1d5e5 2018-06-23 stsp
5989 ffd1d5e5 2018-06-23 stsp static void
5990 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5991 ffd1d5e5 2018-06-23 stsp {
5992 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5993 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5994 fa86c4bf 2020-11-29 stsp int i = 0;
5995 ffd1d5e5 2018-06-23 stsp
5996 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5997 ffd1d5e5 2018-06-23 stsp return;
5998 ffd1d5e5 2018-06-23 stsp
5999 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6000 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6001 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6002 fa86c4bf 2020-11-29 stsp if (!isroot)
6003 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6004 fa86c4bf 2020-11-29 stsp break;
6005 fa86c4bf 2020-11-29 stsp }
6006 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6007 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6008 ffd1d5e5 2018-06-23 stsp }
6009 ffd1d5e5 2018-06-23 stsp }
6010 ffd1d5e5 2018-06-23 stsp
6011 9b058f45 2022-06-30 mark static const struct got_error *
6012 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6013 ffd1d5e5 2018-06-23 stsp {
6014 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6015 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6016 ffd1d5e5 2018-06-23 stsp int n = 0;
6017 ffd1d5e5 2018-06-23 stsp
6018 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6019 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6020 694d3271 2020-12-01 naddy s->first_displayed_entry);
6021 694d3271 2020-12-01 naddy else
6022 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6023 56e0773d 2019-11-28 stsp
6024 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6025 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6026 9b058f45 2022-06-30 mark if (last)
6027 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6028 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6029 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6030 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6031 768394f3 2019-01-24 stsp }
6032 ffd1d5e5 2018-06-23 stsp }
6033 9b058f45 2022-06-30 mark
6034 9b058f45 2022-06-30 mark return NULL;
6035 ffd1d5e5 2018-06-23 stsp }
6036 ffd1d5e5 2018-06-23 stsp
6037 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6038 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6039 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6040 ffd1d5e5 2018-06-23 stsp {
6041 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6042 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6043 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6044 ffd1d5e5 2018-06-23 stsp
6045 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6046 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6047 56e0773d 2019-11-28 stsp + 1 /* slash */;
6048 ce52c690 2018-06-23 stsp if (te)
6049 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6050 ce52c690 2018-06-23 stsp
6051 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6052 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6053 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6054 ffd1d5e5 2018-06-23 stsp
6055 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6056 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6057 d9765a41 2018-06-23 stsp while (pt) {
6058 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6059 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6060 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6061 cb2ebc8a 2018-06-23 stsp goto done;
6062 cb2ebc8a 2018-06-23 stsp }
6063 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6064 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6065 cb2ebc8a 2018-06-23 stsp goto done;
6066 cb2ebc8a 2018-06-23 stsp }
6067 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6068 ffd1d5e5 2018-06-23 stsp }
6069 ce52c690 2018-06-23 stsp if (te) {
6070 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6071 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6072 ce52c690 2018-06-23 stsp goto done;
6073 ce52c690 2018-06-23 stsp }
6074 cb2ebc8a 2018-06-23 stsp }
6075 ce52c690 2018-06-23 stsp done:
6076 ce52c690 2018-06-23 stsp if (err) {
6077 ce52c690 2018-06-23 stsp free(*path);
6078 ce52c690 2018-06-23 stsp *path = NULL;
6079 ce52c690 2018-06-23 stsp }
6080 ce52c690 2018-06-23 stsp return err;
6081 ce52c690 2018-06-23 stsp }
6082 ce52c690 2018-06-23 stsp
6083 ce52c690 2018-06-23 stsp static const struct got_error *
6084 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6085 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6086 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6087 ce52c690 2018-06-23 stsp {
6088 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6089 ce52c690 2018-06-23 stsp char *path;
6090 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6091 a0de39f3 2019-08-09 stsp
6092 a0de39f3 2019-08-09 stsp *new_view = NULL;
6093 69efd4c4 2018-07-18 stsp
6094 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6095 ce52c690 2018-06-23 stsp if (err)
6096 ce52c690 2018-06-23 stsp return err;
6097 ffd1d5e5 2018-06-23 stsp
6098 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6099 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6100 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6101 83ce39e3 2019-08-12 stsp goto done;
6102 83ce39e3 2019-08-12 stsp }
6103 cdf1ee82 2018-08-01 stsp
6104 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6105 e5a0f69f 2018-08-18 stsp if (err) {
6106 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6107 fc06ba56 2019-08-22 stsp err = NULL;
6108 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6109 e5a0f69f 2018-08-18 stsp } else
6110 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6111 83ce39e3 2019-08-12 stsp done:
6112 83ce39e3 2019-08-12 stsp free(path);
6113 69efd4c4 2018-07-18 stsp return err;
6114 69efd4c4 2018-07-18 stsp }
6115 69efd4c4 2018-07-18 stsp
6116 69efd4c4 2018-07-18 stsp static const struct got_error *
6117 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
6118 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6119 69efd4c4 2018-07-18 stsp {
6120 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6121 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6122 69efd4c4 2018-07-18 stsp char *path;
6123 a0de39f3 2019-08-09 stsp
6124 a0de39f3 2019-08-09 stsp *new_view = NULL;
6125 69efd4c4 2018-07-18 stsp
6126 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6127 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6128 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6129 e5a0f69f 2018-08-18 stsp
6130 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6131 69efd4c4 2018-07-18 stsp if (err)
6132 69efd4c4 2018-07-18 stsp return err;
6133 69efd4c4 2018-07-18 stsp
6134 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6135 4e97c21c 2020-12-06 stsp path, 0);
6136 ba4f502b 2018-08-04 stsp if (err)
6137 e5a0f69f 2018-08-18 stsp view_close(log_view);
6138 e5a0f69f 2018-08-18 stsp else
6139 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6140 cb2ebc8a 2018-06-23 stsp free(path);
6141 cb2ebc8a 2018-06-23 stsp return err;
6142 ffd1d5e5 2018-06-23 stsp }
6143 ffd1d5e5 2018-06-23 stsp
6144 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6145 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6146 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6147 ffd1d5e5 2018-06-23 stsp {
6148 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6149 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6150 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6151 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6152 ffd1d5e5 2018-06-23 stsp
6153 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6154 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6155 bc573f3b 2021-07-10 stsp
6156 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6157 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6158 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6159 ffd1d5e5 2018-06-23 stsp
6160 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6161 bc573f3b 2021-07-10 stsp if (err)
6162 bc573f3b 2021-07-10 stsp goto done;
6163 bc573f3b 2021-07-10 stsp
6164 bc573f3b 2021-07-10 stsp /*
6165 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6166 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6167 bc573f3b 2021-07-10 stsp * closed on demand.
6168 bc573f3b 2021-07-10 stsp */
6169 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6170 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6171 bc573f3b 2021-07-10 stsp if (err)
6172 bc573f3b 2021-07-10 stsp goto done;
6173 bc573f3b 2021-07-10 stsp s->tree = s->root;
6174 bc573f3b 2021-07-10 stsp
6175 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6176 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6177 ffd1d5e5 2018-06-23 stsp goto done;
6178 ffd1d5e5 2018-06-23 stsp
6179 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6180 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6181 ffd1d5e5 2018-06-23 stsp goto done;
6182 ffd1d5e5 2018-06-23 stsp }
6183 ffd1d5e5 2018-06-23 stsp
6184 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6185 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6186 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6187 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6188 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6189 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6190 9cd7cbd1 2020-12-07 stsp goto done;
6191 9cd7cbd1 2020-12-07 stsp }
6192 9cd7cbd1 2020-12-07 stsp }
6193 fb2756b9 2018-08-04 stsp s->repo = repo;
6194 c0b01bdb 2019-11-08 stsp
6195 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6196 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6197 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6198 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6199 c0b01bdb 2019-11-08 stsp if (err)
6200 c0b01bdb 2019-11-08 stsp goto done;
6201 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6202 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6203 bc573f3b 2021-07-10 stsp if (err)
6204 c0b01bdb 2019-11-08 stsp goto done;
6205 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6206 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6207 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6208 bc573f3b 2021-07-10 stsp if (err)
6209 c0b01bdb 2019-11-08 stsp goto done;
6210 e5a0f69f 2018-08-18 stsp
6211 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6212 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6213 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6214 bc573f3b 2021-07-10 stsp if (err)
6215 11b20872 2019-11-08 stsp goto done;
6216 11b20872 2019-11-08 stsp
6217 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6218 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6219 bc573f3b 2021-07-10 stsp if (err)
6220 c0b01bdb 2019-11-08 stsp goto done;
6221 c0b01bdb 2019-11-08 stsp }
6222 c0b01bdb 2019-11-08 stsp
6223 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6224 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6225 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6226 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6227 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6228 ad80ab7b 2018-08-04 stsp done:
6229 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6230 bc573f3b 2021-07-10 stsp if (commit)
6231 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6232 bc573f3b 2021-07-10 stsp if (err)
6233 bc573f3b 2021-07-10 stsp close_tree_view(view);
6234 ad80ab7b 2018-08-04 stsp return err;
6235 ad80ab7b 2018-08-04 stsp }
6236 ad80ab7b 2018-08-04 stsp
6237 e5a0f69f 2018-08-18 stsp static const struct got_error *
6238 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6239 ad80ab7b 2018-08-04 stsp {
6240 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6241 ad80ab7b 2018-08-04 stsp
6242 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6243 fb2756b9 2018-08-04 stsp free(s->tree_label);
6244 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6245 6484ec90 2018-09-29 stsp free(s->commit_id);
6246 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6247 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6248 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6249 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6250 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6251 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6252 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6253 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6254 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6255 ad80ab7b 2018-08-04 stsp free(parent);
6256 ad80ab7b 2018-08-04 stsp
6257 ad80ab7b 2018-08-04 stsp }
6258 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6259 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6260 bc573f3b 2021-07-10 stsp if (s->root)
6261 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6262 7c32bd05 2019-06-22 stsp return NULL;
6263 7c32bd05 2019-06-22 stsp }
6264 7c32bd05 2019-06-22 stsp
6265 7c32bd05 2019-06-22 stsp static const struct got_error *
6266 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6267 7c32bd05 2019-06-22 stsp {
6268 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6269 7c32bd05 2019-06-22 stsp
6270 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6271 7c32bd05 2019-06-22 stsp return NULL;
6272 7c32bd05 2019-06-22 stsp }
6273 7c32bd05 2019-06-22 stsp
6274 7c32bd05 2019-06-22 stsp static int
6275 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6276 7c32bd05 2019-06-22 stsp {
6277 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6278 7c32bd05 2019-06-22 stsp
6279 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6280 56e0773d 2019-11-28 stsp 0) == 0;
6281 7c32bd05 2019-06-22 stsp }
6282 7c32bd05 2019-06-22 stsp
6283 7c32bd05 2019-06-22 stsp static const struct got_error *
6284 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6285 7c32bd05 2019-06-22 stsp {
6286 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6287 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6288 7c32bd05 2019-06-22 stsp
6289 7c32bd05 2019-06-22 stsp if (!view->searching) {
6290 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6291 7c32bd05 2019-06-22 stsp return NULL;
6292 7c32bd05 2019-06-22 stsp }
6293 7c32bd05 2019-06-22 stsp
6294 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6295 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6296 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6297 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6298 56e0773d 2019-11-28 stsp s->selected_entry);
6299 7c32bd05 2019-06-22 stsp else
6300 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6301 56e0773d 2019-11-28 stsp } else {
6302 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6303 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6304 56e0773d 2019-11-28 stsp else
6305 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6306 56e0773d 2019-11-28 stsp s->selected_entry);
6307 7c32bd05 2019-06-22 stsp }
6308 7c32bd05 2019-06-22 stsp } else {
6309 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6310 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6311 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6312 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6313 56e0773d 2019-11-28 stsp else
6314 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6315 7c32bd05 2019-06-22 stsp }
6316 7c32bd05 2019-06-22 stsp
6317 7c32bd05 2019-06-22 stsp while (1) {
6318 56e0773d 2019-11-28 stsp if (te == NULL) {
6319 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6320 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6321 ac66afb8 2019-06-24 stsp return NULL;
6322 ac66afb8 2019-06-24 stsp }
6323 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6324 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6325 56e0773d 2019-11-28 stsp else
6326 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6327 7c32bd05 2019-06-22 stsp }
6328 7c32bd05 2019-06-22 stsp
6329 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6330 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6331 56e0773d 2019-11-28 stsp s->matched_entry = te;
6332 7c32bd05 2019-06-22 stsp break;
6333 7c32bd05 2019-06-22 stsp }
6334 7c32bd05 2019-06-22 stsp
6335 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6336 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6337 56e0773d 2019-11-28 stsp else
6338 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6339 7c32bd05 2019-06-22 stsp }
6340 e5a0f69f 2018-08-18 stsp
6341 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6342 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6343 7c32bd05 2019-06-22 stsp s->selected = 0;
6344 7c32bd05 2019-06-22 stsp }
6345 7c32bd05 2019-06-22 stsp
6346 e5a0f69f 2018-08-18 stsp return NULL;
6347 ad80ab7b 2018-08-04 stsp }
6348 ad80ab7b 2018-08-04 stsp
6349 ad80ab7b 2018-08-04 stsp static const struct got_error *
6350 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6351 ad80ab7b 2018-08-04 stsp {
6352 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6353 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6354 e5a0f69f 2018-08-18 stsp char *parent_path;
6355 ad80ab7b 2018-08-04 stsp
6356 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6357 e5a0f69f 2018-08-18 stsp if (err)
6358 e5a0f69f 2018-08-18 stsp return err;
6359 ffd1d5e5 2018-06-23 stsp
6360 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6361 e5a0f69f 2018-08-18 stsp free(parent_path);
6362 669b5ffa 2018-10-07 stsp
6363 9b058f45 2022-06-30 mark view_border(view);
6364 e5a0f69f 2018-08-18 stsp return err;
6365 e5a0f69f 2018-08-18 stsp }
6366 ce52c690 2018-06-23 stsp
6367 e5a0f69f 2018-08-18 stsp static const struct got_error *
6368 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6369 e5a0f69f 2018-08-18 stsp {
6370 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6371 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6372 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6373 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6374 83cc4199 2022-06-13 stsp int begin_x = 0, n, nscroll = view->nlines - 3;
6375 ffd1d5e5 2018-06-23 stsp
6376 e5a0f69f 2018-08-18 stsp switch (ch) {
6377 1e37a5c2 2019-05-12 jcs case 'i':
6378 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6379 640cd7ff 2022-06-22 mark view->count = 0;
6380 1e37a5c2 2019-05-12 jcs break;
6381 1e37a5c2 2019-05-12 jcs case 'l':
6382 640cd7ff 2022-06-22 mark view->count = 0;
6383 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6384 ffd1d5e5 2018-06-23 stsp break;
6385 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6386 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
6387 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
6388 e78dc838 2020-12-04 stsp view->focussed = 0;
6389 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6390 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6391 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6392 1e37a5c2 2019-05-12 jcs if (err)
6393 1e37a5c2 2019-05-12 jcs return err;
6394 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
6395 0dbbbe90 2022-06-17 op if (err)
6396 0dbbbe90 2022-06-17 op return err;
6397 e78dc838 2020-12-04 stsp view->focus_child = 1;
6398 1e37a5c2 2019-05-12 jcs } else
6399 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6400 152c1c93 2020-11-29 stsp break;
6401 152c1c93 2020-11-29 stsp case 'r':
6402 640cd7ff 2022-06-22 mark view->count = 0;
6403 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6404 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
6405 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
6406 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
6407 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6408 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6409 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6410 152c1c93 2020-11-29 stsp if (err) {
6411 152c1c93 2020-11-29 stsp view_close(ref_view);
6412 152c1c93 2020-11-29 stsp return err;
6413 152c1c93 2020-11-29 stsp }
6414 e78dc838 2020-12-04 stsp view->focussed = 0;
6415 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6416 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6417 152c1c93 2020-11-29 stsp err = view_close_child(view);
6418 152c1c93 2020-11-29 stsp if (err)
6419 152c1c93 2020-11-29 stsp return err;
6420 0dbbbe90 2022-06-17 op err = view_set_child(view, ref_view);
6421 0dbbbe90 2022-06-17 op if (err)
6422 0dbbbe90 2022-06-17 op return err;
6423 e78dc838 2020-12-04 stsp view->focus_child = 1;
6424 152c1c93 2020-11-29 stsp } else
6425 152c1c93 2020-11-29 stsp *new_view = ref_view;
6426 e4526bf5 2021-09-03 naddy break;
6427 e4526bf5 2021-09-03 naddy case 'g':
6428 e4526bf5 2021-09-03 naddy case KEY_HOME:
6429 e4526bf5 2021-09-03 naddy s->selected = 0;
6430 640cd7ff 2022-06-22 mark view->count = 0;
6431 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6432 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6433 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6434 e4526bf5 2021-09-03 naddy else
6435 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6436 1e37a5c2 2019-05-12 jcs break;
6437 e4526bf5 2021-09-03 naddy case 'G':
6438 9b058f45 2022-06-30 mark case KEY_END: {
6439 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6440 9b058f45 2022-06-30 mark
6441 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6442 9b058f45 2022-06-30 mark --eos; /* border */
6443 e4526bf5 2021-09-03 naddy s->selected = 0;
6444 640cd7ff 2022-06-22 mark view->count = 0;
6445 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6446 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6447 e4526bf5 2021-09-03 naddy if (te == NULL) {
6448 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6449 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6450 e4526bf5 2021-09-03 naddy n++;
6451 e4526bf5 2021-09-03 naddy }
6452 e4526bf5 2021-09-03 naddy break;
6453 e4526bf5 2021-09-03 naddy }
6454 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6455 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6456 e4526bf5 2021-09-03 naddy }
6457 e4526bf5 2021-09-03 naddy if (n > 0)
6458 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6459 e4526bf5 2021-09-03 naddy break;
6460 9b058f45 2022-06-30 mark }
6461 1e37a5c2 2019-05-12 jcs case 'k':
6462 1e37a5c2 2019-05-12 jcs case KEY_UP:
6463 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6464 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6465 1e37a5c2 2019-05-12 jcs s->selected--;
6466 fa86c4bf 2020-11-29 stsp break;
6467 1e37a5c2 2019-05-12 jcs }
6468 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6469 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6470 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6471 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6472 640cd7ff 2022-06-22 mark view->count = 0;
6473 1e37a5c2 2019-05-12 jcs break;
6474 83cc4199 2022-06-13 stsp case CTRL('u'):
6475 33c3719a 2022-06-15 stsp case 'u':
6476 83cc4199 2022-06-13 stsp nscroll /= 2;
6477 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6478 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6479 ea025d1d 2020-02-22 naddy case CTRL('b'):
6480 61417565 2022-06-20 mark case 'b':
6481 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6482 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6483 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6484 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6485 fa86c4bf 2020-11-29 stsp } else {
6486 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6487 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6488 fa86c4bf 2020-11-29 stsp }
6489 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6490 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6491 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6492 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6493 640cd7ff 2022-06-22 mark view->count = 0;
6494 1e37a5c2 2019-05-12 jcs break;
6495 1e37a5c2 2019-05-12 jcs case 'j':
6496 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6497 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6498 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6499 1e37a5c2 2019-05-12 jcs s->selected++;
6500 1e37a5c2 2019-05-12 jcs break;
6501 1e37a5c2 2019-05-12 jcs }
6502 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6503 640cd7ff 2022-06-22 mark == NULL) {
6504 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6505 640cd7ff 2022-06-22 mark view->count = 0;
6506 1e37a5c2 2019-05-12 jcs break;
6507 640cd7ff 2022-06-22 mark }
6508 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6509 1e37a5c2 2019-05-12 jcs break;
6510 83cc4199 2022-06-13 stsp case CTRL('d'):
6511 33c3719a 2022-06-15 stsp case 'd':
6512 83cc4199 2022-06-13 stsp nscroll /= 2;
6513 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6514 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6515 ea025d1d 2020-02-22 naddy case CTRL('f'):
6516 61417565 2022-06-20 mark case 'f':
6517 48bb96f0 2022-06-20 naddy case ' ':
6518 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6519 1e37a5c2 2019-05-12 jcs == NULL) {
6520 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6521 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6522 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6523 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6524 640cd7ff 2022-06-22 mark else
6525 640cd7ff 2022-06-22 mark view->count = 0;
6526 1e37a5c2 2019-05-12 jcs break;
6527 1e37a5c2 2019-05-12 jcs }
6528 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6529 1e37a5c2 2019-05-12 jcs break;
6530 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6531 1e37a5c2 2019-05-12 jcs case '\r':
6532 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6533 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6534 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6535 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6536 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6537 640cd7ff 2022-06-22 mark view->count = 0;
6538 1e37a5c2 2019-05-12 jcs break;
6539 640cd7ff 2022-06-22 mark }
6540 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6541 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6542 1e37a5c2 2019-05-12 jcs entry);
6543 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6544 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6545 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6546 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6547 1e37a5c2 2019-05-12 jcs s->selected_entry =
6548 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6549 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6550 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6551 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6552 9b058f45 2022-06-30 mark if (err)
6553 9b058f45 2022-06-30 mark break;
6554 9b058f45 2022-06-30 mark }
6555 1e37a5c2 2019-05-12 jcs free(parent);
6556 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6557 56e0773d 2019-11-28 stsp s->selected_entry))) {
6558 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6559 640cd7ff 2022-06-22 mark view->count = 0;
6560 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6561 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6562 1e37a5c2 2019-05-12 jcs if (err)
6563 1e37a5c2 2019-05-12 jcs break;
6564 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6565 941e9f74 2019-05-21 stsp if (err) {
6566 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6567 1e37a5c2 2019-05-12 jcs break;
6568 1e37a5c2 2019-05-12 jcs }
6569 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6570 56e0773d 2019-11-28 stsp s->selected_entry))) {
6571 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6572 9b058f45 2022-06-30 mark int begin_x = 0, begin_y = 0;
6573 1e37a5c2 2019-05-12 jcs
6574 9b058f45 2022-06-30 mark if (view_is_parent_view(view))
6575 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
6576 9b058f45 2022-06-30 mark
6577 9b058f45 2022-06-30 mark err = blame_tree_entry(&blame_view, begin_y, begin_x,
6578 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6579 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6580 1e37a5c2 2019-05-12 jcs if (err)
6581 1e37a5c2 2019-05-12 jcs break;
6582 9b058f45 2022-06-30 mark
6583 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
6584 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
6585 9b058f45 2022-06-30 mark if (err)
6586 9b058f45 2022-06-30 mark break;
6587 9b058f45 2022-06-30 mark }
6588 9b058f45 2022-06-30 mark
6589 640cd7ff 2022-06-22 mark view->count = 0;
6590 e78dc838 2020-12-04 stsp view->focussed = 0;
6591 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6592 9b058f45 2022-06-30 mark blame_view->mode = view->mode;
6593 9b058f45 2022-06-30 mark blame_view->nlines = view->lines - begin_y;
6594 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6595 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6596 669b5ffa 2018-10-07 stsp if (err)
6597 669b5ffa 2018-10-07 stsp return err;
6598 0dbbbe90 2022-06-17 op err = view_set_child(view, blame_view);
6599 0dbbbe90 2022-06-17 op if (err)
6600 0dbbbe90 2022-06-17 op return err;
6601 e78dc838 2020-12-04 stsp view->focus_child = 1;
6602 669b5ffa 2018-10-07 stsp } else
6603 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6604 1e37a5c2 2019-05-12 jcs }
6605 1e37a5c2 2019-05-12 jcs break;
6606 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6607 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6608 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6609 640cd7ff 2022-06-22 mark view->count = 0;
6610 1e37a5c2 2019-05-12 jcs break;
6611 1e37a5c2 2019-05-12 jcs default:
6612 640cd7ff 2022-06-22 mark view->count = 0;
6613 1e37a5c2 2019-05-12 jcs break;
6614 ffd1d5e5 2018-06-23 stsp }
6615 e5a0f69f 2018-08-18 stsp
6616 ffd1d5e5 2018-06-23 stsp return err;
6617 9f7d7167 2018-04-29 stsp }
6618 9f7d7167 2018-04-29 stsp
6619 ffd1d5e5 2018-06-23 stsp __dead static void
6620 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6621 ffd1d5e5 2018-06-23 stsp {
6622 ffd1d5e5 2018-06-23 stsp endwin();
6623 87411fa9 2022-06-16 stsp fprintf(stderr,
6624 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6625 ffd1d5e5 2018-06-23 stsp getprogname());
6626 ffd1d5e5 2018-06-23 stsp exit(1);
6627 ffd1d5e5 2018-06-23 stsp }
6628 ffd1d5e5 2018-06-23 stsp
6629 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6630 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6631 ffd1d5e5 2018-06-23 stsp {
6632 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6633 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6634 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6635 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6636 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6637 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6638 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6639 4e97c21c 2020-12-06 stsp char *label = NULL;
6640 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6641 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6642 ffd1d5e5 2018-06-23 stsp int ch;
6643 5221c383 2018-08-01 stsp struct tog_view *view;
6644 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6645 70ac5f84 2019-03-28 stsp
6646 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6647 ffd1d5e5 2018-06-23 stsp switch (ch) {
6648 ffd1d5e5 2018-06-23 stsp case 'c':
6649 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6650 74283ab8 2020-02-07 stsp break;
6651 74283ab8 2020-02-07 stsp case 'r':
6652 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6653 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6654 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6655 74283ab8 2020-02-07 stsp optarg);
6656 ffd1d5e5 2018-06-23 stsp break;
6657 ffd1d5e5 2018-06-23 stsp default:
6658 e99e2d15 2020-11-24 naddy usage_tree();
6659 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6660 ffd1d5e5 2018-06-23 stsp }
6661 ffd1d5e5 2018-06-23 stsp }
6662 ffd1d5e5 2018-06-23 stsp
6663 ffd1d5e5 2018-06-23 stsp argc -= optind;
6664 ffd1d5e5 2018-06-23 stsp argv += optind;
6665 ffd1d5e5 2018-06-23 stsp
6666 55cccc34 2020-02-20 stsp if (argc > 1)
6667 e99e2d15 2020-11-24 naddy usage_tree();
6668 0ae84acc 2022-06-15 tracey
6669 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6670 0ae84acc 2022-06-15 tracey if (error != NULL)
6671 0ae84acc 2022-06-15 tracey goto done;
6672 74283ab8 2020-02-07 stsp
6673 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6674 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6675 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6676 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6677 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6678 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6679 c156c7a4 2020-12-18 stsp goto done;
6680 55cccc34 2020-02-20 stsp if (worktree)
6681 52185f70 2019-02-05 stsp repo_path =
6682 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6683 55cccc34 2020-02-20 stsp else
6684 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6685 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6686 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6687 c156c7a4 2020-12-18 stsp goto done;
6688 c156c7a4 2020-12-18 stsp }
6689 55cccc34 2020-02-20 stsp }
6690 a915003a 2019-02-05 stsp
6691 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6692 c02c541e 2019-03-29 stsp if (error != NULL)
6693 52185f70 2019-02-05 stsp goto done;
6694 d188b9a6 2019-01-04 stsp
6695 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6696 55cccc34 2020-02-20 stsp repo, worktree);
6697 55cccc34 2020-02-20 stsp if (error)
6698 55cccc34 2020-02-20 stsp goto done;
6699 55cccc34 2020-02-20 stsp
6700 55cccc34 2020-02-20 stsp init_curses();
6701 55cccc34 2020-02-20 stsp
6702 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6703 c02c541e 2019-03-29 stsp if (error)
6704 52185f70 2019-02-05 stsp goto done;
6705 ffd1d5e5 2018-06-23 stsp
6706 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6707 51a10b52 2020-12-26 stsp if (error)
6708 51a10b52 2020-12-26 stsp goto done;
6709 51a10b52 2020-12-26 stsp
6710 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6711 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6712 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6713 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6714 4e97c21c 2020-12-06 stsp if (error)
6715 4e97c21c 2020-12-06 stsp goto done;
6716 4e97c21c 2020-12-06 stsp head_ref_name = label;
6717 4e97c21c 2020-12-06 stsp } else {
6718 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6719 4e97c21c 2020-12-06 stsp if (error == NULL)
6720 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6721 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6722 4e97c21c 2020-12-06 stsp goto done;
6723 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6724 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6725 4e97c21c 2020-12-06 stsp if (error)
6726 4e97c21c 2020-12-06 stsp goto done;
6727 4e97c21c 2020-12-06 stsp }
6728 ffd1d5e5 2018-06-23 stsp
6729 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6730 a44927cc 2022-04-07 stsp if (error)
6731 a44927cc 2022-04-07 stsp goto done;
6732 a44927cc 2022-04-07 stsp
6733 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6734 5221c383 2018-08-01 stsp if (view == NULL) {
6735 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6736 5221c383 2018-08-01 stsp goto done;
6737 5221c383 2018-08-01 stsp }
6738 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6739 ad80ab7b 2018-08-04 stsp if (error)
6740 ad80ab7b 2018-08-04 stsp goto done;
6741 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6742 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
6743 d91faf3b 2020-12-01 naddy in_repo_path);
6744 55cccc34 2020-02-20 stsp if (error)
6745 55cccc34 2020-02-20 stsp goto done;
6746 55cccc34 2020-02-20 stsp }
6747 55cccc34 2020-02-20 stsp
6748 55cccc34 2020-02-20 stsp if (worktree) {
6749 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6750 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6751 55cccc34 2020-02-20 stsp worktree = NULL;
6752 55cccc34 2020-02-20 stsp }
6753 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6754 ffd1d5e5 2018-06-23 stsp done:
6755 52185f70 2019-02-05 stsp free(repo_path);
6756 e4a0e26d 2020-02-20 stsp free(cwd);
6757 ffd1d5e5 2018-06-23 stsp free(commit_id);
6758 4e97c21c 2020-12-06 stsp free(label);
6759 486cd271 2020-12-06 stsp if (ref)
6760 486cd271 2020-12-06 stsp got_ref_close(ref);
6761 1d0f4054 2021-06-17 stsp if (repo) {
6762 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6763 1d0f4054 2021-06-17 stsp if (error == NULL)
6764 1d0f4054 2021-06-17 stsp error = close_err;
6765 1d0f4054 2021-06-17 stsp }
6766 0ae84acc 2022-06-15 tracey if (pack_fds) {
6767 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6768 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6769 0ae84acc 2022-06-15 tracey if (error == NULL)
6770 0ae84acc 2022-06-15 tracey error = pack_err;
6771 0ae84acc 2022-06-15 tracey }
6772 51a10b52 2020-12-26 stsp tog_free_refs();
6773 ffd1d5e5 2018-06-23 stsp return error;
6774 6458efa5 2020-11-24 stsp }
6775 6458efa5 2020-11-24 stsp
6776 6458efa5 2020-11-24 stsp static const struct got_error *
6777 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6778 6458efa5 2020-11-24 stsp {
6779 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6780 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6781 6458efa5 2020-11-24 stsp
6782 6458efa5 2020-11-24 stsp s->nrefs = 0;
6783 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6784 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
6785 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
6786 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
6787 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
6788 6458efa5 2020-11-24 stsp continue;
6789 6458efa5 2020-11-24 stsp
6790 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6791 6458efa5 2020-11-24 stsp if (re == NULL)
6792 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6793 6458efa5 2020-11-24 stsp
6794 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6795 8924d611 2020-12-26 stsp if (re->ref == NULL)
6796 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6797 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6798 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6799 6458efa5 2020-11-24 stsp }
6800 6458efa5 2020-11-24 stsp
6801 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6802 6458efa5 2020-11-24 stsp return NULL;
6803 6458efa5 2020-11-24 stsp }
6804 6458efa5 2020-11-24 stsp
6805 336075a4 2022-06-25 op static void
6806 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6807 6458efa5 2020-11-24 stsp {
6808 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6809 6458efa5 2020-11-24 stsp
6810 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6811 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6812 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6813 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6814 6458efa5 2020-11-24 stsp free(re);
6815 6458efa5 2020-11-24 stsp }
6816 6458efa5 2020-11-24 stsp }
6817 6458efa5 2020-11-24 stsp
6818 6458efa5 2020-11-24 stsp static const struct got_error *
6819 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6820 6458efa5 2020-11-24 stsp {
6821 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6822 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6823 6458efa5 2020-11-24 stsp
6824 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6825 6458efa5 2020-11-24 stsp s->repo = repo;
6826 6458efa5 2020-11-24 stsp
6827 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6828 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6829 6458efa5 2020-11-24 stsp
6830 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6831 6458efa5 2020-11-24 stsp if (err)
6832 6458efa5 2020-11-24 stsp return err;
6833 34ba6917 2020-11-30 stsp
6834 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6835 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6836 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6837 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6838 6458efa5 2020-11-24 stsp if (err)
6839 6458efa5 2020-11-24 stsp goto done;
6840 6458efa5 2020-11-24 stsp
6841 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6842 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6843 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6844 6458efa5 2020-11-24 stsp if (err)
6845 6458efa5 2020-11-24 stsp goto done;
6846 6458efa5 2020-11-24 stsp
6847 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6848 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6849 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6850 cc488aa7 2022-01-23 stsp if (err)
6851 cc488aa7 2022-01-23 stsp goto done;
6852 cc488aa7 2022-01-23 stsp
6853 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
6854 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
6855 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
6856 6458efa5 2020-11-24 stsp if (err)
6857 6458efa5 2020-11-24 stsp goto done;
6858 6458efa5 2020-11-24 stsp }
6859 6458efa5 2020-11-24 stsp
6860 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6861 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6862 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6863 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6864 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6865 6458efa5 2020-11-24 stsp done:
6866 6458efa5 2020-11-24 stsp if (err)
6867 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6868 6458efa5 2020-11-24 stsp return err;
6869 6458efa5 2020-11-24 stsp }
6870 6458efa5 2020-11-24 stsp
6871 6458efa5 2020-11-24 stsp static const struct got_error *
6872 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6873 6458efa5 2020-11-24 stsp {
6874 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6875 6458efa5 2020-11-24 stsp
6876 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6877 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6878 6458efa5 2020-11-24 stsp
6879 6458efa5 2020-11-24 stsp return NULL;
6880 9f7d7167 2018-04-29 stsp }
6881 ce5b7c56 2019-07-09 stsp
6882 6458efa5 2020-11-24 stsp static const struct got_error *
6883 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6884 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6885 6458efa5 2020-11-24 stsp {
6886 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6887 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6888 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6889 6458efa5 2020-11-24 stsp int obj_type;
6890 6458efa5 2020-11-24 stsp
6891 c42c9805 2020-11-24 stsp *commit_id = NULL;
6892 6458efa5 2020-11-24 stsp
6893 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6894 6458efa5 2020-11-24 stsp if (err)
6895 6458efa5 2020-11-24 stsp return err;
6896 6458efa5 2020-11-24 stsp
6897 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6898 6458efa5 2020-11-24 stsp if (err)
6899 6458efa5 2020-11-24 stsp goto done;
6900 6458efa5 2020-11-24 stsp
6901 6458efa5 2020-11-24 stsp switch (obj_type) {
6902 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6903 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6904 6458efa5 2020-11-24 stsp break;
6905 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6906 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6907 6458efa5 2020-11-24 stsp if (err)
6908 6458efa5 2020-11-24 stsp goto done;
6909 c42c9805 2020-11-24 stsp free(obj_id);
6910 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6911 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6912 c42c9805 2020-11-24 stsp if (err)
6913 6458efa5 2020-11-24 stsp goto done;
6914 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6915 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6916 c42c9805 2020-11-24 stsp goto done;
6917 c42c9805 2020-11-24 stsp }
6918 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6919 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6920 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6921 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6922 c42c9805 2020-11-24 stsp goto done;
6923 c42c9805 2020-11-24 stsp }
6924 6458efa5 2020-11-24 stsp break;
6925 6458efa5 2020-11-24 stsp default:
6926 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6927 c42c9805 2020-11-24 stsp break;
6928 6458efa5 2020-11-24 stsp }
6929 6458efa5 2020-11-24 stsp
6930 c42c9805 2020-11-24 stsp done:
6931 c42c9805 2020-11-24 stsp if (tag)
6932 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6933 c42c9805 2020-11-24 stsp if (err) {
6934 c42c9805 2020-11-24 stsp free(*commit_id);
6935 c42c9805 2020-11-24 stsp *commit_id = NULL;
6936 c42c9805 2020-11-24 stsp }
6937 c42c9805 2020-11-24 stsp return err;
6938 c42c9805 2020-11-24 stsp }
6939 c42c9805 2020-11-24 stsp
6940 c42c9805 2020-11-24 stsp static const struct got_error *
6941 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6942 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6943 c42c9805 2020-11-24 stsp {
6944 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6945 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6946 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6947 c42c9805 2020-11-24 stsp
6948 c42c9805 2020-11-24 stsp *new_view = NULL;
6949 c42c9805 2020-11-24 stsp
6950 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6951 c42c9805 2020-11-24 stsp if (err) {
6952 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6953 c42c9805 2020-11-24 stsp return err;
6954 c42c9805 2020-11-24 stsp else
6955 c42c9805 2020-11-24 stsp return NULL;
6956 c42c9805 2020-11-24 stsp }
6957 c42c9805 2020-11-24 stsp
6958 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6959 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6960 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6961 6458efa5 2020-11-24 stsp goto done;
6962 6458efa5 2020-11-24 stsp }
6963 6458efa5 2020-11-24 stsp
6964 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6965 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6966 6458efa5 2020-11-24 stsp done:
6967 6458efa5 2020-11-24 stsp if (err)
6968 6458efa5 2020-11-24 stsp view_close(log_view);
6969 6458efa5 2020-11-24 stsp else
6970 6458efa5 2020-11-24 stsp *new_view = log_view;
6971 c42c9805 2020-11-24 stsp free(commit_id);
6972 6458efa5 2020-11-24 stsp return err;
6973 6458efa5 2020-11-24 stsp }
6974 6458efa5 2020-11-24 stsp
6975 ce5b7c56 2019-07-09 stsp static void
6976 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6977 6458efa5 2020-11-24 stsp {
6978 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6979 34ba6917 2020-11-30 stsp int i = 0;
6980 6458efa5 2020-11-24 stsp
6981 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6982 6458efa5 2020-11-24 stsp return;
6983 6458efa5 2020-11-24 stsp
6984 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6985 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6986 34ba6917 2020-11-30 stsp if (re == NULL)
6987 34ba6917 2020-11-30 stsp break;
6988 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6989 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6990 6458efa5 2020-11-24 stsp }
6991 6458efa5 2020-11-24 stsp }
6992 6458efa5 2020-11-24 stsp
6993 9b058f45 2022-06-30 mark static const struct got_error *
6994 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
6995 6458efa5 2020-11-24 stsp {
6996 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
6997 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6998 6458efa5 2020-11-24 stsp int n = 0;
6999 6458efa5 2020-11-24 stsp
7000 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7001 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7002 6458efa5 2020-11-24 stsp else
7003 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7004 6458efa5 2020-11-24 stsp
7005 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7006 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7007 9b058f45 2022-06-30 mark if (last)
7008 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7009 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7010 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7011 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7012 6458efa5 2020-11-24 stsp }
7013 6458efa5 2020-11-24 stsp }
7014 9b058f45 2022-06-30 mark
7015 9b058f45 2022-06-30 mark return NULL;
7016 6458efa5 2020-11-24 stsp }
7017 6458efa5 2020-11-24 stsp
7018 6458efa5 2020-11-24 stsp static const struct got_error *
7019 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7020 6458efa5 2020-11-24 stsp {
7021 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7022 6458efa5 2020-11-24 stsp
7023 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7024 6458efa5 2020-11-24 stsp return NULL;
7025 6458efa5 2020-11-24 stsp }
7026 6458efa5 2020-11-24 stsp
7027 6458efa5 2020-11-24 stsp static int
7028 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7029 6458efa5 2020-11-24 stsp {
7030 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7031 6458efa5 2020-11-24 stsp
7032 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7033 6458efa5 2020-11-24 stsp 0) == 0;
7034 6458efa5 2020-11-24 stsp }
7035 6458efa5 2020-11-24 stsp
7036 6458efa5 2020-11-24 stsp static const struct got_error *
7037 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7038 6458efa5 2020-11-24 stsp {
7039 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7040 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7041 6458efa5 2020-11-24 stsp
7042 6458efa5 2020-11-24 stsp if (!view->searching) {
7043 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7044 6458efa5 2020-11-24 stsp return NULL;
7045 6458efa5 2020-11-24 stsp }
7046 6458efa5 2020-11-24 stsp
7047 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7048 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7049 6458efa5 2020-11-24 stsp if (s->selected_entry)
7050 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7051 6458efa5 2020-11-24 stsp else
7052 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7053 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7054 6458efa5 2020-11-24 stsp } else {
7055 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7056 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7057 6458efa5 2020-11-24 stsp else
7058 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7059 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7060 6458efa5 2020-11-24 stsp }
7061 6458efa5 2020-11-24 stsp } else {
7062 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7063 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7064 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7065 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7066 6458efa5 2020-11-24 stsp else
7067 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7068 6458efa5 2020-11-24 stsp }
7069 6458efa5 2020-11-24 stsp
7070 6458efa5 2020-11-24 stsp while (1) {
7071 6458efa5 2020-11-24 stsp if (re == NULL) {
7072 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7073 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7074 6458efa5 2020-11-24 stsp return NULL;
7075 6458efa5 2020-11-24 stsp }
7076 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7077 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7078 6458efa5 2020-11-24 stsp else
7079 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7080 6458efa5 2020-11-24 stsp }
7081 6458efa5 2020-11-24 stsp
7082 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7083 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7084 6458efa5 2020-11-24 stsp s->matched_entry = re;
7085 6458efa5 2020-11-24 stsp break;
7086 6458efa5 2020-11-24 stsp }
7087 6458efa5 2020-11-24 stsp
7088 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7089 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7090 6458efa5 2020-11-24 stsp else
7091 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7092 6458efa5 2020-11-24 stsp }
7093 6458efa5 2020-11-24 stsp
7094 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7095 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7096 6458efa5 2020-11-24 stsp s->selected = 0;
7097 6458efa5 2020-11-24 stsp }
7098 6458efa5 2020-11-24 stsp
7099 6458efa5 2020-11-24 stsp return NULL;
7100 6458efa5 2020-11-24 stsp }
7101 6458efa5 2020-11-24 stsp
7102 6458efa5 2020-11-24 stsp static const struct got_error *
7103 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7104 6458efa5 2020-11-24 stsp {
7105 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7106 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7107 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7108 6458efa5 2020-11-24 stsp char *line = NULL;
7109 6458efa5 2020-11-24 stsp wchar_t *wline;
7110 6458efa5 2020-11-24 stsp struct tog_color *tc;
7111 6458efa5 2020-11-24 stsp int width, n;
7112 6458efa5 2020-11-24 stsp int limit = view->nlines;
7113 6458efa5 2020-11-24 stsp
7114 6458efa5 2020-11-24 stsp werase(view->window);
7115 6458efa5 2020-11-24 stsp
7116 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7117 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
7118 9b058f45 2022-06-30 mark view_is_splitscreen(view->child))
7119 9b058f45 2022-06-30 mark --limit; /* border */
7120 6458efa5 2020-11-24 stsp
7121 6458efa5 2020-11-24 stsp if (limit == 0)
7122 6458efa5 2020-11-24 stsp return NULL;
7123 6458efa5 2020-11-24 stsp
7124 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7125 6458efa5 2020-11-24 stsp
7126 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7127 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7128 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7129 6458efa5 2020-11-24 stsp
7130 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7131 6458efa5 2020-11-24 stsp if (err) {
7132 6458efa5 2020-11-24 stsp free(line);
7133 6458efa5 2020-11-24 stsp return err;
7134 6458efa5 2020-11-24 stsp }
7135 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7136 6458efa5 2020-11-24 stsp wstandout(view->window);
7137 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7138 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7139 6458efa5 2020-11-24 stsp wstandend(view->window);
7140 6458efa5 2020-11-24 stsp free(wline);
7141 6458efa5 2020-11-24 stsp wline = NULL;
7142 6458efa5 2020-11-24 stsp free(line);
7143 6458efa5 2020-11-24 stsp line = NULL;
7144 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7145 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7146 6458efa5 2020-11-24 stsp if (--limit <= 0)
7147 6458efa5 2020-11-24 stsp return NULL;
7148 6458efa5 2020-11-24 stsp
7149 6458efa5 2020-11-24 stsp n = 0;
7150 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7151 6458efa5 2020-11-24 stsp char *line = NULL;
7152 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7153 6458efa5 2020-11-24 stsp
7154 b4996bee 2022-06-16 stsp if (s->show_date) {
7155 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7156 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7157 b4996bee 2022-06-16 stsp struct got_object_id *id;
7158 b4996bee 2022-06-16 stsp struct tm tm;
7159 b4996bee 2022-06-16 stsp time_t t;
7160 b4996bee 2022-06-16 stsp
7161 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7162 b4996bee 2022-06-16 stsp if (err)
7163 b4996bee 2022-06-16 stsp return err;
7164 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7165 b4996bee 2022-06-16 stsp if (err) {
7166 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7167 b4996bee 2022-06-16 stsp free(id);
7168 b4996bee 2022-06-16 stsp return err;
7169 b4996bee 2022-06-16 stsp }
7170 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7171 b4996bee 2022-06-16 stsp id);
7172 b4996bee 2022-06-16 stsp if (err) {
7173 b4996bee 2022-06-16 stsp free(id);
7174 b4996bee 2022-06-16 stsp return err;
7175 b4996bee 2022-06-16 stsp }
7176 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7177 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7178 b4996bee 2022-06-16 stsp } else {
7179 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7180 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7181 b4996bee 2022-06-16 stsp }
7182 b4996bee 2022-06-16 stsp free(id);
7183 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7184 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7185 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7186 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7187 b4996bee 2022-06-16 stsp }
7188 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7189 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7190 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7191 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7192 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7193 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7194 6458efa5 2020-11-24 stsp struct got_object_id *id;
7195 6458efa5 2020-11-24 stsp char *id_str;
7196 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7197 6458efa5 2020-11-24 stsp if (err)
7198 6458efa5 2020-11-24 stsp return err;
7199 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7200 6458efa5 2020-11-24 stsp if (err) {
7201 6458efa5 2020-11-24 stsp free(id);
7202 6458efa5 2020-11-24 stsp return err;
7203 6458efa5 2020-11-24 stsp }
7204 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7205 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7206 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7207 6458efa5 2020-11-24 stsp free(id);
7208 6458efa5 2020-11-24 stsp free(id_str);
7209 6458efa5 2020-11-24 stsp return err;
7210 6458efa5 2020-11-24 stsp }
7211 6458efa5 2020-11-24 stsp free(id);
7212 6458efa5 2020-11-24 stsp free(id_str);
7213 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7214 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7215 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7216 6458efa5 2020-11-24 stsp
7217 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7218 ccda2f4d 2022-06-16 stsp 0, 0);
7219 6458efa5 2020-11-24 stsp if (err) {
7220 6458efa5 2020-11-24 stsp free(line);
7221 6458efa5 2020-11-24 stsp return err;
7222 6458efa5 2020-11-24 stsp }
7223 6458efa5 2020-11-24 stsp if (n == s->selected) {
7224 6458efa5 2020-11-24 stsp if (view->focussed)
7225 6458efa5 2020-11-24 stsp wstandout(view->window);
7226 6458efa5 2020-11-24 stsp s->selected_entry = re;
7227 6458efa5 2020-11-24 stsp }
7228 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7229 6458efa5 2020-11-24 stsp if (tc)
7230 6458efa5 2020-11-24 stsp wattr_on(view->window,
7231 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7232 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7233 6458efa5 2020-11-24 stsp if (tc)
7234 6458efa5 2020-11-24 stsp wattr_off(view->window,
7235 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7236 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7237 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7238 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7239 6458efa5 2020-11-24 stsp wstandend(view->window);
7240 6458efa5 2020-11-24 stsp free(line);
7241 6458efa5 2020-11-24 stsp free(wline);
7242 6458efa5 2020-11-24 stsp wline = NULL;
7243 6458efa5 2020-11-24 stsp n++;
7244 6458efa5 2020-11-24 stsp s->ndisplayed++;
7245 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7246 6458efa5 2020-11-24 stsp
7247 6458efa5 2020-11-24 stsp limit--;
7248 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7249 6458efa5 2020-11-24 stsp }
7250 6458efa5 2020-11-24 stsp
7251 9b058f45 2022-06-30 mark view_border(view);
7252 6458efa5 2020-11-24 stsp return err;
7253 6458efa5 2020-11-24 stsp }
7254 6458efa5 2020-11-24 stsp
7255 6458efa5 2020-11-24 stsp static const struct got_error *
7256 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
7257 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7258 c42c9805 2020-11-24 stsp {
7259 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7260 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7261 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7262 c42c9805 2020-11-24 stsp
7263 c42c9805 2020-11-24 stsp *new_view = NULL;
7264 c42c9805 2020-11-24 stsp
7265 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7266 c42c9805 2020-11-24 stsp if (err) {
7267 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7268 c42c9805 2020-11-24 stsp return err;
7269 c42c9805 2020-11-24 stsp else
7270 c42c9805 2020-11-24 stsp return NULL;
7271 c42c9805 2020-11-24 stsp }
7272 c42c9805 2020-11-24 stsp
7273 c42c9805 2020-11-24 stsp
7274 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
7275 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7276 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7277 c42c9805 2020-11-24 stsp goto done;
7278 c42c9805 2020-11-24 stsp }
7279 c42c9805 2020-11-24 stsp
7280 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7281 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7282 c42c9805 2020-11-24 stsp if (err)
7283 c42c9805 2020-11-24 stsp goto done;
7284 c42c9805 2020-11-24 stsp
7285 c42c9805 2020-11-24 stsp *new_view = tree_view;
7286 c42c9805 2020-11-24 stsp done:
7287 c42c9805 2020-11-24 stsp free(commit_id);
7288 c42c9805 2020-11-24 stsp return err;
7289 c42c9805 2020-11-24 stsp }
7290 c42c9805 2020-11-24 stsp static const struct got_error *
7291 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7292 6458efa5 2020-11-24 stsp {
7293 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7294 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7295 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7296 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7297 9b058f45 2022-06-30 mark int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7298 6458efa5 2020-11-24 stsp
7299 6458efa5 2020-11-24 stsp switch (ch) {
7300 6458efa5 2020-11-24 stsp case 'i':
7301 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7302 640cd7ff 2022-06-22 mark view->count = 0;
7303 7f66531d 2021-11-16 stsp break;
7304 b4996bee 2022-06-16 stsp case 'm':
7305 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7306 640cd7ff 2022-06-22 mark view->count = 0;
7307 b4996bee 2022-06-16 stsp break;
7308 07a065fe 2021-11-20 stsp case 'o':
7309 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7310 640cd7ff 2022-06-22 mark view->count = 0;
7311 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7312 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7313 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7314 50617b77 2021-11-20 stsp if (err)
7315 50617b77 2021-11-20 stsp break;
7316 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7317 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7318 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7319 7f66531d 2021-11-16 stsp if (err)
7320 7f66531d 2021-11-16 stsp break;
7321 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7322 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7323 6458efa5 2020-11-24 stsp break;
7324 6458efa5 2020-11-24 stsp case KEY_ENTER:
7325 6458efa5 2020-11-24 stsp case '\r':
7326 640cd7ff 2022-06-22 mark view->count = 0;
7327 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7328 6458efa5 2020-11-24 stsp break;
7329 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7330 9b058f45 2022-06-30 mark view_get_split(view, &begin_y, &begin_x);
7331 9b058f45 2022-06-30 mark
7332 9b058f45 2022-06-30 mark err = log_ref_entry(&log_view, begin_y, begin_x,
7333 9b058f45 2022-06-30 mark s->selected_entry, s->repo);
7334 9b058f45 2022-06-30 mark if (err)
7335 9b058f45 2022-06-30 mark break;
7336 9b058f45 2022-06-30 mark
7337 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
7338 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
7339 9b058f45 2022-06-30 mark if (err)
7340 9b058f45 2022-06-30 mark break;
7341 9b058f45 2022-06-30 mark }
7342 9b058f45 2022-06-30 mark
7343 e78dc838 2020-12-04 stsp view->focussed = 0;
7344 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7345 9b058f45 2022-06-30 mark log_view->mode = view->mode;
7346 9b058f45 2022-06-30 mark log_view->nlines = view->lines - begin_y;
7347 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7348 6458efa5 2020-11-24 stsp err = view_close_child(view);
7349 6458efa5 2020-11-24 stsp if (err)
7350 6458efa5 2020-11-24 stsp return err;
7351 0dbbbe90 2022-06-17 op err = view_set_child(view, log_view);
7352 0dbbbe90 2022-06-17 op if (err)
7353 0dbbbe90 2022-06-17 op return err;
7354 e78dc838 2020-12-04 stsp view->focus_child = 1;
7355 6458efa5 2020-11-24 stsp } else
7356 6458efa5 2020-11-24 stsp *new_view = log_view;
7357 6458efa5 2020-11-24 stsp break;
7358 c42c9805 2020-11-24 stsp case 't':
7359 640cd7ff 2022-06-22 mark view->count = 0;
7360 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7361 c42c9805 2020-11-24 stsp break;
7362 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7363 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
7364 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
7365 c42c9805 2020-11-24 stsp s->repo);
7366 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7367 c42c9805 2020-11-24 stsp break;
7368 e78dc838 2020-12-04 stsp view->focussed = 0;
7369 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7370 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7371 c42c9805 2020-11-24 stsp err = view_close_child(view);
7372 c42c9805 2020-11-24 stsp if (err)
7373 c42c9805 2020-11-24 stsp return err;
7374 0dbbbe90 2022-06-17 op err = view_set_child(view, tree_view);
7375 0dbbbe90 2022-06-17 op if (err)
7376 0dbbbe90 2022-06-17 op return err;
7377 e78dc838 2020-12-04 stsp view->focus_child = 1;
7378 c42c9805 2020-11-24 stsp } else
7379 c42c9805 2020-11-24 stsp *new_view = tree_view;
7380 c42c9805 2020-11-24 stsp break;
7381 e4526bf5 2021-09-03 naddy case 'g':
7382 e4526bf5 2021-09-03 naddy case KEY_HOME:
7383 e4526bf5 2021-09-03 naddy s->selected = 0;
7384 640cd7ff 2022-06-22 mark view->count = 0;
7385 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7386 e4526bf5 2021-09-03 naddy break;
7387 e4526bf5 2021-09-03 naddy case 'G':
7388 9b058f45 2022-06-30 mark case KEY_END: {
7389 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7390 9b058f45 2022-06-30 mark
7391 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7392 9b058f45 2022-06-30 mark --eos; /* border */
7393 e4526bf5 2021-09-03 naddy s->selected = 0;
7394 640cd7ff 2022-06-22 mark view->count = 0;
7395 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7396 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7397 e4526bf5 2021-09-03 naddy if (re == NULL)
7398 e4526bf5 2021-09-03 naddy break;
7399 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7400 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7401 e4526bf5 2021-09-03 naddy }
7402 e4526bf5 2021-09-03 naddy if (n > 0)
7403 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7404 e4526bf5 2021-09-03 naddy break;
7405 9b058f45 2022-06-30 mark }
7406 6458efa5 2020-11-24 stsp case 'k':
7407 6458efa5 2020-11-24 stsp case KEY_UP:
7408 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7409 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7410 6458efa5 2020-11-24 stsp s->selected--;
7411 6458efa5 2020-11-24 stsp break;
7412 34ba6917 2020-11-30 stsp }
7413 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7414 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7415 640cd7ff 2022-06-22 mark view->count = 0;
7416 6458efa5 2020-11-24 stsp break;
7417 83cc4199 2022-06-13 stsp case CTRL('u'):
7418 33c3719a 2022-06-15 stsp case 'u':
7419 83cc4199 2022-06-13 stsp nscroll /= 2;
7420 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7421 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7422 6458efa5 2020-11-24 stsp case CTRL('b'):
7423 61417565 2022-06-20 mark case 'b':
7424 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7425 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7426 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7427 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7428 640cd7ff 2022-06-22 mark view->count = 0;
7429 6458efa5 2020-11-24 stsp break;
7430 6458efa5 2020-11-24 stsp case 'j':
7431 6458efa5 2020-11-24 stsp case KEY_DOWN:
7432 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7433 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7434 6458efa5 2020-11-24 stsp s->selected++;
7435 6458efa5 2020-11-24 stsp break;
7436 6458efa5 2020-11-24 stsp }
7437 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7438 6458efa5 2020-11-24 stsp /* can't scroll any further */
7439 640cd7ff 2022-06-22 mark view->count = 0;
7440 6458efa5 2020-11-24 stsp break;
7441 640cd7ff 2022-06-22 mark }
7442 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7443 6458efa5 2020-11-24 stsp break;
7444 83cc4199 2022-06-13 stsp case CTRL('d'):
7445 33c3719a 2022-06-15 stsp case 'd':
7446 83cc4199 2022-06-13 stsp nscroll /= 2;
7447 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7448 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7449 6458efa5 2020-11-24 stsp case CTRL('f'):
7450 61417565 2022-06-20 mark case 'f':
7451 48bb96f0 2022-06-20 naddy case ' ':
7452 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7453 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7454 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7455 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7456 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7457 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7458 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7459 640cd7ff 2022-06-22 mark view->count = 0;
7460 6458efa5 2020-11-24 stsp break;
7461 6458efa5 2020-11-24 stsp }
7462 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7463 6458efa5 2020-11-24 stsp break;
7464 6458efa5 2020-11-24 stsp case CTRL('l'):
7465 640cd7ff 2022-06-22 mark view->count = 0;
7466 8924d611 2020-12-26 stsp tog_free_refs();
7467 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7468 8924d611 2020-12-26 stsp if (err)
7469 8924d611 2020-12-26 stsp break;
7470 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7471 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7472 6458efa5 2020-11-24 stsp break;
7473 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7474 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7475 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7476 6458efa5 2020-11-24 stsp break;
7477 6458efa5 2020-11-24 stsp default:
7478 640cd7ff 2022-06-22 mark view->count = 0;
7479 6458efa5 2020-11-24 stsp break;
7480 6458efa5 2020-11-24 stsp }
7481 6458efa5 2020-11-24 stsp
7482 6458efa5 2020-11-24 stsp return err;
7483 6458efa5 2020-11-24 stsp }
7484 6458efa5 2020-11-24 stsp
7485 6458efa5 2020-11-24 stsp __dead static void
7486 6458efa5 2020-11-24 stsp usage_ref(void)
7487 6458efa5 2020-11-24 stsp {
7488 6458efa5 2020-11-24 stsp endwin();
7489 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7490 6458efa5 2020-11-24 stsp getprogname());
7491 6458efa5 2020-11-24 stsp exit(1);
7492 6458efa5 2020-11-24 stsp }
7493 6458efa5 2020-11-24 stsp
7494 6458efa5 2020-11-24 stsp static const struct got_error *
7495 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7496 6458efa5 2020-11-24 stsp {
7497 6458efa5 2020-11-24 stsp const struct got_error *error;
7498 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7499 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7500 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7501 6458efa5 2020-11-24 stsp int ch;
7502 6458efa5 2020-11-24 stsp struct tog_view *view;
7503 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7504 6458efa5 2020-11-24 stsp
7505 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7506 6458efa5 2020-11-24 stsp switch (ch) {
7507 6458efa5 2020-11-24 stsp case 'r':
7508 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7509 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7510 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7511 6458efa5 2020-11-24 stsp optarg);
7512 6458efa5 2020-11-24 stsp break;
7513 6458efa5 2020-11-24 stsp default:
7514 e99e2d15 2020-11-24 naddy usage_ref();
7515 6458efa5 2020-11-24 stsp /* NOTREACHED */
7516 6458efa5 2020-11-24 stsp }
7517 6458efa5 2020-11-24 stsp }
7518 6458efa5 2020-11-24 stsp
7519 6458efa5 2020-11-24 stsp argc -= optind;
7520 6458efa5 2020-11-24 stsp argv += optind;
7521 6458efa5 2020-11-24 stsp
7522 6458efa5 2020-11-24 stsp if (argc > 1)
7523 e99e2d15 2020-11-24 naddy usage_ref();
7524 0ae84acc 2022-06-15 tracey
7525 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7526 0ae84acc 2022-06-15 tracey if (error != NULL)
7527 0ae84acc 2022-06-15 tracey goto done;
7528 6458efa5 2020-11-24 stsp
7529 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7530 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7531 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7532 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7533 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7534 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7535 c156c7a4 2020-12-18 stsp goto done;
7536 6458efa5 2020-11-24 stsp if (worktree)
7537 6458efa5 2020-11-24 stsp repo_path =
7538 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7539 6458efa5 2020-11-24 stsp else
7540 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7541 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7542 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7543 c156c7a4 2020-12-18 stsp goto done;
7544 c156c7a4 2020-12-18 stsp }
7545 6458efa5 2020-11-24 stsp }
7546 6458efa5 2020-11-24 stsp
7547 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7548 6458efa5 2020-11-24 stsp if (error != NULL)
7549 6458efa5 2020-11-24 stsp goto done;
7550 6458efa5 2020-11-24 stsp
7551 6458efa5 2020-11-24 stsp init_curses();
7552 6458efa5 2020-11-24 stsp
7553 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7554 51a10b52 2020-12-26 stsp if (error)
7555 51a10b52 2020-12-26 stsp goto done;
7556 51a10b52 2020-12-26 stsp
7557 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7558 6458efa5 2020-11-24 stsp if (error)
7559 6458efa5 2020-11-24 stsp goto done;
7560 6458efa5 2020-11-24 stsp
7561 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7562 6458efa5 2020-11-24 stsp if (view == NULL) {
7563 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7564 6458efa5 2020-11-24 stsp goto done;
7565 6458efa5 2020-11-24 stsp }
7566 6458efa5 2020-11-24 stsp
7567 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7568 6458efa5 2020-11-24 stsp if (error)
7569 6458efa5 2020-11-24 stsp goto done;
7570 6458efa5 2020-11-24 stsp
7571 6458efa5 2020-11-24 stsp if (worktree) {
7572 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7573 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7574 6458efa5 2020-11-24 stsp worktree = NULL;
7575 6458efa5 2020-11-24 stsp }
7576 6458efa5 2020-11-24 stsp error = view_loop(view);
7577 6458efa5 2020-11-24 stsp done:
7578 6458efa5 2020-11-24 stsp free(repo_path);
7579 6458efa5 2020-11-24 stsp free(cwd);
7580 1d0f4054 2021-06-17 stsp if (repo) {
7581 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7582 1d0f4054 2021-06-17 stsp if (close_err)
7583 1d0f4054 2021-06-17 stsp error = close_err;
7584 1d0f4054 2021-06-17 stsp }
7585 0ae84acc 2022-06-15 tracey if (pack_fds) {
7586 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7587 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7588 0ae84acc 2022-06-15 tracey if (error == NULL)
7589 0ae84acc 2022-06-15 tracey error = pack_err;
7590 0ae84acc 2022-06-15 tracey }
7591 51a10b52 2020-12-26 stsp tog_free_refs();
7592 6458efa5 2020-11-24 stsp return error;
7593 6458efa5 2020-11-24 stsp }
7594 6458efa5 2020-11-24 stsp
7595 9b058f45 2022-06-30 mark /*
7596 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7597 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7598 9b058f45 2022-06-30 mark */
7599 6458efa5 2020-11-24 stsp static void
7600 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7601 9b058f45 2022-06-30 mark {
7602 9b058f45 2022-06-30 mark switch (view->type) {
7603 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7604 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7605 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7606 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7607 9b058f45 2022-06-30 mark 1);
7608 9b058f45 2022-06-30 mark break;
7609 9b058f45 2022-06-30 mark }
7610 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7611 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7612 9b058f45 2022-06-30 mark else
7613 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7614 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7615 9b058f45 2022-06-30 mark break;
7616 9b058f45 2022-06-30 mark }
7617 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7618 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7619 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7620 9b058f45 2022-06-30 mark break;
7621 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7622 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7623 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7624 9b058f45 2022-06-30 mark break;
7625 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7626 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7627 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7628 9b058f45 2022-06-30 mark break;
7629 9b058f45 2022-06-30 mark default:
7630 9b058f45 2022-06-30 mark break;
7631 9b058f45 2022-06-30 mark }
7632 9b058f45 2022-06-30 mark
7633 9b058f45 2022-06-30 mark view->offset = 0;
7634 9b058f45 2022-06-30 mark }
7635 9b058f45 2022-06-30 mark
7636 9b058f45 2022-06-30 mark /*
7637 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7638 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7639 9b058f45 2022-06-30 mark */
7640 9b058f45 2022-06-30 mark static const struct got_error *
7641 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7642 9b058f45 2022-06-30 mark {
7643 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7644 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7645 9b058f45 2022-06-30 mark int *selected = NULL;
7646 9b058f45 2022-06-30 mark int header, offset;
7647 9b058f45 2022-06-30 mark
7648 9b058f45 2022-06-30 mark switch (view->type) {
7649 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7650 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7651 9b058f45 2022-06-30 mark header = 3;
7652 9b058f45 2022-06-30 mark scrolld = NULL;
7653 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7654 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7655 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7656 9b058f45 2022-06-30 mark s->selected_line -= offset;
7657 9b058f45 2022-06-30 mark view->offset = offset;
7658 9b058f45 2022-06-30 mark }
7659 9b058f45 2022-06-30 mark break;
7660 9b058f45 2022-06-30 mark }
7661 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7662 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7663 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7664 9b058f45 2022-06-30 mark header = 3;
7665 9b058f45 2022-06-30 mark selected = &s->selected;
7666 9b058f45 2022-06-30 mark break;
7667 9b058f45 2022-06-30 mark }
7668 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7669 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7670 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7671 9b058f45 2022-06-30 mark header = 3;
7672 9b058f45 2022-06-30 mark selected = &s->selected;
7673 9b058f45 2022-06-30 mark break;
7674 9b058f45 2022-06-30 mark }
7675 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7676 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7677 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7678 9b058f45 2022-06-30 mark header = 5;
7679 9b058f45 2022-06-30 mark selected = &s->selected;
7680 9b058f45 2022-06-30 mark break;
7681 9b058f45 2022-06-30 mark }
7682 9b058f45 2022-06-30 mark default:
7683 9b058f45 2022-06-30 mark selected = NULL;
7684 9b058f45 2022-06-30 mark scrolld = NULL;
7685 9b058f45 2022-06-30 mark header = 0;
7686 9b058f45 2022-06-30 mark break;
7687 9b058f45 2022-06-30 mark }
7688 9b058f45 2022-06-30 mark
7689 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7690 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7691 9b058f45 2022-06-30 mark view->offset = offset;
7692 9b058f45 2022-06-30 mark if (scrolld && offset) {
7693 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7694 9b058f45 2022-06-30 mark *selected -= offset;
7695 9b058f45 2022-06-30 mark }
7696 9b058f45 2022-06-30 mark }
7697 9b058f45 2022-06-30 mark
7698 9b058f45 2022-06-30 mark return err;
7699 9b058f45 2022-06-30 mark }
7700 9b058f45 2022-06-30 mark
7701 9b058f45 2022-06-30 mark static void
7702 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7703 ce5b7c56 2019-07-09 stsp {
7704 6059809a 2020-12-17 stsp size_t i;
7705 9f7d7167 2018-04-29 stsp
7706 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7707 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7708 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
7709 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7710 ce5b7c56 2019-07-09 stsp }
7711 6879ba42 2020-10-01 naddy fputc('\n', fp);
7712 ce5b7c56 2019-07-09 stsp }
7713 ce5b7c56 2019-07-09 stsp
7714 4ed7e80c 2018-05-20 stsp __dead static void
7715 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7716 9f7d7167 2018-04-29 stsp {
7717 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7718 6879ba42 2020-10-01 naddy
7719 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7720 6879ba42 2020-10-01 naddy getprogname());
7721 6879ba42 2020-10-01 naddy if (hflag) {
7722 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7723 6879ba42 2020-10-01 naddy list_commands(fp);
7724 ee85c5e8 2020-02-29 stsp }
7725 6879ba42 2020-10-01 naddy exit(status);
7726 9f7d7167 2018-04-29 stsp }
7727 9f7d7167 2018-04-29 stsp
7728 c2301be8 2018-04-30 stsp static char **
7729 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7730 c2301be8 2018-04-30 stsp {
7731 ee85c5e8 2020-02-29 stsp va_list ap;
7732 c2301be8 2018-04-30 stsp char **argv;
7733 ee85c5e8 2020-02-29 stsp int i;
7734 c2301be8 2018-04-30 stsp
7735 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7736 ee85c5e8 2020-02-29 stsp
7737 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7738 c2301be8 2018-04-30 stsp if (argv == NULL)
7739 c2301be8 2018-04-30 stsp err(1, "calloc");
7740 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7741 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7742 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7743 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7744 c2301be8 2018-04-30 stsp }
7745 c2301be8 2018-04-30 stsp
7746 ee85c5e8 2020-02-29 stsp va_end(ap);
7747 c2301be8 2018-04-30 stsp return argv;
7748 ee85c5e8 2020-02-29 stsp }
7749 ee85c5e8 2020-02-29 stsp
7750 ee85c5e8 2020-02-29 stsp /*
7751 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7752 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7753 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7754 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7755 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7756 ee85c5e8 2020-02-29 stsp */
7757 ee85c5e8 2020-02-29 stsp static const struct got_error *
7758 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7759 ee85c5e8 2020-02-29 stsp {
7760 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7761 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7762 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7763 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7764 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7765 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
7766 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7767 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7768 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7769 84de9106 2020-12-26 stsp
7770 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7771 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7772 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7773 ee85c5e8 2020-02-29 stsp
7774 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7775 0ae84acc 2022-06-15 tracey if (error != NULL)
7776 0ae84acc 2022-06-15 tracey goto done;
7777 0ae84acc 2022-06-15 tracey
7778 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7779 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7780 ee85c5e8 2020-02-29 stsp goto done;
7781 ee85c5e8 2020-02-29 stsp
7782 ee85c5e8 2020-02-29 stsp if (worktree)
7783 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7784 ee85c5e8 2020-02-29 stsp else
7785 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7786 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7787 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7788 ee85c5e8 2020-02-29 stsp goto done;
7789 ee85c5e8 2020-02-29 stsp }
7790 ee85c5e8 2020-02-29 stsp
7791 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7792 ee85c5e8 2020-02-29 stsp if (error != NULL)
7793 ee85c5e8 2020-02-29 stsp goto done;
7794 ee85c5e8 2020-02-29 stsp
7795 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7796 ee85c5e8 2020-02-29 stsp repo, worktree);
7797 ee85c5e8 2020-02-29 stsp if (error)
7798 ee85c5e8 2020-02-29 stsp goto done;
7799 ee85c5e8 2020-02-29 stsp
7800 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7801 84de9106 2020-12-26 stsp if (error)
7802 84de9106 2020-12-26 stsp goto done;
7803 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7804 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7805 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7806 ee85c5e8 2020-02-29 stsp if (error)
7807 ee85c5e8 2020-02-29 stsp goto done;
7808 ee85c5e8 2020-02-29 stsp
7809 ee85c5e8 2020-02-29 stsp if (worktree) {
7810 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7811 ee85c5e8 2020-02-29 stsp worktree = NULL;
7812 ee85c5e8 2020-02-29 stsp }
7813 ee85c5e8 2020-02-29 stsp
7814 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7815 a44927cc 2022-04-07 stsp if (error)
7816 a44927cc 2022-04-07 stsp goto done;
7817 a44927cc 2022-04-07 stsp
7818 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7819 ee85c5e8 2020-02-29 stsp if (error) {
7820 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7821 ee85c5e8 2020-02-29 stsp goto done;
7822 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7823 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7824 6879ba42 2020-10-01 naddy usage(1, 1);
7825 ee85c5e8 2020-02-29 stsp /* not reached */
7826 ee85c5e8 2020-02-29 stsp }
7827 ee85c5e8 2020-02-29 stsp
7828 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7829 1d0f4054 2021-06-17 stsp if (error == NULL)
7830 1d0f4054 2021-06-17 stsp error = close_err;
7831 ee85c5e8 2020-02-29 stsp repo = NULL;
7832 ee85c5e8 2020-02-29 stsp
7833 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7834 ee85c5e8 2020-02-29 stsp if (error)
7835 ee85c5e8 2020-02-29 stsp goto done;
7836 ee85c5e8 2020-02-29 stsp
7837 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7838 ee85c5e8 2020-02-29 stsp argc = 4;
7839 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7840 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7841 ee85c5e8 2020-02-29 stsp done:
7842 1d0f4054 2021-06-17 stsp if (repo) {
7843 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7844 1d0f4054 2021-06-17 stsp if (error == NULL)
7845 1d0f4054 2021-06-17 stsp error = close_err;
7846 1d0f4054 2021-06-17 stsp }
7847 a44927cc 2022-04-07 stsp if (commit)
7848 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
7849 ee85c5e8 2020-02-29 stsp if (worktree)
7850 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7851 0ae84acc 2022-06-15 tracey if (pack_fds) {
7852 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7853 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7854 0ae84acc 2022-06-15 tracey if (error == NULL)
7855 0ae84acc 2022-06-15 tracey error = pack_err;
7856 0ae84acc 2022-06-15 tracey }
7857 ee85c5e8 2020-02-29 stsp free(id);
7858 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7859 ee85c5e8 2020-02-29 stsp free(commit_id);
7860 ee85c5e8 2020-02-29 stsp free(cwd);
7861 ee85c5e8 2020-02-29 stsp free(repo_path);
7862 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7863 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7864 ee85c5e8 2020-02-29 stsp int i;
7865 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7866 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7867 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7868 ee85c5e8 2020-02-29 stsp }
7869 87670572 2020-12-26 naddy tog_free_refs();
7870 ee85c5e8 2020-02-29 stsp return error;
7871 c2301be8 2018-04-30 stsp }
7872 c2301be8 2018-04-30 stsp
7873 9f7d7167 2018-04-29 stsp int
7874 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7875 9f7d7167 2018-04-29 stsp {
7876 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7877 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
7878 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7879 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7880 3e166534 2022-02-16 naddy static const struct option longopts[] = {
7881 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7882 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7883 83cd27f8 2020-01-13 stsp };
7884 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
7885 9f7d7167 2018-04-29 stsp
7886 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7887 9f7d7167 2018-04-29 stsp
7888 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7889 9f7d7167 2018-04-29 stsp switch (ch) {
7890 9f7d7167 2018-04-29 stsp case 'h':
7891 9f7d7167 2018-04-29 stsp hflag = 1;
7892 9f7d7167 2018-04-29 stsp break;
7893 53ccebc2 2019-07-30 stsp case 'V':
7894 53ccebc2 2019-07-30 stsp Vflag = 1;
7895 53ccebc2 2019-07-30 stsp break;
7896 9f7d7167 2018-04-29 stsp default:
7897 6879ba42 2020-10-01 naddy usage(hflag, 1);
7898 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7899 9f7d7167 2018-04-29 stsp }
7900 9f7d7167 2018-04-29 stsp }
7901 9f7d7167 2018-04-29 stsp
7902 9f7d7167 2018-04-29 stsp argc -= optind;
7903 9f7d7167 2018-04-29 stsp argv += optind;
7904 9814e6a3 2020-09-27 naddy optind = 1;
7905 c2301be8 2018-04-30 stsp optreset = 1;
7906 9f7d7167 2018-04-29 stsp
7907 53ccebc2 2019-07-30 stsp if (Vflag) {
7908 53ccebc2 2019-07-30 stsp got_version_print_str();
7909 6879ba42 2020-10-01 naddy return 0;
7910 53ccebc2 2019-07-30 stsp }
7911 4010e238 2020-12-04 stsp
7912 4010e238 2020-12-04 stsp #ifndef PROFILE
7913 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7914 4010e238 2020-12-04 stsp NULL) == -1)
7915 4010e238 2020-12-04 stsp err(1, "pledge");
7916 4010e238 2020-12-04 stsp #endif
7917 53ccebc2 2019-07-30 stsp
7918 c2301be8 2018-04-30 stsp if (argc == 0) {
7919 f29d3e89 2018-06-23 stsp if (hflag)
7920 6879ba42 2020-10-01 naddy usage(hflag, 0);
7921 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7922 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7923 c2301be8 2018-04-30 stsp argc = 1;
7924 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7925 c2301be8 2018-04-30 stsp } else {
7926 6059809a 2020-12-17 stsp size_t i;
7927 9f7d7167 2018-04-29 stsp
7928 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7929 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7930 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7931 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7932 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7933 9f7d7167 2018-04-29 stsp break;
7934 9f7d7167 2018-04-29 stsp }
7935 9f7d7167 2018-04-29 stsp }
7936 ee85c5e8 2020-02-29 stsp }
7937 3642c4c6 2019-07-09 stsp
7938 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7939 917d79a7 2022-07-01 stsp if (diff_algo_str) {
7940 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
7941 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7942 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
7943 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
7944 917d79a7 2022-07-01 stsp }
7945 917d79a7 2022-07-01 stsp
7946 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
7947 ee85c5e8 2020-02-29 stsp if (argc != 1)
7948 6879ba42 2020-10-01 naddy usage(0, 1);
7949 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
7950 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
7951 ee85c5e8 2020-02-29 stsp } else {
7952 ee85c5e8 2020-02-29 stsp if (hflag)
7953 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
7954 ee85c5e8 2020-02-29 stsp else
7955 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7956 9f7d7167 2018-04-29 stsp }
7957 9f7d7167 2018-04-29 stsp
7958 9f7d7167 2018-04-29 stsp endwin();
7959 b46c1e04 2020-09-20 naddy putchar('\n');
7960 a2f4a359 2020-02-28 stsp if (cmd_argv) {
7961 a2f4a359 2020-02-28 stsp int i;
7962 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
7963 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
7964 a2f4a359 2020-02-28 stsp free(cmd_argv);
7965 a2f4a359 2020-02-28 stsp }
7966 a2f4a359 2020-02-28 stsp
7967 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
7968 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7969 9f7d7167 2018-04-29 stsp return 0;
7970 9f7d7167 2018-04-29 stsp }