Blame


1 9f7d7167 2018-04-29 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
7 9f7d7167 2018-04-29 stsp *
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 80ddbec8 2018-04-29 stsp
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 6062e8ea 2021-09-21 stsp #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 d7b5a0e8 2022-04-20 stsp #include <sha1.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
43 9f7d7167 2018-04-29 stsp
44 53ccebc2 2019-07-30 stsp #include "got_version.h"
45 9f7d7167 2018-04-29 stsp #include "got_error.h"
46 80ddbec8 2018-04-29 stsp #include "got_object.h"
47 80ddbec8 2018-04-29 stsp #include "got_reference.h"
48 80ddbec8 2018-04-29 stsp #include "got_repository.h"
49 80ddbec8 2018-04-29 stsp #include "got_diff.h"
50 511a516b 2018-05-19 stsp #include "got_opentemp.h"
51 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
52 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
53 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
54 a70480e0 2018-06-23 stsp #include "got_blame.h"
55 c2db6724 2019-01-04 stsp #include "got_privsep.h"
56 1dd54920 2019-05-11 stsp #include "got_path.h"
57 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 9f7d7167 2018-04-29 stsp
59 881b2d3e 2018-04-30 stsp #ifndef MIN
60 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 881b2d3e 2018-04-30 stsp #endif
62 881b2d3e 2018-04-30 stsp
63 2bd27830 2018-10-22 stsp #ifndef MAX
64 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 2bd27830 2018-10-22 stsp #endif
66 2bd27830 2018-10-22 stsp
67 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 2bd27830 2018-10-22 stsp
69 9f7d7167 2018-04-29 stsp #ifndef nitems
70 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp #endif
72 9f7d7167 2018-04-29 stsp
73 9f7d7167 2018-04-29 stsp struct tog_cmd {
74 c2301be8 2018-04-30 stsp const char *name;
75 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
76 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 9f7d7167 2018-04-29 stsp };
78 9f7d7167 2018-04-29 stsp
79 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
83 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
84 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 9f7d7167 2018-04-29 stsp
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
89 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
90 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 9f7d7167 2018-04-29 stsp
92 3e166534 2022-02-16 naddy static const struct tog_cmd tog_commands[] = {
93 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
94 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
95 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
96 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
97 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
98 9f7d7167 2018-04-29 stsp };
99 9f7d7167 2018-04-29 stsp
100 d6b05b5b 2018-08-04 stsp enum tog_view_type {
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
102 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
103 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
104 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
105 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
106 d6b05b5b 2018-08-04 stsp };
107 c3e9aa98 2019-05-13 jcs
108 9b058f45 2022-06-30 mark enum tog_view_mode {
109 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_NONE,
110 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_VERT,
111 9b058f45 2022-06-30 mark TOG_VIEW_SPLIT_HRZN
112 9b058f45 2022-06-30 mark };
113 9b058f45 2022-06-30 mark
114 9b058f45 2022-06-30 mark #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
115 9b058f45 2022-06-30 mark
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 d6b05b5b 2018-08-04 stsp
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
122 1a76625f 2018-10-22 stsp int idx;
123 ba4f502b 2018-08-04 stsp };
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
128 6d17833f 2019-11-08 stsp };
129 6d17833f 2019-11-08 stsp
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
134 15a087fe 2019-02-21 stsp };
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 11b20872 2019-11-08 stsp
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 917d79a7 2022-07-01 stsp static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
140 cc488aa7 2022-01-23 stsp
141 cc488aa7 2022-01-23 stsp static const struct got_error *
142 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 cc488aa7 2022-01-23 stsp struct got_reference* re2)
144 cc488aa7 2022-01-23 stsp {
145 cc488aa7 2022-01-23 stsp const char *name1 = got_ref_get_name(re1);
146 cc488aa7 2022-01-23 stsp const char *name2 = got_ref_get_name(re2);
147 cc488aa7 2022-01-23 stsp int isbackup1, isbackup2;
148 cc488aa7 2022-01-23 stsp
149 cc488aa7 2022-01-23 stsp /* Sort backup refs towards the bottom of the list. */
150 cc488aa7 2022-01-23 stsp isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 cc488aa7 2022-01-23 stsp isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 cc488aa7 2022-01-23 stsp if (!isbackup1 && isbackup2) {
153 cc488aa7 2022-01-23 stsp *cmp = -1;
154 cc488aa7 2022-01-23 stsp return NULL;
155 cc488aa7 2022-01-23 stsp } else if (isbackup1 && !isbackup2) {
156 cc488aa7 2022-01-23 stsp *cmp = 1;
157 cc488aa7 2022-01-23 stsp return NULL;
158 cc488aa7 2022-01-23 stsp }
159 cc488aa7 2022-01-23 stsp
160 cc488aa7 2022-01-23 stsp *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 cc488aa7 2022-01-23 stsp return NULL;
162 cc488aa7 2022-01-23 stsp }
163 51a10b52 2020-12-26 stsp
164 11b20872 2019-11-08 stsp static const struct got_error *
165 7f66531d 2021-11-16 stsp tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp {
167 51a10b52 2020-12-26 stsp const struct got_error *err;
168 51a10b52 2020-12-26 stsp
169 7f66531d 2021-11-16 stsp err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 cc488aa7 2022-01-23 stsp got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 7f66531d 2021-11-16 stsp repo);
172 51a10b52 2020-12-26 stsp if (err)
173 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp
175 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 51a10b52 2020-12-26 stsp repo);
177 51a10b52 2020-12-26 stsp }
178 51a10b52 2020-12-26 stsp
179 51a10b52 2020-12-26 stsp static void
180 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp {
182 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
183 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
184 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp }
186 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp }
188 51a10b52 2020-12-26 stsp
189 51a10b52 2020-12-26 stsp static const struct got_error *
190 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
191 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp {
193 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
194 11b20872 2019-11-08 stsp struct tog_color *tc;
195 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp
197 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
198 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp
200 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp
202 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
203 11b20872 2019-11-08 stsp if (tc == NULL)
204 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
205 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
206 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 11b20872 2019-11-08 stsp if (regerr) {
208 11b20872 2019-11-08 stsp static char regerr_msg[512];
209 11b20872 2019-11-08 stsp static char err_msg[512];
210 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
211 11b20872 2019-11-08 stsp sizeof(regerr_msg));
212 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 11b20872 2019-11-08 stsp regerr_msg);
214 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp free(tc);
216 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp }
218 11b20872 2019-11-08 stsp tc->colorpair = idx;
219 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
220 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp }
222 11b20872 2019-11-08 stsp
223 11b20872 2019-11-08 stsp static void
224 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp {
226 11b20872 2019-11-08 stsp struct tog_color *tc;
227 11b20872 2019-11-08 stsp
228 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
229 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
230 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
231 11b20872 2019-11-08 stsp regfree(&tc->regex);
232 11b20872 2019-11-08 stsp free(tc);
233 11b20872 2019-11-08 stsp }
234 11b20872 2019-11-08 stsp }
235 11b20872 2019-11-08 stsp
236 336075a4 2022-06-25 op static struct tog_color *
237 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp {
239 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 11b20872 2019-11-08 stsp
241 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
242 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
243 11b20872 2019-11-08 stsp return tc;
244 11b20872 2019-11-08 stsp }
245 11b20872 2019-11-08 stsp
246 11b20872 2019-11-08 stsp return NULL;
247 11b20872 2019-11-08 stsp }
248 11b20872 2019-11-08 stsp
249 11b20872 2019-11-08 stsp static int
250 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp {
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 11b20872 2019-11-08 stsp return COLOR_CYAN;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 11b20872 2019-11-08 stsp return COLOR_YELLOW;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 11b20872 2019-11-08 stsp return COLOR_GREEN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 91b8c405 2020-01-25 stsp return COLOR_CYAN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 11b20872 2019-11-08 stsp return COLOR_GREEN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 11b20872 2019-11-08 stsp return COLOR_CYAN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 11b20872 2019-11-08 stsp return COLOR_YELLOW;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_GREEN;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
280 cc488aa7 2022-01-23 stsp if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 cc488aa7 2022-01-23 stsp return COLOR_CYAN;
282 11b20872 2019-11-08 stsp
283 11b20872 2019-11-08 stsp return -1;
284 11b20872 2019-11-08 stsp }
285 11b20872 2019-11-08 stsp
286 11b20872 2019-11-08 stsp static int
287 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp {
289 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp
291 11b20872 2019-11-08 stsp if (val == NULL)
292 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 15a087fe 2019-02-21 stsp
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
295 11b20872 2019-11-08 stsp return COLOR_BLACK;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
297 11b20872 2019-11-08 stsp return COLOR_RED;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
299 11b20872 2019-11-08 stsp return COLOR_GREEN;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
301 11b20872 2019-11-08 stsp return COLOR_YELLOW;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
303 11b20872 2019-11-08 stsp return COLOR_BLUE;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
305 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
307 11b20872 2019-11-08 stsp return COLOR_CYAN;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
309 11b20872 2019-11-08 stsp return COLOR_WHITE;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return -1;
312 11b20872 2019-11-08 stsp
313 11b20872 2019-11-08 stsp return default_color_value(envvar);
314 11b20872 2019-11-08 stsp }
315 11b20872 2019-11-08 stsp
316 11b20872 2019-11-08 stsp
317 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
318 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
319 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 b72706c3 2022-06-01 stsp FILE *f, *f1, *f2;
321 f9d37699 2022-06-28 stsp int fd1, fd2;
322 15a087fe 2019-02-21 stsp int first_displayed_line;
323 15a087fe 2019-02-21 stsp int last_displayed_line;
324 15a087fe 2019-02-21 stsp int eof;
325 15a087fe 2019-02-21 stsp int diff_context;
326 3dbaef42 2020-11-24 stsp int ignore_whitespace;
327 64453f7e 2020-11-21 stsp int force_text_diff;
328 15a087fe 2019-02-21 stsp struct got_repository *repo;
329 bddb1296 2019-11-08 stsp struct tog_colors colors;
330 fe621944 2020-11-10 stsp size_t nlines;
331 f44b1f58 2020-02-02 tracey off_t *line_offsets;
332 f44b1f58 2020-02-02 tracey int matched_line;
333 f44b1f58 2020-02-02 tracey int selected_line;
334 15a087fe 2019-02-21 stsp
335 c0f61fa4 2022-07-11 mark /* passed from log or blame view; may be NULL */
336 c0f61fa4 2022-07-11 mark struct tog_view *parent_view;
337 b01e7d3b 2018-08-04 stsp };
338 b01e7d3b 2018-08-04 stsp
339 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 5629093a 2022-07-11 stsp static volatile sig_atomic_t tog_thread_error;
341 1a76625f 2018-10-22 stsp
342 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
343 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
344 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
345 1a76625f 2018-10-22 stsp int commits_needed;
346 fb280deb 2021-08-30 stsp int load_all;
347 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
348 1a76625f 2018-10-22 stsp struct commit_queue *commits;
349 1a76625f 2018-10-22 stsp const char *in_repo_path;
350 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
351 1a76625f 2018-10-22 stsp struct got_repository *repo;
352 74467cc8 2022-06-15 stsp int *pack_fds;
353 1a76625f 2018-10-22 stsp int log_complete;
354 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
355 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
356 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
357 13add988 2019-10-15 stsp int *searching;
358 13add988 2019-10-15 stsp int *search_next_done;
359 13add988 2019-10-15 stsp regex_t *regex;
360 1a76625f 2018-10-22 stsp };
361 1a76625f 2018-10-22 stsp
362 1a76625f 2018-10-22 stsp struct tog_log_view_state {
363 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
364 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
365 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
366 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
367 b01e7d3b 2018-08-04 stsp int selected;
368 b01e7d3b 2018-08-04 stsp char *in_repo_path;
369 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
370 b672a97a 2020-01-27 stsp int log_branches;
371 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
372 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
373 1a76625f 2018-10-22 stsp sig_atomic_t quit;
374 1a76625f 2018-10-22 stsp pthread_t thread;
375 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
376 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
377 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
378 11b20872 2019-11-08 stsp struct tog_colors colors;
379 10aab77f 2022-07-19 op int use_committer;
380 ba4f502b 2018-08-04 stsp };
381 11b20872 2019-11-08 stsp
382 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
383 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
384 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
386 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
387 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
388 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
390 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
391 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
392 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
393 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
394 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
395 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
396 cc488aa7 2022-01-23 stsp #define TOG_COLOR_REFS_BACKUP 15
397 ba4f502b 2018-08-04 stsp
398 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
399 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
400 e9424729 2018-08-04 stsp int nlines;
401 e9424729 2018-08-04 stsp
402 e9424729 2018-08-04 stsp struct tog_view *view;
403 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
404 e9424729 2018-08-04 stsp int *quit;
405 e9424729 2018-08-04 stsp };
406 e9424729 2018-08-04 stsp
407 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
408 e9424729 2018-08-04 stsp const char *path;
409 e9424729 2018-08-04 stsp struct got_repository *repo;
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
411 e9424729 2018-08-04 stsp int *complete;
412 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
413 fc06ba56 2019-08-22 stsp void *cancel_arg;
414 e9424729 2018-08-04 stsp };
415 e9424729 2018-08-04 stsp
416 e9424729 2018-08-04 stsp struct tog_blame {
417 e9424729 2018-08-04 stsp FILE *f;
418 be659d10 2020-11-18 stsp off_t filesize;
419 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
420 6fcac457 2018-11-19 stsp int nlines;
421 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
422 e9424729 2018-08-04 stsp pthread_t thread;
423 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
424 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
425 e9424729 2018-08-04 stsp const char *path;
426 0ae84acc 2022-06-15 tracey int *pack_fds;
427 e9424729 2018-08-04 stsp };
428 e9424729 2018-08-04 stsp
429 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
430 7cbe629d 2018-08-04 stsp int first_displayed_line;
431 7cbe629d 2018-08-04 stsp int last_displayed_line;
432 7cbe629d 2018-08-04 stsp int selected_line;
433 c0f61fa4 2022-07-11 mark int last_diffed_line;
434 7cbe629d 2018-08-04 stsp int blame_complete;
435 e5a0f69f 2018-08-18 stsp int eof;
436 e5a0f69f 2018-08-18 stsp int done;
437 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
438 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
439 e5a0f69f 2018-08-18 stsp char *path;
440 7cbe629d 2018-08-04 stsp struct got_repository *repo;
441 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
442 136e2bd4 2022-07-23 mark struct got_object_id *id_to_log;
443 e9424729 2018-08-04 stsp struct tog_blame blame;
444 6c4c42e0 2019-06-24 stsp int matched_line;
445 11b20872 2019-11-08 stsp struct tog_colors colors;
446 ad80ab7b 2018-08-04 stsp };
447 ad80ab7b 2018-08-04 stsp
448 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
449 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
450 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
451 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
452 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
453 ad80ab7b 2018-08-04 stsp int selected;
454 ad80ab7b 2018-08-04 stsp };
455 ad80ab7b 2018-08-04 stsp
456 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 ad80ab7b 2018-08-04 stsp
458 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
459 ad80ab7b 2018-08-04 stsp char *tree_label;
460 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
461 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
462 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
465 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
466 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
467 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
469 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
470 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
471 6458efa5 2020-11-24 stsp struct tog_colors colors;
472 6458efa5 2020-11-24 stsp };
473 6458efa5 2020-11-24 stsp
474 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
475 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
476 6458efa5 2020-11-24 stsp struct got_reference *ref;
477 6458efa5 2020-11-24 stsp int idx;
478 6458efa5 2020-11-24 stsp };
479 6458efa5 2020-11-24 stsp
480 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 6458efa5 2020-11-24 stsp
482 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
483 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
484 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
485 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
487 b4996bee 2022-06-16 stsp int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
488 6458efa5 2020-11-24 stsp struct got_repository *repo;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
490 bddb1296 2019-11-08 stsp struct tog_colors colors;
491 7cbe629d 2018-08-04 stsp };
492 7cbe629d 2018-08-04 stsp
493 669b5ffa 2018-10-07 stsp /*
494 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
495 669b5ffa 2018-10-07 stsp *
496 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
497 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
498 669b5ffa 2018-10-07 stsp * there is enough screen estate.
499 669b5ffa 2018-10-07 stsp *
500 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
501 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
502 669b5ffa 2018-10-07 stsp *
503 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
504 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
505 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
506 669b5ffa 2018-10-07 stsp *
507 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
508 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
509 669b5ffa 2018-10-07 stsp */
510 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
511 669b5ffa 2018-10-07 stsp
512 cc3c9aac 2018-08-01 stsp struct tog_view {
513 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
514 26ed57b2 2018-05-19 stsp WINDOW *window;
515 26ed57b2 2018-05-19 stsp PANEL *panel;
516 9b058f45 2022-06-30 mark int nlines, ncols, begin_y, begin_x; /* based on split height/width */
517 3c1dfe12 2022-07-08 mark int resized_y, resized_x; /* begin_y/x based on user resizing */
518 145b6838 2022-06-16 stsp int maxx, x; /* max column and current start column */
519 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
520 9b058f45 2022-06-30 mark int nscrolled, offset; /* lines scrolled and hsplit line offset */
521 640cd7ff 2022-06-22 mark int ch, count; /* current keymap and count prefix */
522 571ccd73 2022-07-19 mark int resized; /* set when in a resize event */
523 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
524 9970f7fc 2020-12-03 stsp int dying;
525 669b5ffa 2018-10-07 stsp struct tog_view *parent;
526 669b5ffa 2018-10-07 stsp struct tog_view *child;
527 5dc9f4bc 2018-08-04 stsp
528 e78dc838 2020-12-04 stsp /*
529 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
530 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
531 e78dc838 2020-12-04 stsp * between parent and child.
532 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
533 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
534 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
535 e78dc838 2020-12-04 stsp * situations.
536 e78dc838 2020-12-04 stsp */
537 e78dc838 2020-12-04 stsp int focus_child;
538 e78dc838 2020-12-04 stsp
539 9b058f45 2022-06-30 mark enum tog_view_mode mode;
540 5dc9f4bc 2018-08-04 stsp /* type-specific state */
541 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
542 5dc9f4bc 2018-08-04 stsp union {
543 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
544 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
545 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
546 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
547 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
548 5dc9f4bc 2018-08-04 stsp } state;
549 e5a0f69f 2018-08-18 stsp
550 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
552 e78dc838 2020-12-04 stsp struct tog_view *, int);
553 917d79a7 2022-07-01 stsp const struct got_error *(*reset)(struct tog_view *);
554 571ccd73 2022-07-19 mark const struct got_error *(*resize)(struct tog_view *, int);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
556 60493ae3 2019-06-20 stsp
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
559 c0c4acc8 2021-01-24 stsp int search_started;
560 60493ae3 2019-06-20 stsp int searching;
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
563 60493ae3 2019-06-20 stsp int search_next_done;
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
566 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
567 1803e47f 2019-06-22 stsp regex_t regex;
568 41605754 2020-11-12 stsp regmatch_t regmatch;
569 cc3c9aac 2018-08-01 stsp };
570 cd0acaa7 2018-05-20 stsp
571 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
572 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
573 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_repository *);
575 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
577 e78dc838 2020-12-04 stsp struct tog_view *, int);
578 917d79a7 2022-07-01 stsp static const struct got_error *reset_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
582 e5a0f69f 2018-08-18 stsp
583 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
585 78756c87 2020-11-24 stsp const char *, const char *, int);
586 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 571ccd73 2022-07-19 mark static const struct got_error *resize_log_view(struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp
594 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
595 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
596 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 917d79a7 2022-07-01 stsp static const struct got_error *reset_blame_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 e5a0f69f 2018-08-18 stsp
604 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
605 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
606 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
608 e78dc838 2020-12-04 stsp struct tog_view *, int);
609 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp
613 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
614 6458efa5 2020-11-24 stsp struct got_repository *);
615 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
617 e78dc838 2020-12-04 stsp struct tog_view *, int);
618 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp
622 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
623 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
624 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
625 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigint_received;
626 2497f032 2022-05-31 stsp static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp
628 25791caa 2018-10-24 stsp static void
629 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp {
631 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
632 83baff54 2019-08-12 stsp }
633 83baff54 2019-08-12 stsp
634 83baff54 2019-08-12 stsp static void
635 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp {
637 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
638 25791caa 2018-10-24 stsp }
639 26ed57b2 2018-05-19 stsp
640 61266923 2020-01-14 stsp static void
641 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp {
643 61266923 2020-01-14 stsp tog_sigcont_received = 1;
644 61266923 2020-01-14 stsp }
645 61266923 2020-01-14 stsp
646 2497f032 2022-05-31 stsp static void
647 2497f032 2022-05-31 stsp tog_sigint(int signo)
648 2497f032 2022-05-31 stsp {
649 2497f032 2022-05-31 stsp tog_sigint_received = 1;
650 2497f032 2022-05-31 stsp }
651 2497f032 2022-05-31 stsp
652 2497f032 2022-05-31 stsp static void
653 2497f032 2022-05-31 stsp tog_sigterm(int signo)
654 2497f032 2022-05-31 stsp {
655 2497f032 2022-05-31 stsp tog_sigterm_received = 1;
656 2497f032 2022-05-31 stsp }
657 2497f032 2022-05-31 stsp
658 2497f032 2022-05-31 stsp static int
659 dd6e31d7 2022-06-17 stsp tog_fatal_signal_received(void)
660 2497f032 2022-05-31 stsp {
661 2497f032 2022-05-31 stsp return (tog_sigpipe_received ||
662 2497f032 2022-05-31 stsp tog_sigint_received || tog_sigint_received);
663 2497f032 2022-05-31 stsp }
664 2497f032 2022-05-31 stsp
665 e5a0f69f 2018-08-18 stsp static const struct got_error *
666 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 ea5e7bb5 2018-08-01 stsp {
668 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *child_err = NULL;
669 e5a0f69f 2018-08-18 stsp
670 669b5ffa 2018-10-07 stsp if (view->child) {
671 5629093a 2022-07-11 stsp child_err = view_close(view->child);
672 669b5ffa 2018-10-07 stsp view->child = NULL;
673 669b5ffa 2018-10-07 stsp }
674 e5a0f69f 2018-08-18 stsp if (view->close)
675 e5a0f69f 2018-08-18 stsp err = view->close(view);
676 ea5e7bb5 2018-08-01 stsp if (view->panel)
677 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
678 ea5e7bb5 2018-08-01 stsp if (view->window)
679 ea5e7bb5 2018-08-01 stsp delwin(view->window);
680 ea5e7bb5 2018-08-01 stsp free(view);
681 5629093a 2022-07-11 stsp return err ? err : child_err;
682 ea5e7bb5 2018-08-01 stsp }
683 ea5e7bb5 2018-08-01 stsp
684 ea5e7bb5 2018-08-01 stsp static struct tog_view *
685 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
686 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ea5e7bb5 2018-08-01 stsp {
688 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp
690 ea5e7bb5 2018-08-01 stsp if (view == NULL)
691 ea5e7bb5 2018-08-01 stsp return NULL;
692 ea5e7bb5 2018-08-01 stsp
693 d6b05b5b 2018-08-04 stsp view->type = type;
694 f7d12f7e 2018-08-01 stsp view->lines = LINES;
695 f7d12f7e 2018-08-01 stsp view->cols = COLS;
696 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
697 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
698 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
699 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
700 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
701 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
702 96a765a8 2018-08-04 stsp view_close(view);
703 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp }
705 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
706 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
707 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
708 96a765a8 2018-08-04 stsp view_close(view);
709 ea5e7bb5 2018-08-01 stsp return NULL;
710 ea5e7bb5 2018-08-01 stsp }
711 ea5e7bb5 2018-08-01 stsp
712 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
713 ea5e7bb5 2018-08-01 stsp return view;
714 cdf1ee82 2018-08-01 stsp }
715 cdf1ee82 2018-08-01 stsp
716 0cf4efb1 2018-09-29 stsp static int
717 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 0cf4efb1 2018-09-29 stsp {
719 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 0cf4efb1 2018-09-29 stsp return 0;
721 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
722 5c60c32a 2018-10-18 stsp }
723 5c60c32a 2018-10-18 stsp
724 9b058f45 2022-06-30 mark /* XXX Stub till we decide what to do. */
725 9b058f45 2022-06-30 mark static int
726 9b058f45 2022-06-30 mark view_split_begin_y(int lines)
727 9b058f45 2022-06-30 mark {
728 9b058f45 2022-06-30 mark return lines * HSPLIT_SCALE;
729 9b058f45 2022-06-30 mark }
730 9b058f45 2022-06-30 mark
731 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp
733 5c60c32a 2018-10-18 stsp static const struct got_error *
734 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp {
736 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 5c60c32a 2018-10-18 stsp
738 571ccd73 2022-07-19 mark if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 3c1dfe12 2022-07-08 mark if (view->resized_y && view->resized_y < view->lines)
740 3c1dfe12 2022-07-08 mark view->begin_y = view->resized_y;
741 3c1dfe12 2022-07-08 mark else
742 3c1dfe12 2022-07-08 mark view->begin_y = view_split_begin_y(view->nlines);
743 9b058f45 2022-06-30 mark view->begin_x = 0;
744 571ccd73 2022-07-19 mark } else if (!view->resized) {
745 3c1dfe12 2022-07-08 mark if (view->resized_x && view->resized_x < view->cols - 1 &&
746 3c1dfe12 2022-07-08 mark view->cols > 119)
747 3c1dfe12 2022-07-08 mark view->begin_x = view->resized_x;
748 3c1dfe12 2022-07-08 mark else
749 3c1dfe12 2022-07-08 mark view->begin_x = view_split_begin_x(0);
750 9b058f45 2022-06-30 mark view->begin_y = 0;
751 9b058f45 2022-06-30 mark }
752 9b058f45 2022-06-30 mark view->nlines = LINES - view->begin_y;
753 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
754 5c60c32a 2018-10-18 stsp view->lines = LINES;
755 5c60c32a 2018-10-18 stsp view->cols = COLS;
756 5c60c32a 2018-10-18 stsp err = view_resize(view);
757 5c60c32a 2018-10-18 stsp if (err)
758 5c60c32a 2018-10-18 stsp return err;
759 5c60c32a 2018-10-18 stsp
760 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 9b058f45 2022-06-30 mark view->parent->nlines = view->begin_y;
762 9b058f45 2022-06-30 mark
763 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
765 5c60c32a 2018-10-18 stsp
766 5c60c32a 2018-10-18 stsp return NULL;
767 5c60c32a 2018-10-18 stsp }
768 5c60c32a 2018-10-18 stsp
769 5c60c32a 2018-10-18 stsp static const struct got_error *
770 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
771 5c60c32a 2018-10-18 stsp {
772 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
773 5c60c32a 2018-10-18 stsp
774 5c60c32a 2018-10-18 stsp view->begin_x = 0;
775 571ccd73 2022-07-19 mark view->begin_y = view->resized ? view->begin_y : 0;
776 571ccd73 2022-07-19 mark view->nlines = view->resized ? view->nlines : LINES;
777 5c60c32a 2018-10-18 stsp view->ncols = COLS;
778 5c60c32a 2018-10-18 stsp view->lines = LINES;
779 5c60c32a 2018-10-18 stsp view->cols = COLS;
780 5c60c32a 2018-10-18 stsp err = view_resize(view);
781 5c60c32a 2018-10-18 stsp if (err)
782 5c60c32a 2018-10-18 stsp return err;
783 5c60c32a 2018-10-18 stsp
784 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
786 5c60c32a 2018-10-18 stsp
787 5c60c32a 2018-10-18 stsp return NULL;
788 0cf4efb1 2018-09-29 stsp }
789 0cf4efb1 2018-09-29 stsp
790 5c60c32a 2018-10-18 stsp static int
791 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
792 5c60c32a 2018-10-18 stsp {
793 5c60c32a 2018-10-18 stsp return view->parent == NULL;
794 5c60c32a 2018-10-18 stsp }
795 5c60c32a 2018-10-18 stsp
796 6131ff18 2022-06-20 mark static int
797 6131ff18 2022-06-20 mark view_is_splitscreen(struct tog_view *view)
798 6131ff18 2022-06-20 mark {
799 9b058f45 2022-06-30 mark return view->begin_x > 0 || view->begin_y > 0;
800 24b9cfdc 2022-06-30 stsp }
801 24b9cfdc 2022-06-30 stsp
802 24b9cfdc 2022-06-30 stsp static int
803 24b9cfdc 2022-06-30 stsp view_is_fullscreen(struct tog_view *view)
804 24b9cfdc 2022-06-30 stsp {
805 24b9cfdc 2022-06-30 stsp return view->nlines == LINES && view->ncols == COLS;
806 6131ff18 2022-06-20 mark }
807 6131ff18 2022-06-20 mark
808 49b24ee5 2022-07-03 mark static int
809 49b24ee5 2022-07-03 mark view_is_hsplit_top(struct tog_view *view)
810 49b24ee5 2022-07-03 mark {
811 49b24ee5 2022-07-03 mark return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 49b24ee5 2022-07-03 mark view_is_splitscreen(view->child);
813 49b24ee5 2022-07-03 mark }
814 49b24ee5 2022-07-03 mark
815 9b058f45 2022-06-30 mark static void
816 9b058f45 2022-06-30 mark view_border(struct tog_view *view)
817 9b058f45 2022-06-30 mark {
818 9b058f45 2022-06-30 mark PANEL *panel;
819 9b058f45 2022-06-30 mark const struct tog_view *view_above;
820 6131ff18 2022-06-20 mark
821 9b058f45 2022-06-30 mark if (view->parent)
822 9b058f45 2022-06-30 mark return view_border(view->parent);
823 9b058f45 2022-06-30 mark
824 9b058f45 2022-06-30 mark panel = panel_above(view->panel);
825 9b058f45 2022-06-30 mark if (panel == NULL)
826 9b058f45 2022-06-30 mark return;
827 9b058f45 2022-06-30 mark
828 9b058f45 2022-06-30 mark view_above = panel_userptr(panel);
829 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 9b058f45 2022-06-30 mark mvwhline(view->window, view_above->begin_y - 1,
831 9b058f45 2022-06-30 mark view->begin_x, got_locale_is_utf8() ?
832 9b058f45 2022-06-30 mark ACS_HLINE : '-', view->ncols);
833 9b058f45 2022-06-30 mark else
834 9b058f45 2022-06-30 mark mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 9b058f45 2022-06-30 mark got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
836 9b058f45 2022-06-30 mark }
837 9b058f45 2022-06-30 mark
838 3c1dfe12 2022-07-08 mark static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 9b058f45 2022-06-30 mark static const struct got_error *request_log_commits(struct tog_view *);
840 9b058f45 2022-06-30 mark static const struct got_error *offset_selection_down(struct tog_view *);
841 9b058f45 2022-06-30 mark static void offset_selection_up(struct tog_view *);
842 3c1dfe12 2022-07-08 mark static void view_get_split(struct tog_view *, int *, int *);
843 9b058f45 2022-06-30 mark
844 4d8c2215 2018-08-19 stsp static const struct got_error *
845 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
846 f7d12f7e 2018-08-01 stsp {
847 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
848 9b058f45 2022-06-30 mark int dif, nlines, ncols;
849 f7d12f7e 2018-08-01 stsp
850 9b058f45 2022-06-30 mark dif = LINES - view->lines; /* line difference */
851 9b058f45 2022-06-30 mark
852 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
853 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
854 0cf4efb1 2018-09-29 stsp else
855 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
856 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
857 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
858 0cf4efb1 2018-09-29 stsp else
859 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
860 6d0fee91 2018-08-01 stsp
861 4dd27a72 2022-06-29 stsp if (view->child) {
862 9b058f45 2022-06-30 mark int hs = view->child->begin_y;
863 9b058f45 2022-06-30 mark
864 24b9cfdc 2022-06-30 stsp if (!view_is_fullscreen(view))
865 c71ed39a 2022-06-29 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
866 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 9b058f45 2022-06-30 mark view->child->begin_x == 0) {
868 0dbbbe90 2022-06-17 op ncols = COLS;
869 0dbbbe90 2022-06-17 op
870 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
871 5c60c32a 2018-10-18 stsp if (view->child->focussed)
872 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
873 5c60c32a 2018-10-18 stsp else
874 5c60c32a 2018-10-18 stsp show_panel(view->panel);
875 5c60c32a 2018-10-18 stsp } else {
876 0dbbbe90 2022-06-17 op ncols = view->child->begin_x;
877 0dbbbe90 2022-06-17 op
878 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
879 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
880 9b058f45 2022-06-30 mark }
881 9b058f45 2022-06-30 mark /*
882 9b058f45 2022-06-30 mark * XXX This is ugly and needs to be moved into the above
883 9b058f45 2022-06-30 mark * logic but "works" for now and my attempts at moving it
884 9b058f45 2022-06-30 mark * break either 'tab' or 'F' key maps in horizontal splits.
885 9b058f45 2022-06-30 mark */
886 9b058f45 2022-06-30 mark if (hs) {
887 9b058f45 2022-06-30 mark err = view_splitscreen(view->child);
888 9b058f45 2022-06-30 mark if (err)
889 9b058f45 2022-06-30 mark return err;
890 9b058f45 2022-06-30 mark if (dif < 0) { /* top split decreased */
891 9b058f45 2022-06-30 mark err = offset_selection_down(view);
892 9b058f45 2022-06-30 mark if (err)
893 9b058f45 2022-06-30 mark return err;
894 9b058f45 2022-06-30 mark }
895 9b058f45 2022-06-30 mark view_border(view);
896 9b058f45 2022-06-30 mark update_panels();
897 9b058f45 2022-06-30 mark doupdate();
898 9b058f45 2022-06-30 mark show_panel(view->child->panel);
899 9b058f45 2022-06-30 mark nlines = view->nlines;
900 9b058f45 2022-06-30 mark }
901 0dbbbe90 2022-06-17 op } else if (view->parent == NULL)
902 0dbbbe90 2022-06-17 op ncols = COLS;
903 669b5ffa 2018-10-07 stsp
904 571ccd73 2022-07-19 mark if (view->resize && dif > 0) {
905 571ccd73 2022-07-19 mark err = view->resize(view, dif);
906 571ccd73 2022-07-19 mark if (err)
907 571ccd73 2022-07-19 mark return err;
908 571ccd73 2022-07-19 mark }
909 571ccd73 2022-07-19 mark
910 0dbbbe90 2022-06-17 op if (wresize(view->window, nlines, ncols) == ERR)
911 0dbbbe90 2022-06-17 op return got_error_from_errno("wresize");
912 0dbbbe90 2022-06-17 op if (replace_panel(view->panel, view->window) == ERR)
913 0dbbbe90 2022-06-17 op return got_error_from_errno("replace_panel");
914 0dbbbe90 2022-06-17 op wclear(view->window);
915 0dbbbe90 2022-06-17 op
916 0dbbbe90 2022-06-17 op view->nlines = nlines;
917 0dbbbe90 2022-06-17 op view->ncols = ncols;
918 0dbbbe90 2022-06-17 op view->lines = LINES;
919 0dbbbe90 2022-06-17 op view->cols = COLS;
920 0dbbbe90 2022-06-17 op
921 5c60c32a 2018-10-18 stsp return NULL;
922 571ccd73 2022-07-19 mark }
923 571ccd73 2022-07-19 mark
924 571ccd73 2022-07-19 mark static const struct got_error *
925 571ccd73 2022-07-19 mark resize_log_view(struct tog_view *view, int increase)
926 571ccd73 2022-07-19 mark {
927 6fe51fee 2022-07-22 mark struct tog_log_view_state *s = &view->state.log;
928 6fe51fee 2022-07-22 mark const struct got_error *err = NULL;
929 6fe51fee 2022-07-22 mark int n = 0;
930 571ccd73 2022-07-19 mark
931 6fe51fee 2022-07-22 mark if (s->selected_entry)
932 6fe51fee 2022-07-22 mark n = s->selected_entry->idx + view->lines - s->selected;
933 6fe51fee 2022-07-22 mark
934 571ccd73 2022-07-19 mark /*
935 571ccd73 2022-07-19 mark * Request commits to account for the increased
936 571ccd73 2022-07-19 mark * height so we have enough to populate the view.
937 571ccd73 2022-07-19 mark */
938 571ccd73 2022-07-19 mark if (s->commits.ncommits < n) {
939 571ccd73 2022-07-19 mark view->nscrolled = n - s->commits.ncommits + increase + 1;
940 571ccd73 2022-07-19 mark err = request_log_commits(view);
941 571ccd73 2022-07-19 mark }
942 571ccd73 2022-07-19 mark
943 571ccd73 2022-07-19 mark return err;
944 d9a7ab53 2022-07-11 mark }
945 d9a7ab53 2022-07-11 mark
946 d9a7ab53 2022-07-11 mark static void
947 d9a7ab53 2022-07-11 mark view_adjust_offset(struct tog_view *view, int n)
948 d9a7ab53 2022-07-11 mark {
949 d9a7ab53 2022-07-11 mark if (n == 0)
950 d9a7ab53 2022-07-11 mark return;
951 d9a7ab53 2022-07-11 mark
952 d9a7ab53 2022-07-11 mark if (view->parent && view->parent->offset) {
953 d9a7ab53 2022-07-11 mark if (view->parent->offset + n >= 0)
954 d9a7ab53 2022-07-11 mark view->parent->offset += n;
955 d9a7ab53 2022-07-11 mark else
956 d9a7ab53 2022-07-11 mark view->parent->offset = 0;
957 d9a7ab53 2022-07-11 mark } else if (view->offset) {
958 d9a7ab53 2022-07-11 mark if (view->offset - n >= 0)
959 d9a7ab53 2022-07-11 mark view->offset -= n;
960 d9a7ab53 2022-07-11 mark else
961 d9a7ab53 2022-07-11 mark view->offset = 0;
962 d9a7ab53 2022-07-11 mark }
963 3c1dfe12 2022-07-08 mark }
964 3c1dfe12 2022-07-08 mark
965 3c1dfe12 2022-07-08 mark static const struct got_error *
966 3c1dfe12 2022-07-08 mark view_resize_split(struct tog_view *view, int resize)
967 3c1dfe12 2022-07-08 mark {
968 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
969 3c1dfe12 2022-07-08 mark struct tog_view *v = NULL;
970 3c1dfe12 2022-07-08 mark
971 3c1dfe12 2022-07-08 mark if (view->parent)
972 3c1dfe12 2022-07-08 mark v = view->parent;
973 3c1dfe12 2022-07-08 mark else
974 3c1dfe12 2022-07-08 mark v = view;
975 3c1dfe12 2022-07-08 mark
976 3c1dfe12 2022-07-08 mark if (!v->child || !view_is_splitscreen(v->child))
977 3c1dfe12 2022-07-08 mark return NULL;
978 3c1dfe12 2022-07-08 mark
979 571ccd73 2022-07-19 mark v->resized = v->child->resized = resize; /* lock for resize event */
980 3c1dfe12 2022-07-08 mark
981 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
982 3c1dfe12 2022-07-08 mark if (v->child->resized_y)
983 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
984 3c1dfe12 2022-07-08 mark if (view->parent)
985 3c1dfe12 2022-07-08 mark v->child->begin_y -= resize;
986 3c1dfe12 2022-07-08 mark else
987 3c1dfe12 2022-07-08 mark v->child->begin_y += resize;
988 3c1dfe12 2022-07-08 mark if (v->child->begin_y < 3) {
989 3c1dfe12 2022-07-08 mark view->count = 0;
990 3c1dfe12 2022-07-08 mark v->child->begin_y = 3;
991 3c1dfe12 2022-07-08 mark } else if (v->child->begin_y > LINES - 1) {
992 3c1dfe12 2022-07-08 mark view->count = 0;
993 3c1dfe12 2022-07-08 mark v->child->begin_y = LINES - 1;
994 3c1dfe12 2022-07-08 mark }
995 3c1dfe12 2022-07-08 mark v->ncols = COLS;
996 3c1dfe12 2022-07-08 mark v->child->ncols = COLS;
997 d9a7ab53 2022-07-11 mark view_adjust_offset(view, resize);
998 3c1dfe12 2022-07-08 mark err = view_init_hsplit(v, v->child->begin_y);
999 3c1dfe12 2022-07-08 mark if (err)
1000 3c1dfe12 2022-07-08 mark return err;
1001 3c1dfe12 2022-07-08 mark v->child->resized_y = v->child->begin_y;
1002 3c1dfe12 2022-07-08 mark } else {
1003 3c1dfe12 2022-07-08 mark if (v->child->resized_x)
1004 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1005 3c1dfe12 2022-07-08 mark if (view->parent)
1006 3c1dfe12 2022-07-08 mark v->child->begin_x -= resize;
1007 3c1dfe12 2022-07-08 mark else
1008 3c1dfe12 2022-07-08 mark v->child->begin_x += resize;
1009 3c1dfe12 2022-07-08 mark if (v->child->begin_x < 11) {
1010 3c1dfe12 2022-07-08 mark view->count = 0;
1011 3c1dfe12 2022-07-08 mark v->child->begin_x = 11;
1012 3c1dfe12 2022-07-08 mark } else if (v->child->begin_x > COLS - 1) {
1013 3c1dfe12 2022-07-08 mark view->count = 0;
1014 3c1dfe12 2022-07-08 mark v->child->begin_x = COLS - 1;
1015 3c1dfe12 2022-07-08 mark }
1016 3c1dfe12 2022-07-08 mark v->child->resized_x = v->child->begin_x;
1017 3c1dfe12 2022-07-08 mark }
1018 3c1dfe12 2022-07-08 mark
1019 3c1dfe12 2022-07-08 mark v->child->mode = v->mode;
1020 3c1dfe12 2022-07-08 mark v->child->nlines = v->lines - v->child->begin_y;
1021 3c1dfe12 2022-07-08 mark v->child->ncols = v->cols - v->child->begin_x;
1022 3c1dfe12 2022-07-08 mark v->focus_child = 1;
1023 3c1dfe12 2022-07-08 mark
1024 3c1dfe12 2022-07-08 mark err = view_fullscreen(v);
1025 3c1dfe12 2022-07-08 mark if (err)
1026 3c1dfe12 2022-07-08 mark return err;
1027 3c1dfe12 2022-07-08 mark err = view_splitscreen(v->child);
1028 3c1dfe12 2022-07-08 mark if (err)
1029 3c1dfe12 2022-07-08 mark return err;
1030 3c1dfe12 2022-07-08 mark
1031 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1032 3c1dfe12 2022-07-08 mark err = offset_selection_down(v->child);
1033 3c1dfe12 2022-07-08 mark if (err)
1034 3c1dfe12 2022-07-08 mark return err;
1035 3c1dfe12 2022-07-08 mark }
1036 3c1dfe12 2022-07-08 mark
1037 6fe51fee 2022-07-22 mark if (v->resize)
1038 6fe51fee 2022-07-22 mark err = v->resize(v, 0);
1039 6fe51fee 2022-07-22 mark else if (v->child->resize)
1040 6fe51fee 2022-07-22 mark err = v->child->resize(v->child, 0);
1041 3c1dfe12 2022-07-08 mark
1042 571ccd73 2022-07-19 mark v->resized = v->child->resized = 0;
1043 3c1dfe12 2022-07-08 mark
1044 3c1dfe12 2022-07-08 mark return err;
1045 3c1dfe12 2022-07-08 mark }
1046 3c1dfe12 2022-07-08 mark
1047 3c1dfe12 2022-07-08 mark static void
1048 3c1dfe12 2022-07-08 mark view_transfer_size(struct tog_view *dst, struct tog_view *src)
1049 3c1dfe12 2022-07-08 mark {
1050 3c1dfe12 2022-07-08 mark struct tog_view *v = src->child ? src->child : src;
1051 3c1dfe12 2022-07-08 mark
1052 3c1dfe12 2022-07-08 mark dst->resized_x = v->resized_x;
1053 3c1dfe12 2022-07-08 mark dst->resized_y = v->resized_y;
1054 669b5ffa 2018-10-07 stsp }
1055 669b5ffa 2018-10-07 stsp
1056 669b5ffa 2018-10-07 stsp static const struct got_error *
1057 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1058 669b5ffa 2018-10-07 stsp {
1059 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1060 669b5ffa 2018-10-07 stsp
1061 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1062 669b5ffa 2018-10-07 stsp return NULL;
1063 669b5ffa 2018-10-07 stsp
1064 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1065 669b5ffa 2018-10-07 stsp view->child = NULL;
1066 669b5ffa 2018-10-07 stsp return err;
1067 669b5ffa 2018-10-07 stsp }
1068 669b5ffa 2018-10-07 stsp
1069 0dbbbe90 2022-06-17 op static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1071 669b5ffa 2018-10-07 stsp {
1072 3c1dfe12 2022-07-08 mark const struct got_error *err = NULL;
1073 3c1dfe12 2022-07-08 mark
1074 669b5ffa 2018-10-07 stsp view->child = child;
1075 669b5ffa 2018-10-07 stsp child->parent = view;
1076 0dbbbe90 2022-06-17 op
1077 3c1dfe12 2022-07-08 mark err = view_resize(view);
1078 3c1dfe12 2022-07-08 mark if (err)
1079 3c1dfe12 2022-07-08 mark return err;
1080 3c1dfe12 2022-07-08 mark
1081 3c1dfe12 2022-07-08 mark if (view->child->resized_x || view->child->resized_y)
1082 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1083 3c1dfe12 2022-07-08 mark
1084 3c1dfe12 2022-07-08 mark return err;
1085 bfddd0d9 2018-09-29 stsp }
1086 136e2bd4 2022-07-23 mark
1087 136e2bd4 2022-07-23 mark static const struct got_error *view_dispatch_request(struct tog_view **,
1088 136e2bd4 2022-07-23 mark struct tog_view *, enum tog_view_type, int, int);
1089 136e2bd4 2022-07-23 mark
1090 136e2bd4 2022-07-23 mark static const struct got_error *
1091 136e2bd4 2022-07-23 mark view_request_new(struct tog_view **requested, struct tog_view *view,
1092 136e2bd4 2022-07-23 mark enum tog_view_type request)
1093 136e2bd4 2022-07-23 mark {
1094 136e2bd4 2022-07-23 mark struct tog_view *new_view = NULL;
1095 136e2bd4 2022-07-23 mark const struct got_error *err;
1096 136e2bd4 2022-07-23 mark int y = 0, x = 0;
1097 136e2bd4 2022-07-23 mark
1098 136e2bd4 2022-07-23 mark *requested = NULL;
1099 136e2bd4 2022-07-23 mark
1100 136e2bd4 2022-07-23 mark if (view_is_parent_view(view))
1101 136e2bd4 2022-07-23 mark view_get_split(view, &y, &x);
1102 bfddd0d9 2018-09-29 stsp
1103 136e2bd4 2022-07-23 mark err = view_dispatch_request(&new_view, view, request, y, x);
1104 136e2bd4 2022-07-23 mark if (err)
1105 136e2bd4 2022-07-23 mark return err;
1106 136e2bd4 2022-07-23 mark
1107 136e2bd4 2022-07-23 mark if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1108 136e2bd4 2022-07-23 mark err = view_init_hsplit(view, y);
1109 136e2bd4 2022-07-23 mark if (err)
1110 136e2bd4 2022-07-23 mark return err;
1111 136e2bd4 2022-07-23 mark }
1112 136e2bd4 2022-07-23 mark
1113 136e2bd4 2022-07-23 mark view->focussed = 0;
1114 136e2bd4 2022-07-23 mark new_view->focussed = 1;
1115 136e2bd4 2022-07-23 mark new_view->mode = view->mode;
1116 136e2bd4 2022-07-23 mark new_view->nlines = view->lines - y;
1117 136e2bd4 2022-07-23 mark
1118 136e2bd4 2022-07-23 mark if (view_is_parent_view(view)) {
1119 136e2bd4 2022-07-23 mark view_transfer_size(new_view, view);
1120 136e2bd4 2022-07-23 mark err = view_close_child(view);
1121 136e2bd4 2022-07-23 mark if (err)
1122 136e2bd4 2022-07-23 mark return err;
1123 136e2bd4 2022-07-23 mark err = view_set_child(view, new_view);
1124 136e2bd4 2022-07-23 mark if (err)
1125 136e2bd4 2022-07-23 mark return err;
1126 136e2bd4 2022-07-23 mark view->focus_child = 1;
1127 136e2bd4 2022-07-23 mark } else
1128 136e2bd4 2022-07-23 mark *requested = new_view;
1129 136e2bd4 2022-07-23 mark
1130 136e2bd4 2022-07-23 mark return NULL;
1131 136e2bd4 2022-07-23 mark }
1132 136e2bd4 2022-07-23 mark
1133 34bc9ec9 2019-02-22 stsp static void
1134 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1135 25791caa 2018-10-24 stsp {
1136 25791caa 2018-10-24 stsp int cols, lines;
1137 25791caa 2018-10-24 stsp struct winsize size;
1138 25791caa 2018-10-24 stsp
1139 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1140 25791caa 2018-10-24 stsp cols = 80; /* Default */
1141 25791caa 2018-10-24 stsp lines = 24;
1142 25791caa 2018-10-24 stsp } else {
1143 25791caa 2018-10-24 stsp cols = size.ws_col;
1144 25791caa 2018-10-24 stsp lines = size.ws_row;
1145 25791caa 2018-10-24 stsp }
1146 25791caa 2018-10-24 stsp resize_term(lines, cols);
1147 2b49a8ae 2019-06-22 stsp }
1148 2b49a8ae 2019-06-22 stsp
1149 2b49a8ae 2019-06-22 stsp static const struct got_error *
1150 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1151 2b49a8ae 2019-06-22 stsp {
1152 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1153 9b058f45 2022-06-30 mark struct tog_view *v = view;
1154 2b49a8ae 2019-06-22 stsp char pattern[1024];
1155 2b49a8ae 2019-06-22 stsp int ret;
1156 c0c4acc8 2021-01-24 stsp
1157 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1158 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1159 c0c4acc8 2021-01-24 stsp view->searching = 0;
1160 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1161 c0c4acc8 2021-01-24 stsp }
1162 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1163 2b49a8ae 2019-06-22 stsp
1164 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1165 2b49a8ae 2019-06-22 stsp return NULL;
1166 2b49a8ae 2019-06-22 stsp
1167 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1168 9b058f45 2022-06-30 mark v = view->child;
1169 2b49a8ae 2019-06-22 stsp
1170 9b058f45 2022-06-30 mark mvwaddstr(v->window, v->nlines - 1, 0, "/");
1171 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1172 9b058f45 2022-06-30 mark
1173 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE); /* block for search term input */
1174 2b49a8ae 2019-06-22 stsp nocbreak();
1175 2b49a8ae 2019-06-22 stsp echo();
1176 9b058f45 2022-06-30 mark ret = wgetnstr(v->window, pattern, sizeof(pattern));
1177 9b058f45 2022-06-30 mark wrefresh(v->window);
1178 2b49a8ae 2019-06-22 stsp cbreak();
1179 2b49a8ae 2019-06-22 stsp noecho();
1180 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1181 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1182 2b49a8ae 2019-06-22 stsp return NULL;
1183 2b49a8ae 2019-06-22 stsp
1184 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1185 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1186 7c32bd05 2019-06-22 stsp if (err) {
1187 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1188 7c32bd05 2019-06-22 stsp return err;
1189 7c32bd05 2019-06-22 stsp }
1190 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1191 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1192 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1193 2b49a8ae 2019-06-22 stsp view->search_next(view);
1194 2b49a8ae 2019-06-22 stsp }
1195 2b49a8ae 2019-06-22 stsp
1196 2b49a8ae 2019-06-22 stsp return NULL;
1197 d2366e29 2022-07-07 mark }
1198 d2366e29 2022-07-07 mark
1199 7532ccda 2022-07-11 mark /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1200 d2366e29 2022-07-07 mark static const struct got_error *
1201 d2366e29 2022-07-07 mark switch_split(struct tog_view *view)
1202 d2366e29 2022-07-07 mark {
1203 d2366e29 2022-07-07 mark const struct got_error *err = NULL;
1204 d2366e29 2022-07-07 mark struct tog_view *v = NULL;
1205 d2366e29 2022-07-07 mark
1206 d2366e29 2022-07-07 mark if (view->parent)
1207 d2366e29 2022-07-07 mark v = view->parent;
1208 d2366e29 2022-07-07 mark else
1209 d2366e29 2022-07-07 mark v = view;
1210 d2366e29 2022-07-07 mark
1211 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN)
1212 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1213 7532ccda 2022-07-11 mark else
1214 d2366e29 2022-07-07 mark v->mode = TOG_VIEW_SPLIT_HRZN;
1215 d2366e29 2022-07-07 mark
1216 7532ccda 2022-07-11 mark if (!v->child)
1217 7532ccda 2022-07-11 mark return NULL;
1218 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1219 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_NONE;
1220 7532ccda 2022-07-11 mark
1221 d2366e29 2022-07-07 mark view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1222 3c1dfe12 2022-07-08 mark if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1223 3c1dfe12 2022-07-08 mark v->child->begin_y = v->child->resized_y;
1224 7532ccda 2022-07-11 mark else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1225 3c1dfe12 2022-07-08 mark v->child->begin_x = v->child->resized_x;
1226 d2366e29 2022-07-07 mark
1227 7532ccda 2022-07-11 mark
1228 d2366e29 2022-07-07 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1229 d2366e29 2022-07-07 mark v->ncols = COLS;
1230 d2366e29 2022-07-07 mark v->child->ncols = COLS;
1231 7532ccda 2022-07-11 mark v->child->nscrolled = LINES - v->child->nlines;
1232 d2366e29 2022-07-07 mark
1233 d2366e29 2022-07-07 mark err = view_init_hsplit(v, v->child->begin_y);
1234 d2366e29 2022-07-07 mark if (err)
1235 d2366e29 2022-07-07 mark return err;
1236 d2366e29 2022-07-07 mark }
1237 d2366e29 2022-07-07 mark v->child->mode = v->mode;
1238 d2366e29 2022-07-07 mark v->child->nlines = v->lines - v->child->begin_y;
1239 d2366e29 2022-07-07 mark v->focus_child = 1;
1240 d2366e29 2022-07-07 mark
1241 d2366e29 2022-07-07 mark err = view_fullscreen(v);
1242 d2366e29 2022-07-07 mark if (err)
1243 d2366e29 2022-07-07 mark return err;
1244 d2366e29 2022-07-07 mark err = view_splitscreen(v->child);
1245 d2366e29 2022-07-07 mark if (err)
1246 d2366e29 2022-07-07 mark return err;
1247 d2366e29 2022-07-07 mark
1248 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_NONE)
1249 7532ccda 2022-07-11 mark v->mode = TOG_VIEW_SPLIT_VERT;
1250 7532ccda 2022-07-11 mark if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1251 7532ccda 2022-07-11 mark err = offset_selection_down(v);
1252 279d2047 2022-07-29 stsp if (err)
1253 279d2047 2022-07-29 stsp return err;
1254 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1255 279d2047 2022-07-29 stsp if (err)
1256 279d2047 2022-07-29 stsp return err;
1257 7532ccda 2022-07-11 mark } else {
1258 7532ccda 2022-07-11 mark offset_selection_up(v);
1259 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1260 d2366e29 2022-07-07 mark }
1261 dff91825 2022-07-22 mark if (v->resize)
1262 dff91825 2022-07-22 mark err = v->resize(v, 0);
1263 dff91825 2022-07-22 mark else if (v->child->resize)
1264 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1265 d2366e29 2022-07-07 mark
1266 d2366e29 2022-07-07 mark return err;
1267 0cf4efb1 2018-09-29 stsp }
1268 6d0fee91 2018-08-01 stsp
1269 640cd7ff 2022-06-22 mark /*
1270 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1271 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1272 640cd7ff 2022-06-22 mark */
1273 640cd7ff 2022-06-22 mark static int
1274 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1275 640cd7ff 2022-06-22 mark {
1276 9b058f45 2022-06-30 mark struct tog_view *v = view;
1277 9b058f45 2022-06-30 mark int x, n = 0;
1278 640cd7ff 2022-06-22 mark
1279 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1280 9b058f45 2022-06-30 mark v = view->child;
1281 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1282 9b058f45 2022-06-30 mark v = view->parent;
1283 9b058f45 2022-06-30 mark
1284 640cd7ff 2022-06-22 mark view->count = 0;
1285 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1286 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1287 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1288 9b058f45 2022-06-30 mark waddch(v->window, ':');
1289 640cd7ff 2022-06-22 mark
1290 640cd7ff 2022-06-22 mark do {
1291 9b058f45 2022-06-30 mark x = getcurx(v->window);
1292 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1293 9b058f45 2022-06-30 mark waddch(v->window, c);
1294 9b058f45 2022-06-30 mark wrefresh(v->window);
1295 9b058f45 2022-06-30 mark }
1296 9b058f45 2022-06-30 mark
1297 640cd7ff 2022-06-22 mark /*
1298 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1299 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1300 640cd7ff 2022-06-22 mark */
1301 640cd7ff 2022-06-22 mark if (n >= 9999999)
1302 640cd7ff 2022-06-22 mark n = 9999999;
1303 640cd7ff 2022-06-22 mark else
1304 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1305 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1306 640cd7ff 2022-06-22 mark
1307 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1308 640cd7ff 2022-06-22 mark view->count = n;
1309 640cd7ff 2022-06-22 mark
1310 640cd7ff 2022-06-22 mark return c;
1311 640cd7ff 2022-06-22 mark }
1312 640cd7ff 2022-06-22 mark
1313 0cf4efb1 2018-09-29 stsp static const struct got_error *
1314 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1315 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1316 e5a0f69f 2018-08-18 stsp {
1317 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1318 669b5ffa 2018-10-07 stsp struct tog_view *v;
1319 1a76625f 2018-10-22 stsp int ch, errcode;
1320 e5a0f69f 2018-08-18 stsp
1321 e5a0f69f 2018-08-18 stsp *new = NULL;
1322 8f4ed634 2020-03-26 stsp
1323 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1324 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1325 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1326 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1327 640cd7ff 2022-06-22 mark view->count = 0;
1328 640cd7ff 2022-06-22 mark }
1329 e5a0f69f 2018-08-18 stsp
1330 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1331 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1332 82954512 2020-02-03 stsp if (errcode)
1333 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1334 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1335 3da8ef85 2021-09-21 stsp sched_yield();
1336 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1337 82954512 2020-02-03 stsp if (errcode)
1338 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1339 82954512 2020-02-03 stsp "pthread_mutex_lock");
1340 60493ae3 2019-06-20 stsp view->search_next(view);
1341 60493ae3 2019-06-20 stsp return NULL;
1342 60493ae3 2019-06-20 stsp }
1343 60493ae3 2019-06-20 stsp
1344 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1345 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1346 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1347 1a76625f 2018-10-22 stsp if (errcode)
1348 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1349 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1350 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1351 a6d37fac 2022-07-03 mark cbreak();
1352 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1353 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1354 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1355 a6d37fac 2022-07-03 mark view->count = 0;
1356 a6d37fac 2022-07-03 mark else
1357 a6d37fac 2022-07-03 mark ch = view->ch;
1358 a6d37fac 2022-07-03 mark } else {
1359 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1360 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1361 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1362 640cd7ff 2022-06-22 mark }
1363 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1364 1a76625f 2018-10-22 stsp if (errcode)
1365 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1366 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1367 25791caa 2018-10-24 stsp
1368 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1369 25791caa 2018-10-24 stsp tog_resizeterm();
1370 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1371 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1372 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1373 25791caa 2018-10-24 stsp err = view_resize(v);
1374 25791caa 2018-10-24 stsp if (err)
1375 25791caa 2018-10-24 stsp return err;
1376 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1377 25791caa 2018-10-24 stsp if (err)
1378 25791caa 2018-10-24 stsp return err;
1379 cdfcfb03 2020-12-06 stsp if (v->child) {
1380 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1381 cdfcfb03 2020-12-06 stsp if (err)
1382 cdfcfb03 2020-12-06 stsp return err;
1383 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1384 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1385 cdfcfb03 2020-12-06 stsp if (err)
1386 cdfcfb03 2020-12-06 stsp return err;
1387 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1388 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1389 3c1dfe12 2022-07-08 mark if (err)
1390 3c1dfe12 2022-07-08 mark return err;
1391 3c1dfe12 2022-07-08 mark }
1392 cdfcfb03 2020-12-06 stsp }
1393 25791caa 2018-10-24 stsp }
1394 25791caa 2018-10-24 stsp }
1395 25791caa 2018-10-24 stsp
1396 e5a0f69f 2018-08-18 stsp switch (ch) {
1397 1e37a5c2 2019-05-12 jcs case '\t':
1398 640cd7ff 2022-06-22 mark view->count = 0;
1399 1e37a5c2 2019-05-12 jcs if (view->child) {
1400 e78dc838 2020-12-04 stsp view->focussed = 0;
1401 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1402 e78dc838 2020-12-04 stsp view->focus_child = 1;
1403 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1404 e78dc838 2020-12-04 stsp view->focussed = 0;
1405 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1406 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1407 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1408 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1409 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1410 6fe51fee 2022-07-22 mark 0);
1411 9b058f45 2022-06-30 mark if (err)
1412 9b058f45 2022-06-30 mark return err;
1413 9b058f45 2022-06-30 mark }
1414 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1415 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1416 9b058f45 2022-06-30 mark if (err)
1417 9b058f45 2022-06-30 mark return err;
1418 9b058f45 2022-06-30 mark }
1419 1e37a5c2 2019-05-12 jcs }
1420 1e37a5c2 2019-05-12 jcs break;
1421 1e37a5c2 2019-05-12 jcs case 'q':
1422 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1423 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1424 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1425 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1426 9b058f45 2022-06-30 mark if (err)
1427 9b058f45 2022-06-30 mark break;
1428 9b058f45 2022-06-30 mark }
1429 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1430 9b058f45 2022-06-30 mark }
1431 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1432 9970f7fc 2020-12-03 stsp view->dying = 1;
1433 1e37a5c2 2019-05-12 jcs break;
1434 1e37a5c2 2019-05-12 jcs case 'Q':
1435 1e37a5c2 2019-05-12 jcs *done = 1;
1436 1e37a5c2 2019-05-12 jcs break;
1437 61417565 2022-06-20 mark case 'F':
1438 640cd7ff 2022-06-22 mark view->count = 0;
1439 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1440 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1441 1e37a5c2 2019-05-12 jcs break;
1442 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1443 e78dc838 2020-12-04 stsp view->focussed = 0;
1444 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1445 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1446 3c1dfe12 2022-07-08 mark } else {
1447 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1448 3c1dfe12 2022-07-08 mark if (!err)
1449 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1450 3c1dfe12 2022-07-08 mark }
1451 1e37a5c2 2019-05-12 jcs if (err)
1452 1e37a5c2 2019-05-12 jcs break;
1453 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1454 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1455 1e37a5c2 2019-05-12 jcs } else {
1456 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1457 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1458 e78dc838 2020-12-04 stsp view->focussed = 1;
1459 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1460 1e37a5c2 2019-05-12 jcs } else {
1461 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1462 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1463 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1464 3c1dfe12 2022-07-08 mark if (!err)
1465 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1466 669b5ffa 2018-10-07 stsp }
1467 1e37a5c2 2019-05-12 jcs if (err)
1468 1e37a5c2 2019-05-12 jcs break;
1469 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1470 1e37a5c2 2019-05-12 jcs }
1471 9b058f45 2022-06-30 mark if (err)
1472 9b058f45 2022-06-30 mark break;
1473 6fe51fee 2022-07-22 mark if (view->resize) {
1474 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1475 9b058f45 2022-06-30 mark if (err)
1476 9b058f45 2022-06-30 mark break;
1477 9b058f45 2022-06-30 mark }
1478 9b058f45 2022-06-30 mark if (view->parent)
1479 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1480 9b058f45 2022-06-30 mark if (!err)
1481 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1482 1e37a5c2 2019-05-12 jcs break;
1483 d2366e29 2022-07-07 mark case 'S':
1484 3c1dfe12 2022-07-08 mark view->count = 0;
1485 d2366e29 2022-07-07 mark err = switch_split(view);
1486 3c1dfe12 2022-07-08 mark break;
1487 3c1dfe12 2022-07-08 mark case '-':
1488 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1489 d2366e29 2022-07-07 mark break;
1490 3c1dfe12 2022-07-08 mark case '+':
1491 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1492 3c1dfe12 2022-07-08 mark break;
1493 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1494 60493ae3 2019-06-20 stsp break;
1495 60493ae3 2019-06-20 stsp case '/':
1496 640cd7ff 2022-06-22 mark view->count = 0;
1497 60493ae3 2019-06-20 stsp if (view->search_start)
1498 2b49a8ae 2019-06-22 stsp view_search_start(view);
1499 60493ae3 2019-06-20 stsp else
1500 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1501 1e37a5c2 2019-05-12 jcs break;
1502 b1bf1435 2019-06-21 stsp case 'N':
1503 60493ae3 2019-06-20 stsp case 'n':
1504 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1505 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1506 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1507 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1508 60493ae3 2019-06-20 stsp view->search_next(view);
1509 60493ae3 2019-06-20 stsp } else
1510 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1511 917d79a7 2022-07-01 stsp break;
1512 917d79a7 2022-07-01 stsp case 'A':
1513 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1514 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1515 917d79a7 2022-07-01 stsp else
1516 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1517 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1518 917d79a7 2022-07-01 stsp if (v->reset) {
1519 917d79a7 2022-07-01 stsp err = v->reset(v);
1520 917d79a7 2022-07-01 stsp if (err)
1521 917d79a7 2022-07-01 stsp return err;
1522 917d79a7 2022-07-01 stsp }
1523 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1524 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1525 917d79a7 2022-07-01 stsp if (err)
1526 917d79a7 2022-07-01 stsp return err;
1527 917d79a7 2022-07-01 stsp }
1528 917d79a7 2022-07-01 stsp }
1529 60493ae3 2019-06-20 stsp break;
1530 1e37a5c2 2019-05-12 jcs default:
1531 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1532 1e37a5c2 2019-05-12 jcs break;
1533 e5a0f69f 2018-08-18 stsp }
1534 e5a0f69f 2018-08-18 stsp
1535 e5a0f69f 2018-08-18 stsp return err;
1536 e5a0f69f 2018-08-18 stsp }
1537 e5a0f69f 2018-08-18 stsp
1538 336075a4 2022-06-25 op static int
1539 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1540 a3404814 2018-09-02 stsp {
1541 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1542 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1543 669b5ffa 2018-10-07 stsp return 0;
1544 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1545 669b5ffa 2018-10-07 stsp return 0;
1546 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1547 a3404814 2018-09-02 stsp return 0;
1548 a3404814 2018-09-02 stsp
1549 669b5ffa 2018-10-07 stsp return view->focussed;
1550 a3404814 2018-09-02 stsp }
1551 a3404814 2018-09-02 stsp
1552 bcbd79e2 2018-08-19 stsp static const struct got_error *
1553 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1554 e5a0f69f 2018-08-18 stsp {
1555 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1556 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1557 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1558 d2366e29 2022-07-07 mark char *mode;
1559 fd823528 2018-10-22 stsp int fast_refresh = 10;
1560 1a76625f 2018-10-22 stsp int done = 0, errcode;
1561 e5a0f69f 2018-08-18 stsp
1562 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1563 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1564 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1565 d2366e29 2022-07-07 mark else
1566 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1567 d2366e29 2022-07-07 mark
1568 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1569 1a76625f 2018-10-22 stsp if (errcode)
1570 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1571 1a76625f 2018-10-22 stsp
1572 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1573 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1574 e5a0f69f 2018-08-18 stsp
1575 1004088d 2018-09-29 stsp view->focussed = 1;
1576 878940b7 2018-09-29 stsp err = view->show(view);
1577 0cf4efb1 2018-09-29 stsp if (err)
1578 0cf4efb1 2018-09-29 stsp return err;
1579 0cf4efb1 2018-09-29 stsp update_panels();
1580 0cf4efb1 2018-09-29 stsp doupdate();
1581 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1582 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1583 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1584 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1585 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1586 fd823528 2018-10-22 stsp
1587 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1588 e5a0f69f 2018-08-18 stsp if (err)
1589 e5a0f69f 2018-08-18 stsp break;
1590 9970f7fc 2020-12-03 stsp if (view->dying) {
1591 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1592 669b5ffa 2018-10-07 stsp
1593 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1594 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1595 9970f7fc 2020-12-03 stsp entry);
1596 e78dc838 2020-12-04 stsp else if (view->parent)
1597 669b5ffa 2018-10-07 stsp prev = view->parent;
1598 669b5ffa 2018-10-07 stsp
1599 e78dc838 2020-12-04 stsp if (view->parent) {
1600 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1601 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1602 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1603 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1604 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1605 0dbbbe90 2022-06-17 op if (err)
1606 0dbbbe90 2022-06-17 op break;
1607 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1608 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1609 e78dc838 2020-12-04 stsp } else
1610 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1611 669b5ffa 2018-10-07 stsp
1612 9970f7fc 2020-12-03 stsp err = view_close(view);
1613 fb59748f 2020-12-05 stsp if (err)
1614 e5a0f69f 2018-08-18 stsp goto done;
1615 669b5ffa 2018-10-07 stsp
1616 e78dc838 2020-12-04 stsp view = NULL;
1617 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1618 e78dc838 2020-12-04 stsp if (v->focussed)
1619 e78dc838 2020-12-04 stsp break;
1620 0cf4efb1 2018-09-29 stsp }
1621 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1622 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1623 e78dc838 2020-12-04 stsp if (prev)
1624 e78dc838 2020-12-04 stsp view = prev;
1625 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1626 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1627 e78dc838 2020-12-04 stsp tog_view_list_head);
1628 e78dc838 2020-12-04 stsp }
1629 e78dc838 2020-12-04 stsp if (view) {
1630 e78dc838 2020-12-04 stsp if (view->focus_child) {
1631 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1632 e78dc838 2020-12-04 stsp view = view->child;
1633 e78dc838 2020-12-04 stsp } else
1634 e78dc838 2020-12-04 stsp view->focussed = 1;
1635 e78dc838 2020-12-04 stsp }
1636 e78dc838 2020-12-04 stsp }
1637 e5a0f69f 2018-08-18 stsp }
1638 bcbd79e2 2018-08-19 stsp if (new_view) {
1639 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1640 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1641 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1642 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1643 86c66b02 2018-10-18 stsp continue;
1644 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1645 86c66b02 2018-10-18 stsp err = view_close(v);
1646 86c66b02 2018-10-18 stsp if (err)
1647 86c66b02 2018-10-18 stsp goto done;
1648 86c66b02 2018-10-18 stsp break;
1649 86c66b02 2018-10-18 stsp }
1650 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1651 fed7eaa8 2018-10-24 stsp view = new_view;
1652 0ae84acc 2022-06-15 tracey }
1653 669b5ffa 2018-10-07 stsp if (view) {
1654 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1655 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1656 e78dc838 2020-12-04 stsp view = view->child;
1657 e78dc838 2020-12-04 stsp } else {
1658 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1659 e78dc838 2020-12-04 stsp view = view->parent;
1660 1a76625f 2018-10-22 stsp }
1661 e78dc838 2020-12-04 stsp show_panel(view->panel);
1662 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1663 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1664 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1665 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1666 669b5ffa 2018-10-07 stsp if (err)
1667 1a76625f 2018-10-22 stsp goto done;
1668 669b5ffa 2018-10-07 stsp }
1669 669b5ffa 2018-10-07 stsp err = view->show(view);
1670 0cf4efb1 2018-09-29 stsp if (err)
1671 1a76625f 2018-10-22 stsp goto done;
1672 669b5ffa 2018-10-07 stsp if (view->child) {
1673 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1674 669b5ffa 2018-10-07 stsp if (err)
1675 1a76625f 2018-10-22 stsp goto done;
1676 669b5ffa 2018-10-07 stsp }
1677 1a76625f 2018-10-22 stsp update_panels();
1678 1a76625f 2018-10-22 stsp doupdate();
1679 0cf4efb1 2018-09-29 stsp }
1680 e5a0f69f 2018-08-18 stsp }
1681 e5a0f69f 2018-08-18 stsp done:
1682 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1683 5629093a 2022-07-11 stsp const struct got_error *close_err;
1684 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1685 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1686 5629093a 2022-07-11 stsp close_err = view_close(view);
1687 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1688 5629093a 2022-07-11 stsp err = close_err;
1689 e5a0f69f 2018-08-18 stsp }
1690 1a76625f 2018-10-22 stsp
1691 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1692 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1693 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1694 1a76625f 2018-10-22 stsp
1695 e5a0f69f 2018-08-18 stsp return err;
1696 ea5e7bb5 2018-08-01 stsp }
1697 ea5e7bb5 2018-08-01 stsp
1698 4ed7e80c 2018-05-20 stsp __dead static void
1699 9f7d7167 2018-04-29 stsp usage_log(void)
1700 9f7d7167 2018-04-29 stsp {
1701 80ddbec8 2018-04-29 stsp endwin();
1702 c70c5802 2018-08-01 stsp fprintf(stderr,
1703 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1704 9f7d7167 2018-04-29 stsp getprogname());
1705 9f7d7167 2018-04-29 stsp exit(1);
1706 80ddbec8 2018-04-29 stsp }
1707 80ddbec8 2018-04-29 stsp
1708 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1709 80ddbec8 2018-04-29 stsp static const struct got_error *
1710 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1711 963b370f 2018-05-20 stsp {
1712 00dfcb92 2018-06-11 stsp char *vis = NULL;
1713 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1714 963b370f 2018-05-20 stsp
1715 963b370f 2018-05-20 stsp *ws = NULL;
1716 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1717 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1718 00dfcb92 2018-06-11 stsp int vislen;
1719 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1720 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1721 00dfcb92 2018-06-11 stsp
1722 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1723 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1724 00dfcb92 2018-06-11 stsp if (err)
1725 00dfcb92 2018-06-11 stsp return err;
1726 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1727 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1728 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1729 a7f50699 2018-06-11 stsp goto done;
1730 a7f50699 2018-06-11 stsp }
1731 00dfcb92 2018-06-11 stsp }
1732 963b370f 2018-05-20 stsp
1733 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1734 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1735 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1736 a7f50699 2018-06-11 stsp goto done;
1737 a7f50699 2018-06-11 stsp }
1738 963b370f 2018-05-20 stsp
1739 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1740 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1741 a7f50699 2018-06-11 stsp done:
1742 00dfcb92 2018-06-11 stsp free(vis);
1743 963b370f 2018-05-20 stsp if (err) {
1744 963b370f 2018-05-20 stsp free(*ws);
1745 963b370f 2018-05-20 stsp *ws = NULL;
1746 963b370f 2018-05-20 stsp *wlen = 0;
1747 963b370f 2018-05-20 stsp }
1748 963b370f 2018-05-20 stsp return err;
1749 145b6838 2022-06-16 stsp }
1750 145b6838 2022-06-16 stsp
1751 145b6838 2022-06-16 stsp static const struct got_error *
1752 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1753 145b6838 2022-06-16 stsp {
1754 145b6838 2022-06-16 stsp char *dst;
1755 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1756 145b6838 2022-06-16 stsp
1757 145b6838 2022-06-16 stsp *ptr = NULL;
1758 145b6838 2022-06-16 stsp n = len = strlen(src);
1759 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1760 145b6838 2022-06-16 stsp if (dst == NULL)
1761 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1762 145b6838 2022-06-16 stsp
1763 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1764 145b6838 2022-06-16 stsp const char c = src[idx];
1765 145b6838 2022-06-16 stsp
1766 145b6838 2022-06-16 stsp if (c == '\t') {
1767 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1768 367ddf28 2022-06-16 mark char *p;
1769 367ddf28 2022-06-16 mark
1770 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1771 6e1c41ad 2022-06-16 mark if (p == NULL) {
1772 6e1c41ad 2022-06-16 mark free(dst);
1773 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1774 6e1c41ad 2022-06-16 mark
1775 6e1c41ad 2022-06-16 mark }
1776 6e1c41ad 2022-06-16 mark dst = p;
1777 145b6838 2022-06-16 stsp n += nb;
1778 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1779 145b6838 2022-06-16 stsp sz += nb;
1780 145b6838 2022-06-16 stsp } else
1781 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1782 145b6838 2022-06-16 stsp ++idx;
1783 145b6838 2022-06-16 stsp }
1784 145b6838 2022-06-16 stsp
1785 145b6838 2022-06-16 stsp dst[sz] = '\0';
1786 145b6838 2022-06-16 stsp *ptr = dst;
1787 145b6838 2022-06-16 stsp return NULL;
1788 963b370f 2018-05-20 stsp }
1789 963b370f 2018-05-20 stsp
1790 4e4a9ac8 2022-06-17 op /*
1791 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1792 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1793 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1794 4e4a9ac8 2022-06-17 op * *rcol.
1795 ccda2f4d 2022-06-16 stsp */
1796 4e4a9ac8 2022-06-17 op static int
1797 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1798 4e4a9ac8 2022-06-17 op {
1799 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1800 ccda2f4d 2022-06-16 stsp
1801 4e4a9ac8 2022-06-17 op if (n == 0) {
1802 4e4a9ac8 2022-06-17 op *rcol = cols;
1803 4e4a9ac8 2022-06-17 op return off;
1804 4e4a9ac8 2022-06-17 op }
1805 ccda2f4d 2022-06-16 stsp
1806 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1807 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1808 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1809 4e4a9ac8 2022-06-17 op else
1810 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1811 ccda2f4d 2022-06-16 stsp
1812 4e4a9ac8 2022-06-17 op if (width == -1) {
1813 4e4a9ac8 2022-06-17 op width = 1;
1814 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1815 ccda2f4d 2022-06-16 stsp }
1816 ccda2f4d 2022-06-16 stsp
1817 4e4a9ac8 2022-06-17 op if (cols + width > n)
1818 4e4a9ac8 2022-06-17 op break;
1819 4e4a9ac8 2022-06-17 op cols += width;
1820 ccda2f4d 2022-06-16 stsp }
1821 ccda2f4d 2022-06-16 stsp
1822 4e4a9ac8 2022-06-17 op *rcol = cols;
1823 4e4a9ac8 2022-06-17 op return i;
1824 ccda2f4d 2022-06-16 stsp }
1825 ccda2f4d 2022-06-16 stsp
1826 ccda2f4d 2022-06-16 stsp /*
1827 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1828 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1829 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1830 ccda2f4d 2022-06-16 stsp */
1831 ccda2f4d 2022-06-16 stsp static const struct got_error *
1832 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1833 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1834 ccda2f4d 2022-06-16 stsp {
1835 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1836 4e4a9ac8 2022-06-17 op int cols;
1837 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1838 145b6838 2022-06-16 stsp char *exstr = NULL;
1839 963b370f 2018-05-20 stsp size_t wlen;
1840 4e4a9ac8 2022-06-17 op int i, scrollx;
1841 963b370f 2018-05-20 stsp
1842 963b370f 2018-05-20 stsp *wlinep = NULL;
1843 b700b5d6 2018-07-10 stsp *widthp = 0;
1844 963b370f 2018-05-20 stsp
1845 145b6838 2022-06-16 stsp if (expand) {
1846 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1847 145b6838 2022-06-16 stsp if (err)
1848 145b6838 2022-06-16 stsp return err;
1849 145b6838 2022-06-16 stsp }
1850 145b6838 2022-06-16 stsp
1851 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1852 145b6838 2022-06-16 stsp free(exstr);
1853 963b370f 2018-05-20 stsp if (err)
1854 963b370f 2018-05-20 stsp return err;
1855 963b370f 2018-05-20 stsp
1856 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1857 ccda2f4d 2022-06-16 stsp
1858 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1859 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1860 3f670bfb 2020-12-10 stsp wlen--;
1861 3f670bfb 2020-12-10 stsp }
1862 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1863 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1864 3f670bfb 2020-12-10 stsp wlen--;
1865 3f670bfb 2020-12-10 stsp }
1866 3f670bfb 2020-12-10 stsp
1867 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1868 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1869 27a741e5 2019-09-11 stsp
1870 b700b5d6 2018-07-10 stsp if (widthp)
1871 b700b5d6 2018-07-10 stsp *widthp = cols;
1872 ccda2f4d 2022-06-16 stsp if (scrollxp)
1873 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1874 963b370f 2018-05-20 stsp if (err)
1875 963b370f 2018-05-20 stsp free(wline);
1876 963b370f 2018-05-20 stsp else
1877 963b370f 2018-05-20 stsp *wlinep = wline;
1878 963b370f 2018-05-20 stsp return err;
1879 963b370f 2018-05-20 stsp }
1880 963b370f 2018-05-20 stsp
1881 8b473291 2019-02-21 stsp static const struct got_error*
1882 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1883 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1884 8b473291 2019-02-21 stsp {
1885 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1886 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1887 8b473291 2019-02-21 stsp char *s;
1888 8b473291 2019-02-21 stsp const char *name;
1889 8b473291 2019-02-21 stsp
1890 8b473291 2019-02-21 stsp *refs_str = NULL;
1891 8b473291 2019-02-21 stsp
1892 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1893 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1894 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1895 52b5abe1 2019-08-13 stsp int cmp;
1896 52b5abe1 2019-08-13 stsp
1897 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1898 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1899 8b473291 2019-02-21 stsp continue;
1900 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1901 8b473291 2019-02-21 stsp name += 5;
1902 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1903 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1904 7143d404 2019-03-12 stsp continue;
1905 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1906 8b473291 2019-02-21 stsp name += 6;
1907 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1908 8b473291 2019-02-21 stsp name += 8;
1909 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1910 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1911 79cc719f 2020-04-24 stsp continue;
1912 79cc719f 2020-04-24 stsp }
1913 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1914 48cae60d 2020-09-22 stsp if (err)
1915 48cae60d 2020-09-22 stsp break;
1916 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1917 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1918 5d844a1e 2019-08-13 stsp if (err) {
1919 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1920 48cae60d 2020-09-22 stsp free(ref_id);
1921 5d844a1e 2019-08-13 stsp break;
1922 48cae60d 2020-09-22 stsp }
1923 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1924 5d844a1e 2019-08-13 stsp err = NULL;
1925 5d844a1e 2019-08-13 stsp tag = NULL;
1926 5d844a1e 2019-08-13 stsp }
1927 52b5abe1 2019-08-13 stsp }
1928 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1929 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1930 48cae60d 2020-09-22 stsp free(ref_id);
1931 52b5abe1 2019-08-13 stsp if (tag)
1932 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1933 52b5abe1 2019-08-13 stsp if (cmp != 0)
1934 52b5abe1 2019-08-13 stsp continue;
1935 8b473291 2019-02-21 stsp s = *refs_str;
1936 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1937 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1938 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1939 8b473291 2019-02-21 stsp free(s);
1940 8b473291 2019-02-21 stsp *refs_str = NULL;
1941 8b473291 2019-02-21 stsp break;
1942 8b473291 2019-02-21 stsp }
1943 8b473291 2019-02-21 stsp free(s);
1944 8b473291 2019-02-21 stsp }
1945 8b473291 2019-02-21 stsp
1946 8b473291 2019-02-21 stsp return err;
1947 8b473291 2019-02-21 stsp }
1948 8b473291 2019-02-21 stsp
1949 963b370f 2018-05-20 stsp static const struct got_error *
1950 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1951 27a741e5 2019-09-11 stsp int col_tab_align)
1952 5813d178 2019-03-09 stsp {
1953 e6b8b890 2020-12-29 naddy char *smallerthan;
1954 5813d178 2019-03-09 stsp
1955 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1956 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1957 5813d178 2019-03-09 stsp author = smallerthan + 1;
1958 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1959 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1960 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1961 5813d178 2019-03-09 stsp }
1962 5813d178 2019-03-09 stsp
1963 5813d178 2019-03-09 stsp static const struct got_error *
1964 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1965 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1966 8fdc79fe 2020-12-01 naddy int author_display_cols)
1967 80ddbec8 2018-04-29 stsp {
1968 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1969 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1970 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1971 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1972 5813d178 2019-03-09 stsp char *author = NULL;
1973 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1974 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1975 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1976 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1977 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1978 ccb26ccd 2018-11-05 stsp struct tm tm;
1979 45d799e2 2018-12-23 stsp time_t committer_time;
1980 11b20872 2019-11-08 stsp struct tog_color *tc;
1981 80ddbec8 2018-04-29 stsp
1982 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1983 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1984 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1985 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1986 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1987 b39d25c7 2018-07-10 stsp
1988 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1989 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1990 b39d25c7 2018-07-10 stsp else
1991 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1992 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1993 11b20872 2019-11-08 stsp if (tc)
1994 11b20872 2019-11-08 stsp wattr_on(view->window,
1995 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1996 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1997 11b20872 2019-11-08 stsp if (tc)
1998 11b20872 2019-11-08 stsp wattr_off(view->window,
1999 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2000 27a741e5 2019-09-11 stsp col = limit;
2001 b39d25c7 2018-07-10 stsp if (col > avail)
2002 b39d25c7 2018-07-10 stsp goto done;
2003 6570a66d 2019-11-08 stsp
2004 6570a66d 2019-11-08 stsp if (avail >= 120) {
2005 6570a66d 2019-11-08 stsp char *id_str;
2006 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2007 6570a66d 2019-11-08 stsp if (err)
2008 6570a66d 2019-11-08 stsp goto done;
2009 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_on(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2014 11b20872 2019-11-08 stsp if (tc)
2015 11b20872 2019-11-08 stsp wattr_off(view->window,
2016 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2017 6570a66d 2019-11-08 stsp free(id_str);
2018 6570a66d 2019-11-08 stsp col += 9;
2019 6570a66d 2019-11-08 stsp if (col > avail)
2020 6570a66d 2019-11-08 stsp goto done;
2021 6570a66d 2019-11-08 stsp }
2022 b39d25c7 2018-07-10 stsp
2023 10aab77f 2022-07-19 op if (s->use_committer)
2024 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2025 10aab77f 2022-07-19 op else
2026 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2027 5813d178 2019-03-09 stsp if (author == NULL) {
2028 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2029 80ddbec8 2018-04-29 stsp goto done;
2030 80ddbec8 2018-04-29 stsp }
2031 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2032 bb737323 2018-05-20 stsp if (err)
2033 bb737323 2018-05-20 stsp goto done;
2034 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2035 11b20872 2019-11-08 stsp if (tc)
2036 11b20872 2019-11-08 stsp wattr_on(view->window,
2037 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2038 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2039 11b20872 2019-11-08 stsp if (tc)
2040 11b20872 2019-11-08 stsp wattr_off(view->window,
2041 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2042 bb737323 2018-05-20 stsp col += author_width;
2043 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2044 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2045 bb737323 2018-05-20 stsp col++;
2046 bb737323 2018-05-20 stsp author_width++;
2047 bb737323 2018-05-20 stsp }
2048 9c2eaf34 2018-05-20 stsp if (col > avail)
2049 9c2eaf34 2018-05-20 stsp goto done;
2050 80ddbec8 2018-04-29 stsp
2051 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2052 5943eee2 2019-08-13 stsp if (err)
2053 6d9fbc00 2018-04-29 stsp goto done;
2054 bb737323 2018-05-20 stsp logmsg = logmsg0;
2055 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2056 bb737323 2018-05-20 stsp logmsg++;
2057 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2058 bb737323 2018-05-20 stsp if (newline)
2059 bb737323 2018-05-20 stsp *newline = '\0';
2060 ccda2f4d 2022-06-16 stsp limit = avail - col;
2061 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2062 4d1f6af3 2022-06-17 op limit--; /* for the border */
2063 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2064 ccda2f4d 2022-06-16 stsp limit, col, 1);
2065 29688b02 2022-06-16 stsp if (err)
2066 29688b02 2022-06-16 stsp goto done;
2067 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2068 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2069 27a741e5 2019-09-11 stsp while (col < avail) {
2070 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2071 bb737323 2018-05-20 stsp col++;
2072 881b2d3e 2018-04-30 stsp }
2073 80ddbec8 2018-04-29 stsp done:
2074 80ddbec8 2018-04-29 stsp free(logmsg0);
2075 bb737323 2018-05-20 stsp free(wlogmsg);
2076 5813d178 2019-03-09 stsp free(author);
2077 bb737323 2018-05-20 stsp free(wauthor);
2078 80ddbec8 2018-04-29 stsp free(line);
2079 80ddbec8 2018-04-29 stsp return err;
2080 80ddbec8 2018-04-29 stsp }
2081 26ed57b2 2018-05-19 stsp
2082 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2083 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2084 899d86c2 2018-05-10 stsp struct got_object_id *id)
2085 80ddbec8 2018-04-29 stsp {
2086 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2087 80ddbec8 2018-04-29 stsp
2088 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2089 80ddbec8 2018-04-29 stsp if (entry == NULL)
2090 899d86c2 2018-05-10 stsp return NULL;
2091 99db9666 2018-05-07 stsp
2092 899d86c2 2018-05-10 stsp entry->id = id;
2093 99db9666 2018-05-07 stsp entry->commit = commit;
2094 899d86c2 2018-05-10 stsp return entry;
2095 99db9666 2018-05-07 stsp }
2096 80ddbec8 2018-04-29 stsp
2097 99db9666 2018-05-07 stsp static void
2098 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2099 99db9666 2018-05-07 stsp {
2100 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2101 99db9666 2018-05-07 stsp
2102 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2103 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2104 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2105 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2106 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2107 99db9666 2018-05-07 stsp free(entry);
2108 99db9666 2018-05-07 stsp }
2109 99db9666 2018-05-07 stsp
2110 99db9666 2018-05-07 stsp static void
2111 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2112 99db9666 2018-05-07 stsp {
2113 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2114 99db9666 2018-05-07 stsp pop_commit(commits);
2115 c4972b91 2018-05-07 stsp }
2116 c4972b91 2018-05-07 stsp
2117 c4972b91 2018-05-07 stsp static const struct got_error *
2118 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2119 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2120 13add988 2019-10-15 stsp {
2121 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2122 13add988 2019-10-15 stsp regmatch_t regmatch;
2123 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2124 13add988 2019-10-15 stsp
2125 13add988 2019-10-15 stsp *have_match = 0;
2126 13add988 2019-10-15 stsp
2127 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2128 13add988 2019-10-15 stsp if (err)
2129 13add988 2019-10-15 stsp return err;
2130 13add988 2019-10-15 stsp
2131 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2132 13add988 2019-10-15 stsp if (err)
2133 13add988 2019-10-15 stsp goto done;
2134 13add988 2019-10-15 stsp
2135 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2136 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2137 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2138 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2139 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2140 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2141 13add988 2019-10-15 stsp *have_match = 1;
2142 13add988 2019-10-15 stsp done:
2143 13add988 2019-10-15 stsp free(id_str);
2144 13add988 2019-10-15 stsp free(logmsg);
2145 13add988 2019-10-15 stsp return err;
2146 13add988 2019-10-15 stsp }
2147 13add988 2019-10-15 stsp
2148 13add988 2019-10-15 stsp static const struct got_error *
2149 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2150 c4972b91 2018-05-07 stsp {
2151 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2152 9ba79e04 2018-06-11 stsp
2153 1a76625f 2018-10-22 stsp /*
2154 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2155 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2156 1a76625f 2018-10-22 stsp * while updating the display.
2157 1a76625f 2018-10-22 stsp */
2158 4e0d2870 2020-12-07 naddy do {
2159 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2160 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2161 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2162 1a76625f 2018-10-22 stsp int errcode;
2163 899d86c2 2018-05-10 stsp
2164 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2165 4e0d2870 2020-12-07 naddy NULL, NULL);
2166 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2167 ecb28ae0 2018-07-16 stsp break;
2168 899d86c2 2018-05-10 stsp
2169 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2170 9ba79e04 2018-06-11 stsp if (err)
2171 9ba79e04 2018-06-11 stsp break;
2172 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2173 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2174 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2175 9ba79e04 2018-06-11 stsp break;
2176 9ba79e04 2018-06-11 stsp }
2177 93e45b7c 2018-09-24 stsp
2178 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2179 1a76625f 2018-10-22 stsp if (errcode) {
2180 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2181 13add988 2019-10-15 stsp "pthread_mutex_lock");
2182 1a76625f 2018-10-22 stsp break;
2183 1a76625f 2018-10-22 stsp }
2184 1a76625f 2018-10-22 stsp
2185 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2186 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2187 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2188 1a76625f 2018-10-22 stsp
2189 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2190 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2191 7c1452c1 2020-03-26 stsp int have_match;
2192 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2193 7c1452c1 2020-03-26 stsp if (err)
2194 7c1452c1 2020-03-26 stsp break;
2195 7c1452c1 2020-03-26 stsp if (have_match)
2196 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2197 13add988 2019-10-15 stsp }
2198 13add988 2019-10-15 stsp
2199 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2200 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2201 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2202 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2203 7c1452c1 2020-03-26 stsp if (err)
2204 13add988 2019-10-15 stsp break;
2205 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2206 899d86c2 2018-05-10 stsp
2207 9ba79e04 2018-06-11 stsp return err;
2208 0553a4e3 2018-04-30 stsp }
2209 0553a4e3 2018-04-30 stsp
2210 2b779855 2020-12-05 naddy static void
2211 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2212 2b779855 2020-12-05 naddy {
2213 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2214 2b779855 2020-12-05 naddy int ncommits = 0;
2215 2b779855 2020-12-05 naddy
2216 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2217 2b779855 2020-12-05 naddy while (entry) {
2218 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2219 2b779855 2020-12-05 naddy s->selected_entry = entry;
2220 2b779855 2020-12-05 naddy break;
2221 2b779855 2020-12-05 naddy }
2222 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2223 2b779855 2020-12-05 naddy ncommits++;
2224 2b779855 2020-12-05 naddy }
2225 2b779855 2020-12-05 naddy }
2226 2b779855 2020-12-05 naddy
2227 0553a4e3 2018-04-30 stsp static const struct got_error *
2228 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2229 0553a4e3 2018-04-30 stsp {
2230 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2231 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2232 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2233 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2234 60493ae3 2019-06-20 stsp int width;
2235 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2236 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2237 8b473291 2019-02-21 stsp char *refs_str = NULL;
2238 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2239 11b20872 2019-11-08 stsp struct tog_color *tc;
2240 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2241 0553a4e3 2018-04-30 stsp
2242 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2243 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2244 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2245 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2246 1a76625f 2018-10-22 stsp if (err)
2247 ecb28ae0 2018-07-16 stsp return err;
2248 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2249 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2250 d2075bf3 2020-12-25 stsp if (refs) {
2251 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2252 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2253 d2075bf3 2020-12-25 stsp if (err)
2254 d2075bf3 2020-12-25 stsp goto done;
2255 d2075bf3 2020-12-25 stsp }
2256 867c6645 2018-07-10 stsp }
2257 359bfafd 2019-02-22 stsp
2258 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2259 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2260 1a76625f 2018-10-22 stsp
2261 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2262 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2263 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2264 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2265 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2266 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2267 8f4ed634 2020-03-26 stsp goto done;
2268 8f4ed634 2020-03-26 stsp }
2269 8f4ed634 2020-03-26 stsp } else {
2270 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2271 f9686aa5 2020-03-27 stsp
2272 f9686aa5 2020-03-27 stsp if (view->searching) {
2273 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2274 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2275 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2276 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2277 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2278 f9686aa5 2020-03-27 stsp search_str = "searching...";
2279 f9686aa5 2020-03-27 stsp }
2280 f9686aa5 2020-03-27 stsp
2281 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2282 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2283 f9686aa5 2020-03-27 stsp search_str ? search_str :
2284 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2285 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2286 8f4ed634 2020-03-26 stsp goto done;
2287 8f4ed634 2020-03-26 stsp }
2288 8b473291 2019-02-21 stsp }
2289 1a76625f 2018-10-22 stsp
2290 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2291 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2292 87411fa9 2022-06-16 stsp "........................................",
2293 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2294 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2295 1a76625f 2018-10-22 stsp header = NULL;
2296 1a76625f 2018-10-22 stsp goto done;
2297 1a76625f 2018-10-22 stsp }
2298 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2299 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2300 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2301 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2302 1a76625f 2018-10-22 stsp header = NULL;
2303 1a76625f 2018-10-22 stsp goto done;
2304 ecb28ae0 2018-07-16 stsp }
2305 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2306 1a76625f 2018-10-22 stsp if (err)
2307 1a76625f 2018-10-22 stsp goto done;
2308 867c6645 2018-07-10 stsp
2309 2814baeb 2018-08-01 stsp werase(view->window);
2310 867c6645 2018-07-10 stsp
2311 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2312 a3404814 2018-09-02 stsp wstandout(view->window);
2313 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2314 11b20872 2019-11-08 stsp if (tc)
2315 11b20872 2019-11-08 stsp wattr_on(view->window,
2316 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2317 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2318 11b20872 2019-11-08 stsp if (tc)
2319 11b20872 2019-11-08 stsp wattr_off(view->window,
2320 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2321 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2322 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2323 1a76625f 2018-10-22 stsp width++;
2324 1a76625f 2018-10-22 stsp }
2325 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2326 a3404814 2018-09-02 stsp wstandend(view->window);
2327 ecb28ae0 2018-07-16 stsp free(wline);
2328 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2329 1a76625f 2018-10-22 stsp goto done;
2330 0553a4e3 2018-04-30 stsp
2331 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2332 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2333 5813d178 2019-03-09 stsp ncommits = 0;
2334 145b6838 2022-06-16 stsp view->maxx = 0;
2335 5813d178 2019-03-09 stsp while (entry) {
2336 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2337 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2338 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2339 5813d178 2019-03-09 stsp int width;
2340 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2341 5813d178 2019-03-09 stsp break;
2342 10aab77f 2022-07-19 op if (s->use_committer)
2343 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2344 10aab77f 2022-07-19 op else
2345 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2346 5813d178 2019-03-09 stsp if (author == NULL) {
2347 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2348 5813d178 2019-03-09 stsp goto done;
2349 5813d178 2019-03-09 stsp }
2350 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2351 27a741e5 2019-09-11 stsp date_display_cols);
2352 5813d178 2019-03-09 stsp if (author_cols < width)
2353 5813d178 2019-03-09 stsp author_cols = width;
2354 5813d178 2019-03-09 stsp free(wauthor);
2355 5813d178 2019-03-09 stsp free(author);
2356 a310d9c3 2022-07-21 florian if (err)
2357 a310d9c3 2022-07-21 florian goto done;
2358 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2359 145b6838 2022-06-16 stsp if (err)
2360 145b6838 2022-06-16 stsp goto done;
2361 145b6838 2022-06-16 stsp msg = msg0;
2362 145b6838 2022-06-16 stsp while (*msg == '\n')
2363 145b6838 2022-06-16 stsp ++msg;
2364 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2365 29688b02 2022-06-16 stsp *eol = '\0';
2366 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2367 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2368 29688b02 2022-06-16 stsp if (err)
2369 29688b02 2022-06-16 stsp goto done;
2370 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2371 145b6838 2022-06-16 stsp free(msg0);
2372 29688b02 2022-06-16 stsp free(wmsg);
2373 7ca04879 2019-10-19 stsp ncommits++;
2374 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2375 5813d178 2019-03-09 stsp }
2376 5813d178 2019-03-09 stsp
2377 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2378 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2379 867c6645 2018-07-10 stsp ncommits = 0;
2380 899d86c2 2018-05-10 stsp while (entry) {
2381 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2382 899d86c2 2018-05-10 stsp break;
2383 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2384 2814baeb 2018-08-01 stsp wstandout(view->window);
2385 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2386 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2387 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2388 2814baeb 2018-08-01 stsp wstandend(view->window);
2389 0553a4e3 2018-04-30 stsp if (err)
2390 60493ae3 2019-06-20 stsp goto done;
2391 0553a4e3 2018-04-30 stsp ncommits++;
2392 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2393 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2394 80ddbec8 2018-04-29 stsp }
2395 80ddbec8 2018-04-29 stsp
2396 9b058f45 2022-06-30 mark view_border(view);
2397 1a76625f 2018-10-22 stsp done:
2398 1a76625f 2018-10-22 stsp free(id_str);
2399 8b473291 2019-02-21 stsp free(refs_str);
2400 1a76625f 2018-10-22 stsp free(ncommits_str);
2401 1a76625f 2018-10-22 stsp free(header);
2402 80ddbec8 2018-04-29 stsp return err;
2403 9f7d7167 2018-04-29 stsp }
2404 07b55e75 2018-05-10 stsp
2405 07b55e75 2018-05-10 stsp static void
2406 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2407 07b55e75 2018-05-10 stsp {
2408 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2409 07b55e75 2018-05-10 stsp int nscrolled = 0;
2410 07b55e75 2018-05-10 stsp
2411 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2412 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2413 07b55e75 2018-05-10 stsp return;
2414 9f7d7167 2018-04-29 stsp
2415 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2416 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2417 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2418 07b55e75 2018-05-10 stsp if (entry) {
2419 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2420 07b55e75 2018-05-10 stsp nscrolled++;
2421 07b55e75 2018-05-10 stsp }
2422 07b55e75 2018-05-10 stsp }
2423 aa075928 2018-05-10 stsp }
2424 aa075928 2018-05-10 stsp
2425 aa075928 2018-05-10 stsp static const struct got_error *
2426 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2427 aa075928 2018-05-10 stsp {
2428 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2429 5e224a3e 2019-02-22 stsp int errcode;
2430 8a42fca8 2019-02-22 stsp
2431 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2432 aa075928 2018-05-10 stsp
2433 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2434 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2435 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2436 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2437 7aafa0d1 2019-02-22 stsp if (errcode)
2438 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2439 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2440 7c1452c1 2020-03-26 stsp
2441 7c1452c1 2020-03-26 stsp /*
2442 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2443 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2444 7c1452c1 2020-03-26 stsp */
2445 7c1452c1 2020-03-26 stsp if (!wait)
2446 7c1452c1 2020-03-26 stsp break;
2447 7c1452c1 2020-03-26 stsp
2448 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2449 ffe38506 2020-12-01 naddy show_log_view(view);
2450 7c1452c1 2020-03-26 stsp update_panels();
2451 7c1452c1 2020-03-26 stsp doupdate();
2452 7c1452c1 2020-03-26 stsp
2453 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2454 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2455 82954512 2020-02-03 stsp if (errcode)
2456 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2457 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2458 82954512 2020-02-03 stsp
2459 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2460 ffe38506 2020-12-01 naddy show_log_view(view);
2461 7c1452c1 2020-03-26 stsp update_panels();
2462 7c1452c1 2020-03-26 stsp doupdate();
2463 5e224a3e 2019-02-22 stsp }
2464 5e224a3e 2019-02-22 stsp
2465 5e224a3e 2019-02-22 stsp return NULL;
2466 5e224a3e 2019-02-22 stsp }
2467 5e224a3e 2019-02-22 stsp
2468 5e224a3e 2019-02-22 stsp static const struct got_error *
2469 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2470 9b058f45 2022-06-30 mark {
2471 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2472 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2473 2525dccb 2022-07-13 mark
2474 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2475 2525dccb 2022-07-13 mark return NULL;
2476 9b058f45 2022-06-30 mark
2477 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2478 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2479 9b058f45 2022-06-30 mark view->nscrolled = 0;
2480 9b058f45 2022-06-30 mark
2481 9b058f45 2022-06-30 mark return err;
2482 9b058f45 2022-06-30 mark }
2483 9b058f45 2022-06-30 mark
2484 9b058f45 2022-06-30 mark static const struct got_error *
2485 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2486 5e224a3e 2019-02-22 stsp {
2487 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2488 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2489 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2490 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2491 5e224a3e 2019-02-22 stsp
2492 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2493 5e224a3e 2019-02-22 stsp return NULL;
2494 5e224a3e 2019-02-22 stsp
2495 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2496 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2497 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2498 08ebd0a9 2019-02-22 stsp /*
2499 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2500 08ebd0a9 2019-02-22 stsp */
2501 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2502 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2503 5e224a3e 2019-02-22 stsp if (err)
2504 5e224a3e 2019-02-22 stsp return err;
2505 7aafa0d1 2019-02-22 stsp }
2506 b295e71b 2019-02-22 stsp
2507 7aafa0d1 2019-02-22 stsp do {
2508 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2509 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2510 88048b54 2019-02-21 stsp break;
2511 88048b54 2019-02-21 stsp
2512 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2513 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2514 aa075928 2018-05-10 stsp
2515 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2516 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2517 dd0a52c1 2018-05-20 stsp break;
2518 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2519 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2520 aa075928 2018-05-10 stsp
2521 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2522 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2523 9b058f45 2022-06-30 mark else
2524 9b058f45 2022-06-30 mark view->nscrolled = 0;
2525 9b058f45 2022-06-30 mark
2526 dd0a52c1 2018-05-20 stsp return err;
2527 07b55e75 2018-05-10 stsp }
2528 4a7f7875 2018-05-10 stsp
2529 cd0acaa7 2018-05-20 stsp static const struct got_error *
2530 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2531 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2532 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2533 cd0acaa7 2018-05-20 stsp {
2534 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2535 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2536 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2537 cd0acaa7 2018-05-20 stsp
2538 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2539 15a94983 2018-12-23 stsp if (diff_view == NULL)
2540 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2541 ea5e7bb5 2018-08-01 stsp
2542 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2543 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2544 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2545 e5a0f69f 2018-08-18 stsp if (err == NULL)
2546 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2547 cd0acaa7 2018-05-20 stsp return err;
2548 4a7f7875 2018-05-10 stsp }
2549 4a7f7875 2018-05-10 stsp
2550 80ddbec8 2018-04-29 stsp static const struct got_error *
2551 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2552 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2553 9343a5fb 2018-06-23 stsp {
2554 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2555 941e9f74 2019-05-21 stsp
2556 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2557 941e9f74 2019-05-21 stsp if (parent == NULL)
2558 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2559 941e9f74 2019-05-21 stsp
2560 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2561 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2562 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2563 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2564 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2565 941e9f74 2019-05-21 stsp s->tree = subtree;
2566 941e9f74 2019-05-21 stsp s->selected = 0;
2567 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2568 941e9f74 2019-05-21 stsp return NULL;
2569 941e9f74 2019-05-21 stsp }
2570 941e9f74 2019-05-21 stsp
2571 941e9f74 2019-05-21 stsp static const struct got_error *
2572 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2573 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2574 941e9f74 2019-05-21 stsp {
2575 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2576 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2577 941e9f74 2019-05-21 stsp const char *p;
2578 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2579 9343a5fb 2018-06-23 stsp
2580 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2581 941e9f74 2019-05-21 stsp p = path;
2582 941e9f74 2019-05-21 stsp while (*p) {
2583 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2584 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2585 56e0773d 2019-11-28 stsp char *te_name;
2586 33cbf02b 2020-01-12 stsp
2587 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2588 33cbf02b 2020-01-12 stsp p++;
2589 941e9f74 2019-05-21 stsp
2590 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2591 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2592 941e9f74 2019-05-21 stsp if (slash == NULL)
2593 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2594 33cbf02b 2020-01-12 stsp else
2595 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2596 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2597 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2598 56e0773d 2019-11-28 stsp break;
2599 941e9f74 2019-05-21 stsp }
2600 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2601 56e0773d 2019-11-28 stsp if (te == NULL) {
2602 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2603 56e0773d 2019-11-28 stsp free(te_name);
2604 941e9f74 2019-05-21 stsp break;
2605 941e9f74 2019-05-21 stsp }
2606 56e0773d 2019-11-28 stsp free(te_name);
2607 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2608 941e9f74 2019-05-21 stsp
2609 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2610 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2611 b03c880f 2019-05-21 stsp
2612 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2613 941e9f74 2019-05-21 stsp if (slash)
2614 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2615 941e9f74 2019-05-21 stsp else
2616 941e9f74 2019-05-21 stsp subpath = strdup(path);
2617 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2618 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2619 941e9f74 2019-05-21 stsp break;
2620 941e9f74 2019-05-21 stsp }
2621 941e9f74 2019-05-21 stsp
2622 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2623 941e9f74 2019-05-21 stsp subpath);
2624 941e9f74 2019-05-21 stsp if (err)
2625 941e9f74 2019-05-21 stsp break;
2626 941e9f74 2019-05-21 stsp
2627 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2628 941e9f74 2019-05-21 stsp free(tree_id);
2629 941e9f74 2019-05-21 stsp if (err)
2630 941e9f74 2019-05-21 stsp break;
2631 941e9f74 2019-05-21 stsp
2632 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2633 941e9f74 2019-05-21 stsp if (err) {
2634 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2635 941e9f74 2019-05-21 stsp break;
2636 941e9f74 2019-05-21 stsp }
2637 941e9f74 2019-05-21 stsp if (slash == NULL)
2638 941e9f74 2019-05-21 stsp break;
2639 941e9f74 2019-05-21 stsp free(subpath);
2640 941e9f74 2019-05-21 stsp subpath = NULL;
2641 941e9f74 2019-05-21 stsp p = slash;
2642 941e9f74 2019-05-21 stsp }
2643 941e9f74 2019-05-21 stsp
2644 941e9f74 2019-05-21 stsp free(subpath);
2645 1a76625f 2018-10-22 stsp return err;
2646 61266923 2020-01-14 stsp }
2647 61266923 2020-01-14 stsp
2648 61266923 2020-01-14 stsp static const struct got_error *
2649 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2650 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2651 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2652 55cccc34 2020-02-20 stsp {
2653 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2654 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2655 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2656 55cccc34 2020-02-20 stsp
2657 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2658 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2659 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2660 55cccc34 2020-02-20 stsp
2661 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2662 bc573f3b 2021-07-10 stsp if (err)
2663 55cccc34 2020-02-20 stsp return err;
2664 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2665 55cccc34 2020-02-20 stsp
2666 55cccc34 2020-02-20 stsp *new_view = tree_view;
2667 55cccc34 2020-02-20 stsp
2668 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2669 55cccc34 2020-02-20 stsp return NULL;
2670 55cccc34 2020-02-20 stsp
2671 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2672 55cccc34 2020-02-20 stsp }
2673 55cccc34 2020-02-20 stsp
2674 55cccc34 2020-02-20 stsp static const struct got_error *
2675 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2676 61266923 2020-01-14 stsp {
2677 61266923 2020-01-14 stsp sigset_t sigset;
2678 61266923 2020-01-14 stsp int errcode;
2679 61266923 2020-01-14 stsp
2680 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2681 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2682 61266923 2020-01-14 stsp
2683 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2684 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2685 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2686 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2687 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2688 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2689 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2690 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2691 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2692 61266923 2020-01-14 stsp
2693 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2694 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2695 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2696 61266923 2020-01-14 stsp
2697 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2698 61266923 2020-01-14 stsp if (errcode)
2699 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2700 61266923 2020-01-14 stsp
2701 61266923 2020-01-14 stsp return NULL;
2702 1a76625f 2018-10-22 stsp }
2703 1a76625f 2018-10-22 stsp
2704 1a76625f 2018-10-22 stsp static void *
2705 1a76625f 2018-10-22 stsp log_thread(void *arg)
2706 1a76625f 2018-10-22 stsp {
2707 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2708 1a76625f 2018-10-22 stsp int errcode = 0;
2709 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2710 1a76625f 2018-10-22 stsp int done = 0;
2711 61266923 2020-01-14 stsp
2712 5629093a 2022-07-11 stsp /*
2713 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2714 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2715 5629093a 2022-07-11 stsp */
2716 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2717 5629093a 2022-07-11 stsp if (errcode) {
2718 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2719 61266923 2020-01-14 stsp return (void *)err;
2720 5629093a 2022-07-11 stsp }
2721 1a76625f 2018-10-22 stsp
2722 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2723 5629093a 2022-07-11 stsp if (err) {
2724 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2725 5629093a 2022-07-11 stsp goto done;
2726 5629093a 2022-07-11 stsp }
2727 5629093a 2022-07-11 stsp
2728 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2729 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2730 5629093a 2022-07-11 stsp if (errcode) {
2731 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2732 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2733 5629093a 2022-07-11 stsp goto done;
2734 5629093a 2022-07-11 stsp }
2735 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2736 1a76625f 2018-10-22 stsp if (err) {
2737 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2738 5629093a 2022-07-11 stsp goto done;
2739 1a76625f 2018-10-22 stsp err = NULL;
2740 1a76625f 2018-10-22 stsp done = 1;
2741 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2742 1a76625f 2018-10-22 stsp a->commits_needed--;
2743 1a76625f 2018-10-22 stsp
2744 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2745 3abe8080 2019-04-10 stsp if (errcode) {
2746 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2747 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2748 5629093a 2022-07-11 stsp goto done;
2749 3abe8080 2019-04-10 stsp } else if (*a->quit)
2750 1a76625f 2018-10-22 stsp done = 1;
2751 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2752 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2753 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2754 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2755 1a76625f 2018-10-22 stsp }
2756 1a76625f 2018-10-22 stsp
2757 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2758 7c1452c1 2020-03-26 stsp if (errcode) {
2759 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2760 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2761 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2762 5629093a 2022-07-11 stsp goto done;
2763 7c1452c1 2020-03-26 stsp }
2764 7c1452c1 2020-03-26 stsp
2765 1a76625f 2018-10-22 stsp if (done)
2766 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2767 7c1452c1 2020-03-26 stsp else {
2768 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2769 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2770 7c1452c1 2020-03-26 stsp &tog_mutex);
2771 5629093a 2022-07-11 stsp if (errcode) {
2772 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2773 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2774 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2775 5629093a 2022-07-11 stsp goto done;
2776 5629093a 2022-07-11 stsp }
2777 21355643 2020-12-06 stsp if (*a->quit)
2778 21355643 2020-12-06 stsp done = 1;
2779 7c1452c1 2020-03-26 stsp }
2780 1a76625f 2018-10-22 stsp }
2781 1a76625f 2018-10-22 stsp }
2782 3abe8080 2019-04-10 stsp a->log_complete = 1;
2783 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2784 5629093a 2022-07-11 stsp if (errcode)
2785 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2786 5629093a 2022-07-11 stsp done:
2787 5629093a 2022-07-11 stsp if (err) {
2788 5629093a 2022-07-11 stsp tog_thread_error = 1;
2789 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2790 5629093a 2022-07-11 stsp }
2791 1a76625f 2018-10-22 stsp return (void *)err;
2792 1a76625f 2018-10-22 stsp }
2793 1a76625f 2018-10-22 stsp
2794 1a76625f 2018-10-22 stsp static const struct got_error *
2795 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2796 1a76625f 2018-10-22 stsp {
2797 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2798 1a76625f 2018-10-22 stsp int errcode;
2799 1a76625f 2018-10-22 stsp
2800 1a76625f 2018-10-22 stsp if (s->thread) {
2801 1a76625f 2018-10-22 stsp s->quit = 1;
2802 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2803 1a76625f 2018-10-22 stsp if (errcode)
2804 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2805 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2806 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2807 1a76625f 2018-10-22 stsp if (errcode)
2808 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2809 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2810 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2811 1a76625f 2018-10-22 stsp if (errcode)
2812 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2813 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2814 1a76625f 2018-10-22 stsp if (errcode)
2815 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2816 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2817 1a76625f 2018-10-22 stsp s->thread = NULL;
2818 1a76625f 2018-10-22 stsp }
2819 1a76625f 2018-10-22 stsp
2820 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2821 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2822 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2823 74467cc8 2022-06-15 stsp }
2824 74467cc8 2022-06-15 stsp
2825 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2826 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2827 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2828 74467cc8 2022-06-15 stsp if (err == NULL)
2829 74467cc8 2022-06-15 stsp err = pack_err;
2830 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2831 1a76625f 2018-10-22 stsp }
2832 1a76625f 2018-10-22 stsp
2833 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2834 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2835 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2836 1a76625f 2018-10-22 stsp }
2837 1a76625f 2018-10-22 stsp
2838 5629093a 2022-07-11 stsp return err ? err : thread_err;
2839 9343a5fb 2018-06-23 stsp }
2840 9343a5fb 2018-06-23 stsp
2841 9343a5fb 2018-06-23 stsp static const struct got_error *
2842 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2843 1a76625f 2018-10-22 stsp {
2844 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2845 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2846 276b94a1 2020-11-13 naddy int errcode;
2847 1a76625f 2018-10-22 stsp
2848 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2849 276b94a1 2020-11-13 naddy
2850 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2851 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2852 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2853 276b94a1 2020-11-13 naddy
2854 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2855 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2856 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2857 276b94a1 2020-11-13 naddy
2858 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2859 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2860 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2861 1a76625f 2018-10-22 stsp free(s->start_id);
2862 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2863 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2864 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2865 1a76625f 2018-10-22 stsp return err;
2866 1a76625f 2018-10-22 stsp }
2867 1a76625f 2018-10-22 stsp
2868 1a76625f 2018-10-22 stsp static const struct got_error *
2869 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2870 60493ae3 2019-06-20 stsp {
2871 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2872 60493ae3 2019-06-20 stsp
2873 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2874 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2875 60493ae3 2019-06-20 stsp return NULL;
2876 60493ae3 2019-06-20 stsp }
2877 60493ae3 2019-06-20 stsp
2878 60493ae3 2019-06-20 stsp static const struct got_error *
2879 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2880 60493ae3 2019-06-20 stsp {
2881 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2882 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2883 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2884 60493ae3 2019-06-20 stsp
2885 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2886 f9686aa5 2020-03-27 stsp show_log_view(view);
2887 f9686aa5 2020-03-27 stsp update_panels();
2888 f9686aa5 2020-03-27 stsp doupdate();
2889 f9686aa5 2020-03-27 stsp
2890 96e2b566 2019-07-08 stsp if (s->search_entry) {
2891 13add988 2019-10-15 stsp int errcode, ch;
2892 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2893 13add988 2019-10-15 stsp if (errcode)
2894 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2895 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2896 13add988 2019-10-15 stsp ch = wgetch(view->window);
2897 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2898 13add988 2019-10-15 stsp if (errcode)
2899 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2900 13add988 2019-10-15 stsp "pthread_mutex_lock");
2901 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2902 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2903 678cbce5 2019-07-28 stsp return NULL;
2904 678cbce5 2019-07-28 stsp }
2905 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2906 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2907 96e2b566 2019-07-08 stsp else
2908 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2909 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2910 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2911 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2912 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2913 364ac6fd 2022-06-18 stsp
2914 364ac6fd 2022-06-18 stsp /*
2915 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2916 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2917 f704b35c 2022-06-18 stsp * might have changed.
2918 364ac6fd 2022-06-18 stsp */
2919 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2920 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2921 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2922 364ac6fd 2022-06-18 stsp else
2923 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2924 4bfe9f0a 2022-06-18 stsp } else {
2925 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2926 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2927 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2928 364ac6fd 2022-06-18 stsp else
2929 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2930 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2931 4bfe9f0a 2022-06-18 stsp }
2932 20be8d96 2019-06-21 stsp } else {
2933 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2934 20be8d96 2019-06-21 stsp }
2935 60493ae3 2019-06-20 stsp
2936 60493ae3 2019-06-20 stsp while (1) {
2937 13add988 2019-10-15 stsp int have_match = 0;
2938 13add988 2019-10-15 stsp
2939 60493ae3 2019-06-20 stsp if (entry == NULL) {
2940 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2941 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2942 f9967bca 2020-03-27 stsp view->search_next_done =
2943 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2944 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2945 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2946 f801134a 2019-06-25 stsp return NULL;
2947 60493ae3 2019-06-20 stsp }
2948 96e2b566 2019-07-08 stsp /*
2949 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2950 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2951 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2952 96e2b566 2019-07-08 stsp */
2953 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2954 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2955 60493ae3 2019-06-20 stsp }
2956 60493ae3 2019-06-20 stsp
2957 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2958 13add988 2019-10-15 stsp &view->regex);
2959 5943eee2 2019-08-13 stsp if (err)
2960 13add988 2019-10-15 stsp break;
2961 13add988 2019-10-15 stsp if (have_match) {
2962 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2963 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2964 60493ae3 2019-06-20 stsp break;
2965 60493ae3 2019-06-20 stsp }
2966 13add988 2019-10-15 stsp
2967 96e2b566 2019-07-08 stsp s->search_entry = entry;
2968 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2969 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2970 b1bf1435 2019-06-21 stsp else
2971 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2972 60493ae3 2019-06-20 stsp }
2973 60493ae3 2019-06-20 stsp
2974 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2975 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2976 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2977 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2978 60493ae3 2019-06-20 stsp if (err)
2979 60493ae3 2019-06-20 stsp return err;
2980 ead14cbe 2019-06-21 stsp cur++;
2981 ead14cbe 2019-06-21 stsp }
2982 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2983 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2984 60493ae3 2019-06-20 stsp if (err)
2985 60493ae3 2019-06-20 stsp return err;
2986 ead14cbe 2019-06-21 stsp cur--;
2987 60493ae3 2019-06-20 stsp }
2988 60493ae3 2019-06-20 stsp }
2989 60493ae3 2019-06-20 stsp
2990 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2991 96e2b566 2019-07-08 stsp
2992 60493ae3 2019-06-20 stsp return NULL;
2993 60493ae3 2019-06-20 stsp }
2994 60493ae3 2019-06-20 stsp
2995 60493ae3 2019-06-20 stsp static const struct got_error *
2996 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2997 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2998 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2999 80ddbec8 2018-04-29 stsp {
3000 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3001 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3002 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3003 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3004 1a76625f 2018-10-22 stsp int errcode;
3005 80ddbec8 2018-04-29 stsp
3006 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3007 f135c941 2020-02-20 stsp free(s->in_repo_path);
3008 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3009 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3010 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3011 f135c941 2020-02-20 stsp }
3012 ecb28ae0 2018-07-16 stsp
3013 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3014 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3015 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3016 78756c87 2020-11-24 stsp
3017 fb2756b9 2018-08-04 stsp s->repo = repo;
3018 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3019 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3020 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3021 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3022 9cd7cbd1 2020-12-07 stsp goto done;
3023 9cd7cbd1 2020-12-07 stsp }
3024 9cd7cbd1 2020-12-07 stsp }
3025 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3026 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3027 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3028 5036bf37 2018-09-24 stsp goto done;
3029 5036bf37 2018-09-24 stsp }
3030 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3031 e5a0f69f 2018-08-18 stsp
3032 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3033 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3034 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3035 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3036 11b20872 2019-11-08 stsp if (err)
3037 11b20872 2019-11-08 stsp goto done;
3038 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3039 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3040 11b20872 2019-11-08 stsp if (err) {
3041 11b20872 2019-11-08 stsp free_colors(&s->colors);
3042 11b20872 2019-11-08 stsp goto done;
3043 11b20872 2019-11-08 stsp }
3044 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3045 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3046 11b20872 2019-11-08 stsp if (err) {
3047 11b20872 2019-11-08 stsp free_colors(&s->colors);
3048 11b20872 2019-11-08 stsp goto done;
3049 11b20872 2019-11-08 stsp }
3050 11b20872 2019-11-08 stsp }
3051 11b20872 2019-11-08 stsp
3052 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3053 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3054 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3055 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3056 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3057 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3058 1a76625f 2018-10-22 stsp
3059 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3060 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3061 74467cc8 2022-06-15 stsp if (err)
3062 74467cc8 2022-06-15 stsp goto done;
3063 74467cc8 2022-06-15 stsp }
3064 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3065 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3066 0ae84acc 2022-06-15 tracey if (err)
3067 0ae84acc 2022-06-15 tracey goto done;
3068 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3069 b672a97a 2020-01-27 stsp !s->log_branches);
3070 1a76625f 2018-10-22 stsp if (err)
3071 1a76625f 2018-10-22 stsp goto done;
3072 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3073 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3074 c5b78334 2020-01-12 stsp if (err)
3075 c5b78334 2020-01-12 stsp goto done;
3076 1a76625f 2018-10-22 stsp
3077 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3078 1a76625f 2018-10-22 stsp if (errcode) {
3079 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3080 1a76625f 2018-10-22 stsp goto done;
3081 1a76625f 2018-10-22 stsp }
3082 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3083 7c1452c1 2020-03-26 stsp if (errcode) {
3084 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3085 7c1452c1 2020-03-26 stsp goto done;
3086 7c1452c1 2020-03-26 stsp }
3087 1a76625f 2018-10-22 stsp
3088 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3089 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3090 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3091 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3092 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3093 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3094 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3095 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3096 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3097 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3098 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3099 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3100 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3101 ba4f502b 2018-08-04 stsp done:
3102 1a76625f 2018-10-22 stsp if (err)
3103 1a76625f 2018-10-22 stsp close_log_view(view);
3104 ba4f502b 2018-08-04 stsp return err;
3105 ba4f502b 2018-08-04 stsp }
3106 ba4f502b 2018-08-04 stsp
3107 e5a0f69f 2018-08-18 stsp static const struct got_error *
3108 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3109 ba4f502b 2018-08-04 stsp {
3110 f2f6d207 2020-11-24 stsp const struct got_error *err;
3111 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3112 ba4f502b 2018-08-04 stsp
3113 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3114 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3115 2b380cc8 2018-10-24 stsp &s->thread_args);
3116 2b380cc8 2018-10-24 stsp if (errcode)
3117 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3118 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3119 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3120 f2f6d207 2020-11-24 stsp if (err)
3121 f2f6d207 2020-11-24 stsp return err;
3122 f2f6d207 2020-11-24 stsp }
3123 2b380cc8 2018-10-24 stsp }
3124 2b380cc8 2018-10-24 stsp
3125 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3126 b880cc75 2022-06-30 mark }
3127 b880cc75 2022-06-30 mark
3128 b880cc75 2022-06-30 mark static void
3129 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3130 b880cc75 2022-06-30 mark {
3131 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3132 b880cc75 2022-06-30 mark
3133 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3134 b880cc75 2022-06-30 mark view->count = 0;
3135 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3136 b880cc75 2022-06-30 mark return;
3137 b880cc75 2022-06-30 mark
3138 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3139 b880cc75 2022-06-30 mark || home)
3140 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3141 b880cc75 2022-06-30 mark
3142 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3143 b880cc75 2022-06-30 mark --s->selected;
3144 b880cc75 2022-06-30 mark else
3145 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3146 b880cc75 2022-06-30 mark
3147 b880cc75 2022-06-30 mark select_commit(s);
3148 b880cc75 2022-06-30 mark return;
3149 b880cc75 2022-06-30 mark }
3150 b880cc75 2022-06-30 mark
3151 b880cc75 2022-06-30 mark static const struct got_error *
3152 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3153 b880cc75 2022-06-30 mark {
3154 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3155 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3156 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3157 b880cc75 2022-06-30 mark
3158 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3159 b880cc75 2022-06-30 mark if (first == NULL) {
3160 b880cc75 2022-06-30 mark view->count = 0;
3161 b880cc75 2022-06-30 mark return NULL;
3162 b880cc75 2022-06-30 mark }
3163 b880cc75 2022-06-30 mark
3164 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3165 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3166 b880cc75 2022-06-30 mark return NULL;
3167 b880cc75 2022-06-30 mark
3168 b880cc75 2022-06-30 mark if (!page) {
3169 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3170 b880cc75 2022-06-30 mark
3171 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3172 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3173 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3174 b880cc75 2022-06-30 mark ++s->selected;
3175 b880cc75 2022-06-30 mark else
3176 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3177 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3178 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3179 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3180 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3181 b880cc75 2022-06-30 mark else
3182 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3183 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3184 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3185 b880cc75 2022-06-30 mark } else {
3186 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3187 b880cc75 2022-06-30 mark if (err)
3188 b880cc75 2022-06-30 mark return err;
3189 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3190 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3191 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3192 b880cc75 2022-06-30 mark }
3193 b880cc75 2022-06-30 mark }
3194 b880cc75 2022-06-30 mark if (err)
3195 b880cc75 2022-06-30 mark return err;
3196 b880cc75 2022-06-30 mark
3197 9b058f45 2022-06-30 mark /*
3198 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3199 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3200 9b058f45 2022-06-30 mark */
3201 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3202 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3203 9b058f45 2022-06-30 mark
3204 b880cc75 2022-06-30 mark select_commit(s);
3205 b880cc75 2022-06-30 mark
3206 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3207 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3208 b880cc75 2022-06-30 mark view->count = 0;
3209 b880cc75 2022-06-30 mark
3210 b880cc75 2022-06-30 mark return NULL;
3211 e5a0f69f 2018-08-18 stsp }
3212 04cc582a 2018-08-01 stsp
3213 9b058f45 2022-06-30 mark static void
3214 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3215 9b058f45 2022-06-30 mark {
3216 76364b2d 2022-07-02 mark *x = 0;
3217 76364b2d 2022-07-02 mark *y = 0;
3218 76364b2d 2022-07-02 mark
3219 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3220 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3221 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3222 7532ccda 2022-07-11 mark else if (view->resized_y)
3223 7532ccda 2022-07-11 mark *y = view->resized_y;
3224 3c1dfe12 2022-07-08 mark else
3225 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3226 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3227 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3228 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3229 7532ccda 2022-07-11 mark else if (view->resized_x)
3230 7532ccda 2022-07-11 mark *x = view->resized_x;
3231 3c1dfe12 2022-07-08 mark else
3232 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3233 3c1dfe12 2022-07-08 mark }
3234 9b058f45 2022-06-30 mark }
3235 9b058f45 2022-06-30 mark
3236 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3237 e5a0f69f 2018-08-18 stsp static const struct got_error *
3238 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3239 9b058f45 2022-06-30 mark {
3240 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3241 9b058f45 2022-06-30 mark
3242 9b058f45 2022-06-30 mark view->nlines = y;
3243 d2366e29 2022-07-07 mark view->ncols = COLS;
3244 9b058f45 2022-06-30 mark err = view_resize(view);
3245 9b058f45 2022-06-30 mark if (err)
3246 9b058f45 2022-06-30 mark return err;
3247 9b058f45 2022-06-30 mark
3248 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3249 9b058f45 2022-06-30 mark
3250 9b058f45 2022-06-30 mark return err;
3251 9b058f45 2022-06-30 mark }
3252 9b058f45 2022-06-30 mark
3253 9b058f45 2022-06-30 mark static const struct got_error *
3254 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3255 e5a0f69f 2018-08-18 stsp {
3256 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3257 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3258 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3259 136e2bd4 2022-07-23 mark int eos, n, nscroll;
3260 80ddbec8 2018-04-29 stsp
3261 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3262 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3263 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3264 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3265 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3266 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3267 fb280deb 2021-08-30 stsp }
3268 b880cc75 2022-06-30 mark return err;
3269 528dedf3 2021-08-30 stsp }
3270 0dca135e 2022-06-30 mark
3271 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3272 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3273 0dca135e 2022-06-30 mark --eos; /* border */
3274 0dca135e 2022-06-30 mark
3275 528dedf3 2021-08-30 stsp switch (ch) {
3276 1e37a5c2 2019-05-12 jcs case 'q':
3277 1e37a5c2 2019-05-12 jcs s->quit = 1;
3278 145b6838 2022-06-16 stsp break;
3279 145b6838 2022-06-16 stsp case '0':
3280 145b6838 2022-06-16 stsp view->x = 0;
3281 145b6838 2022-06-16 stsp break;
3282 145b6838 2022-06-16 stsp case '$':
3283 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3284 640cd7ff 2022-06-22 mark view->count = 0;
3285 145b6838 2022-06-16 stsp break;
3286 145b6838 2022-06-16 stsp case KEY_RIGHT:
3287 145b6838 2022-06-16 stsp case 'l':
3288 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3289 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3290 640cd7ff 2022-06-22 mark else
3291 640cd7ff 2022-06-22 mark view->count = 0;
3292 1e37a5c2 2019-05-12 jcs break;
3293 145b6838 2022-06-16 stsp case KEY_LEFT:
3294 145b6838 2022-06-16 stsp case 'h':
3295 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3296 640cd7ff 2022-06-22 mark if (view->x <= 0)
3297 640cd7ff 2022-06-22 mark view->count = 0;
3298 145b6838 2022-06-16 stsp break;
3299 1e37a5c2 2019-05-12 jcs case 'k':
3300 1e37a5c2 2019-05-12 jcs case KEY_UP:
3301 1e37a5c2 2019-05-12 jcs case '<':
3302 1e37a5c2 2019-05-12 jcs case ',':
3303 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3304 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3305 912a3f79 2021-08-30 j break;
3306 912a3f79 2021-08-30 j case 'g':
3307 912a3f79 2021-08-30 j case KEY_HOME:
3308 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3309 640cd7ff 2022-06-22 mark view->count = 0;
3310 1e37a5c2 2019-05-12 jcs break;
3311 83cc4199 2022-06-13 stsp case CTRL('u'):
3312 33c3719a 2022-06-15 stsp case 'u':
3313 83cc4199 2022-06-13 stsp nscroll /= 2;
3314 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3315 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3316 a4292ac5 2019-05-12 jcs case CTRL('b'):
3317 61417565 2022-06-20 mark case 'b':
3318 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3319 1e37a5c2 2019-05-12 jcs break;
3320 1e37a5c2 2019-05-12 jcs case 'j':
3321 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3322 1e37a5c2 2019-05-12 jcs case '>':
3323 1e37a5c2 2019-05-12 jcs case '.':
3324 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3325 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3326 912a3f79 2021-08-30 j break;
3327 10aab77f 2022-07-19 op case '@':
3328 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3329 10aab77f 2022-07-19 op break;
3330 912a3f79 2021-08-30 j case 'G':
3331 912a3f79 2021-08-30 j case KEY_END: {
3332 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3333 912a3f79 2021-08-30 j * traverse them all. */
3334 640cd7ff 2022-06-22 mark view->count = 0;
3335 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3336 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3337 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3338 80ddbec8 2018-04-29 stsp }
3339 912a3f79 2021-08-30 j
3340 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3341 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3342 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3343 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3344 f3bc9f1d 2021-09-05 naddy break;
3345 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3346 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3347 f3bc9f1d 2021-09-05 naddy }
3348 f3bc9f1d 2021-09-05 naddy if (n > 0)
3349 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3350 2b779855 2020-12-05 naddy select_commit(s);
3351 1e37a5c2 2019-05-12 jcs break;
3352 912a3f79 2021-08-30 j }
3353 80b7e8da 2022-06-11 stsp case CTRL('d'):
3354 33c3719a 2022-06-15 stsp case 'd':
3355 83cc4199 2022-06-13 stsp nscroll /= 2;
3356 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3357 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3358 61417565 2022-06-20 mark case CTRL('f'):
3359 48bb96f0 2022-06-20 naddy case 'f':
3360 b880cc75 2022-06-30 mark case ' ':
3361 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3362 1e37a5c2 2019-05-12 jcs break;
3363 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3364 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3365 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3366 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3367 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3368 2b779855 2020-12-05 naddy select_commit(s);
3369 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3370 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3371 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3372 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3373 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3374 0bf7f153 2020-12-02 naddy }
3375 1e37a5c2 2019-05-12 jcs break;
3376 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3377 49b24ee5 2022-07-03 mark case '\r':
3378 640cd7ff 2022-06-22 mark view->count = 0;
3379 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3380 e5a0f69f 2018-08-18 stsp break;
3381 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3382 1e37a5c2 2019-05-12 jcs break;
3383 5e98fb33 2022-07-22 mark case 'T':
3384 640cd7ff 2022-06-22 mark view->count = 0;
3385 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3386 5036bf37 2018-09-24 stsp break;
3387 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3388 1e37a5c2 2019-05-12 jcs break;
3389 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3390 21355643 2020-12-06 stsp case CTRL('l'):
3391 21355643 2020-12-06 stsp case 'B':
3392 640cd7ff 2022-06-22 mark view->count = 0;
3393 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3394 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3395 1e37a5c2 2019-05-12 jcs break;
3396 21355643 2020-12-06 stsp err = stop_log_thread(s);
3397 74cfe85e 2020-10-20 stsp if (err)
3398 74cfe85e 2020-10-20 stsp return err;
3399 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3400 21355643 2020-12-06 stsp char *parent_path;
3401 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3402 21355643 2020-12-06 stsp if (err)
3403 21355643 2020-12-06 stsp return err;
3404 21355643 2020-12-06 stsp free(s->in_repo_path);
3405 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3406 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3407 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3408 21355643 2020-12-06 stsp struct got_object_id *start_id;
3409 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3410 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3411 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3412 21355643 2020-12-06 stsp if (err)
3413 21355643 2020-12-06 stsp return err;
3414 21355643 2020-12-06 stsp free(s->start_id);
3415 21355643 2020-12-06 stsp s->start_id = start_id;
3416 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3417 21355643 2020-12-06 stsp } else /* 'B' */
3418 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3419 21355643 2020-12-06 stsp
3420 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3421 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3422 b0dd8d27 2022-06-16 stsp if (err)
3423 b0dd8d27 2022-06-16 stsp return err;
3424 b0dd8d27 2022-06-16 stsp }
3425 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3426 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3427 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3428 74cfe85e 2020-10-20 stsp if (err)
3429 21355643 2020-12-06 stsp return err;
3430 51a10b52 2020-12-26 stsp tog_free_refs();
3431 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3432 ca51c541 2020-12-07 stsp if (err)
3433 ca51c541 2020-12-07 stsp return err;
3434 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3435 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3436 d01904d4 2019-06-25 stsp if (err)
3437 d01904d4 2019-06-25 stsp return err;
3438 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3439 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3440 b672a97a 2020-01-27 stsp if (err)
3441 b672a97a 2020-01-27 stsp return err;
3442 21355643 2020-12-06 stsp free_commits(&s->commits);
3443 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3444 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3445 21355643 2020-12-06 stsp s->selected_entry = NULL;
3446 21355643 2020-12-06 stsp s->selected = 0;
3447 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3448 21355643 2020-12-06 stsp s->quit = 0;
3449 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3450 dfee752e 2022-06-18 op s->matched_entry = NULL;
3451 dfee752e 2022-06-18 op s->search_entry = NULL;
3452 01a7bcaf 2022-07-22 mark view->offset = 0;
3453 d01904d4 2019-06-25 stsp break;
3454 5e98fb33 2022-07-22 mark case 'R':
3455 640cd7ff 2022-06-22 mark view->count = 0;
3456 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3457 6458efa5 2020-11-24 stsp break;
3458 1e37a5c2 2019-05-12 jcs default:
3459 640cd7ff 2022-06-22 mark view->count = 0;
3460 1e37a5c2 2019-05-12 jcs break;
3461 899d86c2 2018-05-10 stsp }
3462 e5a0f69f 2018-08-18 stsp
3463 80ddbec8 2018-04-29 stsp return err;
3464 80ddbec8 2018-04-29 stsp }
3465 80ddbec8 2018-04-29 stsp
3466 4ed7e80c 2018-05-20 stsp static const struct got_error *
3467 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3468 c2db6724 2019-01-04 stsp {
3469 c2db6724 2019-01-04 stsp const struct got_error *error;
3470 c2db6724 2019-01-04 stsp
3471 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3472 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3473 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3474 37c06ea4 2019-07-15 stsp #endif
3475 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3476 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3477 c2db6724 2019-01-04 stsp
3478 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3479 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3480 c2db6724 2019-01-04 stsp
3481 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3482 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3483 c2db6724 2019-01-04 stsp
3484 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3485 c2db6724 2019-01-04 stsp if (error != NULL)
3486 c2db6724 2019-01-04 stsp return error;
3487 c2db6724 2019-01-04 stsp
3488 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3489 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3490 c2db6724 2019-01-04 stsp
3491 c2db6724 2019-01-04 stsp return NULL;
3492 c2db6724 2019-01-04 stsp }
3493 c2db6724 2019-01-04 stsp
3494 a915003a 2019-02-05 stsp static void
3495 a915003a 2019-02-05 stsp init_curses(void)
3496 a915003a 2019-02-05 stsp {
3497 2497f032 2022-05-31 stsp /*
3498 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3499 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3500 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3501 2497f032 2022-05-31 stsp */
3502 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3503 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3504 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3505 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3506 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3507 2497f032 2022-05-31 stsp
3508 a915003a 2019-02-05 stsp initscr();
3509 a915003a 2019-02-05 stsp cbreak();
3510 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3511 a915003a 2019-02-05 stsp noecho();
3512 a915003a 2019-02-05 stsp nonl();
3513 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3514 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3515 a915003a 2019-02-05 stsp curs_set(0);
3516 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3517 6d17833f 2019-11-08 stsp start_color();
3518 6d17833f 2019-11-08 stsp use_default_colors();
3519 6d17833f 2019-11-08 stsp }
3520 a915003a 2019-02-05 stsp }
3521 a915003a 2019-02-05 stsp
3522 c2db6724 2019-01-04 stsp static const struct got_error *
3523 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3524 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3525 f135c941 2020-02-20 stsp {
3526 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3527 f135c941 2020-02-20 stsp
3528 f135c941 2020-02-20 stsp if (argc == 0) {
3529 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3530 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3531 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3532 f135c941 2020-02-20 stsp return NULL;
3533 f135c941 2020-02-20 stsp }
3534 f135c941 2020-02-20 stsp
3535 f135c941 2020-02-20 stsp if (worktree) {
3536 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3537 bfd61697 2020-11-14 stsp char *p;
3538 f135c941 2020-02-20 stsp
3539 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3540 f135c941 2020-02-20 stsp if (err)
3541 f135c941 2020-02-20 stsp return err;
3542 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3543 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3544 bfd61697 2020-11-14 stsp p) == -1) {
3545 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3546 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3547 f135c941 2020-02-20 stsp }
3548 f135c941 2020-02-20 stsp free(p);
3549 f135c941 2020-02-20 stsp } else
3550 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3551 f135c941 2020-02-20 stsp
3552 f135c941 2020-02-20 stsp return err;
3553 f135c941 2020-02-20 stsp }
3554 f135c941 2020-02-20 stsp
3555 f135c941 2020-02-20 stsp static const struct got_error *
3556 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3557 9f7d7167 2018-04-29 stsp {
3558 80ddbec8 2018-04-29 stsp const struct got_error *error;
3559 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3560 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3561 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3562 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3563 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3564 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3565 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3566 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3567 04cc582a 2018-08-01 stsp struct tog_view *view;
3568 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3569 80ddbec8 2018-04-29 stsp
3570 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3571 80ddbec8 2018-04-29 stsp switch (ch) {
3572 b672a97a 2020-01-27 stsp case 'b':
3573 b672a97a 2020-01-27 stsp log_branches = 1;
3574 b672a97a 2020-01-27 stsp break;
3575 80ddbec8 2018-04-29 stsp case 'c':
3576 80ddbec8 2018-04-29 stsp start_commit = optarg;
3577 80ddbec8 2018-04-29 stsp break;
3578 ecb28ae0 2018-07-16 stsp case 'r':
3579 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3580 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3581 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3582 9ba1d308 2019-10-21 stsp optarg);
3583 ecb28ae0 2018-07-16 stsp break;
3584 80ddbec8 2018-04-29 stsp default:
3585 17020d27 2019-03-07 stsp usage_log();
3586 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3587 80ddbec8 2018-04-29 stsp }
3588 80ddbec8 2018-04-29 stsp }
3589 80ddbec8 2018-04-29 stsp
3590 80ddbec8 2018-04-29 stsp argc -= optind;
3591 80ddbec8 2018-04-29 stsp argv += optind;
3592 80ddbec8 2018-04-29 stsp
3593 f135c941 2020-02-20 stsp if (argc > 1)
3594 f135c941 2020-02-20 stsp usage_log();
3595 963f97a1 2019-03-18 stsp
3596 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3597 0ae84acc 2022-06-15 tracey if (error != NULL)
3598 0ae84acc 2022-06-15 tracey goto done;
3599 0ae84acc 2022-06-15 tracey
3600 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3601 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3602 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3603 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3604 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3605 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3606 c156c7a4 2020-12-18 stsp goto done;
3607 a1fbf39a 2019-08-11 stsp if (worktree)
3608 6962eb72 2020-02-20 stsp repo_path =
3609 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3610 a1fbf39a 2019-08-11 stsp else
3611 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3612 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3613 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3614 c156c7a4 2020-12-18 stsp goto done;
3615 c156c7a4 2020-12-18 stsp }
3616 ecb28ae0 2018-07-16 stsp }
3617 ecb28ae0 2018-07-16 stsp
3618 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3619 80ddbec8 2018-04-29 stsp if (error != NULL)
3620 ecb28ae0 2018-07-16 stsp goto done;
3621 80ddbec8 2018-04-29 stsp
3622 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3623 f135c941 2020-02-20 stsp repo, worktree);
3624 f135c941 2020-02-20 stsp if (error)
3625 f135c941 2020-02-20 stsp goto done;
3626 f135c941 2020-02-20 stsp
3627 f135c941 2020-02-20 stsp init_curses();
3628 f135c941 2020-02-20 stsp
3629 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3630 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3631 c02c541e 2019-03-29 stsp if (error)
3632 c02c541e 2019-03-29 stsp goto done;
3633 c02c541e 2019-03-29 stsp
3634 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3635 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3636 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3637 87670572 2020-12-26 naddy if (error)
3638 87670572 2020-12-26 naddy goto done;
3639 87670572 2020-12-26 naddy }
3640 51a10b52 2020-12-26 stsp
3641 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3642 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3643 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3644 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3645 d8f38dc4 2020-12-05 stsp if (error)
3646 d8f38dc4 2020-12-05 stsp goto done;
3647 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3648 d8f38dc4 2020-12-05 stsp } else {
3649 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3650 d8f38dc4 2020-12-05 stsp if (error == NULL)
3651 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3652 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3653 d8f38dc4 2020-12-05 stsp goto done;
3654 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3655 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3656 d8f38dc4 2020-12-05 stsp if (error)
3657 d8f38dc4 2020-12-05 stsp goto done;
3658 d8f38dc4 2020-12-05 stsp }
3659 8b473291 2019-02-21 stsp
3660 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3661 04cc582a 2018-08-01 stsp if (view == NULL) {
3662 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3663 04cc582a 2018-08-01 stsp goto done;
3664 04cc582a 2018-08-01 stsp }
3665 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3666 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3667 ba4f502b 2018-08-04 stsp if (error)
3668 ba4f502b 2018-08-04 stsp goto done;
3669 2fc00ff4 2019-08-31 stsp if (worktree) {
3670 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3671 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3672 2fc00ff4 2019-08-31 stsp worktree = NULL;
3673 2fc00ff4 2019-08-31 stsp }
3674 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3675 ecb28ae0 2018-07-16 stsp done:
3676 f135c941 2020-02-20 stsp free(in_repo_path);
3677 ecb28ae0 2018-07-16 stsp free(repo_path);
3678 ecb28ae0 2018-07-16 stsp free(cwd);
3679 899d86c2 2018-05-10 stsp free(start_id);
3680 d8f38dc4 2020-12-05 stsp free(label);
3681 d8f38dc4 2020-12-05 stsp if (ref)
3682 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3683 1d0f4054 2021-06-17 stsp if (repo) {
3684 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3685 1d0f4054 2021-06-17 stsp if (error == NULL)
3686 1d0f4054 2021-06-17 stsp error = close_err;
3687 1d0f4054 2021-06-17 stsp }
3688 ec142235 2019-03-07 stsp if (worktree)
3689 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3690 0ae84acc 2022-06-15 tracey if (pack_fds) {
3691 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3692 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3693 0ae84acc 2022-06-15 tracey if (error == NULL)
3694 0ae84acc 2022-06-15 tracey error = pack_err;
3695 0ae84acc 2022-06-15 tracey }
3696 51a10b52 2020-12-26 stsp tog_free_refs();
3697 80ddbec8 2018-04-29 stsp return error;
3698 9f7d7167 2018-04-29 stsp }
3699 9f7d7167 2018-04-29 stsp
3700 4ed7e80c 2018-05-20 stsp __dead static void
3701 9f7d7167 2018-04-29 stsp usage_diff(void)
3702 9f7d7167 2018-04-29 stsp {
3703 80ddbec8 2018-04-29 stsp endwin();
3704 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3705 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3706 9f7d7167 2018-04-29 stsp exit(1);
3707 b304db33 2018-05-20 stsp }
3708 b304db33 2018-05-20 stsp
3709 6d17833f 2019-11-08 stsp static int
3710 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3711 41605754 2020-11-12 stsp regmatch_t *regmatch)
3712 6d17833f 2019-11-08 stsp {
3713 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3714 6d17833f 2019-11-08 stsp }
3715 6d17833f 2019-11-08 stsp
3716 336075a4 2022-06-25 op static struct tog_color *
3717 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3718 6d17833f 2019-11-08 stsp {
3719 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3720 6d17833f 2019-11-08 stsp
3721 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3722 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3723 6d17833f 2019-11-08 stsp return tc;
3724 6d17833f 2019-11-08 stsp }
3725 6d17833f 2019-11-08 stsp
3726 6d17833f 2019-11-08 stsp return NULL;
3727 6d17833f 2019-11-08 stsp }
3728 6d17833f 2019-11-08 stsp
3729 4ed7e80c 2018-05-20 stsp static const struct got_error *
3730 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3731 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3732 41605754 2020-11-12 stsp {
3733 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3734 44a87665 2022-06-16 stsp char *exstr = NULL;
3735 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3736 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3737 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3738 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3739 41605754 2020-11-12 stsp
3740 41605754 2020-11-12 stsp *wtotal = 0;
3741 1853e0f4 2022-06-16 stsp
3742 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3743 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3744 41605754 2020-11-12 stsp
3745 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3746 44a87665 2022-06-16 stsp if (err)
3747 44a87665 2022-06-16 stsp return err;
3748 44a87665 2022-06-16 stsp
3749 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3750 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3751 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3752 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3753 44a87665 2022-06-16 stsp goto done;
3754 44a87665 2022-06-16 stsp }
3755 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3756 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3757 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3758 1853e0f4 2022-06-16 stsp goto done;
3759 1853e0f4 2022-06-16 stsp }
3760 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3761 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3762 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3763 1853e0f4 2022-06-16 stsp goto done;
3764 1853e0f4 2022-06-16 stsp }
3765 145b6838 2022-06-16 stsp
3766 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3767 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3768 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3769 1853e0f4 2022-06-16 stsp if (err)
3770 1853e0f4 2022-06-16 stsp goto done;
3771 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3772 145b6838 2022-06-16 stsp if (n) {
3773 1853e0f4 2022-06-16 stsp free(wline);
3774 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3775 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3776 1853e0f4 2022-06-16 stsp if (err)
3777 1853e0f4 2022-06-16 stsp goto done;
3778 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3779 1853e0f4 2022-06-16 stsp wlimit -= width;
3780 1853e0f4 2022-06-16 stsp *wtotal += width;
3781 41605754 2020-11-12 stsp }
3782 41605754 2020-11-12 stsp
3783 41605754 2020-11-12 stsp if (wlimit > 0) {
3784 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3785 1853e0f4 2022-06-16 stsp size_t wlen;
3786 1853e0f4 2022-06-16 stsp
3787 1853e0f4 2022-06-16 stsp free(wline);
3788 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3789 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3790 1853e0f4 2022-06-16 stsp if (err)
3791 1853e0f4 2022-06-16 stsp goto done;
3792 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3793 1853e0f4 2022-06-16 stsp while (i < wlen) {
3794 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3795 1853e0f4 2022-06-16 stsp if (width == -1) {
3796 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3797 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3798 1853e0f4 2022-06-16 stsp goto done;
3799 1853e0f4 2022-06-16 stsp }
3800 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3801 1853e0f4 2022-06-16 stsp break;
3802 61417565 2022-06-20 mark w += width;
3803 1853e0f4 2022-06-16 stsp i++;
3804 41605754 2020-11-12 stsp }
3805 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3806 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3807 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3808 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3809 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3810 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3811 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3812 41605754 2020-11-12 stsp }
3813 41605754 2020-11-12 stsp }
3814 41605754 2020-11-12 stsp
3815 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3816 1853e0f4 2022-06-16 stsp free(wline);
3817 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3818 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3819 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3820 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3821 1853e0f4 2022-06-16 stsp if (err)
3822 1853e0f4 2022-06-16 stsp goto done;
3823 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3824 1853e0f4 2022-06-16 stsp } else {
3825 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3826 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3827 1853e0f4 2022-06-16 stsp if (err)
3828 1853e0f4 2022-06-16 stsp goto done;
3829 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3830 1853e0f4 2022-06-16 stsp }
3831 1853e0f4 2022-06-16 stsp *wtotal += width2;
3832 41605754 2020-11-12 stsp }
3833 1853e0f4 2022-06-16 stsp done:
3834 145b6838 2022-06-16 stsp free(wline);
3835 44a87665 2022-06-16 stsp free(exstr);
3836 1853e0f4 2022-06-16 stsp free(seg0);
3837 1853e0f4 2022-06-16 stsp free(seg1);
3838 1853e0f4 2022-06-16 stsp free(seg2);
3839 1853e0f4 2022-06-16 stsp return err;
3840 41605754 2020-11-12 stsp }
3841 41605754 2020-11-12 stsp
3842 41605754 2020-11-12 stsp static const struct got_error *
3843 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3844 26ed57b2 2018-05-19 stsp {
3845 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3846 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3847 61e69b96 2018-05-20 stsp const struct got_error *err;
3848 fe621944 2020-11-10 stsp int nprinted = 0;
3849 b304db33 2018-05-20 stsp char *line;
3850 826082fe 2020-12-10 stsp size_t linesize = 0;
3851 826082fe 2020-12-10 stsp ssize_t linelen;
3852 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3853 61e69b96 2018-05-20 stsp wchar_t *wline;
3854 e0b650dd 2018-05-20 stsp int width;
3855 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3856 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3857 fe621944 2020-11-10 stsp off_t line_offset;
3858 26ed57b2 2018-05-19 stsp
3859 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3860 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3861 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3862 fe621944 2020-11-10 stsp
3863 f7d12f7e 2018-08-01 stsp werase(view->window);
3864 a3404814 2018-09-02 stsp
3865 a3404814 2018-09-02 stsp if (header) {
3866 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3867 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3868 135a2da0 2020-11-11 stsp header) == -1)
3869 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3870 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3871 ccda2f4d 2022-06-16 stsp 0, 0);
3872 135a2da0 2020-11-11 stsp free(line);
3873 135a2da0 2020-11-11 stsp if (err)
3874 a3404814 2018-09-02 stsp return err;
3875 a3404814 2018-09-02 stsp
3876 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3877 a3404814 2018-09-02 stsp wstandout(view->window);
3878 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3879 e54cc94a 2020-11-11 stsp free(wline);
3880 e54cc94a 2020-11-11 stsp wline = NULL;
3881 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3882 a3404814 2018-09-02 stsp wstandend(view->window);
3883 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3884 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3885 26ed57b2 2018-05-19 stsp
3886 a3404814 2018-09-02 stsp if (max_lines <= 1)
3887 a3404814 2018-09-02 stsp return NULL;
3888 a3404814 2018-09-02 stsp max_lines--;
3889 a3404814 2018-09-02 stsp }
3890 a3404814 2018-09-02 stsp
3891 89f1a395 2020-12-01 naddy s->eof = 0;
3892 145b6838 2022-06-16 stsp view->maxx = 0;
3893 826082fe 2020-12-10 stsp line = NULL;
3894 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3895 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3896 826082fe 2020-12-10 stsp if (linelen == -1) {
3897 826082fe 2020-12-10 stsp if (feof(s->f)) {
3898 826082fe 2020-12-10 stsp s->eof = 1;
3899 826082fe 2020-12-10 stsp break;
3900 826082fe 2020-12-10 stsp }
3901 826082fe 2020-12-10 stsp free(line);
3902 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3903 61e69b96 2018-05-20 stsp }
3904 145b6838 2022-06-16 stsp
3905 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3906 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3907 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3908 1853e0f4 2022-06-16 stsp if (err) {
3909 1853e0f4 2022-06-16 stsp free(line);
3910 1853e0f4 2022-06-16 stsp return err;
3911 1853e0f4 2022-06-16 stsp }
3912 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3913 1853e0f4 2022-06-16 stsp free(wline);
3914 1853e0f4 2022-06-16 stsp wline = NULL;
3915 1853e0f4 2022-06-16 stsp
3916 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3917 f26dddb7 2019-11-08 stsp if (tc)
3918 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3919 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3920 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3921 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3922 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3923 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3924 41605754 2020-11-12 stsp if (err) {
3925 41605754 2020-11-12 stsp free(line);
3926 41605754 2020-11-12 stsp return err;
3927 41605754 2020-11-12 stsp }
3928 41605754 2020-11-12 stsp } else {
3929 969c159c 2022-06-16 stsp int skip;
3930 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3931 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3932 969c159c 2022-06-16 stsp if (err) {
3933 969c159c 2022-06-16 stsp free(line);
3934 969c159c 2022-06-16 stsp return err;
3935 969c159c 2022-06-16 stsp }
3936 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3937 969c159c 2022-06-16 stsp free(wline);
3938 41605754 2020-11-12 stsp wline = NULL;
3939 41605754 2020-11-12 stsp }
3940 f26dddb7 2019-11-08 stsp if (tc)
3941 6d17833f 2019-11-08 stsp wattr_off(view->window,
3942 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3943 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3944 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3945 fe621944 2020-11-10 stsp nprinted++;
3946 826082fe 2020-12-10 stsp }
3947 826082fe 2020-12-10 stsp free(line);
3948 fe621944 2020-11-10 stsp if (nprinted >= 1)
3949 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3950 89f1a395 2020-12-01 naddy (nprinted - 1);
3951 fe621944 2020-11-10 stsp else
3952 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3953 26ed57b2 2018-05-19 stsp
3954 9b058f45 2022-06-30 mark view_border(view);
3955 c3e9aa98 2019-05-13 jcs
3956 89f1a395 2020-12-01 naddy if (s->eof) {
3957 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3958 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3959 c3e9aa98 2019-05-13 jcs nprinted++;
3960 c3e9aa98 2019-05-13 jcs }
3961 c3e9aa98 2019-05-13 jcs
3962 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3963 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3964 c3e9aa98 2019-05-13 jcs if (err) {
3965 c3e9aa98 2019-05-13 jcs return err;
3966 c3e9aa98 2019-05-13 jcs }
3967 26ed57b2 2018-05-19 stsp
3968 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3969 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3970 e54cc94a 2020-11-11 stsp free(wline);
3971 e54cc94a 2020-11-11 stsp wline = NULL;
3972 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3973 c3e9aa98 2019-05-13 jcs }
3974 c3e9aa98 2019-05-13 jcs
3975 26ed57b2 2018-05-19 stsp return NULL;
3976 abd2672a 2018-12-23 stsp }
3977 abd2672a 2018-12-23 stsp
3978 abd2672a 2018-12-23 stsp static char *
3979 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3980 abd2672a 2018-12-23 stsp {
3981 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3982 09867e48 2019-08-13 stsp char *p, *s;
3983 09867e48 2019-08-13 stsp
3984 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3985 09867e48 2019-08-13 stsp if (tm == NULL)
3986 09867e48 2019-08-13 stsp return NULL;
3987 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3988 09867e48 2019-08-13 stsp if (s == NULL)
3989 09867e48 2019-08-13 stsp return NULL;
3990 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3991 abd2672a 2018-12-23 stsp if (p)
3992 abd2672a 2018-12-23 stsp *p = '\0';
3993 abd2672a 2018-12-23 stsp return s;
3994 9f7d7167 2018-04-29 stsp }
3995 9f7d7167 2018-04-29 stsp
3996 4ed7e80c 2018-05-20 stsp static const struct got_error *
3997 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3998 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3999 0208f208 2020-05-05 stsp {
4000 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4001 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4002 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4003 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4004 0208f208 2020-05-05 stsp
4005 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4006 0208f208 2020-05-05 stsp if (qid != NULL) {
4007 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4008 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4009 d7b5a0e8 2022-04-20 stsp &qid->id);
4010 0208f208 2020-05-05 stsp if (err)
4011 0208f208 2020-05-05 stsp return err;
4012 0208f208 2020-05-05 stsp
4013 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4014 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4015 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4016 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4017 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4018 aa8b5dd0 2021-08-01 stsp }
4019 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4020 0208f208 2020-05-05 stsp
4021 0208f208 2020-05-05 stsp }
4022 0208f208 2020-05-05 stsp
4023 0208f208 2020-05-05 stsp if (tree_id1) {
4024 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4025 0208f208 2020-05-05 stsp if (err)
4026 0208f208 2020-05-05 stsp goto done;
4027 0208f208 2020-05-05 stsp }
4028 0208f208 2020-05-05 stsp
4029 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4030 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4031 0208f208 2020-05-05 stsp if (err)
4032 0208f208 2020-05-05 stsp goto done;
4033 0208f208 2020-05-05 stsp
4034 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4035 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4036 0208f208 2020-05-05 stsp done:
4037 0208f208 2020-05-05 stsp if (tree1)
4038 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4039 0208f208 2020-05-05 stsp if (tree2)
4040 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4041 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4042 0208f208 2020-05-05 stsp return err;
4043 0208f208 2020-05-05 stsp }
4044 0208f208 2020-05-05 stsp
4045 0208f208 2020-05-05 stsp static const struct got_error *
4046 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4047 abd2672a 2018-12-23 stsp {
4048 fe621944 2020-11-10 stsp off_t *p;
4049 fe621944 2020-11-10 stsp
4050 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4051 fe621944 2020-11-10 stsp if (p == NULL)
4052 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4053 fe621944 2020-11-10 stsp *line_offsets = p;
4054 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4055 fe621944 2020-11-10 stsp (*nlines)++;
4056 fe621944 2020-11-10 stsp return NULL;
4057 fe621944 2020-11-10 stsp }
4058 fe621944 2020-11-10 stsp
4059 fe621944 2020-11-10 stsp static const struct got_error *
4060 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4061 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4062 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4063 fe621944 2020-11-10 stsp {
4064 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4065 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4066 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4067 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4068 45d799e2 2018-12-23 stsp time_t committer_time;
4069 45d799e2 2018-12-23 stsp const char *author, *committer;
4070 8b473291 2019-02-21 stsp char *refs_str = NULL;
4071 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4072 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4073 fe621944 2020-11-10 stsp off_t outoff = 0;
4074 fe621944 2020-11-10 stsp int n;
4075 abd2672a 2018-12-23 stsp
4076 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4077 0208f208 2020-05-05 stsp
4078 8b473291 2019-02-21 stsp if (refs) {
4079 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4080 8b473291 2019-02-21 stsp if (err)
4081 8b473291 2019-02-21 stsp return err;
4082 8b473291 2019-02-21 stsp }
4083 8b473291 2019-02-21 stsp
4084 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4085 abd2672a 2018-12-23 stsp if (err)
4086 abd2672a 2018-12-23 stsp return err;
4087 abd2672a 2018-12-23 stsp
4088 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4089 15a94983 2018-12-23 stsp if (err) {
4090 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4091 15a94983 2018-12-23 stsp goto done;
4092 15a94983 2018-12-23 stsp }
4093 abd2672a 2018-12-23 stsp
4094 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4095 fe621944 2020-11-10 stsp if (err)
4096 fe621944 2020-11-10 stsp goto done;
4097 fe621944 2020-11-10 stsp
4098 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4099 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4100 fe621944 2020-11-10 stsp if (n < 0) {
4101 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4102 abd2672a 2018-12-23 stsp goto done;
4103 abd2672a 2018-12-23 stsp }
4104 fe621944 2020-11-10 stsp outoff += n;
4105 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4106 fe621944 2020-11-10 stsp if (err)
4107 fe621944 2020-11-10 stsp goto done;
4108 fe621944 2020-11-10 stsp
4109 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4110 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4111 fe621944 2020-11-10 stsp if (n < 0) {
4112 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4113 abd2672a 2018-12-23 stsp goto done;
4114 abd2672a 2018-12-23 stsp }
4115 fe621944 2020-11-10 stsp outoff += n;
4116 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4117 fe621944 2020-11-10 stsp if (err)
4118 fe621944 2020-11-10 stsp goto done;
4119 fe621944 2020-11-10 stsp
4120 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4121 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4122 fe621944 2020-11-10 stsp if (datestr) {
4123 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4124 fe621944 2020-11-10 stsp if (n < 0) {
4125 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4126 fe621944 2020-11-10 stsp goto done;
4127 fe621944 2020-11-10 stsp }
4128 fe621944 2020-11-10 stsp outoff += n;
4129 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4130 fe621944 2020-11-10 stsp if (err)
4131 fe621944 2020-11-10 stsp goto done;
4132 abd2672a 2018-12-23 stsp }
4133 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4134 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4135 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4136 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4137 fe621944 2020-11-10 stsp if (n < 0) {
4138 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4139 fe621944 2020-11-10 stsp goto done;
4140 fe621944 2020-11-10 stsp }
4141 fe621944 2020-11-10 stsp outoff += n;
4142 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4143 fe621944 2020-11-10 stsp if (err)
4144 fe621944 2020-11-10 stsp goto done;
4145 abd2672a 2018-12-23 stsp }
4146 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4147 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4148 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4149 9f98ca05 2021-09-24 stsp int pn = 1;
4150 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4151 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4152 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4153 9f98ca05 2021-09-24 stsp if (err)
4154 9f98ca05 2021-09-24 stsp goto done;
4155 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4156 9f98ca05 2021-09-24 stsp if (n < 0) {
4157 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4158 9f98ca05 2021-09-24 stsp goto done;
4159 9f98ca05 2021-09-24 stsp }
4160 9f98ca05 2021-09-24 stsp outoff += n;
4161 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4162 9f98ca05 2021-09-24 stsp if (err)
4163 9f98ca05 2021-09-24 stsp goto done;
4164 9f98ca05 2021-09-24 stsp free(id_str);
4165 9f98ca05 2021-09-24 stsp id_str = NULL;
4166 9f98ca05 2021-09-24 stsp }
4167 9f98ca05 2021-09-24 stsp }
4168 9f98ca05 2021-09-24 stsp
4169 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4170 5943eee2 2019-08-13 stsp if (err)
4171 5943eee2 2019-08-13 stsp goto done;
4172 fe621944 2020-11-10 stsp s = logmsg;
4173 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4174 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4175 fe621944 2020-11-10 stsp if (n < 0) {
4176 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4177 fe621944 2020-11-10 stsp goto done;
4178 fe621944 2020-11-10 stsp }
4179 fe621944 2020-11-10 stsp outoff += n;
4180 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4181 fe621944 2020-11-10 stsp if (err)
4182 fe621944 2020-11-10 stsp goto done;
4183 abd2672a 2018-12-23 stsp }
4184 fe621944 2020-11-10 stsp
4185 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4186 0208f208 2020-05-05 stsp if (err)
4187 0208f208 2020-05-05 stsp goto done;
4188 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4189 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4190 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4191 fe621944 2020-11-10 stsp if (n < 0) {
4192 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4193 fe621944 2020-11-10 stsp goto done;
4194 fe621944 2020-11-10 stsp }
4195 fe621944 2020-11-10 stsp outoff += n;
4196 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4197 fe621944 2020-11-10 stsp if (err)
4198 fe621944 2020-11-10 stsp goto done;
4199 0208f208 2020-05-05 stsp free((char *)pe->path);
4200 0208f208 2020-05-05 stsp free(pe->data);
4201 0208f208 2020-05-05 stsp }
4202 fe621944 2020-11-10 stsp
4203 0208f208 2020-05-05 stsp fputc('\n', outfile);
4204 fe621944 2020-11-10 stsp outoff++;
4205 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4206 abd2672a 2018-12-23 stsp done:
4207 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4208 abd2672a 2018-12-23 stsp free(id_str);
4209 5943eee2 2019-08-13 stsp free(logmsg);
4210 8b473291 2019-02-21 stsp free(refs_str);
4211 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4212 7510f233 2020-08-09 stsp if (err) {
4213 ae6a6978 2020-08-09 stsp free(*line_offsets);
4214 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4215 ae6a6978 2020-08-09 stsp *nlines = 0;
4216 7510f233 2020-08-09 stsp }
4217 fe621944 2020-11-10 stsp return err;
4218 abd2672a 2018-12-23 stsp }
4219 abd2672a 2018-12-23 stsp
4220 abd2672a 2018-12-23 stsp static const struct got_error *
4221 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4222 26ed57b2 2018-05-19 stsp {
4223 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4224 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4225 15a94983 2018-12-23 stsp int obj_type;
4226 fe621944 2020-11-10 stsp
4227 fe621944 2020-11-10 stsp free(s->line_offsets);
4228 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4229 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4230 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4231 fe621944 2020-11-10 stsp s->nlines = 0;
4232 26ed57b2 2018-05-19 stsp
4233 511a516b 2018-05-19 stsp f = got_opentemp();
4234 48ae06ee 2018-10-18 stsp if (f == NULL) {
4235 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4236 48ae06ee 2018-10-18 stsp goto done;
4237 48ae06ee 2018-10-18 stsp }
4238 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4239 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4240 fb43ecf1 2019-02-11 stsp goto done;
4241 fb43ecf1 2019-02-11 stsp }
4242 48ae06ee 2018-10-18 stsp s->f = f;
4243 26ed57b2 2018-05-19 stsp
4244 15a94983 2018-12-23 stsp if (s->id1)
4245 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4246 15a94983 2018-12-23 stsp else
4247 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4248 15a94983 2018-12-23 stsp if (err)
4249 15a94983 2018-12-23 stsp goto done;
4250 15a94983 2018-12-23 stsp
4251 15a94983 2018-12-23 stsp switch (obj_type) {
4252 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4253 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4254 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4255 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4256 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4257 26ed57b2 2018-05-19 stsp break;
4258 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4259 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4260 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4261 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4262 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4263 26ed57b2 2018-05-19 stsp break;
4264 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4265 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4266 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4267 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4268 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4269 abd2672a 2018-12-23 stsp
4270 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4271 abd2672a 2018-12-23 stsp if (err)
4272 3ffacbe1 2020-02-02 tracey goto done;
4273 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4274 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4275 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4276 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4277 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4278 f44b1f58 2020-02-02 tracey if (err)
4279 f44b1f58 2020-02-02 tracey goto done;
4280 f44b1f58 2020-02-02 tracey } else {
4281 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4282 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4283 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4284 fe621944 2020-11-10 stsp err = write_commit_info(
4285 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4286 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4287 f44b1f58 2020-02-02 tracey if (err)
4288 f44b1f58 2020-02-02 tracey goto done;
4289 f5404e4e 2020-02-02 tracey break;
4290 15a087fe 2019-02-21 stsp }
4291 abd2672a 2018-12-23 stsp }
4292 abd2672a 2018-12-23 stsp }
4293 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4294 abd2672a 2018-12-23 stsp
4295 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4296 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4297 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4298 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4299 26ed57b2 2018-05-19 stsp break;
4300 abd2672a 2018-12-23 stsp }
4301 26ed57b2 2018-05-19 stsp default:
4302 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4303 48ae06ee 2018-10-18 stsp break;
4304 26ed57b2 2018-05-19 stsp }
4305 f44b1f58 2020-02-02 tracey if (err)
4306 f44b1f58 2020-02-02 tracey goto done;
4307 48ae06ee 2018-10-18 stsp done:
4308 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4309 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4310 48ae06ee 2018-10-18 stsp return err;
4311 48ae06ee 2018-10-18 stsp }
4312 26ed57b2 2018-05-19 stsp
4313 f5215bb9 2019-02-22 stsp static void
4314 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4315 f5215bb9 2019-02-22 stsp {
4316 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4317 f5215bb9 2019-02-22 stsp update_panels();
4318 f5215bb9 2019-02-22 stsp doupdate();
4319 f44b1f58 2020-02-02 tracey }
4320 f44b1f58 2020-02-02 tracey
4321 f44b1f58 2020-02-02 tracey static const struct got_error *
4322 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4323 f44b1f58 2020-02-02 tracey {
4324 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4325 f44b1f58 2020-02-02 tracey
4326 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4327 f44b1f58 2020-02-02 tracey return NULL;
4328 f44b1f58 2020-02-02 tracey }
4329 f44b1f58 2020-02-02 tracey
4330 f44b1f58 2020-02-02 tracey static const struct got_error *
4331 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4332 f44b1f58 2020-02-02 tracey {
4333 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4334 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4335 f44b1f58 2020-02-02 tracey int lineno;
4336 cb713507 2022-06-16 stsp char *line = NULL;
4337 826082fe 2020-12-10 stsp size_t linesize = 0;
4338 826082fe 2020-12-10 stsp ssize_t linelen;
4339 f44b1f58 2020-02-02 tracey
4340 f44b1f58 2020-02-02 tracey if (!view->searching) {
4341 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4342 f44b1f58 2020-02-02 tracey return NULL;
4343 f44b1f58 2020-02-02 tracey }
4344 f44b1f58 2020-02-02 tracey
4345 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4346 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4347 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4348 f44b1f58 2020-02-02 tracey else
4349 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4350 487cd7d2 2021-12-17 stsp } else
4351 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4352 f44b1f58 2020-02-02 tracey
4353 f44b1f58 2020-02-02 tracey while (1) {
4354 f44b1f58 2020-02-02 tracey off_t offset;
4355 f44b1f58 2020-02-02 tracey
4356 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4357 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4358 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4359 f44b1f58 2020-02-02 tracey break;
4360 f44b1f58 2020-02-02 tracey }
4361 f44b1f58 2020-02-02 tracey
4362 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4363 f44b1f58 2020-02-02 tracey lineno = 1;
4364 f44b1f58 2020-02-02 tracey else
4365 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4366 f44b1f58 2020-02-02 tracey }
4367 f44b1f58 2020-02-02 tracey
4368 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4369 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4370 f44b1f58 2020-02-02 tracey free(line);
4371 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4372 f44b1f58 2020-02-02 tracey }
4373 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4374 cb713507 2022-06-16 stsp if (linelen != -1) {
4375 cb713507 2022-06-16 stsp char *exstr;
4376 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4377 cb713507 2022-06-16 stsp if (err)
4378 cb713507 2022-06-16 stsp break;
4379 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4380 cb713507 2022-06-16 stsp &view->regmatch)) {
4381 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4382 cb713507 2022-06-16 stsp s->matched_line = lineno;
4383 cb713507 2022-06-16 stsp free(exstr);
4384 cb713507 2022-06-16 stsp break;
4385 cb713507 2022-06-16 stsp }
4386 cb713507 2022-06-16 stsp free(exstr);
4387 f44b1f58 2020-02-02 tracey }
4388 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4389 f44b1f58 2020-02-02 tracey lineno++;
4390 f44b1f58 2020-02-02 tracey else
4391 f44b1f58 2020-02-02 tracey lineno--;
4392 f44b1f58 2020-02-02 tracey }
4393 826082fe 2020-12-10 stsp free(line);
4394 f44b1f58 2020-02-02 tracey
4395 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4396 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4397 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4398 f44b1f58 2020-02-02 tracey }
4399 f44b1f58 2020-02-02 tracey
4400 145b6838 2022-06-16 stsp return err;
4401 f5215bb9 2019-02-22 stsp }
4402 f5215bb9 2019-02-22 stsp
4403 48ae06ee 2018-10-18 stsp static const struct got_error *
4404 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4405 b72706c3 2022-06-01 stsp {
4406 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4407 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4408 b72706c3 2022-06-01 stsp
4409 b72706c3 2022-06-01 stsp free(s->id1);
4410 b72706c3 2022-06-01 stsp s->id1 = NULL;
4411 b72706c3 2022-06-01 stsp free(s->id2);
4412 b72706c3 2022-06-01 stsp s->id2 = NULL;
4413 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4414 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4415 b72706c3 2022-06-01 stsp s->f = NULL;
4416 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4417 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4418 b72706c3 2022-06-01 stsp s->f1 = NULL;
4419 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4420 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4421 b72706c3 2022-06-01 stsp s->f2 = NULL;
4422 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4423 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4424 f9d37699 2022-06-28 stsp s->fd1 = -1;
4425 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4426 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4427 f9d37699 2022-06-28 stsp s->fd2 = -1;
4428 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4429 b72706c3 2022-06-01 stsp free(s->line_offsets);
4430 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4431 b72706c3 2022-06-01 stsp s->nlines = 0;
4432 b72706c3 2022-06-01 stsp return err;
4433 b72706c3 2022-06-01 stsp }
4434 b72706c3 2022-06-01 stsp
4435 b72706c3 2022-06-01 stsp static const struct got_error *
4436 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4437 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4438 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4439 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4440 48ae06ee 2018-10-18 stsp {
4441 48ae06ee 2018-10-18 stsp const struct got_error *err;
4442 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4443 5dc9f4bc 2018-08-04 stsp
4444 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4445 f9d37699 2022-06-28 stsp s->fd1 = -1;
4446 f9d37699 2022-06-28 stsp s->fd2 = -1;
4447 b72706c3 2022-06-01 stsp
4448 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4449 15a94983 2018-12-23 stsp int type1, type2;
4450 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4451 15a94983 2018-12-23 stsp if (err)
4452 15a94983 2018-12-23 stsp return err;
4453 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4454 15a94983 2018-12-23 stsp if (err)
4455 15a94983 2018-12-23 stsp return err;
4456 15a94983 2018-12-23 stsp
4457 15a94983 2018-12-23 stsp if (type1 != type2)
4458 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4459 15a94983 2018-12-23 stsp }
4460 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4461 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4462 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4463 f44b1f58 2020-02-02 tracey s->repo = repo;
4464 f44b1f58 2020-02-02 tracey s->id1 = id1;
4465 f44b1f58 2020-02-02 tracey s->id2 = id2;
4466 3dbaef42 2020-11-24 stsp s->label1 = label1;
4467 3dbaef42 2020-11-24 stsp s->label2 = label2;
4468 48ae06ee 2018-10-18 stsp
4469 15a94983 2018-12-23 stsp if (id1) {
4470 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4471 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4472 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4473 48ae06ee 2018-10-18 stsp } else
4474 5465d566 2020-02-01 tracey s->id1 = NULL;
4475 48ae06ee 2018-10-18 stsp
4476 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4477 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4478 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4479 b72706c3 2022-06-01 stsp goto done;
4480 48ae06ee 2018-10-18 stsp }
4481 b72706c3 2022-06-01 stsp
4482 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4483 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4484 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4485 a00719e9 2022-06-17 stsp goto done;
4486 a00719e9 2022-06-17 stsp }
4487 a00719e9 2022-06-17 stsp
4488 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4489 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4490 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4491 b72706c3 2022-06-01 stsp goto done;
4492 b72706c3 2022-06-01 stsp }
4493 b72706c3 2022-06-01 stsp
4494 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4495 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4496 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4497 f9d37699 2022-06-28 stsp goto done;
4498 f9d37699 2022-06-28 stsp }
4499 f9d37699 2022-06-28 stsp
4500 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4501 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4502 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4503 f9d37699 2022-06-28 stsp goto done;
4504 f9d37699 2022-06-28 stsp }
4505 f9d37699 2022-06-28 stsp
4506 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4507 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4508 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4509 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4510 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4511 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4512 5465d566 2020-02-01 tracey s->repo = repo;
4513 6d17833f 2019-11-08 stsp
4514 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4515 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4516 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4517 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4518 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4519 6d17833f 2019-11-08 stsp if (err)
4520 b72706c3 2022-06-01 stsp goto done;
4521 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4522 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4523 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4524 b72706c3 2022-06-01 stsp if (err)
4525 b72706c3 2022-06-01 stsp goto done;
4526 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4527 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4528 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4529 b72706c3 2022-06-01 stsp if (err)
4530 b72706c3 2022-06-01 stsp goto done;
4531 6d17833f 2019-11-08 stsp
4532 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4533 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4534 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4535 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4536 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4537 b72706c3 2022-06-01 stsp if (err)
4538 b72706c3 2022-06-01 stsp goto done;
4539 11b20872 2019-11-08 stsp
4540 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4541 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4543 b72706c3 2022-06-01 stsp if (err)
4544 b72706c3 2022-06-01 stsp goto done;
4545 11b20872 2019-11-08 stsp
4546 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4547 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4548 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4549 b72706c3 2022-06-01 stsp if (err)
4550 b72706c3 2022-06-01 stsp goto done;
4551 6d17833f 2019-11-08 stsp }
4552 5dc9f4bc 2018-08-04 stsp
4553 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4554 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4555 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4556 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4557 f5215bb9 2019-02-22 stsp
4558 5465d566 2020-02-01 tracey err = create_diff(s);
4559 48ae06ee 2018-10-18 stsp
4560 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4561 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4562 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4563 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4564 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4565 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4566 b72706c3 2022-06-01 stsp done:
4567 b72706c3 2022-06-01 stsp if (err)
4568 b72706c3 2022-06-01 stsp close_diff_view(view);
4569 e5a0f69f 2018-08-18 stsp return err;
4570 5dc9f4bc 2018-08-04 stsp }
4571 5dc9f4bc 2018-08-04 stsp
4572 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4573 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4574 5dc9f4bc 2018-08-04 stsp {
4575 a3404814 2018-09-02 stsp const struct got_error *err;
4576 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4577 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4578 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4579 a3404814 2018-09-02 stsp
4580 a3404814 2018-09-02 stsp if (s->id1) {
4581 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4582 a3404814 2018-09-02 stsp if (err)
4583 a3404814 2018-09-02 stsp return err;
4584 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4585 3dbaef42 2020-11-24 stsp } else
4586 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4587 3dbaef42 2020-11-24 stsp
4588 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4589 a3404814 2018-09-02 stsp if (err)
4590 a3404814 2018-09-02 stsp return err;
4591 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4592 26ed57b2 2018-05-19 stsp
4593 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4594 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4595 a3404814 2018-09-02 stsp free(id_str1);
4596 a3404814 2018-09-02 stsp free(id_str2);
4597 a3404814 2018-09-02 stsp return err;
4598 a3404814 2018-09-02 stsp }
4599 a3404814 2018-09-02 stsp free(id_str1);
4600 a3404814 2018-09-02 stsp free(id_str2);
4601 a3404814 2018-09-02 stsp
4602 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4603 267bb3b8 2021-08-01 stsp free(header);
4604 267bb3b8 2021-08-01 stsp return err;
4605 15a087fe 2019-02-21 stsp }
4606 15a087fe 2019-02-21 stsp
4607 15a087fe 2019-02-21 stsp static const struct got_error *
4608 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4609 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4610 15a087fe 2019-02-21 stsp {
4611 d7a04538 2019-02-21 stsp const struct got_error *err;
4612 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4613 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4614 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4615 15a087fe 2019-02-21 stsp
4616 15a087fe 2019-02-21 stsp free(s->id2);
4617 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4618 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4619 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4620 15a087fe 2019-02-21 stsp
4621 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4622 d7a04538 2019-02-21 stsp if (err)
4623 d7a04538 2019-02-21 stsp return err;
4624 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4625 15a087fe 2019-02-21 stsp free(s->id1);
4626 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4627 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4628 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4629 15a087fe 2019-02-21 stsp return NULL;
4630 0cf4efb1 2018-09-29 stsp }
4631 0cf4efb1 2018-09-29 stsp
4632 0cf4efb1 2018-09-29 stsp static const struct got_error *
4633 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4634 917d79a7 2022-07-01 stsp {
4635 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4636 917d79a7 2022-07-01 stsp
4637 917d79a7 2022-07-01 stsp view->count = 0;
4638 917d79a7 2022-07-01 stsp wclear(view->window);
4639 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4640 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4641 917d79a7 2022-07-01 stsp s->matched_line = 0;
4642 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4643 917d79a7 2022-07-01 stsp return create_diff(s);
4644 917d79a7 2022-07-01 stsp }
4645 917d79a7 2022-07-01 stsp
4646 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4647 c0f61fa4 2022-07-11 mark int, int, int);
4648 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4649 c0f61fa4 2022-07-11 mark int, int);
4650 c0f61fa4 2022-07-11 mark
4651 917d79a7 2022-07-01 stsp static const struct got_error *
4652 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4653 e5a0f69f 2018-08-18 stsp {
4654 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4655 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4656 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4657 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4658 826082fe 2020-12-10 stsp char *line = NULL;
4659 826082fe 2020-12-10 stsp size_t linesize = 0;
4660 826082fe 2020-12-10 stsp ssize_t linelen;
4661 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4662 e5a0f69f 2018-08-18 stsp
4663 e5a0f69f 2018-08-18 stsp switch (ch) {
4664 145b6838 2022-06-16 stsp case '0':
4665 145b6838 2022-06-16 stsp view->x = 0;
4666 145b6838 2022-06-16 stsp break;
4667 145b6838 2022-06-16 stsp case '$':
4668 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4669 640cd7ff 2022-06-22 mark view->count = 0;
4670 145b6838 2022-06-16 stsp break;
4671 145b6838 2022-06-16 stsp case KEY_RIGHT:
4672 145b6838 2022-06-16 stsp case 'l':
4673 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4674 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4675 640cd7ff 2022-06-22 mark else
4676 640cd7ff 2022-06-22 mark view->count = 0;
4677 145b6838 2022-06-16 stsp break;
4678 145b6838 2022-06-16 stsp case KEY_LEFT:
4679 145b6838 2022-06-16 stsp case 'h':
4680 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4681 640cd7ff 2022-06-22 mark if (view->x <= 0)
4682 640cd7ff 2022-06-22 mark view->count = 0;
4683 145b6838 2022-06-16 stsp break;
4684 64453f7e 2020-11-21 stsp case 'a':
4685 3dbaef42 2020-11-24 stsp case 'w':
4686 3dbaef42 2020-11-24 stsp if (ch == 'a')
4687 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4688 3dbaef42 2020-11-24 stsp if (ch == 'w')
4689 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4690 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4691 912a3f79 2021-08-30 j break;
4692 912a3f79 2021-08-30 j case 'g':
4693 912a3f79 2021-08-30 j case KEY_HOME:
4694 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4695 640cd7ff 2022-06-22 mark view->count = 0;
4696 64453f7e 2020-11-21 stsp break;
4697 912a3f79 2021-08-30 j case 'G':
4698 912a3f79 2021-08-30 j case KEY_END:
4699 640cd7ff 2022-06-22 mark view->count = 0;
4700 912a3f79 2021-08-30 j if (s->eof)
4701 912a3f79 2021-08-30 j break;
4702 912a3f79 2021-08-30 j
4703 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4704 912a3f79 2021-08-30 j s->eof = 1;
4705 912a3f79 2021-08-30 j break;
4706 1e37a5c2 2019-05-12 jcs case 'k':
4707 1e37a5c2 2019-05-12 jcs case KEY_UP:
4708 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4709 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4710 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4711 640cd7ff 2022-06-22 mark else
4712 640cd7ff 2022-06-22 mark view->count = 0;
4713 1e37a5c2 2019-05-12 jcs break;
4714 83cc4199 2022-06-13 stsp case CTRL('u'):
4715 33c3719a 2022-06-15 stsp case 'u':
4716 83cc4199 2022-06-13 stsp nscroll /= 2;
4717 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4718 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4719 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4720 61417565 2022-06-20 mark case 'b':
4721 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4722 640cd7ff 2022-06-22 mark view->count = 0;
4723 26ed57b2 2018-05-19 stsp break;
4724 640cd7ff 2022-06-22 mark }
4725 1e37a5c2 2019-05-12 jcs i = 0;
4726 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4727 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4728 1e37a5c2 2019-05-12 jcs break;
4729 1e37a5c2 2019-05-12 jcs case 'j':
4730 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4731 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4732 1e37a5c2 2019-05-12 jcs if (!s->eof)
4733 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4734 640cd7ff 2022-06-22 mark else
4735 640cd7ff 2022-06-22 mark view->count = 0;
4736 1e37a5c2 2019-05-12 jcs break;
4737 83cc4199 2022-06-13 stsp case CTRL('d'):
4738 33c3719a 2022-06-15 stsp case 'd':
4739 83cc4199 2022-06-13 stsp nscroll /= 2;
4740 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4741 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4742 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4743 61417565 2022-06-20 mark case 'f':
4744 1e37a5c2 2019-05-12 jcs case ' ':
4745 640cd7ff 2022-06-22 mark if (s->eof) {
4746 640cd7ff 2022-06-22 mark view->count = 0;
4747 1e37a5c2 2019-05-12 jcs break;
4748 640cd7ff 2022-06-22 mark }
4749 1e37a5c2 2019-05-12 jcs i = 0;
4750 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4751 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4753 826082fe 2020-12-10 stsp if (linelen == -1) {
4754 826082fe 2020-12-10 stsp if (feof(s->f)) {
4755 826082fe 2020-12-10 stsp s->eof = 1;
4756 826082fe 2020-12-10 stsp } else
4757 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4758 34bc9ec9 2019-02-22 stsp break;
4759 826082fe 2020-12-10 stsp }
4760 1e37a5c2 2019-05-12 jcs }
4761 826082fe 2020-12-10 stsp free(line);
4762 1e37a5c2 2019-05-12 jcs break;
4763 1e37a5c2 2019-05-12 jcs case '[':
4764 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4765 1e37a5c2 2019-05-12 jcs s->diff_context--;
4766 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4767 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4768 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4769 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4770 27829c9e 2020-11-21 stsp s->nlines) {
4771 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4772 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4773 27829c9e 2020-11-21 stsp }
4774 640cd7ff 2022-06-22 mark } else
4775 640cd7ff 2022-06-22 mark view->count = 0;
4776 1e37a5c2 2019-05-12 jcs break;
4777 1e37a5c2 2019-05-12 jcs case ']':
4778 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4779 1e37a5c2 2019-05-12 jcs s->diff_context++;
4780 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4781 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4782 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4783 640cd7ff 2022-06-22 mark } else
4784 640cd7ff 2022-06-22 mark view->count = 0;
4785 1e37a5c2 2019-05-12 jcs break;
4786 1e37a5c2 2019-05-12 jcs case '<':
4787 1e37a5c2 2019-05-12 jcs case ',':
4788 2b3e6702 2022-07-20 mark case 'K':
4789 c0f61fa4 2022-07-11 mark up = 1;
4790 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4791 c0f61fa4 2022-07-11 mark case '>':
4792 c0f61fa4 2022-07-11 mark case '.':
4793 2b3e6702 2022-07-20 mark case 'J':
4794 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4795 640cd7ff 2022-06-22 mark view->count = 0;
4796 48ae06ee 2018-10-18 stsp break;
4797 640cd7ff 2022-06-22 mark }
4798 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4799 6524637e 2019-02-21 stsp
4800 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4801 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4802 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4803 15a087fe 2019-02-21 stsp
4804 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4805 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4806 c0f61fa4 2022-07-11 mark if (err)
4807 c0f61fa4 2022-07-11 mark break;
4808 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4809 15a087fe 2019-02-21 stsp
4810 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4811 c0f61fa4 2022-07-11 mark break;
4812 15a087fe 2019-02-21 stsp
4813 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4814 c0f61fa4 2022-07-11 mark if (err)
4815 c0f61fa4 2022-07-11 mark break;
4816 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4817 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4818 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4819 5e224a3e 2019-02-22 stsp
4820 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4821 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4822 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4823 c0f61fa4 2022-07-11 mark
4824 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4825 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4826 c0f61fa4 2022-07-11 mark if (err)
4827 c0f61fa4 2022-07-11 mark break;
4828 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4829 5a8b5076 2020-12-05 stsp
4830 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4831 c0f61fa4 2022-07-11 mark break;
4832 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4833 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4834 c0f61fa4 2022-07-11 mark bs->selected_line);
4835 c0f61fa4 2022-07-11 mark if (id == NULL)
4836 c0f61fa4 2022-07-11 mark break;
4837 15a087fe 2019-02-21 stsp
4838 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4839 c0f61fa4 2022-07-11 mark break;
4840 15a087fe 2019-02-21 stsp
4841 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4842 c0f61fa4 2022-07-11 mark if (err)
4843 c0f61fa4 2022-07-11 mark break;
4844 c0f61fa4 2022-07-11 mark }
4845 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4846 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4847 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4848 145b6838 2022-06-16 stsp view->x = 0;
4849 1e37a5c2 2019-05-12 jcs
4850 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4851 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4852 1e37a5c2 2019-05-12 jcs break;
4853 1e37a5c2 2019-05-12 jcs default:
4854 640cd7ff 2022-06-22 mark view->count = 0;
4855 1e37a5c2 2019-05-12 jcs break;
4856 26ed57b2 2018-05-19 stsp }
4857 e5a0f69f 2018-08-18 stsp
4858 bcbd79e2 2018-08-19 stsp return err;
4859 26ed57b2 2018-05-19 stsp }
4860 26ed57b2 2018-05-19 stsp
4861 4ed7e80c 2018-05-20 stsp static const struct got_error *
4862 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4863 9f7d7167 2018-04-29 stsp {
4864 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4865 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4866 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4867 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4868 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4869 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4870 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4871 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4872 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4873 3dbaef42 2020-11-24 stsp const char *errstr;
4874 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4875 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4876 70ac5f84 2019-03-28 stsp
4877 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4878 26ed57b2 2018-05-19 stsp switch (ch) {
4879 64453f7e 2020-11-21 stsp case 'a':
4880 64453f7e 2020-11-21 stsp force_text_diff = 1;
4881 3dbaef42 2020-11-24 stsp break;
4882 3dbaef42 2020-11-24 stsp case 'C':
4883 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4884 3dbaef42 2020-11-24 stsp &errstr);
4885 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4886 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4887 5a20d08d 2022-02-09 op errstr, errstr);
4888 64453f7e 2020-11-21 stsp break;
4889 09b5bff8 2020-02-23 naddy case 'r':
4890 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4891 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4892 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4893 09b5bff8 2020-02-23 naddy optarg);
4894 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4895 09b5bff8 2020-02-23 naddy break;
4896 3dbaef42 2020-11-24 stsp case 'w':
4897 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4898 3dbaef42 2020-11-24 stsp break;
4899 26ed57b2 2018-05-19 stsp default:
4900 17020d27 2019-03-07 stsp usage_diff();
4901 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4902 26ed57b2 2018-05-19 stsp }
4903 26ed57b2 2018-05-19 stsp }
4904 26ed57b2 2018-05-19 stsp
4905 26ed57b2 2018-05-19 stsp argc -= optind;
4906 26ed57b2 2018-05-19 stsp argv += optind;
4907 26ed57b2 2018-05-19 stsp
4908 26ed57b2 2018-05-19 stsp if (argc == 0) {
4909 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4910 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4911 15a94983 2018-12-23 stsp id_str1 = argv[0];
4912 15a94983 2018-12-23 stsp id_str2 = argv[1];
4913 26ed57b2 2018-05-19 stsp } else
4914 26ed57b2 2018-05-19 stsp usage_diff();
4915 eb6600df 2019-01-04 stsp
4916 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4917 0ae84acc 2022-06-15 tracey if (error)
4918 0ae84acc 2022-06-15 tracey goto done;
4919 0ae84acc 2022-06-15 tracey
4920 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4921 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4922 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4923 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4924 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4925 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4926 c156c7a4 2020-12-18 stsp goto done;
4927 a273ac94 2020-02-23 naddy if (worktree)
4928 a273ac94 2020-02-23 naddy repo_path =
4929 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4930 a273ac94 2020-02-23 naddy else
4931 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4932 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4933 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4934 c156c7a4 2020-12-18 stsp goto done;
4935 c156c7a4 2020-12-18 stsp }
4936 a273ac94 2020-02-23 naddy }
4937 a273ac94 2020-02-23 naddy
4938 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4939 eb6600df 2019-01-04 stsp if (error)
4940 eb6600df 2019-01-04 stsp goto done;
4941 26ed57b2 2018-05-19 stsp
4942 a273ac94 2020-02-23 naddy init_curses();
4943 a273ac94 2020-02-23 naddy
4944 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4945 51a10b52 2020-12-26 stsp if (error)
4946 51a10b52 2020-12-26 stsp goto done;
4947 51a10b52 2020-12-26 stsp
4948 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4949 26ed57b2 2018-05-19 stsp if (error)
4950 26ed57b2 2018-05-19 stsp goto done;
4951 26ed57b2 2018-05-19 stsp
4952 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4953 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4954 26ed57b2 2018-05-19 stsp if (error)
4955 26ed57b2 2018-05-19 stsp goto done;
4956 26ed57b2 2018-05-19 stsp
4957 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4958 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4959 26ed57b2 2018-05-19 stsp if (error)
4960 26ed57b2 2018-05-19 stsp goto done;
4961 26ed57b2 2018-05-19 stsp
4962 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4963 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4964 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4965 ea5e7bb5 2018-08-01 stsp goto done;
4966 ea5e7bb5 2018-08-01 stsp }
4967 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4968 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4969 5dc9f4bc 2018-08-04 stsp if (error)
4970 5dc9f4bc 2018-08-04 stsp goto done;
4971 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4972 26ed57b2 2018-05-19 stsp done:
4973 3dbaef42 2020-11-24 stsp free(label1);
4974 3dbaef42 2020-11-24 stsp free(label2);
4975 c02c541e 2019-03-29 stsp free(repo_path);
4976 a273ac94 2020-02-23 naddy free(cwd);
4977 1d0f4054 2021-06-17 stsp if (repo) {
4978 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4979 1d0f4054 2021-06-17 stsp if (error == NULL)
4980 1d0f4054 2021-06-17 stsp error = close_err;
4981 1d0f4054 2021-06-17 stsp }
4982 a273ac94 2020-02-23 naddy if (worktree)
4983 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4984 0ae84acc 2022-06-15 tracey if (pack_fds) {
4985 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4986 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4987 0ae84acc 2022-06-15 tracey if (error == NULL)
4988 0ae84acc 2022-06-15 tracey error = pack_err;
4989 0ae84acc 2022-06-15 tracey }
4990 51a10b52 2020-12-26 stsp tog_free_refs();
4991 26ed57b2 2018-05-19 stsp return error;
4992 9f7d7167 2018-04-29 stsp }
4993 9f7d7167 2018-04-29 stsp
4994 4ed7e80c 2018-05-20 stsp __dead static void
4995 9f7d7167 2018-04-29 stsp usage_blame(void)
4996 9f7d7167 2018-04-29 stsp {
4997 80ddbec8 2018-04-29 stsp endwin();
4998 87411fa9 2022-06-16 stsp fprintf(stderr,
4999 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
5000 9f7d7167 2018-04-29 stsp getprogname());
5001 9f7d7167 2018-04-29 stsp exit(1);
5002 9f7d7167 2018-04-29 stsp }
5003 84451b3e 2018-07-10 stsp
5004 84451b3e 2018-07-10 stsp struct tog_blame_line {
5005 84451b3e 2018-07-10 stsp int annotated;
5006 84451b3e 2018-07-10 stsp struct got_object_id *id;
5007 84451b3e 2018-07-10 stsp };
5008 9f7d7167 2018-04-29 stsp
5009 4ed7e80c 2018-05-20 stsp static const struct got_error *
5010 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5011 84451b3e 2018-07-10 stsp {
5012 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5013 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5014 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5015 84451b3e 2018-07-10 stsp const struct got_error *err;
5016 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5017 826082fe 2020-12-10 stsp char *line = NULL;
5018 826082fe 2020-12-10 stsp size_t linesize = 0;
5019 826082fe 2020-12-10 stsp ssize_t linelen;
5020 84451b3e 2018-07-10 stsp wchar_t *wline;
5021 27a741e5 2019-09-11 stsp int width;
5022 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5023 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5024 ab089a2a 2018-07-12 stsp char *id_str;
5025 11b20872 2019-11-08 stsp struct tog_color *tc;
5026 ab089a2a 2018-07-12 stsp
5027 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5028 ab089a2a 2018-07-12 stsp if (err)
5029 ab089a2a 2018-07-12 stsp return err;
5030 84451b3e 2018-07-10 stsp
5031 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5032 f7d12f7e 2018-08-01 stsp werase(view->window);
5033 84451b3e 2018-07-10 stsp
5034 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5035 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5036 ab089a2a 2018-07-12 stsp free(id_str);
5037 ab089a2a 2018-07-12 stsp return err;
5038 ab089a2a 2018-07-12 stsp }
5039 ab089a2a 2018-07-12 stsp
5040 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5041 ab089a2a 2018-07-12 stsp free(line);
5042 2550e4c3 2018-07-13 stsp line = NULL;
5043 1cae65b4 2019-09-22 stsp if (err)
5044 1cae65b4 2019-09-22 stsp return err;
5045 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5046 a3404814 2018-09-02 stsp wstandout(view->window);
5047 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5048 11b20872 2019-11-08 stsp if (tc)
5049 11b20872 2019-11-08 stsp wattr_on(view->window,
5050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5051 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5052 11b20872 2019-11-08 stsp if (tc)
5053 11b20872 2019-11-08 stsp wattr_off(view->window,
5054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5055 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5056 a3404814 2018-09-02 stsp wstandend(view->window);
5057 2550e4c3 2018-07-13 stsp free(wline);
5058 2550e4c3 2018-07-13 stsp wline = NULL;
5059 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5060 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5061 ab089a2a 2018-07-12 stsp
5062 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5063 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5064 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5065 ab089a2a 2018-07-12 stsp free(id_str);
5066 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5067 ab089a2a 2018-07-12 stsp }
5068 ab089a2a 2018-07-12 stsp free(id_str);
5069 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5070 3f60a8ef 2018-07-10 stsp free(line);
5071 2550e4c3 2018-07-13 stsp line = NULL;
5072 3f60a8ef 2018-07-10 stsp if (err)
5073 3f60a8ef 2018-07-10 stsp return err;
5074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5075 2550e4c3 2018-07-13 stsp free(wline);
5076 2550e4c3 2018-07-13 stsp wline = NULL;
5077 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5078 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5079 3f60a8ef 2018-07-10 stsp
5080 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5081 145b6838 2022-06-16 stsp view->maxx = 0;
5082 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5083 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5084 826082fe 2020-12-10 stsp if (linelen == -1) {
5085 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5086 826082fe 2020-12-10 stsp s->eof = 1;
5087 826082fe 2020-12-10 stsp break;
5088 826082fe 2020-12-10 stsp }
5089 84451b3e 2018-07-10 stsp free(line);
5090 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5091 84451b3e 2018-07-10 stsp }
5092 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5093 826082fe 2020-12-10 stsp continue;
5094 1853e0f4 2022-06-16 stsp
5095 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5096 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5097 1853e0f4 2022-06-16 stsp if (err) {
5098 1853e0f4 2022-06-16 stsp free(line);
5099 1853e0f4 2022-06-16 stsp return err;
5100 1853e0f4 2022-06-16 stsp }
5101 1853e0f4 2022-06-16 stsp free(wline);
5102 1853e0f4 2022-06-16 stsp wline = NULL;
5103 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5104 84451b3e 2018-07-10 stsp
5105 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5106 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5107 b700b5d6 2018-07-10 stsp
5108 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5109 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5110 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5111 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5112 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5113 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5114 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5115 8d0fe45a 2019-08-12 stsp char *id_str;
5116 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5117 87411fa9 2022-06-16 stsp blame_line->id);
5118 8d0fe45a 2019-08-12 stsp if (err) {
5119 8d0fe45a 2019-08-12 stsp free(line);
5120 8d0fe45a 2019-08-12 stsp return err;
5121 8d0fe45a 2019-08-12 stsp }
5122 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5123 11b20872 2019-11-08 stsp if (tc)
5124 11b20872 2019-11-08 stsp wattr_on(view->window,
5125 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5126 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5127 11b20872 2019-11-08 stsp if (tc)
5128 11b20872 2019-11-08 stsp wattr_off(view->window,
5129 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5130 8d0fe45a 2019-08-12 stsp free(id_str);
5131 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5132 8d0fe45a 2019-08-12 stsp } else {
5133 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5134 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5135 84451b3e 2018-07-10 stsp }
5136 ee41ec32 2018-07-10 stsp } else {
5137 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5138 ee41ec32 2018-07-10 stsp prev_id = NULL;
5139 ee41ec32 2018-07-10 stsp }
5140 84451b3e 2018-07-10 stsp
5141 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5142 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5143 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5144 27a741e5 2019-09-11 stsp
5145 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5146 41605754 2020-11-12 stsp width = 9;
5147 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5148 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5149 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5150 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5151 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5152 41605754 2020-11-12 stsp if (err) {
5153 41605754 2020-11-12 stsp free(line);
5154 41605754 2020-11-12 stsp return err;
5155 41605754 2020-11-12 stsp }
5156 41605754 2020-11-12 stsp width += 9;
5157 41605754 2020-11-12 stsp } else {
5158 4cb9d869 2022-06-16 stsp int skip;
5159 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5160 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5161 4cb9d869 2022-06-16 stsp if (err) {
5162 4cb9d869 2022-06-16 stsp free(line);
5163 4cb9d869 2022-06-16 stsp return err;
5164 145b6838 2022-06-16 stsp }
5165 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5166 a75b3e2d 2022-06-16 stsp width += 9;
5167 41605754 2020-11-12 stsp free(wline);
5168 41605754 2020-11-12 stsp wline = NULL;
5169 41605754 2020-11-12 stsp }
5170 41605754 2020-11-12 stsp
5171 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5172 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5173 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5174 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5175 84451b3e 2018-07-10 stsp }
5176 826082fe 2020-12-10 stsp free(line);
5177 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5178 84451b3e 2018-07-10 stsp
5179 9b058f45 2022-06-30 mark view_border(view);
5180 84451b3e 2018-07-10 stsp
5181 84451b3e 2018-07-10 stsp return NULL;
5182 84451b3e 2018-07-10 stsp }
5183 84451b3e 2018-07-10 stsp
5184 84451b3e 2018-07-10 stsp static const struct got_error *
5185 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5186 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5187 84451b3e 2018-07-10 stsp {
5188 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5189 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5190 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5191 1a76625f 2018-10-22 stsp int errcode;
5192 84451b3e 2018-07-10 stsp
5193 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5194 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5195 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5196 84451b3e 2018-07-10 stsp
5197 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5198 1a76625f 2018-10-22 stsp if (errcode)
5199 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5200 84451b3e 2018-07-10 stsp
5201 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5202 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5203 d68a0a7d 2018-07-10 stsp goto done;
5204 d68a0a7d 2018-07-10 stsp }
5205 d68a0a7d 2018-07-10 stsp
5206 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5207 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5208 d68a0a7d 2018-07-10 stsp
5209 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5210 d68a0a7d 2018-07-10 stsp if (line->annotated)
5211 d68a0a7d 2018-07-10 stsp goto done;
5212 d68a0a7d 2018-07-10 stsp
5213 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5214 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5215 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5216 84451b3e 2018-07-10 stsp goto done;
5217 84451b3e 2018-07-10 stsp }
5218 84451b3e 2018-07-10 stsp line->annotated = 1;
5219 84451b3e 2018-07-10 stsp done:
5220 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5221 1a76625f 2018-10-22 stsp if (errcode)
5222 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5223 84451b3e 2018-07-10 stsp return err;
5224 84451b3e 2018-07-10 stsp }
5225 84451b3e 2018-07-10 stsp
5226 84451b3e 2018-07-10 stsp static void *
5227 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5228 84451b3e 2018-07-10 stsp {
5229 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5230 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5231 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5232 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5233 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5234 1b484788 2022-06-28 tracey
5235 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5236 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5237 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5238 e6e73e55 2022-06-30 tracey
5239 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5240 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5241 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5242 e6e73e55 2022-06-30 tracey goto done;
5243 e6e73e55 2022-06-30 tracey }
5244 18430de3 2018-07-10 stsp
5245 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5246 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5247 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5248 e6e73e55 2022-06-30 tracey goto done;
5249 e6e73e55 2022-06-30 tracey }
5250 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5251 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5252 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5253 e6e73e55 2022-06-30 tracey goto done;
5254 e6e73e55 2022-06-30 tracey }
5255 e6e73e55 2022-06-30 tracey
5256 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5257 61266923 2020-01-14 stsp if (err)
5258 e6e73e55 2022-06-30 tracey goto done;
5259 61266923 2020-01-14 stsp
5260 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5261 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5262 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5263 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5264 fc06ba56 2019-08-22 stsp err = NULL;
5265 18430de3 2018-07-10 stsp
5266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5267 e6e73e55 2022-06-30 tracey if (errcode) {
5268 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5269 e6e73e55 2022-06-30 tracey goto done;
5270 e6e73e55 2022-06-30 tracey }
5271 18430de3 2018-07-10 stsp
5272 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5273 1d0f4054 2021-06-17 stsp if (err == NULL)
5274 1d0f4054 2021-06-17 stsp err = close_err;
5275 c9beca56 2018-07-22 stsp ta->repo = NULL;
5276 c9beca56 2018-07-22 stsp *ta->complete = 1;
5277 18430de3 2018-07-10 stsp
5278 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5279 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5280 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5281 18430de3 2018-07-10 stsp
5282 e6e73e55 2022-06-30 tracey done:
5283 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5284 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5285 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5286 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5287 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5288 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5289 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5290 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5291 1b484788 2022-06-28 tracey
5292 18430de3 2018-07-10 stsp return (void *)err;
5293 84451b3e 2018-07-10 stsp }
5294 84451b3e 2018-07-10 stsp
5295 245d91c1 2018-07-12 stsp static struct got_object_id *
5296 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5297 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5298 245d91c1 2018-07-12 stsp {
5299 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5300 8d0fe45a 2019-08-12 stsp
5301 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5302 8d0fe45a 2019-08-12 stsp return NULL;
5303 b880a918 2018-07-10 stsp
5304 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5305 c0f61fa4 2022-07-11 mark if (!line->annotated)
5306 c0f61fa4 2022-07-11 mark return NULL;
5307 c0f61fa4 2022-07-11 mark
5308 c0f61fa4 2022-07-11 mark return line->id;
5309 c0f61fa4 2022-07-11 mark }
5310 c0f61fa4 2022-07-11 mark
5311 c0f61fa4 2022-07-11 mark static struct got_object_id *
5312 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5313 c0f61fa4 2022-07-11 mark int lineno)
5314 c0f61fa4 2022-07-11 mark {
5315 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5316 c0f61fa4 2022-07-11 mark
5317 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5318 c0f61fa4 2022-07-11 mark return NULL;
5319 c0f61fa4 2022-07-11 mark
5320 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5321 245d91c1 2018-07-12 stsp if (!line->annotated)
5322 245d91c1 2018-07-12 stsp return NULL;
5323 245d91c1 2018-07-12 stsp
5324 245d91c1 2018-07-12 stsp return line->id;
5325 b880a918 2018-07-10 stsp }
5326 245d91c1 2018-07-12 stsp
5327 b880a918 2018-07-10 stsp static const struct got_error *
5328 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5329 a70480e0 2018-06-23 stsp {
5330 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5331 245d91c1 2018-07-12 stsp int i;
5332 245d91c1 2018-07-12 stsp
5333 245d91c1 2018-07-12 stsp if (blame->thread) {
5334 1a76625f 2018-10-22 stsp int errcode;
5335 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5336 1a76625f 2018-10-22 stsp if (errcode)
5337 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5338 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5339 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5340 1a76625f 2018-10-22 stsp if (errcode)
5341 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5342 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5343 1a76625f 2018-10-22 stsp if (errcode)
5344 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5345 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5346 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5347 245d91c1 2018-07-12 stsp err = NULL;
5348 245d91c1 2018-07-12 stsp blame->thread = NULL;
5349 245d91c1 2018-07-12 stsp }
5350 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5351 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5352 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5353 1d0f4054 2021-06-17 stsp if (err == NULL)
5354 1d0f4054 2021-06-17 stsp err = close_err;
5355 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5356 245d91c1 2018-07-12 stsp }
5357 245d91c1 2018-07-12 stsp if (blame->f) {
5358 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5359 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5360 245d91c1 2018-07-12 stsp blame->f = NULL;
5361 245d91c1 2018-07-12 stsp }
5362 57670559 2018-12-23 stsp if (blame->lines) {
5363 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5364 57670559 2018-12-23 stsp free(blame->lines[i].id);
5365 57670559 2018-12-23 stsp free(blame->lines);
5366 57670559 2018-12-23 stsp blame->lines = NULL;
5367 57670559 2018-12-23 stsp }
5368 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5369 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5370 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5371 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5372 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5373 0ae84acc 2022-06-15 tracey if (err == NULL)
5374 0ae84acc 2022-06-15 tracey err = pack_err;
5375 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5376 0ae84acc 2022-06-15 tracey }
5377 245d91c1 2018-07-12 stsp return err;
5378 245d91c1 2018-07-12 stsp }
5379 245d91c1 2018-07-12 stsp
5380 245d91c1 2018-07-12 stsp static const struct got_error *
5381 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5382 fc06ba56 2019-08-22 stsp {
5383 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5384 fc06ba56 2019-08-22 stsp int *done = arg;
5385 fc06ba56 2019-08-22 stsp int errcode;
5386 fc06ba56 2019-08-22 stsp
5387 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5388 fc06ba56 2019-08-22 stsp if (errcode)
5389 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5390 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5391 fc06ba56 2019-08-22 stsp
5392 fc06ba56 2019-08-22 stsp if (*done)
5393 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5394 fc06ba56 2019-08-22 stsp
5395 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5396 fc06ba56 2019-08-22 stsp if (errcode)
5397 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5398 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5399 fc06ba56 2019-08-22 stsp
5400 fc06ba56 2019-08-22 stsp return err;
5401 fc06ba56 2019-08-22 stsp }
5402 fc06ba56 2019-08-22 stsp
5403 fc06ba56 2019-08-22 stsp static const struct got_error *
5404 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5405 245d91c1 2018-07-12 stsp {
5406 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5407 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5408 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5409 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5410 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5411 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5412 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5413 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5414 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5415 a70480e0 2018-06-23 stsp
5416 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5417 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5418 27d434c2 2018-09-15 stsp if (err)
5419 15a94983 2018-12-23 stsp return err;
5420 eb81bc23 2022-06-28 tracey
5421 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5422 eb81bc23 2022-06-28 tracey if (fd == -1) {
5423 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5424 eb81bc23 2022-06-28 tracey goto done;
5425 eb81bc23 2022-06-28 tracey }
5426 a44927cc 2022-04-07 stsp
5427 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5428 a44927cc 2022-04-07 stsp if (err)
5429 a44927cc 2022-04-07 stsp goto done;
5430 27d434c2 2018-09-15 stsp
5431 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5432 84451b3e 2018-07-10 stsp if (err)
5433 84451b3e 2018-07-10 stsp goto done;
5434 27d434c2 2018-09-15 stsp
5435 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5436 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5437 84451b3e 2018-07-10 stsp goto done;
5438 84451b3e 2018-07-10 stsp }
5439 a70480e0 2018-06-23 stsp
5440 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5441 a70480e0 2018-06-23 stsp if (err)
5442 a70480e0 2018-06-23 stsp goto done;
5443 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5444 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5445 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5446 84451b3e 2018-07-10 stsp goto done;
5447 84451b3e 2018-07-10 stsp }
5448 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5449 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5450 1fddf795 2021-01-20 stsp if (err)
5451 1fddf795 2021-01-20 stsp goto done;
5452 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5453 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5454 84451b3e 2018-07-10 stsp goto done;
5455 1fddf795 2021-01-20 stsp }
5456 b02560ec 2019-08-19 stsp
5457 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5458 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5459 b02560ec 2019-08-19 stsp blame->nlines--;
5460 a70480e0 2018-06-23 stsp
5461 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5462 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5463 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5464 84451b3e 2018-07-10 stsp goto done;
5465 84451b3e 2018-07-10 stsp }
5466 a70480e0 2018-06-23 stsp
5467 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5468 bd24772e 2018-07-11 stsp if (err)
5469 bd24772e 2018-07-11 stsp goto done;
5470 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5471 0ae84acc 2022-06-15 tracey pack_fds);
5472 0ae84acc 2022-06-15 tracey if (err)
5473 0ae84acc 2022-06-15 tracey goto done;
5474 bd24772e 2018-07-11 stsp
5475 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5476 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5477 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5478 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5479 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5480 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5481 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5482 245d91c1 2018-07-12 stsp goto done;
5483 245d91c1 2018-07-12 stsp }
5484 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5485 245d91c1 2018-07-12 stsp
5486 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5487 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5488 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5489 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5490 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5491 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5492 a5388363 2020-12-01 naddy s->blame_complete = 0;
5493 f5a09613 2020-12-13 naddy
5494 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5495 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5496 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5497 f5a09613 2020-12-13 naddy s->selected_line = 1;
5498 f5a09613 2020-12-13 naddy }
5499 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5500 245d91c1 2018-07-12 stsp
5501 245d91c1 2018-07-12 stsp done:
5502 a44927cc 2022-04-07 stsp if (commit)
5503 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5504 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5505 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5506 245d91c1 2018-07-12 stsp if (blob)
5507 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5508 27d434c2 2018-09-15 stsp free(obj_id);
5509 245d91c1 2018-07-12 stsp if (err)
5510 245d91c1 2018-07-12 stsp stop_blame(blame);
5511 245d91c1 2018-07-12 stsp return err;
5512 245d91c1 2018-07-12 stsp }
5513 245d91c1 2018-07-12 stsp
5514 245d91c1 2018-07-12 stsp static const struct got_error *
5515 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5516 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5517 245d91c1 2018-07-12 stsp {
5518 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5519 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5520 dbc6a6b6 2018-07-12 stsp
5521 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5522 245d91c1 2018-07-12 stsp
5523 c4843652 2019-08-12 stsp s->path = strdup(path);
5524 c4843652 2019-08-12 stsp if (s->path == NULL)
5525 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5526 c4843652 2019-08-12 stsp
5527 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5528 c4843652 2019-08-12 stsp if (err) {
5529 c4843652 2019-08-12 stsp free(s->path);
5530 7cbe629d 2018-08-04 stsp return err;
5531 c4843652 2019-08-12 stsp }
5532 245d91c1 2018-07-12 stsp
5533 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5534 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5535 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5536 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5537 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5538 fb2756b9 2018-08-04 stsp s->repo = repo;
5539 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5540 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5541 7cbe629d 2018-08-04 stsp
5542 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5543 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5544 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5545 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5546 11b20872 2019-11-08 stsp if (err)
5547 11b20872 2019-11-08 stsp return err;
5548 11b20872 2019-11-08 stsp }
5549 11b20872 2019-11-08 stsp
5550 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5551 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5552 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5553 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5554 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5555 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5556 e5a0f69f 2018-08-18 stsp
5557 a5388363 2020-12-01 naddy return run_blame(view);
5558 7cbe629d 2018-08-04 stsp }
5559 7cbe629d 2018-08-04 stsp
5560 e5a0f69f 2018-08-18 stsp static const struct got_error *
5561 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5562 7cbe629d 2018-08-04 stsp {
5563 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5564 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5565 7cbe629d 2018-08-04 stsp
5566 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5567 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5568 e5a0f69f 2018-08-18 stsp
5569 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5570 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5571 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5572 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5573 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5574 7cbe629d 2018-08-04 stsp }
5575 e5a0f69f 2018-08-18 stsp
5576 e5a0f69f 2018-08-18 stsp free(s->path);
5577 11b20872 2019-11-08 stsp free_colors(&s->colors);
5578 e5a0f69f 2018-08-18 stsp return err;
5579 7cbe629d 2018-08-04 stsp }
5580 7cbe629d 2018-08-04 stsp
5581 7cbe629d 2018-08-04 stsp static const struct got_error *
5582 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5583 6c4c42e0 2019-06-24 stsp {
5584 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5585 6c4c42e0 2019-06-24 stsp
5586 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5587 6c4c42e0 2019-06-24 stsp return NULL;
5588 6c4c42e0 2019-06-24 stsp }
5589 6c4c42e0 2019-06-24 stsp
5590 6c4c42e0 2019-06-24 stsp static const struct got_error *
5591 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5592 6c4c42e0 2019-06-24 stsp {
5593 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5594 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5595 6c4c42e0 2019-06-24 stsp int lineno;
5596 cb713507 2022-06-16 stsp char *line = NULL;
5597 826082fe 2020-12-10 stsp size_t linesize = 0;
5598 826082fe 2020-12-10 stsp ssize_t linelen;
5599 6c4c42e0 2019-06-24 stsp
5600 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5601 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5602 6c4c42e0 2019-06-24 stsp return NULL;
5603 6c4c42e0 2019-06-24 stsp }
5604 6c4c42e0 2019-06-24 stsp
5605 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5606 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5607 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5608 6c4c42e0 2019-06-24 stsp else
5609 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5610 487cd7d2 2021-12-17 stsp } else
5611 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5612 6c4c42e0 2019-06-24 stsp
5613 6c4c42e0 2019-06-24 stsp while (1) {
5614 6c4c42e0 2019-06-24 stsp off_t offset;
5615 6c4c42e0 2019-06-24 stsp
5616 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5617 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5618 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5619 6c4c42e0 2019-06-24 stsp break;
5620 6c4c42e0 2019-06-24 stsp }
5621 2246482e 2019-06-25 stsp
5622 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5623 6c4c42e0 2019-06-24 stsp lineno = 1;
5624 6c4c42e0 2019-06-24 stsp else
5625 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5626 6c4c42e0 2019-06-24 stsp }
5627 6c4c42e0 2019-06-24 stsp
5628 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5629 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5630 6c4c42e0 2019-06-24 stsp free(line);
5631 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5632 6c4c42e0 2019-06-24 stsp }
5633 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5634 cb713507 2022-06-16 stsp if (linelen != -1) {
5635 cb713507 2022-06-16 stsp char *exstr;
5636 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5637 cb713507 2022-06-16 stsp if (err)
5638 cb713507 2022-06-16 stsp break;
5639 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5640 cb713507 2022-06-16 stsp &view->regmatch)) {
5641 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5642 cb713507 2022-06-16 stsp s->matched_line = lineno;
5643 cb713507 2022-06-16 stsp free(exstr);
5644 cb713507 2022-06-16 stsp break;
5645 cb713507 2022-06-16 stsp }
5646 cb713507 2022-06-16 stsp free(exstr);
5647 6c4c42e0 2019-06-24 stsp }
5648 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5649 6c4c42e0 2019-06-24 stsp lineno++;
5650 6c4c42e0 2019-06-24 stsp else
5651 6c4c42e0 2019-06-24 stsp lineno--;
5652 6c4c42e0 2019-06-24 stsp }
5653 826082fe 2020-12-10 stsp free(line);
5654 6c4c42e0 2019-06-24 stsp
5655 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5656 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5657 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5658 6c4c42e0 2019-06-24 stsp }
5659 6c4c42e0 2019-06-24 stsp
5660 145b6838 2022-06-16 stsp return err;
5661 6c4c42e0 2019-06-24 stsp }
5662 6c4c42e0 2019-06-24 stsp
5663 6c4c42e0 2019-06-24 stsp static const struct got_error *
5664 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5665 7cbe629d 2018-08-04 stsp {
5666 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5667 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5668 2b380cc8 2018-10-24 stsp int errcode;
5669 2b380cc8 2018-10-24 stsp
5670 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5671 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5672 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5673 2b380cc8 2018-10-24 stsp if (errcode)
5674 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5675 51fe7530 2019-08-19 stsp
5676 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5677 2b380cc8 2018-10-24 stsp }
5678 e5a0f69f 2018-08-18 stsp
5679 51fe7530 2019-08-19 stsp if (s->blame_complete)
5680 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5681 51fe7530 2019-08-19 stsp
5682 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5683 e5a0f69f 2018-08-18 stsp
5684 9b058f45 2022-06-30 mark view_border(view);
5685 e5a0f69f 2018-08-18 stsp return err;
5686 e5a0f69f 2018-08-18 stsp }
5687 e5a0f69f 2018-08-18 stsp
5688 e5a0f69f 2018-08-18 stsp static const struct got_error *
5689 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5690 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5691 05f04cdf 2022-07-20 mark {
5692 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5693 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5694 05f04cdf 2022-07-20 mark
5695 05f04cdf 2022-07-20 mark *new_view = NULL;
5696 05f04cdf 2022-07-20 mark
5697 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5698 05f04cdf 2022-07-20 mark if (log_view == NULL)
5699 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5700 05f04cdf 2022-07-20 mark
5701 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5702 05f04cdf 2022-07-20 mark if (err)
5703 05f04cdf 2022-07-20 mark view_close(log_view);
5704 05f04cdf 2022-07-20 mark else
5705 05f04cdf 2022-07-20 mark *new_view = log_view;
5706 05f04cdf 2022-07-20 mark
5707 05f04cdf 2022-07-20 mark return err;
5708 05f04cdf 2022-07-20 mark }
5709 05f04cdf 2022-07-20 mark
5710 05f04cdf 2022-07-20 mark static const struct got_error *
5711 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5712 e5a0f69f 2018-08-18 stsp {
5713 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5714 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
5715 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5716 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5717 9b058f45 2022-06-30 mark
5718 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5719 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5720 9b058f45 2022-06-30 mark --eos; /* border */
5721 7cbe629d 2018-08-04 stsp
5722 e5a0f69f 2018-08-18 stsp switch (ch) {
5723 145b6838 2022-06-16 stsp case '0':
5724 145b6838 2022-06-16 stsp view->x = 0;
5725 145b6838 2022-06-16 stsp break;
5726 145b6838 2022-06-16 stsp case '$':
5727 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5728 640cd7ff 2022-06-22 mark view->count = 0;
5729 145b6838 2022-06-16 stsp break;
5730 145b6838 2022-06-16 stsp case KEY_RIGHT:
5731 145b6838 2022-06-16 stsp case 'l':
5732 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5733 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5734 640cd7ff 2022-06-22 mark else
5735 640cd7ff 2022-06-22 mark view->count = 0;
5736 145b6838 2022-06-16 stsp break;
5737 145b6838 2022-06-16 stsp case KEY_LEFT:
5738 145b6838 2022-06-16 stsp case 'h':
5739 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5740 640cd7ff 2022-06-22 mark if (view->x <= 0)
5741 640cd7ff 2022-06-22 mark view->count = 0;
5742 145b6838 2022-06-16 stsp break;
5743 1e37a5c2 2019-05-12 jcs case 'q':
5744 1e37a5c2 2019-05-12 jcs s->done = 1;
5745 4deef56f 2021-09-02 naddy break;
5746 4deef56f 2021-09-02 naddy case 'g':
5747 4deef56f 2021-09-02 naddy case KEY_HOME:
5748 4deef56f 2021-09-02 naddy s->selected_line = 1;
5749 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5750 640cd7ff 2022-06-22 mark view->count = 0;
5751 4deef56f 2021-09-02 naddy break;
5752 4deef56f 2021-09-02 naddy case 'G':
5753 4deef56f 2021-09-02 naddy case KEY_END:
5754 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5755 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5756 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5757 4deef56f 2021-09-02 naddy } else {
5758 9b058f45 2022-06-30 mark s->selected_line = eos;
5759 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5760 4deef56f 2021-09-02 naddy }
5761 640cd7ff 2022-06-22 mark view->count = 0;
5762 1e37a5c2 2019-05-12 jcs break;
5763 1e37a5c2 2019-05-12 jcs case 'k':
5764 1e37a5c2 2019-05-12 jcs case KEY_UP:
5765 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5766 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5767 1e37a5c2 2019-05-12 jcs s->selected_line--;
5768 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5769 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5771 640cd7ff 2022-06-22 mark else
5772 640cd7ff 2022-06-22 mark view->count = 0;
5773 1e37a5c2 2019-05-12 jcs break;
5774 83cc4199 2022-06-13 stsp case CTRL('u'):
5775 33c3719a 2022-06-15 stsp case 'u':
5776 83cc4199 2022-06-13 stsp nscroll /= 2;
5777 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5778 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5779 ea025d1d 2020-02-22 naddy case CTRL('b'):
5780 61417565 2022-06-20 mark case 'b':
5781 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5782 640cd7ff 2022-06-22 mark if (view->count > 1)
5783 640cd7ff 2022-06-22 mark nscroll += nscroll;
5784 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5785 640cd7ff 2022-06-22 mark view->count = 0;
5786 e5a0f69f 2018-08-18 stsp break;
5787 1e37a5c2 2019-05-12 jcs }
5788 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5789 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5790 1e37a5c2 2019-05-12 jcs else
5791 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5792 1e37a5c2 2019-05-12 jcs break;
5793 1e37a5c2 2019-05-12 jcs case 'j':
5794 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5795 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5796 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5797 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5798 1e37a5c2 2019-05-12 jcs s->selected_line++;
5799 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5800 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5801 640cd7ff 2022-06-22 mark else
5802 640cd7ff 2022-06-22 mark view->count = 0;
5803 1e37a5c2 2019-05-12 jcs break;
5804 61417565 2022-06-20 mark case 'c':
5805 1e37a5c2 2019-05-12 jcs case 'p': {
5806 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5807 640cd7ff 2022-06-22 mark
5808 640cd7ff 2022-06-22 mark view->count = 0;
5809 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5810 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5811 1e37a5c2 2019-05-12 jcs if (id == NULL)
5812 e5a0f69f 2018-08-18 stsp break;
5813 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5814 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5815 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5816 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5817 1e37a5c2 2019-05-12 jcs int obj_type;
5818 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5819 1e37a5c2 2019-05-12 jcs s->repo, id);
5820 e5a0f69f 2018-08-18 stsp if (err)
5821 e5a0f69f 2018-08-18 stsp break;
5822 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5823 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5824 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5825 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5826 e5a0f69f 2018-08-18 stsp break;
5827 e5a0f69f 2018-08-18 stsp }
5828 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5829 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5830 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5831 a44927cc 2022-04-07 stsp if (err)
5832 a44927cc 2022-04-07 stsp break;
5833 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5834 a44927cc 2022-04-07 stsp pcommit, s->path);
5835 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5836 e5a0f69f 2018-08-18 stsp if (err) {
5837 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5838 1e37a5c2 2019-05-12 jcs err = NULL;
5839 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5840 e5a0f69f 2018-08-18 stsp break;
5841 e5a0f69f 2018-08-18 stsp }
5842 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5843 1e37a5c2 2019-05-12 jcs blob_id);
5844 1e37a5c2 2019-05-12 jcs free(blob_id);
5845 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5846 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5847 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5848 e5a0f69f 2018-08-18 stsp break;
5849 1e37a5c2 2019-05-12 jcs }
5850 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5851 d7b5a0e8 2022-04-20 stsp &pid->id);
5852 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5853 1e37a5c2 2019-05-12 jcs } else {
5854 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5855 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5856 1e37a5c2 2019-05-12 jcs break;
5857 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5858 1e37a5c2 2019-05-12 jcs id);
5859 1e37a5c2 2019-05-12 jcs }
5860 1e37a5c2 2019-05-12 jcs if (err)
5861 e5a0f69f 2018-08-18 stsp break;
5862 1e37a5c2 2019-05-12 jcs s->done = 1;
5863 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5864 1e37a5c2 2019-05-12 jcs s->done = 0;
5865 1e37a5c2 2019-05-12 jcs if (thread_err)
5866 1e37a5c2 2019-05-12 jcs break;
5867 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5868 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5869 a5388363 2020-12-01 naddy err = run_blame(view);
5870 1e37a5c2 2019-05-12 jcs if (err)
5871 1e37a5c2 2019-05-12 jcs break;
5872 1e37a5c2 2019-05-12 jcs break;
5873 1e37a5c2 2019-05-12 jcs }
5874 61417565 2022-06-20 mark case 'C': {
5875 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5876 640cd7ff 2022-06-22 mark
5877 640cd7ff 2022-06-22 mark view->count = 0;
5878 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5879 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5880 1e37a5c2 2019-05-12 jcs break;
5881 1e37a5c2 2019-05-12 jcs s->done = 1;
5882 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5883 1e37a5c2 2019-05-12 jcs s->done = 0;
5884 1e37a5c2 2019-05-12 jcs if (thread_err)
5885 1e37a5c2 2019-05-12 jcs break;
5886 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5887 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5888 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5889 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5890 a5388363 2020-12-01 naddy err = run_blame(view);
5891 1e37a5c2 2019-05-12 jcs if (err)
5892 1e37a5c2 2019-05-12 jcs break;
5893 1e37a5c2 2019-05-12 jcs break;
5894 1e37a5c2 2019-05-12 jcs }
5895 136e2bd4 2022-07-23 mark case 'L':
5896 05f04cdf 2022-07-20 mark view->count = 0;
5897 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
5898 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
5899 136e2bd4 2022-07-23 mark if (s->id_to_log)
5900 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
5901 05f04cdf 2022-07-20 mark break;
5902 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5903 1e37a5c2 2019-05-12 jcs case '\r': {
5904 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5905 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5906 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5907 640cd7ff 2022-06-22 mark
5908 640cd7ff 2022-06-22 mark view->count = 0;
5909 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5910 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5911 1e37a5c2 2019-05-12 jcs if (id == NULL)
5912 1e37a5c2 2019-05-12 jcs break;
5913 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5914 1e37a5c2 2019-05-12 jcs if (err)
5915 1e37a5c2 2019-05-12 jcs break;
5916 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5917 c0f61fa4 2022-07-11 mark if (*new_view) {
5918 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5919 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5920 c0f61fa4 2022-07-11 mark if (err)
5921 c0f61fa4 2022-07-11 mark break;
5922 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5923 c0f61fa4 2022-07-11 mark } else {
5924 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5925 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
5926 9b058f45 2022-06-30 mark
5927 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
5928 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
5929 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
5930 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
5931 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
5932 c0f61fa4 2022-07-11 mark break;
5933 c0f61fa4 2022-07-11 mark }
5934 15a94983 2018-12-23 stsp }
5935 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5936 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
5937 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5938 1e37a5c2 2019-05-12 jcs if (err) {
5939 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5940 1e37a5c2 2019-05-12 jcs break;
5941 1e37a5c2 2019-05-12 jcs }
5942 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
5943 c0f61fa4 2022-07-11 mark s->selected_line;
5944 c0f61fa4 2022-07-11 mark if (*new_view)
5945 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
5946 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5947 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5948 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5949 9b058f45 2022-06-30 mark if (err)
5950 9b058f45 2022-06-30 mark break;
5951 9b058f45 2022-06-30 mark }
5952 9b058f45 2022-06-30 mark
5953 e78dc838 2020-12-04 stsp view->focussed = 0;
5954 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5955 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5956 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5957 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5958 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
5959 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5960 1e37a5c2 2019-05-12 jcs if (err)
5961 34bc9ec9 2019-02-22 stsp break;
5962 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5963 0dbbbe90 2022-06-17 op if (err)
5964 0dbbbe90 2022-06-17 op break;
5965 e78dc838 2020-12-04 stsp view->focus_child = 1;
5966 1e37a5c2 2019-05-12 jcs } else
5967 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5968 1e37a5c2 2019-05-12 jcs if (err)
5969 e5a0f69f 2018-08-18 stsp break;
5970 1e37a5c2 2019-05-12 jcs break;
5971 1e37a5c2 2019-05-12 jcs }
5972 83cc4199 2022-06-13 stsp case CTRL('d'):
5973 33c3719a 2022-06-15 stsp case 'd':
5974 83cc4199 2022-06-13 stsp nscroll /= 2;
5975 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5976 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5977 ea025d1d 2020-02-22 naddy case CTRL('f'):
5978 61417565 2022-06-20 mark case 'f':
5979 1e37a5c2 2019-05-12 jcs case ' ':
5980 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5981 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5982 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5983 640cd7ff 2022-06-22 mark view->count = 0;
5984 e5a0f69f 2018-08-18 stsp break;
5985 1e37a5c2 2019-05-12 jcs }
5986 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5987 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5988 83cc4199 2022-06-13 stsp s->selected_line +=
5989 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5990 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5991 1e37a5c2 2019-05-12 jcs }
5992 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5993 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5994 1e37a5c2 2019-05-12 jcs else
5995 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5996 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5997 1e37a5c2 2019-05-12 jcs break;
5998 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5999 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6000 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6001 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6002 1e37a5c2 2019-05-12 jcs }
6003 1e37a5c2 2019-05-12 jcs break;
6004 1e37a5c2 2019-05-12 jcs default:
6005 640cd7ff 2022-06-22 mark view->count = 0;
6006 1e37a5c2 2019-05-12 jcs break;
6007 a70480e0 2018-06-23 stsp }
6008 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6009 917d79a7 2022-07-01 stsp }
6010 917d79a7 2022-07-01 stsp
6011 917d79a7 2022-07-01 stsp static const struct got_error *
6012 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6013 917d79a7 2022-07-01 stsp {
6014 917d79a7 2022-07-01 stsp const struct got_error *err;
6015 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6016 917d79a7 2022-07-01 stsp
6017 917d79a7 2022-07-01 stsp view->count = 0;
6018 917d79a7 2022-07-01 stsp s->done = 1;
6019 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6020 917d79a7 2022-07-01 stsp s->done = 0;
6021 917d79a7 2022-07-01 stsp if (err)
6022 917d79a7 2022-07-01 stsp return err;
6023 917d79a7 2022-07-01 stsp return run_blame(view);
6024 a70480e0 2018-06-23 stsp }
6025 a70480e0 2018-06-23 stsp
6026 a70480e0 2018-06-23 stsp static const struct got_error *
6027 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6028 9f7d7167 2018-04-29 stsp {
6029 a70480e0 2018-06-23 stsp const struct got_error *error;
6030 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6031 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6032 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6033 0587e10c 2020-07-23 stsp char *link_target = NULL;
6034 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6035 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6036 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6037 a70480e0 2018-06-23 stsp int ch;
6038 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6039 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6040 a70480e0 2018-06-23 stsp
6041 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6042 a70480e0 2018-06-23 stsp switch (ch) {
6043 a70480e0 2018-06-23 stsp case 'c':
6044 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6045 a70480e0 2018-06-23 stsp break;
6046 69069811 2018-08-02 stsp case 'r':
6047 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6048 69069811 2018-08-02 stsp if (repo_path == NULL)
6049 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6050 9ba1d308 2019-10-21 stsp optarg);
6051 69069811 2018-08-02 stsp break;
6052 a70480e0 2018-06-23 stsp default:
6053 17020d27 2019-03-07 stsp usage_blame();
6054 a70480e0 2018-06-23 stsp /* NOTREACHED */
6055 a70480e0 2018-06-23 stsp }
6056 a70480e0 2018-06-23 stsp }
6057 a70480e0 2018-06-23 stsp
6058 a70480e0 2018-06-23 stsp argc -= optind;
6059 a70480e0 2018-06-23 stsp argv += optind;
6060 a70480e0 2018-06-23 stsp
6061 f135c941 2020-02-20 stsp if (argc != 1)
6062 a70480e0 2018-06-23 stsp usage_blame();
6063 6962eb72 2020-02-20 stsp
6064 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6065 0ae84acc 2022-06-15 tracey if (error != NULL)
6066 0ae84acc 2022-06-15 tracey goto done;
6067 0ae84acc 2022-06-15 tracey
6068 69069811 2018-08-02 stsp if (repo_path == NULL) {
6069 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6070 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6071 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6072 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6073 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6074 c156c7a4 2020-12-18 stsp goto done;
6075 f135c941 2020-02-20 stsp if (worktree)
6076 eb41ed75 2019-02-05 stsp repo_path =
6077 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6078 f135c941 2020-02-20 stsp else
6079 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6080 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6081 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6082 c156c7a4 2020-12-18 stsp goto done;
6083 c156c7a4 2020-12-18 stsp }
6084 f135c941 2020-02-20 stsp }
6085 a915003a 2019-02-05 stsp
6086 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6087 c02c541e 2019-03-29 stsp if (error != NULL)
6088 8e94dd5b 2019-01-04 stsp goto done;
6089 69069811 2018-08-02 stsp
6090 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6091 f135c941 2020-02-20 stsp worktree);
6092 c02c541e 2019-03-29 stsp if (error)
6093 92205607 2019-01-04 stsp goto done;
6094 69069811 2018-08-02 stsp
6095 f135c941 2020-02-20 stsp init_curses();
6096 f135c941 2020-02-20 stsp
6097 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6098 51a10b52 2020-12-26 stsp if (error)
6099 51a10b52 2020-12-26 stsp goto done;
6100 51a10b52 2020-12-26 stsp
6101 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6102 eb41ed75 2019-02-05 stsp if (error)
6103 69069811 2018-08-02 stsp goto done;
6104 a70480e0 2018-06-23 stsp
6105 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6106 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6107 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6108 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6109 a70480e0 2018-06-23 stsp if (error != NULL)
6110 66b4983c 2018-06-23 stsp goto done;
6111 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6112 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6113 a70480e0 2018-06-23 stsp } else {
6114 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6115 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6116 a70480e0 2018-06-23 stsp }
6117 a19e88aa 2018-06-23 stsp if (error != NULL)
6118 8b473291 2019-02-21 stsp goto done;
6119 8b473291 2019-02-21 stsp
6120 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6121 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6122 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6123 e1cd8fed 2018-08-01 stsp goto done;
6124 e1cd8fed 2018-08-01 stsp }
6125 0587e10c 2020-07-23 stsp
6126 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6127 a44927cc 2022-04-07 stsp if (error)
6128 a44927cc 2022-04-07 stsp goto done;
6129 a44927cc 2022-04-07 stsp
6130 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6131 a44927cc 2022-04-07 stsp commit, repo);
6132 7cbe629d 2018-08-04 stsp if (error)
6133 7cbe629d 2018-08-04 stsp goto done;
6134 0587e10c 2020-07-23 stsp
6135 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6136 78756c87 2020-11-24 stsp commit_id, repo);
6137 0587e10c 2020-07-23 stsp if (error)
6138 0587e10c 2020-07-23 stsp goto done;
6139 12314ad4 2019-08-31 stsp if (worktree) {
6140 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6141 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6142 12314ad4 2019-08-31 stsp worktree = NULL;
6143 12314ad4 2019-08-31 stsp }
6144 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6145 a70480e0 2018-06-23 stsp done:
6146 69069811 2018-08-02 stsp free(repo_path);
6147 f135c941 2020-02-20 stsp free(in_repo_path);
6148 0587e10c 2020-07-23 stsp free(link_target);
6149 69069811 2018-08-02 stsp free(cwd);
6150 a70480e0 2018-06-23 stsp free(commit_id);
6151 a44927cc 2022-04-07 stsp if (commit)
6152 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6153 eb41ed75 2019-02-05 stsp if (worktree)
6154 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6155 1d0f4054 2021-06-17 stsp if (repo) {
6156 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6157 1d0f4054 2021-06-17 stsp if (error == NULL)
6158 1d0f4054 2021-06-17 stsp error = close_err;
6159 1d0f4054 2021-06-17 stsp }
6160 0ae84acc 2022-06-15 tracey if (pack_fds) {
6161 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6162 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6163 0ae84acc 2022-06-15 tracey if (error == NULL)
6164 0ae84acc 2022-06-15 tracey error = pack_err;
6165 0ae84acc 2022-06-15 tracey }
6166 51a10b52 2020-12-26 stsp tog_free_refs();
6167 a70480e0 2018-06-23 stsp return error;
6168 ffd1d5e5 2018-06-23 stsp }
6169 ffd1d5e5 2018-06-23 stsp
6170 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6171 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6172 ffd1d5e5 2018-06-23 stsp {
6173 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6174 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6175 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6176 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6177 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6178 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6179 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6180 ffd1d5e5 2018-06-23 stsp
6181 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6182 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6183 9b058f45 2022-06-30 mark --limit; /* border */
6184 ffd1d5e5 2018-06-23 stsp
6185 f7d12f7e 2018-08-01 stsp werase(view->window);
6186 ffd1d5e5 2018-06-23 stsp
6187 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6188 ffd1d5e5 2018-06-23 stsp return NULL;
6189 ffd1d5e5 2018-06-23 stsp
6190 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6191 ccda2f4d 2022-06-16 stsp 0, 0);
6192 ffd1d5e5 2018-06-23 stsp if (err)
6193 ffd1d5e5 2018-06-23 stsp return err;
6194 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6195 a3404814 2018-09-02 stsp wstandout(view->window);
6196 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6197 11b20872 2019-11-08 stsp if (tc)
6198 11b20872 2019-11-08 stsp wattr_on(view->window,
6199 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6200 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6201 11b20872 2019-11-08 stsp if (tc)
6202 11b20872 2019-11-08 stsp wattr_off(view->window,
6203 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6204 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6205 a3404814 2018-09-02 stsp wstandend(view->window);
6206 2550e4c3 2018-07-13 stsp free(wline);
6207 2550e4c3 2018-07-13 stsp wline = NULL;
6208 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6209 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6210 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6211 ffd1d5e5 2018-06-23 stsp return NULL;
6212 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6213 ccda2f4d 2022-06-16 stsp 0, 0);
6214 ce52c690 2018-06-23 stsp if (err)
6215 ce52c690 2018-06-23 stsp return err;
6216 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6217 2550e4c3 2018-07-13 stsp free(wline);
6218 2550e4c3 2018-07-13 stsp wline = NULL;
6219 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6220 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6221 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6222 ffd1d5e5 2018-06-23 stsp return NULL;
6223 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6224 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6225 a1eca9bb 2018-06-23 stsp return NULL;
6226 ffd1d5e5 2018-06-23 stsp
6227 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6228 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6229 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6230 0cf4efb1 2018-09-29 stsp if (view->focussed)
6231 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6232 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6233 ffd1d5e5 2018-06-23 stsp }
6234 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6235 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6236 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6237 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6238 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6239 ffd1d5e5 2018-06-23 stsp return NULL;
6240 ffd1d5e5 2018-06-23 stsp n = 1;
6241 ffd1d5e5 2018-06-23 stsp } else {
6242 ffd1d5e5 2018-06-23 stsp n = 0;
6243 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6244 ffd1d5e5 2018-06-23 stsp }
6245 ffd1d5e5 2018-06-23 stsp
6246 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6247 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6248 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6249 848d6979 2019-08-12 stsp const char *modestr = "";
6250 56e0773d 2019-11-28 stsp mode_t mode;
6251 1d13200f 2018-07-12 stsp
6252 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6253 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6254 56e0773d 2019-11-28 stsp
6255 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6256 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6257 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6258 1d13200f 2018-07-12 stsp if (err)
6259 638f9024 2019-05-13 stsp return got_error_from_errno(
6260 230a42bd 2019-05-11 jcs "got_object_id_str");
6261 1d13200f 2018-07-12 stsp }
6262 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6263 63c5ca5d 2019-08-24 stsp modestr = "$";
6264 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6265 0d6c6ee3 2020-05-20 stsp int i;
6266 0d6c6ee3 2020-05-20 stsp
6267 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6268 d86d3b18 2020-12-01 naddy te, s->repo);
6269 0d6c6ee3 2020-05-20 stsp if (err) {
6270 0d6c6ee3 2020-05-20 stsp free(id_str);
6271 0d6c6ee3 2020-05-20 stsp return err;
6272 0d6c6ee3 2020-05-20 stsp }
6273 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6274 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6275 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6276 0d6c6ee3 2020-05-20 stsp }
6277 848d6979 2019-08-12 stsp modestr = "@";
6278 0d6c6ee3 2020-05-20 stsp }
6279 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6280 848d6979 2019-08-12 stsp modestr = "/";
6281 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6282 848d6979 2019-08-12 stsp modestr = "*";
6283 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6284 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6285 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6286 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6287 1d13200f 2018-07-12 stsp free(id_str);
6288 0d6c6ee3 2020-05-20 stsp free(link_target);
6289 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6290 1d13200f 2018-07-12 stsp }
6291 1d13200f 2018-07-12 stsp free(id_str);
6292 0d6c6ee3 2020-05-20 stsp free(link_target);
6293 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6294 ccda2f4d 2022-06-16 stsp 0, 0);
6295 ffd1d5e5 2018-06-23 stsp if (err) {
6296 ffd1d5e5 2018-06-23 stsp free(line);
6297 ffd1d5e5 2018-06-23 stsp break;
6298 ffd1d5e5 2018-06-23 stsp }
6299 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6300 0cf4efb1 2018-09-29 stsp if (view->focussed)
6301 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6302 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6303 ffd1d5e5 2018-06-23 stsp }
6304 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6305 f26dddb7 2019-11-08 stsp if (tc)
6306 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6307 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6308 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6309 f26dddb7 2019-11-08 stsp if (tc)
6310 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6311 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6312 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6313 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6314 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6315 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6316 ffd1d5e5 2018-06-23 stsp free(line);
6317 2550e4c3 2018-07-13 stsp free(wline);
6318 2550e4c3 2018-07-13 stsp wline = NULL;
6319 ffd1d5e5 2018-06-23 stsp n++;
6320 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6321 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6322 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6323 ffd1d5e5 2018-06-23 stsp break;
6324 ffd1d5e5 2018-06-23 stsp }
6325 ffd1d5e5 2018-06-23 stsp
6326 ffd1d5e5 2018-06-23 stsp return err;
6327 ffd1d5e5 2018-06-23 stsp }
6328 ffd1d5e5 2018-06-23 stsp
6329 ffd1d5e5 2018-06-23 stsp static void
6330 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6331 ffd1d5e5 2018-06-23 stsp {
6332 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6333 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6334 fa86c4bf 2020-11-29 stsp int i = 0;
6335 ffd1d5e5 2018-06-23 stsp
6336 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6337 ffd1d5e5 2018-06-23 stsp return;
6338 ffd1d5e5 2018-06-23 stsp
6339 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6340 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6341 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6342 fa86c4bf 2020-11-29 stsp if (!isroot)
6343 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6344 fa86c4bf 2020-11-29 stsp break;
6345 fa86c4bf 2020-11-29 stsp }
6346 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6347 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6348 ffd1d5e5 2018-06-23 stsp }
6349 ffd1d5e5 2018-06-23 stsp }
6350 ffd1d5e5 2018-06-23 stsp
6351 9b058f45 2022-06-30 mark static const struct got_error *
6352 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6353 ffd1d5e5 2018-06-23 stsp {
6354 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6355 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6356 ffd1d5e5 2018-06-23 stsp int n = 0;
6357 ffd1d5e5 2018-06-23 stsp
6358 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6359 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6360 694d3271 2020-12-01 naddy s->first_displayed_entry);
6361 694d3271 2020-12-01 naddy else
6362 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6363 56e0773d 2019-11-28 stsp
6364 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6365 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6366 9b058f45 2022-06-30 mark if (last)
6367 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6368 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6369 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6370 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6371 768394f3 2019-01-24 stsp }
6372 ffd1d5e5 2018-06-23 stsp }
6373 9b058f45 2022-06-30 mark
6374 9b058f45 2022-06-30 mark return NULL;
6375 ffd1d5e5 2018-06-23 stsp }
6376 ffd1d5e5 2018-06-23 stsp
6377 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6378 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6379 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6380 ffd1d5e5 2018-06-23 stsp {
6381 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6382 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6383 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6384 ffd1d5e5 2018-06-23 stsp
6385 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6386 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6387 56e0773d 2019-11-28 stsp + 1 /* slash */;
6388 ce52c690 2018-06-23 stsp if (te)
6389 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6390 ce52c690 2018-06-23 stsp
6391 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6392 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6393 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6394 ffd1d5e5 2018-06-23 stsp
6395 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6396 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6397 d9765a41 2018-06-23 stsp while (pt) {
6398 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6399 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6400 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6401 cb2ebc8a 2018-06-23 stsp goto done;
6402 cb2ebc8a 2018-06-23 stsp }
6403 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6404 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6405 cb2ebc8a 2018-06-23 stsp goto done;
6406 cb2ebc8a 2018-06-23 stsp }
6407 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6408 ffd1d5e5 2018-06-23 stsp }
6409 ce52c690 2018-06-23 stsp if (te) {
6410 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6411 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6412 ce52c690 2018-06-23 stsp goto done;
6413 ce52c690 2018-06-23 stsp }
6414 cb2ebc8a 2018-06-23 stsp }
6415 ce52c690 2018-06-23 stsp done:
6416 ce52c690 2018-06-23 stsp if (err) {
6417 ce52c690 2018-06-23 stsp free(*path);
6418 ce52c690 2018-06-23 stsp *path = NULL;
6419 ce52c690 2018-06-23 stsp }
6420 ce52c690 2018-06-23 stsp return err;
6421 ce52c690 2018-06-23 stsp }
6422 ce52c690 2018-06-23 stsp
6423 ce52c690 2018-06-23 stsp static const struct got_error *
6424 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6425 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6426 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6427 ce52c690 2018-06-23 stsp {
6428 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6429 ce52c690 2018-06-23 stsp char *path;
6430 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6431 a0de39f3 2019-08-09 stsp
6432 a0de39f3 2019-08-09 stsp *new_view = NULL;
6433 69efd4c4 2018-07-18 stsp
6434 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6435 ce52c690 2018-06-23 stsp if (err)
6436 ce52c690 2018-06-23 stsp return err;
6437 ffd1d5e5 2018-06-23 stsp
6438 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6439 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6440 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6441 83ce39e3 2019-08-12 stsp goto done;
6442 83ce39e3 2019-08-12 stsp }
6443 cdf1ee82 2018-08-01 stsp
6444 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6445 e5a0f69f 2018-08-18 stsp if (err) {
6446 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6447 fc06ba56 2019-08-22 stsp err = NULL;
6448 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6449 e5a0f69f 2018-08-18 stsp } else
6450 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6451 83ce39e3 2019-08-12 stsp done:
6452 83ce39e3 2019-08-12 stsp free(path);
6453 69efd4c4 2018-07-18 stsp return err;
6454 69efd4c4 2018-07-18 stsp }
6455 69efd4c4 2018-07-18 stsp
6456 69efd4c4 2018-07-18 stsp static const struct got_error *
6457 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6458 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6459 69efd4c4 2018-07-18 stsp {
6460 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6461 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6462 69efd4c4 2018-07-18 stsp char *path;
6463 a0de39f3 2019-08-09 stsp
6464 a0de39f3 2019-08-09 stsp *new_view = NULL;
6465 69efd4c4 2018-07-18 stsp
6466 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6467 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6468 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6469 e5a0f69f 2018-08-18 stsp
6470 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6471 69efd4c4 2018-07-18 stsp if (err)
6472 69efd4c4 2018-07-18 stsp return err;
6473 69efd4c4 2018-07-18 stsp
6474 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6475 4e97c21c 2020-12-06 stsp path, 0);
6476 ba4f502b 2018-08-04 stsp if (err)
6477 e5a0f69f 2018-08-18 stsp view_close(log_view);
6478 e5a0f69f 2018-08-18 stsp else
6479 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6480 cb2ebc8a 2018-06-23 stsp free(path);
6481 cb2ebc8a 2018-06-23 stsp return err;
6482 ffd1d5e5 2018-06-23 stsp }
6483 ffd1d5e5 2018-06-23 stsp
6484 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6485 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6486 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6487 ffd1d5e5 2018-06-23 stsp {
6488 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6489 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6490 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6491 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6492 ffd1d5e5 2018-06-23 stsp
6493 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6494 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6495 bc573f3b 2021-07-10 stsp
6496 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6497 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6498 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6499 ffd1d5e5 2018-06-23 stsp
6500 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6501 bc573f3b 2021-07-10 stsp if (err)
6502 bc573f3b 2021-07-10 stsp goto done;
6503 bc573f3b 2021-07-10 stsp
6504 bc573f3b 2021-07-10 stsp /*
6505 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6506 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6507 bc573f3b 2021-07-10 stsp * closed on demand.
6508 bc573f3b 2021-07-10 stsp */
6509 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6510 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6511 bc573f3b 2021-07-10 stsp if (err)
6512 bc573f3b 2021-07-10 stsp goto done;
6513 bc573f3b 2021-07-10 stsp s->tree = s->root;
6514 bc573f3b 2021-07-10 stsp
6515 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6516 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6517 ffd1d5e5 2018-06-23 stsp goto done;
6518 ffd1d5e5 2018-06-23 stsp
6519 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6520 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6521 ffd1d5e5 2018-06-23 stsp goto done;
6522 ffd1d5e5 2018-06-23 stsp }
6523 ffd1d5e5 2018-06-23 stsp
6524 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6525 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6526 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6527 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6528 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6529 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6530 9cd7cbd1 2020-12-07 stsp goto done;
6531 9cd7cbd1 2020-12-07 stsp }
6532 9cd7cbd1 2020-12-07 stsp }
6533 fb2756b9 2018-08-04 stsp s->repo = repo;
6534 c0b01bdb 2019-11-08 stsp
6535 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6536 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6537 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6538 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6539 c0b01bdb 2019-11-08 stsp if (err)
6540 c0b01bdb 2019-11-08 stsp goto done;
6541 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6543 bc573f3b 2021-07-10 stsp if (err)
6544 c0b01bdb 2019-11-08 stsp goto done;
6545 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6546 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6547 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6548 bc573f3b 2021-07-10 stsp if (err)
6549 c0b01bdb 2019-11-08 stsp goto done;
6550 e5a0f69f 2018-08-18 stsp
6551 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6552 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6553 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6554 bc573f3b 2021-07-10 stsp if (err)
6555 11b20872 2019-11-08 stsp goto done;
6556 11b20872 2019-11-08 stsp
6557 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6558 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6559 bc573f3b 2021-07-10 stsp if (err)
6560 c0b01bdb 2019-11-08 stsp goto done;
6561 c0b01bdb 2019-11-08 stsp }
6562 c0b01bdb 2019-11-08 stsp
6563 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6564 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6565 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6566 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6567 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6568 ad80ab7b 2018-08-04 stsp done:
6569 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6570 bc573f3b 2021-07-10 stsp if (commit)
6571 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6572 bc573f3b 2021-07-10 stsp if (err)
6573 bc573f3b 2021-07-10 stsp close_tree_view(view);
6574 ad80ab7b 2018-08-04 stsp return err;
6575 ad80ab7b 2018-08-04 stsp }
6576 ad80ab7b 2018-08-04 stsp
6577 e5a0f69f 2018-08-18 stsp static const struct got_error *
6578 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6579 ad80ab7b 2018-08-04 stsp {
6580 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6581 ad80ab7b 2018-08-04 stsp
6582 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6583 fb2756b9 2018-08-04 stsp free(s->tree_label);
6584 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6585 6484ec90 2018-09-29 stsp free(s->commit_id);
6586 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6587 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6588 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6589 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6590 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6591 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6592 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6593 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6594 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6595 ad80ab7b 2018-08-04 stsp free(parent);
6596 ad80ab7b 2018-08-04 stsp
6597 ad80ab7b 2018-08-04 stsp }
6598 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6599 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6600 bc573f3b 2021-07-10 stsp if (s->root)
6601 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6602 7c32bd05 2019-06-22 stsp return NULL;
6603 7c32bd05 2019-06-22 stsp }
6604 7c32bd05 2019-06-22 stsp
6605 7c32bd05 2019-06-22 stsp static const struct got_error *
6606 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6607 7c32bd05 2019-06-22 stsp {
6608 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6609 7c32bd05 2019-06-22 stsp
6610 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6611 7c32bd05 2019-06-22 stsp return NULL;
6612 7c32bd05 2019-06-22 stsp }
6613 7c32bd05 2019-06-22 stsp
6614 7c32bd05 2019-06-22 stsp static int
6615 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6616 7c32bd05 2019-06-22 stsp {
6617 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6618 7c32bd05 2019-06-22 stsp
6619 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6620 56e0773d 2019-11-28 stsp 0) == 0;
6621 7c32bd05 2019-06-22 stsp }
6622 7c32bd05 2019-06-22 stsp
6623 7c32bd05 2019-06-22 stsp static const struct got_error *
6624 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6625 7c32bd05 2019-06-22 stsp {
6626 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6627 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6628 7c32bd05 2019-06-22 stsp
6629 7c32bd05 2019-06-22 stsp if (!view->searching) {
6630 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6631 7c32bd05 2019-06-22 stsp return NULL;
6632 7c32bd05 2019-06-22 stsp }
6633 7c32bd05 2019-06-22 stsp
6634 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6635 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6636 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6637 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6638 56e0773d 2019-11-28 stsp s->selected_entry);
6639 7c32bd05 2019-06-22 stsp else
6640 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6641 56e0773d 2019-11-28 stsp } else {
6642 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6643 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6644 56e0773d 2019-11-28 stsp else
6645 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6646 56e0773d 2019-11-28 stsp s->selected_entry);
6647 7c32bd05 2019-06-22 stsp }
6648 7c32bd05 2019-06-22 stsp } else {
6649 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6650 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6651 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6652 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6653 56e0773d 2019-11-28 stsp else
6654 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6655 7c32bd05 2019-06-22 stsp }
6656 7c32bd05 2019-06-22 stsp
6657 7c32bd05 2019-06-22 stsp while (1) {
6658 56e0773d 2019-11-28 stsp if (te == NULL) {
6659 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6660 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6661 ac66afb8 2019-06-24 stsp return NULL;
6662 ac66afb8 2019-06-24 stsp }
6663 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6664 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6665 56e0773d 2019-11-28 stsp else
6666 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6667 7c32bd05 2019-06-22 stsp }
6668 7c32bd05 2019-06-22 stsp
6669 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6670 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6671 56e0773d 2019-11-28 stsp s->matched_entry = te;
6672 7c32bd05 2019-06-22 stsp break;
6673 7c32bd05 2019-06-22 stsp }
6674 7c32bd05 2019-06-22 stsp
6675 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6676 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6677 56e0773d 2019-11-28 stsp else
6678 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6679 7c32bd05 2019-06-22 stsp }
6680 e5a0f69f 2018-08-18 stsp
6681 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6682 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6683 7c32bd05 2019-06-22 stsp s->selected = 0;
6684 7c32bd05 2019-06-22 stsp }
6685 7c32bd05 2019-06-22 stsp
6686 e5a0f69f 2018-08-18 stsp return NULL;
6687 ad80ab7b 2018-08-04 stsp }
6688 ad80ab7b 2018-08-04 stsp
6689 ad80ab7b 2018-08-04 stsp static const struct got_error *
6690 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6691 ad80ab7b 2018-08-04 stsp {
6692 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6693 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6694 e5a0f69f 2018-08-18 stsp char *parent_path;
6695 ad80ab7b 2018-08-04 stsp
6696 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6697 e5a0f69f 2018-08-18 stsp if (err)
6698 e5a0f69f 2018-08-18 stsp return err;
6699 ffd1d5e5 2018-06-23 stsp
6700 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6701 e5a0f69f 2018-08-18 stsp free(parent_path);
6702 669b5ffa 2018-10-07 stsp
6703 9b058f45 2022-06-30 mark view_border(view);
6704 e5a0f69f 2018-08-18 stsp return err;
6705 e5a0f69f 2018-08-18 stsp }
6706 ce52c690 2018-06-23 stsp
6707 e5a0f69f 2018-08-18 stsp static const struct got_error *
6708 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6709 e5a0f69f 2018-08-18 stsp {
6710 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6711 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6712 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6713 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
6714 ffd1d5e5 2018-06-23 stsp
6715 e5a0f69f 2018-08-18 stsp switch (ch) {
6716 1e37a5c2 2019-05-12 jcs case 'i':
6717 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6718 640cd7ff 2022-06-22 mark view->count = 0;
6719 1e37a5c2 2019-05-12 jcs break;
6720 5e98fb33 2022-07-22 mark case 'L':
6721 640cd7ff 2022-06-22 mark view->count = 0;
6722 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6723 ffd1d5e5 2018-06-23 stsp break;
6724 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6725 152c1c93 2020-11-29 stsp break;
6726 5e98fb33 2022-07-22 mark case 'R':
6727 640cd7ff 2022-06-22 mark view->count = 0;
6728 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
6729 e4526bf5 2021-09-03 naddy break;
6730 e4526bf5 2021-09-03 naddy case 'g':
6731 e4526bf5 2021-09-03 naddy case KEY_HOME:
6732 e4526bf5 2021-09-03 naddy s->selected = 0;
6733 640cd7ff 2022-06-22 mark view->count = 0;
6734 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6735 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6736 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6737 e4526bf5 2021-09-03 naddy else
6738 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6739 1e37a5c2 2019-05-12 jcs break;
6740 e4526bf5 2021-09-03 naddy case 'G':
6741 9b058f45 2022-06-30 mark case KEY_END: {
6742 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6743 9b058f45 2022-06-30 mark
6744 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6745 9b058f45 2022-06-30 mark --eos; /* border */
6746 e4526bf5 2021-09-03 naddy s->selected = 0;
6747 640cd7ff 2022-06-22 mark view->count = 0;
6748 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6749 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6750 e4526bf5 2021-09-03 naddy if (te == NULL) {
6751 ad055527 2022-07-16 mark if (s->tree != s->root) {
6752 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6753 e4526bf5 2021-09-03 naddy n++;
6754 e4526bf5 2021-09-03 naddy }
6755 e4526bf5 2021-09-03 naddy break;
6756 e4526bf5 2021-09-03 naddy }
6757 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6758 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6759 e4526bf5 2021-09-03 naddy }
6760 e4526bf5 2021-09-03 naddy if (n > 0)
6761 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6762 e4526bf5 2021-09-03 naddy break;
6763 9b058f45 2022-06-30 mark }
6764 1e37a5c2 2019-05-12 jcs case 'k':
6765 1e37a5c2 2019-05-12 jcs case KEY_UP:
6766 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6767 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6768 1e37a5c2 2019-05-12 jcs s->selected--;
6769 fa86c4bf 2020-11-29 stsp break;
6770 1e37a5c2 2019-05-12 jcs }
6771 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6772 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6773 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6774 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6775 640cd7ff 2022-06-22 mark view->count = 0;
6776 1e37a5c2 2019-05-12 jcs break;
6777 83cc4199 2022-06-13 stsp case CTRL('u'):
6778 33c3719a 2022-06-15 stsp case 'u':
6779 83cc4199 2022-06-13 stsp nscroll /= 2;
6780 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6781 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6782 ea025d1d 2020-02-22 naddy case CTRL('b'):
6783 61417565 2022-06-20 mark case 'b':
6784 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6785 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6786 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6787 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6788 fa86c4bf 2020-11-29 stsp } else {
6789 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6790 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6791 fa86c4bf 2020-11-29 stsp }
6792 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6793 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6794 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6795 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6796 640cd7ff 2022-06-22 mark view->count = 0;
6797 1e37a5c2 2019-05-12 jcs break;
6798 1e37a5c2 2019-05-12 jcs case 'j':
6799 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6800 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6801 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6802 1e37a5c2 2019-05-12 jcs s->selected++;
6803 1e37a5c2 2019-05-12 jcs break;
6804 1e37a5c2 2019-05-12 jcs }
6805 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6806 640cd7ff 2022-06-22 mark == NULL) {
6807 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6808 640cd7ff 2022-06-22 mark view->count = 0;
6809 1e37a5c2 2019-05-12 jcs break;
6810 640cd7ff 2022-06-22 mark }
6811 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6812 1e37a5c2 2019-05-12 jcs break;
6813 83cc4199 2022-06-13 stsp case CTRL('d'):
6814 33c3719a 2022-06-15 stsp case 'd':
6815 83cc4199 2022-06-13 stsp nscroll /= 2;
6816 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6817 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6818 ea025d1d 2020-02-22 naddy case CTRL('f'):
6819 61417565 2022-06-20 mark case 'f':
6820 48bb96f0 2022-06-20 naddy case ' ':
6821 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6822 1e37a5c2 2019-05-12 jcs == NULL) {
6823 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6824 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6825 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6826 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6827 640cd7ff 2022-06-22 mark else
6828 640cd7ff 2022-06-22 mark view->count = 0;
6829 1e37a5c2 2019-05-12 jcs break;
6830 1e37a5c2 2019-05-12 jcs }
6831 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6832 1e37a5c2 2019-05-12 jcs break;
6833 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6834 1e37a5c2 2019-05-12 jcs case '\r':
6835 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6836 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6837 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6838 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6839 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6840 640cd7ff 2022-06-22 mark view->count = 0;
6841 1e37a5c2 2019-05-12 jcs break;
6842 640cd7ff 2022-06-22 mark }
6843 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6844 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6845 1e37a5c2 2019-05-12 jcs entry);
6846 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6847 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6848 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6849 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6850 1e37a5c2 2019-05-12 jcs s->selected_entry =
6851 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6852 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6853 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6854 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6855 9b058f45 2022-06-30 mark if (err)
6856 9b058f45 2022-06-30 mark break;
6857 9b058f45 2022-06-30 mark }
6858 1e37a5c2 2019-05-12 jcs free(parent);
6859 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6860 56e0773d 2019-11-28 stsp s->selected_entry))) {
6861 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6862 640cd7ff 2022-06-22 mark view->count = 0;
6863 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6864 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6865 1e37a5c2 2019-05-12 jcs if (err)
6866 1e37a5c2 2019-05-12 jcs break;
6867 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6868 941e9f74 2019-05-21 stsp if (err) {
6869 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6870 1e37a5c2 2019-05-12 jcs break;
6871 1e37a5c2 2019-05-12 jcs }
6872 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6873 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6874 1e37a5c2 2019-05-12 jcs break;
6875 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6876 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6877 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6878 640cd7ff 2022-06-22 mark view->count = 0;
6879 1e37a5c2 2019-05-12 jcs break;
6880 1e37a5c2 2019-05-12 jcs default:
6881 640cd7ff 2022-06-22 mark view->count = 0;
6882 1e37a5c2 2019-05-12 jcs break;
6883 ffd1d5e5 2018-06-23 stsp }
6884 e5a0f69f 2018-08-18 stsp
6885 ffd1d5e5 2018-06-23 stsp return err;
6886 9f7d7167 2018-04-29 stsp }
6887 9f7d7167 2018-04-29 stsp
6888 ffd1d5e5 2018-06-23 stsp __dead static void
6889 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6890 ffd1d5e5 2018-06-23 stsp {
6891 ffd1d5e5 2018-06-23 stsp endwin();
6892 87411fa9 2022-06-16 stsp fprintf(stderr,
6893 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6894 ffd1d5e5 2018-06-23 stsp getprogname());
6895 ffd1d5e5 2018-06-23 stsp exit(1);
6896 ffd1d5e5 2018-06-23 stsp }
6897 ffd1d5e5 2018-06-23 stsp
6898 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6899 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6900 ffd1d5e5 2018-06-23 stsp {
6901 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6902 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6903 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6904 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6905 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6906 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6907 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6908 4e97c21c 2020-12-06 stsp char *label = NULL;
6909 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6910 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6911 ffd1d5e5 2018-06-23 stsp int ch;
6912 5221c383 2018-08-01 stsp struct tog_view *view;
6913 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6914 70ac5f84 2019-03-28 stsp
6915 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6916 ffd1d5e5 2018-06-23 stsp switch (ch) {
6917 ffd1d5e5 2018-06-23 stsp case 'c':
6918 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6919 74283ab8 2020-02-07 stsp break;
6920 74283ab8 2020-02-07 stsp case 'r':
6921 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6922 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6923 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6924 74283ab8 2020-02-07 stsp optarg);
6925 ffd1d5e5 2018-06-23 stsp break;
6926 ffd1d5e5 2018-06-23 stsp default:
6927 e99e2d15 2020-11-24 naddy usage_tree();
6928 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6929 ffd1d5e5 2018-06-23 stsp }
6930 ffd1d5e5 2018-06-23 stsp }
6931 ffd1d5e5 2018-06-23 stsp
6932 ffd1d5e5 2018-06-23 stsp argc -= optind;
6933 ffd1d5e5 2018-06-23 stsp argv += optind;
6934 ffd1d5e5 2018-06-23 stsp
6935 55cccc34 2020-02-20 stsp if (argc > 1)
6936 e99e2d15 2020-11-24 naddy usage_tree();
6937 0ae84acc 2022-06-15 tracey
6938 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6939 0ae84acc 2022-06-15 tracey if (error != NULL)
6940 0ae84acc 2022-06-15 tracey goto done;
6941 74283ab8 2020-02-07 stsp
6942 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6943 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6944 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6945 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6946 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6947 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6948 c156c7a4 2020-12-18 stsp goto done;
6949 55cccc34 2020-02-20 stsp if (worktree)
6950 52185f70 2019-02-05 stsp repo_path =
6951 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6952 55cccc34 2020-02-20 stsp else
6953 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6954 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6955 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6956 c156c7a4 2020-12-18 stsp goto done;
6957 c156c7a4 2020-12-18 stsp }
6958 55cccc34 2020-02-20 stsp }
6959 a915003a 2019-02-05 stsp
6960 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6961 c02c541e 2019-03-29 stsp if (error != NULL)
6962 52185f70 2019-02-05 stsp goto done;
6963 d188b9a6 2019-01-04 stsp
6964 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6965 55cccc34 2020-02-20 stsp repo, worktree);
6966 55cccc34 2020-02-20 stsp if (error)
6967 55cccc34 2020-02-20 stsp goto done;
6968 55cccc34 2020-02-20 stsp
6969 55cccc34 2020-02-20 stsp init_curses();
6970 55cccc34 2020-02-20 stsp
6971 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6972 c02c541e 2019-03-29 stsp if (error)
6973 52185f70 2019-02-05 stsp goto done;
6974 ffd1d5e5 2018-06-23 stsp
6975 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6976 51a10b52 2020-12-26 stsp if (error)
6977 51a10b52 2020-12-26 stsp goto done;
6978 51a10b52 2020-12-26 stsp
6979 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6980 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6981 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6982 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6983 4e97c21c 2020-12-06 stsp if (error)
6984 4e97c21c 2020-12-06 stsp goto done;
6985 4e97c21c 2020-12-06 stsp head_ref_name = label;
6986 4e97c21c 2020-12-06 stsp } else {
6987 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6988 4e97c21c 2020-12-06 stsp if (error == NULL)
6989 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6990 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6991 4e97c21c 2020-12-06 stsp goto done;
6992 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6993 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6994 4e97c21c 2020-12-06 stsp if (error)
6995 4e97c21c 2020-12-06 stsp goto done;
6996 4e97c21c 2020-12-06 stsp }
6997 ffd1d5e5 2018-06-23 stsp
6998 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6999 a44927cc 2022-04-07 stsp if (error)
7000 a44927cc 2022-04-07 stsp goto done;
7001 a44927cc 2022-04-07 stsp
7002 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7003 5221c383 2018-08-01 stsp if (view == NULL) {
7004 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7005 5221c383 2018-08-01 stsp goto done;
7006 5221c383 2018-08-01 stsp }
7007 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7008 ad80ab7b 2018-08-04 stsp if (error)
7009 ad80ab7b 2018-08-04 stsp goto done;
7010 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7011 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7012 d91faf3b 2020-12-01 naddy in_repo_path);
7013 55cccc34 2020-02-20 stsp if (error)
7014 55cccc34 2020-02-20 stsp goto done;
7015 55cccc34 2020-02-20 stsp }
7016 55cccc34 2020-02-20 stsp
7017 55cccc34 2020-02-20 stsp if (worktree) {
7018 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7019 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7020 55cccc34 2020-02-20 stsp worktree = NULL;
7021 55cccc34 2020-02-20 stsp }
7022 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7023 ffd1d5e5 2018-06-23 stsp done:
7024 52185f70 2019-02-05 stsp free(repo_path);
7025 e4a0e26d 2020-02-20 stsp free(cwd);
7026 ffd1d5e5 2018-06-23 stsp free(commit_id);
7027 4e97c21c 2020-12-06 stsp free(label);
7028 486cd271 2020-12-06 stsp if (ref)
7029 486cd271 2020-12-06 stsp got_ref_close(ref);
7030 1d0f4054 2021-06-17 stsp if (repo) {
7031 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7032 1d0f4054 2021-06-17 stsp if (error == NULL)
7033 1d0f4054 2021-06-17 stsp error = close_err;
7034 1d0f4054 2021-06-17 stsp }
7035 0ae84acc 2022-06-15 tracey if (pack_fds) {
7036 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7037 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7038 0ae84acc 2022-06-15 tracey if (error == NULL)
7039 0ae84acc 2022-06-15 tracey error = pack_err;
7040 0ae84acc 2022-06-15 tracey }
7041 51a10b52 2020-12-26 stsp tog_free_refs();
7042 ffd1d5e5 2018-06-23 stsp return error;
7043 6458efa5 2020-11-24 stsp }
7044 6458efa5 2020-11-24 stsp
7045 6458efa5 2020-11-24 stsp static const struct got_error *
7046 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7047 6458efa5 2020-11-24 stsp {
7048 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7049 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7050 6458efa5 2020-11-24 stsp
7051 6458efa5 2020-11-24 stsp s->nrefs = 0;
7052 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7053 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7054 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7055 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7056 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7057 6458efa5 2020-11-24 stsp continue;
7058 6458efa5 2020-11-24 stsp
7059 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7060 6458efa5 2020-11-24 stsp if (re == NULL)
7061 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7062 6458efa5 2020-11-24 stsp
7063 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7064 8924d611 2020-12-26 stsp if (re->ref == NULL)
7065 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7066 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7067 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7068 6458efa5 2020-11-24 stsp }
7069 6458efa5 2020-11-24 stsp
7070 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7071 6458efa5 2020-11-24 stsp return NULL;
7072 6458efa5 2020-11-24 stsp }
7073 6458efa5 2020-11-24 stsp
7074 336075a4 2022-06-25 op static void
7075 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7076 6458efa5 2020-11-24 stsp {
7077 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7078 6458efa5 2020-11-24 stsp
7079 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7080 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7081 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7082 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7083 6458efa5 2020-11-24 stsp free(re);
7084 6458efa5 2020-11-24 stsp }
7085 6458efa5 2020-11-24 stsp }
7086 6458efa5 2020-11-24 stsp
7087 6458efa5 2020-11-24 stsp static const struct got_error *
7088 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7089 6458efa5 2020-11-24 stsp {
7090 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7091 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7092 6458efa5 2020-11-24 stsp
7093 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7094 6458efa5 2020-11-24 stsp s->repo = repo;
7095 6458efa5 2020-11-24 stsp
7096 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7097 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7098 6458efa5 2020-11-24 stsp
7099 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7100 6458efa5 2020-11-24 stsp if (err)
7101 6458efa5 2020-11-24 stsp return err;
7102 34ba6917 2020-11-30 stsp
7103 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7104 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7105 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7106 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7107 6458efa5 2020-11-24 stsp if (err)
7108 6458efa5 2020-11-24 stsp goto done;
7109 6458efa5 2020-11-24 stsp
7110 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7111 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7112 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7113 6458efa5 2020-11-24 stsp if (err)
7114 6458efa5 2020-11-24 stsp goto done;
7115 6458efa5 2020-11-24 stsp
7116 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7117 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7118 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7119 cc488aa7 2022-01-23 stsp if (err)
7120 cc488aa7 2022-01-23 stsp goto done;
7121 cc488aa7 2022-01-23 stsp
7122 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7123 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7124 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7125 6458efa5 2020-11-24 stsp if (err)
7126 6458efa5 2020-11-24 stsp goto done;
7127 6458efa5 2020-11-24 stsp }
7128 6458efa5 2020-11-24 stsp
7129 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7130 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7131 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7132 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7133 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7134 6458efa5 2020-11-24 stsp done:
7135 6458efa5 2020-11-24 stsp if (err)
7136 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7137 6458efa5 2020-11-24 stsp return err;
7138 6458efa5 2020-11-24 stsp }
7139 6458efa5 2020-11-24 stsp
7140 6458efa5 2020-11-24 stsp static const struct got_error *
7141 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7142 6458efa5 2020-11-24 stsp {
7143 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7144 6458efa5 2020-11-24 stsp
7145 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7146 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7147 6458efa5 2020-11-24 stsp
7148 6458efa5 2020-11-24 stsp return NULL;
7149 9f7d7167 2018-04-29 stsp }
7150 ce5b7c56 2019-07-09 stsp
7151 6458efa5 2020-11-24 stsp static const struct got_error *
7152 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7153 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7154 6458efa5 2020-11-24 stsp {
7155 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7156 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7157 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7158 6458efa5 2020-11-24 stsp int obj_type;
7159 6458efa5 2020-11-24 stsp
7160 c42c9805 2020-11-24 stsp *commit_id = NULL;
7161 6458efa5 2020-11-24 stsp
7162 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7163 6458efa5 2020-11-24 stsp if (err)
7164 6458efa5 2020-11-24 stsp return err;
7165 6458efa5 2020-11-24 stsp
7166 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7167 6458efa5 2020-11-24 stsp if (err)
7168 6458efa5 2020-11-24 stsp goto done;
7169 6458efa5 2020-11-24 stsp
7170 6458efa5 2020-11-24 stsp switch (obj_type) {
7171 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7172 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7173 6458efa5 2020-11-24 stsp break;
7174 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7175 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7176 6458efa5 2020-11-24 stsp if (err)
7177 6458efa5 2020-11-24 stsp goto done;
7178 c42c9805 2020-11-24 stsp free(obj_id);
7179 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7180 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7181 c42c9805 2020-11-24 stsp if (err)
7182 6458efa5 2020-11-24 stsp goto done;
7183 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7184 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7185 c42c9805 2020-11-24 stsp goto done;
7186 c42c9805 2020-11-24 stsp }
7187 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7188 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7189 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7190 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7191 c42c9805 2020-11-24 stsp goto done;
7192 c42c9805 2020-11-24 stsp }
7193 6458efa5 2020-11-24 stsp break;
7194 6458efa5 2020-11-24 stsp default:
7195 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7196 c42c9805 2020-11-24 stsp break;
7197 6458efa5 2020-11-24 stsp }
7198 6458efa5 2020-11-24 stsp
7199 c42c9805 2020-11-24 stsp done:
7200 c42c9805 2020-11-24 stsp if (tag)
7201 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7202 c42c9805 2020-11-24 stsp if (err) {
7203 c42c9805 2020-11-24 stsp free(*commit_id);
7204 c42c9805 2020-11-24 stsp *commit_id = NULL;
7205 c42c9805 2020-11-24 stsp }
7206 c42c9805 2020-11-24 stsp return err;
7207 c42c9805 2020-11-24 stsp }
7208 c42c9805 2020-11-24 stsp
7209 c42c9805 2020-11-24 stsp static const struct got_error *
7210 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7211 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7212 c42c9805 2020-11-24 stsp {
7213 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7214 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7215 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7216 c42c9805 2020-11-24 stsp
7217 c42c9805 2020-11-24 stsp *new_view = NULL;
7218 c42c9805 2020-11-24 stsp
7219 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7220 c42c9805 2020-11-24 stsp if (err) {
7221 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7222 c42c9805 2020-11-24 stsp return err;
7223 c42c9805 2020-11-24 stsp else
7224 c42c9805 2020-11-24 stsp return NULL;
7225 c42c9805 2020-11-24 stsp }
7226 c42c9805 2020-11-24 stsp
7227 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7228 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7229 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7230 6458efa5 2020-11-24 stsp goto done;
7231 6458efa5 2020-11-24 stsp }
7232 6458efa5 2020-11-24 stsp
7233 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7234 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7235 6458efa5 2020-11-24 stsp done:
7236 6458efa5 2020-11-24 stsp if (err)
7237 6458efa5 2020-11-24 stsp view_close(log_view);
7238 6458efa5 2020-11-24 stsp else
7239 6458efa5 2020-11-24 stsp *new_view = log_view;
7240 c42c9805 2020-11-24 stsp free(commit_id);
7241 6458efa5 2020-11-24 stsp return err;
7242 6458efa5 2020-11-24 stsp }
7243 6458efa5 2020-11-24 stsp
7244 ce5b7c56 2019-07-09 stsp static void
7245 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7246 6458efa5 2020-11-24 stsp {
7247 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7248 34ba6917 2020-11-30 stsp int i = 0;
7249 6458efa5 2020-11-24 stsp
7250 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7251 6458efa5 2020-11-24 stsp return;
7252 6458efa5 2020-11-24 stsp
7253 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7254 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7255 34ba6917 2020-11-30 stsp if (re == NULL)
7256 34ba6917 2020-11-30 stsp break;
7257 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7258 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7259 6458efa5 2020-11-24 stsp }
7260 6458efa5 2020-11-24 stsp }
7261 6458efa5 2020-11-24 stsp
7262 9b058f45 2022-06-30 mark static const struct got_error *
7263 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7264 6458efa5 2020-11-24 stsp {
7265 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7266 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7267 6458efa5 2020-11-24 stsp int n = 0;
7268 6458efa5 2020-11-24 stsp
7269 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7270 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7271 6458efa5 2020-11-24 stsp else
7272 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7273 6458efa5 2020-11-24 stsp
7274 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7275 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7276 9b058f45 2022-06-30 mark if (last)
7277 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7278 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7279 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7280 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7281 6458efa5 2020-11-24 stsp }
7282 6458efa5 2020-11-24 stsp }
7283 9b058f45 2022-06-30 mark
7284 9b058f45 2022-06-30 mark return NULL;
7285 6458efa5 2020-11-24 stsp }
7286 6458efa5 2020-11-24 stsp
7287 6458efa5 2020-11-24 stsp static const struct got_error *
7288 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7289 6458efa5 2020-11-24 stsp {
7290 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7291 6458efa5 2020-11-24 stsp
7292 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7293 6458efa5 2020-11-24 stsp return NULL;
7294 6458efa5 2020-11-24 stsp }
7295 6458efa5 2020-11-24 stsp
7296 6458efa5 2020-11-24 stsp static int
7297 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7298 6458efa5 2020-11-24 stsp {
7299 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7300 6458efa5 2020-11-24 stsp
7301 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7302 6458efa5 2020-11-24 stsp 0) == 0;
7303 6458efa5 2020-11-24 stsp }
7304 6458efa5 2020-11-24 stsp
7305 6458efa5 2020-11-24 stsp static const struct got_error *
7306 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7307 6458efa5 2020-11-24 stsp {
7308 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7309 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7310 6458efa5 2020-11-24 stsp
7311 6458efa5 2020-11-24 stsp if (!view->searching) {
7312 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7313 6458efa5 2020-11-24 stsp return NULL;
7314 6458efa5 2020-11-24 stsp }
7315 6458efa5 2020-11-24 stsp
7316 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7317 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7318 6458efa5 2020-11-24 stsp if (s->selected_entry)
7319 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7320 6458efa5 2020-11-24 stsp else
7321 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7322 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7323 6458efa5 2020-11-24 stsp } else {
7324 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7325 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7326 6458efa5 2020-11-24 stsp else
7327 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7328 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7329 6458efa5 2020-11-24 stsp }
7330 6458efa5 2020-11-24 stsp } else {
7331 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7332 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7333 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7334 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7335 6458efa5 2020-11-24 stsp else
7336 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7337 6458efa5 2020-11-24 stsp }
7338 6458efa5 2020-11-24 stsp
7339 6458efa5 2020-11-24 stsp while (1) {
7340 6458efa5 2020-11-24 stsp if (re == NULL) {
7341 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7342 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7343 6458efa5 2020-11-24 stsp return NULL;
7344 6458efa5 2020-11-24 stsp }
7345 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7346 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7347 6458efa5 2020-11-24 stsp else
7348 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7349 6458efa5 2020-11-24 stsp }
7350 6458efa5 2020-11-24 stsp
7351 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7352 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7353 6458efa5 2020-11-24 stsp s->matched_entry = re;
7354 6458efa5 2020-11-24 stsp break;
7355 6458efa5 2020-11-24 stsp }
7356 6458efa5 2020-11-24 stsp
7357 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7358 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7359 6458efa5 2020-11-24 stsp else
7360 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7361 6458efa5 2020-11-24 stsp }
7362 6458efa5 2020-11-24 stsp
7363 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7364 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7365 6458efa5 2020-11-24 stsp s->selected = 0;
7366 6458efa5 2020-11-24 stsp }
7367 6458efa5 2020-11-24 stsp
7368 6458efa5 2020-11-24 stsp return NULL;
7369 6458efa5 2020-11-24 stsp }
7370 6458efa5 2020-11-24 stsp
7371 6458efa5 2020-11-24 stsp static const struct got_error *
7372 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7373 6458efa5 2020-11-24 stsp {
7374 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7375 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7376 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7377 6458efa5 2020-11-24 stsp char *line = NULL;
7378 6458efa5 2020-11-24 stsp wchar_t *wline;
7379 6458efa5 2020-11-24 stsp struct tog_color *tc;
7380 6458efa5 2020-11-24 stsp int width, n;
7381 6458efa5 2020-11-24 stsp int limit = view->nlines;
7382 6458efa5 2020-11-24 stsp
7383 6458efa5 2020-11-24 stsp werase(view->window);
7384 6458efa5 2020-11-24 stsp
7385 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7386 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7387 9b058f45 2022-06-30 mark --limit; /* border */
7388 6458efa5 2020-11-24 stsp
7389 6458efa5 2020-11-24 stsp if (limit == 0)
7390 6458efa5 2020-11-24 stsp return NULL;
7391 6458efa5 2020-11-24 stsp
7392 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7393 6458efa5 2020-11-24 stsp
7394 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7395 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7396 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7397 6458efa5 2020-11-24 stsp
7398 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7399 6458efa5 2020-11-24 stsp if (err) {
7400 6458efa5 2020-11-24 stsp free(line);
7401 6458efa5 2020-11-24 stsp return err;
7402 6458efa5 2020-11-24 stsp }
7403 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7404 6458efa5 2020-11-24 stsp wstandout(view->window);
7405 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7406 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7407 6458efa5 2020-11-24 stsp wstandend(view->window);
7408 6458efa5 2020-11-24 stsp free(wline);
7409 6458efa5 2020-11-24 stsp wline = NULL;
7410 6458efa5 2020-11-24 stsp free(line);
7411 6458efa5 2020-11-24 stsp line = NULL;
7412 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7413 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7414 6458efa5 2020-11-24 stsp if (--limit <= 0)
7415 6458efa5 2020-11-24 stsp return NULL;
7416 6458efa5 2020-11-24 stsp
7417 6458efa5 2020-11-24 stsp n = 0;
7418 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7419 6458efa5 2020-11-24 stsp char *line = NULL;
7420 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7421 6458efa5 2020-11-24 stsp
7422 b4996bee 2022-06-16 stsp if (s->show_date) {
7423 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7424 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7425 b4996bee 2022-06-16 stsp struct got_object_id *id;
7426 b4996bee 2022-06-16 stsp struct tm tm;
7427 b4996bee 2022-06-16 stsp time_t t;
7428 b4996bee 2022-06-16 stsp
7429 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7430 b4996bee 2022-06-16 stsp if (err)
7431 b4996bee 2022-06-16 stsp return err;
7432 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7433 b4996bee 2022-06-16 stsp if (err) {
7434 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7435 b4996bee 2022-06-16 stsp free(id);
7436 b4996bee 2022-06-16 stsp return err;
7437 b4996bee 2022-06-16 stsp }
7438 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7439 b4996bee 2022-06-16 stsp id);
7440 b4996bee 2022-06-16 stsp if (err) {
7441 b4996bee 2022-06-16 stsp free(id);
7442 b4996bee 2022-06-16 stsp return err;
7443 b4996bee 2022-06-16 stsp }
7444 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7445 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7446 b4996bee 2022-06-16 stsp } else {
7447 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7448 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7449 b4996bee 2022-06-16 stsp }
7450 b4996bee 2022-06-16 stsp free(id);
7451 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7452 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7453 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7454 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7455 b4996bee 2022-06-16 stsp }
7456 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7457 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7458 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7459 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7460 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7461 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7462 6458efa5 2020-11-24 stsp struct got_object_id *id;
7463 6458efa5 2020-11-24 stsp char *id_str;
7464 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7465 6458efa5 2020-11-24 stsp if (err)
7466 6458efa5 2020-11-24 stsp return err;
7467 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7468 6458efa5 2020-11-24 stsp if (err) {
7469 6458efa5 2020-11-24 stsp free(id);
7470 6458efa5 2020-11-24 stsp return err;
7471 6458efa5 2020-11-24 stsp }
7472 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7473 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7474 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7475 6458efa5 2020-11-24 stsp free(id);
7476 6458efa5 2020-11-24 stsp free(id_str);
7477 6458efa5 2020-11-24 stsp return err;
7478 6458efa5 2020-11-24 stsp }
7479 6458efa5 2020-11-24 stsp free(id);
7480 6458efa5 2020-11-24 stsp free(id_str);
7481 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7482 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7483 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7484 6458efa5 2020-11-24 stsp
7485 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7486 ccda2f4d 2022-06-16 stsp 0, 0);
7487 6458efa5 2020-11-24 stsp if (err) {
7488 6458efa5 2020-11-24 stsp free(line);
7489 6458efa5 2020-11-24 stsp return err;
7490 6458efa5 2020-11-24 stsp }
7491 6458efa5 2020-11-24 stsp if (n == s->selected) {
7492 6458efa5 2020-11-24 stsp if (view->focussed)
7493 6458efa5 2020-11-24 stsp wstandout(view->window);
7494 6458efa5 2020-11-24 stsp s->selected_entry = re;
7495 6458efa5 2020-11-24 stsp }
7496 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7497 6458efa5 2020-11-24 stsp if (tc)
7498 6458efa5 2020-11-24 stsp wattr_on(view->window,
7499 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7500 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7501 6458efa5 2020-11-24 stsp if (tc)
7502 6458efa5 2020-11-24 stsp wattr_off(view->window,
7503 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7504 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7505 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7506 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7507 6458efa5 2020-11-24 stsp wstandend(view->window);
7508 6458efa5 2020-11-24 stsp free(line);
7509 6458efa5 2020-11-24 stsp free(wline);
7510 6458efa5 2020-11-24 stsp wline = NULL;
7511 6458efa5 2020-11-24 stsp n++;
7512 6458efa5 2020-11-24 stsp s->ndisplayed++;
7513 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7514 6458efa5 2020-11-24 stsp
7515 6458efa5 2020-11-24 stsp limit--;
7516 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7517 6458efa5 2020-11-24 stsp }
7518 6458efa5 2020-11-24 stsp
7519 9b058f45 2022-06-30 mark view_border(view);
7520 6458efa5 2020-11-24 stsp return err;
7521 6458efa5 2020-11-24 stsp }
7522 6458efa5 2020-11-24 stsp
7523 6458efa5 2020-11-24 stsp static const struct got_error *
7524 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7525 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7526 c42c9805 2020-11-24 stsp {
7527 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7528 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7529 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7530 c42c9805 2020-11-24 stsp
7531 c42c9805 2020-11-24 stsp *new_view = NULL;
7532 c42c9805 2020-11-24 stsp
7533 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7534 c42c9805 2020-11-24 stsp if (err) {
7535 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7536 c42c9805 2020-11-24 stsp return err;
7537 c42c9805 2020-11-24 stsp else
7538 c42c9805 2020-11-24 stsp return NULL;
7539 c42c9805 2020-11-24 stsp }
7540 c42c9805 2020-11-24 stsp
7541 c42c9805 2020-11-24 stsp
7542 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7543 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7544 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7545 c42c9805 2020-11-24 stsp goto done;
7546 c42c9805 2020-11-24 stsp }
7547 c42c9805 2020-11-24 stsp
7548 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7549 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7550 c42c9805 2020-11-24 stsp if (err)
7551 c42c9805 2020-11-24 stsp goto done;
7552 c42c9805 2020-11-24 stsp
7553 c42c9805 2020-11-24 stsp *new_view = tree_view;
7554 c42c9805 2020-11-24 stsp done:
7555 c42c9805 2020-11-24 stsp free(commit_id);
7556 c42c9805 2020-11-24 stsp return err;
7557 c42c9805 2020-11-24 stsp }
7558 c42c9805 2020-11-24 stsp static const struct got_error *
7559 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7560 6458efa5 2020-11-24 stsp {
7561 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7562 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7563 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7564 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
7565 6458efa5 2020-11-24 stsp
7566 6458efa5 2020-11-24 stsp switch (ch) {
7567 6458efa5 2020-11-24 stsp case 'i':
7568 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7569 640cd7ff 2022-06-22 mark view->count = 0;
7570 7f66531d 2021-11-16 stsp break;
7571 b4996bee 2022-06-16 stsp case 'm':
7572 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7573 640cd7ff 2022-06-22 mark view->count = 0;
7574 b4996bee 2022-06-16 stsp break;
7575 07a065fe 2021-11-20 stsp case 'o':
7576 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7577 640cd7ff 2022-06-22 mark view->count = 0;
7578 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7579 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7580 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7581 50617b77 2021-11-20 stsp if (err)
7582 50617b77 2021-11-20 stsp break;
7583 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7584 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7585 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7586 7f66531d 2021-11-16 stsp if (err)
7587 7f66531d 2021-11-16 stsp break;
7588 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7589 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7590 6458efa5 2020-11-24 stsp break;
7591 6458efa5 2020-11-24 stsp case KEY_ENTER:
7592 6458efa5 2020-11-24 stsp case '\r':
7593 640cd7ff 2022-06-22 mark view->count = 0;
7594 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7595 9b058f45 2022-06-30 mark break;
7596 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7597 6458efa5 2020-11-24 stsp break;
7598 5e98fb33 2022-07-22 mark case 'T':
7599 640cd7ff 2022-06-22 mark view->count = 0;
7600 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7601 c42c9805 2020-11-24 stsp break;
7602 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
7603 c42c9805 2020-11-24 stsp break;
7604 e4526bf5 2021-09-03 naddy case 'g':
7605 e4526bf5 2021-09-03 naddy case KEY_HOME:
7606 e4526bf5 2021-09-03 naddy s->selected = 0;
7607 640cd7ff 2022-06-22 mark view->count = 0;
7608 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7609 e4526bf5 2021-09-03 naddy break;
7610 e4526bf5 2021-09-03 naddy case 'G':
7611 9b058f45 2022-06-30 mark case KEY_END: {
7612 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7613 9b058f45 2022-06-30 mark
7614 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7615 9b058f45 2022-06-30 mark --eos; /* border */
7616 e4526bf5 2021-09-03 naddy s->selected = 0;
7617 640cd7ff 2022-06-22 mark view->count = 0;
7618 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7619 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7620 e4526bf5 2021-09-03 naddy if (re == NULL)
7621 e4526bf5 2021-09-03 naddy break;
7622 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7623 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7624 e4526bf5 2021-09-03 naddy }
7625 e4526bf5 2021-09-03 naddy if (n > 0)
7626 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7627 e4526bf5 2021-09-03 naddy break;
7628 9b058f45 2022-06-30 mark }
7629 6458efa5 2020-11-24 stsp case 'k':
7630 6458efa5 2020-11-24 stsp case KEY_UP:
7631 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7632 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7633 6458efa5 2020-11-24 stsp s->selected--;
7634 6458efa5 2020-11-24 stsp break;
7635 34ba6917 2020-11-30 stsp }
7636 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7637 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7638 640cd7ff 2022-06-22 mark view->count = 0;
7639 6458efa5 2020-11-24 stsp break;
7640 83cc4199 2022-06-13 stsp case CTRL('u'):
7641 33c3719a 2022-06-15 stsp case 'u':
7642 83cc4199 2022-06-13 stsp nscroll /= 2;
7643 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7644 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7645 6458efa5 2020-11-24 stsp case CTRL('b'):
7646 61417565 2022-06-20 mark case 'b':
7647 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7648 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7649 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7650 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7651 640cd7ff 2022-06-22 mark view->count = 0;
7652 6458efa5 2020-11-24 stsp break;
7653 6458efa5 2020-11-24 stsp case 'j':
7654 6458efa5 2020-11-24 stsp case KEY_DOWN:
7655 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7656 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7657 6458efa5 2020-11-24 stsp s->selected++;
7658 6458efa5 2020-11-24 stsp break;
7659 6458efa5 2020-11-24 stsp }
7660 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7661 6458efa5 2020-11-24 stsp /* can't scroll any further */
7662 640cd7ff 2022-06-22 mark view->count = 0;
7663 6458efa5 2020-11-24 stsp break;
7664 640cd7ff 2022-06-22 mark }
7665 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7666 6458efa5 2020-11-24 stsp break;
7667 83cc4199 2022-06-13 stsp case CTRL('d'):
7668 33c3719a 2022-06-15 stsp case 'd':
7669 83cc4199 2022-06-13 stsp nscroll /= 2;
7670 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7671 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7672 6458efa5 2020-11-24 stsp case CTRL('f'):
7673 61417565 2022-06-20 mark case 'f':
7674 48bb96f0 2022-06-20 naddy case ' ':
7675 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7676 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7677 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7678 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7679 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7680 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7681 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7682 640cd7ff 2022-06-22 mark view->count = 0;
7683 6458efa5 2020-11-24 stsp break;
7684 6458efa5 2020-11-24 stsp }
7685 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7686 6458efa5 2020-11-24 stsp break;
7687 6458efa5 2020-11-24 stsp case CTRL('l'):
7688 640cd7ff 2022-06-22 mark view->count = 0;
7689 8924d611 2020-12-26 stsp tog_free_refs();
7690 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7691 8924d611 2020-12-26 stsp if (err)
7692 8924d611 2020-12-26 stsp break;
7693 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7694 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7695 6458efa5 2020-11-24 stsp break;
7696 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7697 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7698 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7699 6458efa5 2020-11-24 stsp break;
7700 6458efa5 2020-11-24 stsp default:
7701 640cd7ff 2022-06-22 mark view->count = 0;
7702 6458efa5 2020-11-24 stsp break;
7703 6458efa5 2020-11-24 stsp }
7704 6458efa5 2020-11-24 stsp
7705 6458efa5 2020-11-24 stsp return err;
7706 6458efa5 2020-11-24 stsp }
7707 6458efa5 2020-11-24 stsp
7708 6458efa5 2020-11-24 stsp __dead static void
7709 6458efa5 2020-11-24 stsp usage_ref(void)
7710 6458efa5 2020-11-24 stsp {
7711 6458efa5 2020-11-24 stsp endwin();
7712 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7713 6458efa5 2020-11-24 stsp getprogname());
7714 6458efa5 2020-11-24 stsp exit(1);
7715 6458efa5 2020-11-24 stsp }
7716 6458efa5 2020-11-24 stsp
7717 6458efa5 2020-11-24 stsp static const struct got_error *
7718 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7719 6458efa5 2020-11-24 stsp {
7720 6458efa5 2020-11-24 stsp const struct got_error *error;
7721 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7722 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7723 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7724 6458efa5 2020-11-24 stsp int ch;
7725 6458efa5 2020-11-24 stsp struct tog_view *view;
7726 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7727 6458efa5 2020-11-24 stsp
7728 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7729 6458efa5 2020-11-24 stsp switch (ch) {
7730 6458efa5 2020-11-24 stsp case 'r':
7731 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7732 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7733 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7734 6458efa5 2020-11-24 stsp optarg);
7735 6458efa5 2020-11-24 stsp break;
7736 6458efa5 2020-11-24 stsp default:
7737 e99e2d15 2020-11-24 naddy usage_ref();
7738 6458efa5 2020-11-24 stsp /* NOTREACHED */
7739 6458efa5 2020-11-24 stsp }
7740 6458efa5 2020-11-24 stsp }
7741 6458efa5 2020-11-24 stsp
7742 6458efa5 2020-11-24 stsp argc -= optind;
7743 6458efa5 2020-11-24 stsp argv += optind;
7744 6458efa5 2020-11-24 stsp
7745 6458efa5 2020-11-24 stsp if (argc > 1)
7746 e99e2d15 2020-11-24 naddy usage_ref();
7747 0ae84acc 2022-06-15 tracey
7748 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7749 0ae84acc 2022-06-15 tracey if (error != NULL)
7750 0ae84acc 2022-06-15 tracey goto done;
7751 6458efa5 2020-11-24 stsp
7752 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7753 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7754 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7755 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7756 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7757 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7758 c156c7a4 2020-12-18 stsp goto done;
7759 6458efa5 2020-11-24 stsp if (worktree)
7760 6458efa5 2020-11-24 stsp repo_path =
7761 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7762 6458efa5 2020-11-24 stsp else
7763 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7764 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7765 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7766 c156c7a4 2020-12-18 stsp goto done;
7767 c156c7a4 2020-12-18 stsp }
7768 6458efa5 2020-11-24 stsp }
7769 6458efa5 2020-11-24 stsp
7770 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7771 6458efa5 2020-11-24 stsp if (error != NULL)
7772 6458efa5 2020-11-24 stsp goto done;
7773 6458efa5 2020-11-24 stsp
7774 6458efa5 2020-11-24 stsp init_curses();
7775 6458efa5 2020-11-24 stsp
7776 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7777 51a10b52 2020-12-26 stsp if (error)
7778 51a10b52 2020-12-26 stsp goto done;
7779 51a10b52 2020-12-26 stsp
7780 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7781 6458efa5 2020-11-24 stsp if (error)
7782 6458efa5 2020-11-24 stsp goto done;
7783 6458efa5 2020-11-24 stsp
7784 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7785 6458efa5 2020-11-24 stsp if (view == NULL) {
7786 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7787 6458efa5 2020-11-24 stsp goto done;
7788 6458efa5 2020-11-24 stsp }
7789 6458efa5 2020-11-24 stsp
7790 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7791 6458efa5 2020-11-24 stsp if (error)
7792 6458efa5 2020-11-24 stsp goto done;
7793 6458efa5 2020-11-24 stsp
7794 6458efa5 2020-11-24 stsp if (worktree) {
7795 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7796 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7797 6458efa5 2020-11-24 stsp worktree = NULL;
7798 6458efa5 2020-11-24 stsp }
7799 6458efa5 2020-11-24 stsp error = view_loop(view);
7800 6458efa5 2020-11-24 stsp done:
7801 6458efa5 2020-11-24 stsp free(repo_path);
7802 6458efa5 2020-11-24 stsp free(cwd);
7803 1d0f4054 2021-06-17 stsp if (repo) {
7804 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7805 1d0f4054 2021-06-17 stsp if (close_err)
7806 1d0f4054 2021-06-17 stsp error = close_err;
7807 1d0f4054 2021-06-17 stsp }
7808 0ae84acc 2022-06-15 tracey if (pack_fds) {
7809 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7810 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7811 0ae84acc 2022-06-15 tracey if (error == NULL)
7812 0ae84acc 2022-06-15 tracey error = pack_err;
7813 0ae84acc 2022-06-15 tracey }
7814 51a10b52 2020-12-26 stsp tog_free_refs();
7815 6458efa5 2020-11-24 stsp return error;
7816 6458efa5 2020-11-24 stsp }
7817 6458efa5 2020-11-24 stsp
7818 136e2bd4 2022-07-23 mark static const struct got_error *
7819 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7820 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
7821 136e2bd4 2022-07-23 mark {
7822 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
7823 136e2bd4 2022-07-23 mark
7824 136e2bd4 2022-07-23 mark *new_view = NULL;
7825 136e2bd4 2022-07-23 mark
7826 136e2bd4 2022-07-23 mark switch (request) {
7827 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
7828 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
7829 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
7830 136e2bd4 2022-07-23 mark
7831 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
7832 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
7833 136e2bd4 2022-07-23 mark view, s->repo);
7834 136e2bd4 2022-07-23 mark } else
7835 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7836 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7837 136e2bd4 2022-07-23 mark break;
7838 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
7839 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
7840 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
7841 136e2bd4 2022-07-23 mark
7842 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
7843 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
7844 136e2bd4 2022-07-23 mark s->repo);
7845 136e2bd4 2022-07-23 mark } else
7846 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7847 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7848 136e2bd4 2022-07-23 mark break;
7849 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
7850 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
7851 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
7852 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
7853 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
7854 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
7855 136e2bd4 2022-07-23 mark &view->state.tree);
7856 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
7857 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
7858 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
7859 136e2bd4 2022-07-23 mark view->state.ref.repo);
7860 136e2bd4 2022-07-23 mark else
7861 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7862 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7863 136e2bd4 2022-07-23 mark break;
7864 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
7865 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
7866 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
7867 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
7868 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
7869 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
7870 136e2bd4 2022-07-23 mark view->state.log.repo);
7871 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
7872 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
7873 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
7874 136e2bd4 2022-07-23 mark view->state.ref.repo);
7875 136e2bd4 2022-07-23 mark else
7876 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7877 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7878 136e2bd4 2022-07-23 mark break;
7879 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
7880 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7881 136e2bd4 2022-07-23 mark if (*new_view == NULL)
7882 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
7883 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
7884 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
7885 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
7886 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
7887 136e2bd4 2022-07-23 mark else
7888 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
7889 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7890 136e2bd4 2022-07-23 mark if (err)
7891 136e2bd4 2022-07-23 mark view_close(*new_view);
7892 136e2bd4 2022-07-23 mark break;
7893 136e2bd4 2022-07-23 mark default:
7894 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7895 136e2bd4 2022-07-23 mark }
7896 136e2bd4 2022-07-23 mark
7897 136e2bd4 2022-07-23 mark return err;
7898 136e2bd4 2022-07-23 mark }
7899 136e2bd4 2022-07-23 mark
7900 9b058f45 2022-06-30 mark /*
7901 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7902 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7903 9b058f45 2022-06-30 mark */
7904 6458efa5 2020-11-24 stsp static void
7905 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7906 9b058f45 2022-06-30 mark {
7907 9b058f45 2022-06-30 mark switch (view->type) {
7908 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7909 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7910 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7911 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7912 9b058f45 2022-06-30 mark 1);
7913 9b058f45 2022-06-30 mark break;
7914 9b058f45 2022-06-30 mark }
7915 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7916 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7917 9b058f45 2022-06-30 mark else
7918 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7919 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7920 9b058f45 2022-06-30 mark break;
7921 9b058f45 2022-06-30 mark }
7922 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7923 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7924 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7925 9b058f45 2022-06-30 mark break;
7926 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7927 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7928 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7929 9b058f45 2022-06-30 mark break;
7930 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7931 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7932 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7933 9b058f45 2022-06-30 mark break;
7934 9b058f45 2022-06-30 mark default:
7935 9b058f45 2022-06-30 mark break;
7936 9b058f45 2022-06-30 mark }
7937 9b058f45 2022-06-30 mark
7938 9b058f45 2022-06-30 mark view->offset = 0;
7939 9b058f45 2022-06-30 mark }
7940 9b058f45 2022-06-30 mark
7941 9b058f45 2022-06-30 mark /*
7942 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7943 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7944 9b058f45 2022-06-30 mark */
7945 9b058f45 2022-06-30 mark static const struct got_error *
7946 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7947 9b058f45 2022-06-30 mark {
7948 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7949 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7950 9b058f45 2022-06-30 mark int *selected = NULL;
7951 9b058f45 2022-06-30 mark int header, offset;
7952 9b058f45 2022-06-30 mark
7953 9b058f45 2022-06-30 mark switch (view->type) {
7954 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7955 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7956 9b058f45 2022-06-30 mark header = 3;
7957 9b058f45 2022-06-30 mark scrolld = NULL;
7958 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7959 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7960 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7961 9b058f45 2022-06-30 mark s->selected_line -= offset;
7962 9b058f45 2022-06-30 mark view->offset = offset;
7963 9b058f45 2022-06-30 mark }
7964 9b058f45 2022-06-30 mark break;
7965 9b058f45 2022-06-30 mark }
7966 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7967 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7968 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7969 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
7970 9b058f45 2022-06-30 mark selected = &s->selected;
7971 9b058f45 2022-06-30 mark break;
7972 9b058f45 2022-06-30 mark }
7973 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7974 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7975 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7976 9b058f45 2022-06-30 mark header = 3;
7977 9b058f45 2022-06-30 mark selected = &s->selected;
7978 9b058f45 2022-06-30 mark break;
7979 9b058f45 2022-06-30 mark }
7980 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7981 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7982 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7983 9b058f45 2022-06-30 mark header = 5;
7984 9b058f45 2022-06-30 mark selected = &s->selected;
7985 9b058f45 2022-06-30 mark break;
7986 9b058f45 2022-06-30 mark }
7987 9b058f45 2022-06-30 mark default:
7988 9b058f45 2022-06-30 mark selected = NULL;
7989 9b058f45 2022-06-30 mark scrolld = NULL;
7990 9b058f45 2022-06-30 mark header = 0;
7991 9b058f45 2022-06-30 mark break;
7992 9b058f45 2022-06-30 mark }
7993 9b058f45 2022-06-30 mark
7994 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7995 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7996 9b058f45 2022-06-30 mark view->offset = offset;
7997 9b058f45 2022-06-30 mark if (scrolld && offset) {
7998 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7999 9b058f45 2022-06-30 mark *selected -= offset;
8000 9b058f45 2022-06-30 mark }
8001 9b058f45 2022-06-30 mark }
8002 9b058f45 2022-06-30 mark
8003 9b058f45 2022-06-30 mark return err;
8004 9b058f45 2022-06-30 mark }
8005 9b058f45 2022-06-30 mark
8006 9b058f45 2022-06-30 mark static void
8007 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8008 ce5b7c56 2019-07-09 stsp {
8009 6059809a 2020-12-17 stsp size_t i;
8010 9f7d7167 2018-04-29 stsp
8011 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8012 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8013 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8014 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8015 ce5b7c56 2019-07-09 stsp }
8016 6879ba42 2020-10-01 naddy fputc('\n', fp);
8017 ce5b7c56 2019-07-09 stsp }
8018 ce5b7c56 2019-07-09 stsp
8019 4ed7e80c 2018-05-20 stsp __dead static void
8020 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8021 9f7d7167 2018-04-29 stsp {
8022 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8023 6879ba42 2020-10-01 naddy
8024 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8025 6879ba42 2020-10-01 naddy getprogname());
8026 6879ba42 2020-10-01 naddy if (hflag) {
8027 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8028 6879ba42 2020-10-01 naddy list_commands(fp);
8029 ee85c5e8 2020-02-29 stsp }
8030 6879ba42 2020-10-01 naddy exit(status);
8031 9f7d7167 2018-04-29 stsp }
8032 9f7d7167 2018-04-29 stsp
8033 c2301be8 2018-04-30 stsp static char **
8034 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8035 c2301be8 2018-04-30 stsp {
8036 ee85c5e8 2020-02-29 stsp va_list ap;
8037 c2301be8 2018-04-30 stsp char **argv;
8038 ee85c5e8 2020-02-29 stsp int i;
8039 c2301be8 2018-04-30 stsp
8040 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8041 ee85c5e8 2020-02-29 stsp
8042 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8043 c2301be8 2018-04-30 stsp if (argv == NULL)
8044 c2301be8 2018-04-30 stsp err(1, "calloc");
8045 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8046 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8047 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8048 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8049 c2301be8 2018-04-30 stsp }
8050 c2301be8 2018-04-30 stsp
8051 ee85c5e8 2020-02-29 stsp va_end(ap);
8052 c2301be8 2018-04-30 stsp return argv;
8053 ee85c5e8 2020-02-29 stsp }
8054 ee85c5e8 2020-02-29 stsp
8055 ee85c5e8 2020-02-29 stsp /*
8056 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8057 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8058 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8059 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8060 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8061 ee85c5e8 2020-02-29 stsp */
8062 ee85c5e8 2020-02-29 stsp static const struct got_error *
8063 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8064 ee85c5e8 2020-02-29 stsp {
8065 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8066 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8067 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8068 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8069 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8070 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8071 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8072 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8073 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8074 84de9106 2020-12-26 stsp
8075 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8076 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8077 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8078 ee85c5e8 2020-02-29 stsp
8079 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8080 0ae84acc 2022-06-15 tracey if (error != NULL)
8081 0ae84acc 2022-06-15 tracey goto done;
8082 0ae84acc 2022-06-15 tracey
8083 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8084 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8085 ee85c5e8 2020-02-29 stsp goto done;
8086 ee85c5e8 2020-02-29 stsp
8087 ee85c5e8 2020-02-29 stsp if (worktree)
8088 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8089 ee85c5e8 2020-02-29 stsp else
8090 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8091 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8092 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8093 ee85c5e8 2020-02-29 stsp goto done;
8094 ee85c5e8 2020-02-29 stsp }
8095 ee85c5e8 2020-02-29 stsp
8096 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8097 ee85c5e8 2020-02-29 stsp if (error != NULL)
8098 ee85c5e8 2020-02-29 stsp goto done;
8099 ee85c5e8 2020-02-29 stsp
8100 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8101 ee85c5e8 2020-02-29 stsp repo, worktree);
8102 ee85c5e8 2020-02-29 stsp if (error)
8103 ee85c5e8 2020-02-29 stsp goto done;
8104 ee85c5e8 2020-02-29 stsp
8105 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8106 84de9106 2020-12-26 stsp if (error)
8107 84de9106 2020-12-26 stsp goto done;
8108 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8109 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8110 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8111 ee85c5e8 2020-02-29 stsp if (error)
8112 ee85c5e8 2020-02-29 stsp goto done;
8113 ee85c5e8 2020-02-29 stsp
8114 ee85c5e8 2020-02-29 stsp if (worktree) {
8115 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8116 ee85c5e8 2020-02-29 stsp worktree = NULL;
8117 ee85c5e8 2020-02-29 stsp }
8118 ee85c5e8 2020-02-29 stsp
8119 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8120 a44927cc 2022-04-07 stsp if (error)
8121 a44927cc 2022-04-07 stsp goto done;
8122 a44927cc 2022-04-07 stsp
8123 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8124 ee85c5e8 2020-02-29 stsp if (error) {
8125 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8126 ee85c5e8 2020-02-29 stsp goto done;
8127 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8128 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8129 6879ba42 2020-10-01 naddy usage(1, 1);
8130 ee85c5e8 2020-02-29 stsp /* not reached */
8131 ee85c5e8 2020-02-29 stsp }
8132 ee85c5e8 2020-02-29 stsp
8133 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8134 ee85c5e8 2020-02-29 stsp if (error)
8135 ee85c5e8 2020-02-29 stsp goto done;
8136 ee85c5e8 2020-02-29 stsp
8137 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8138 ee85c5e8 2020-02-29 stsp argc = 4;
8139 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8140 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8141 ee85c5e8 2020-02-29 stsp done:
8142 1d0f4054 2021-06-17 stsp if (repo) {
8143 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8144 1d0f4054 2021-06-17 stsp if (error == NULL)
8145 1d0f4054 2021-06-17 stsp error = close_err;
8146 1d0f4054 2021-06-17 stsp }
8147 a44927cc 2022-04-07 stsp if (commit)
8148 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8149 ee85c5e8 2020-02-29 stsp if (worktree)
8150 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8151 0ae84acc 2022-06-15 tracey if (pack_fds) {
8152 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8153 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8154 0ae84acc 2022-06-15 tracey if (error == NULL)
8155 0ae84acc 2022-06-15 tracey error = pack_err;
8156 0ae84acc 2022-06-15 tracey }
8157 ee85c5e8 2020-02-29 stsp free(id);
8158 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8159 ee85c5e8 2020-02-29 stsp free(commit_id);
8160 ee85c5e8 2020-02-29 stsp free(cwd);
8161 ee85c5e8 2020-02-29 stsp free(repo_path);
8162 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8163 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8164 ee85c5e8 2020-02-29 stsp int i;
8165 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8166 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8167 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8168 ee85c5e8 2020-02-29 stsp }
8169 87670572 2020-12-26 naddy tog_free_refs();
8170 ee85c5e8 2020-02-29 stsp return error;
8171 c2301be8 2018-04-30 stsp }
8172 c2301be8 2018-04-30 stsp
8173 9f7d7167 2018-04-29 stsp int
8174 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8175 9f7d7167 2018-04-29 stsp {
8176 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8177 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8178 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8179 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8180 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8181 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8182 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8183 83cd27f8 2020-01-13 stsp };
8184 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8185 9f7d7167 2018-04-29 stsp
8186 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8187 9f7d7167 2018-04-29 stsp
8188 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8189 9f7d7167 2018-04-29 stsp switch (ch) {
8190 9f7d7167 2018-04-29 stsp case 'h':
8191 9f7d7167 2018-04-29 stsp hflag = 1;
8192 9f7d7167 2018-04-29 stsp break;
8193 53ccebc2 2019-07-30 stsp case 'V':
8194 53ccebc2 2019-07-30 stsp Vflag = 1;
8195 53ccebc2 2019-07-30 stsp break;
8196 9f7d7167 2018-04-29 stsp default:
8197 6879ba42 2020-10-01 naddy usage(hflag, 1);
8198 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8199 9f7d7167 2018-04-29 stsp }
8200 9f7d7167 2018-04-29 stsp }
8201 9f7d7167 2018-04-29 stsp
8202 9f7d7167 2018-04-29 stsp argc -= optind;
8203 9f7d7167 2018-04-29 stsp argv += optind;
8204 9814e6a3 2020-09-27 naddy optind = 1;
8205 c2301be8 2018-04-30 stsp optreset = 1;
8206 9f7d7167 2018-04-29 stsp
8207 53ccebc2 2019-07-30 stsp if (Vflag) {
8208 53ccebc2 2019-07-30 stsp got_version_print_str();
8209 6879ba42 2020-10-01 naddy return 0;
8210 53ccebc2 2019-07-30 stsp }
8211 4010e238 2020-12-04 stsp
8212 4010e238 2020-12-04 stsp #ifndef PROFILE
8213 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8214 4010e238 2020-12-04 stsp NULL) == -1)
8215 4010e238 2020-12-04 stsp err(1, "pledge");
8216 4010e238 2020-12-04 stsp #endif
8217 53ccebc2 2019-07-30 stsp
8218 c2301be8 2018-04-30 stsp if (argc == 0) {
8219 f29d3e89 2018-06-23 stsp if (hflag)
8220 6879ba42 2020-10-01 naddy usage(hflag, 0);
8221 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8222 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8223 c2301be8 2018-04-30 stsp argc = 1;
8224 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8225 c2301be8 2018-04-30 stsp } else {
8226 6059809a 2020-12-17 stsp size_t i;
8227 9f7d7167 2018-04-29 stsp
8228 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8229 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8230 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8231 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8232 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8233 9f7d7167 2018-04-29 stsp break;
8234 9f7d7167 2018-04-29 stsp }
8235 9f7d7167 2018-04-29 stsp }
8236 ee85c5e8 2020-02-29 stsp }
8237 3642c4c6 2019-07-09 stsp
8238 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8239 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8240 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8241 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8242 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8243 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8244 917d79a7 2022-07-01 stsp }
8245 917d79a7 2022-07-01 stsp
8246 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8247 ee85c5e8 2020-02-29 stsp if (argc != 1)
8248 6879ba42 2020-10-01 naddy usage(0, 1);
8249 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8250 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8251 ee85c5e8 2020-02-29 stsp } else {
8252 ee85c5e8 2020-02-29 stsp if (hflag)
8253 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8254 ee85c5e8 2020-02-29 stsp else
8255 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8256 9f7d7167 2018-04-29 stsp }
8257 9f7d7167 2018-04-29 stsp
8258 9f7d7167 2018-04-29 stsp endwin();
8259 b46c1e04 2020-09-20 naddy putchar('\n');
8260 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8261 a2f4a359 2020-02-28 stsp int i;
8262 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8263 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8264 a2f4a359 2020-02-28 stsp free(cmd_argv);
8265 a2f4a359 2020-02-28 stsp }
8266 a2f4a359 2020-02-28 stsp
8267 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8268 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8269 9f7d7167 2018-04-29 stsp return 0;
8270 9f7d7167 2018-04-29 stsp }