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 d2366e29 2022-07-07 mark err = offset_selection_down(v->child);
1253 7532ccda 2022-07-11 mark } else {
1254 7532ccda 2022-07-11 mark offset_selection_up(v);
1255 7532ccda 2022-07-11 mark offset_selection_up(v->child);
1256 d2366e29 2022-07-07 mark }
1257 dff91825 2022-07-22 mark if (v->resize)
1258 dff91825 2022-07-22 mark err = v->resize(v, 0);
1259 dff91825 2022-07-22 mark else if (v->child->resize)
1260 dff91825 2022-07-22 mark err = v->child->resize(v->child, 0);
1261 d2366e29 2022-07-07 mark
1262 d2366e29 2022-07-07 mark return err;
1263 0cf4efb1 2018-09-29 stsp }
1264 6d0fee91 2018-08-01 stsp
1265 640cd7ff 2022-06-22 mark /*
1266 f0032ce6 2022-07-02 mark * Compute view->count from numeric input. Assign total to view->count and
1267 f0032ce6 2022-07-02 mark * return first non-numeric key entered.
1268 640cd7ff 2022-06-22 mark */
1269 640cd7ff 2022-06-22 mark static int
1270 640cd7ff 2022-06-22 mark get_compound_key(struct tog_view *view, int c)
1271 640cd7ff 2022-06-22 mark {
1272 9b058f45 2022-06-30 mark struct tog_view *v = view;
1273 9b058f45 2022-06-30 mark int x, n = 0;
1274 640cd7ff 2022-06-22 mark
1275 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
1276 9b058f45 2022-06-30 mark v = view->child;
1277 9b058f45 2022-06-30 mark else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1278 9b058f45 2022-06-30 mark v = view->parent;
1279 9b058f45 2022-06-30 mark
1280 640cd7ff 2022-06-22 mark view->count = 0;
1281 f0032ce6 2022-07-02 mark cbreak(); /* block for input */
1282 9b058f45 2022-06-30 mark wmove(v->window, v->nlines - 1, 0);
1283 9b058f45 2022-06-30 mark wclrtoeol(v->window);
1284 9b058f45 2022-06-30 mark waddch(v->window, ':');
1285 640cd7ff 2022-06-22 mark
1286 640cd7ff 2022-06-22 mark do {
1287 9b058f45 2022-06-30 mark x = getcurx(v->window);
1288 9b058f45 2022-06-30 mark if (x != ERR && x < view->ncols) {
1289 9b058f45 2022-06-30 mark waddch(v->window, c);
1290 9b058f45 2022-06-30 mark wrefresh(v->window);
1291 9b058f45 2022-06-30 mark }
1292 9b058f45 2022-06-30 mark
1293 640cd7ff 2022-06-22 mark /*
1294 640cd7ff 2022-06-22 mark * Don't overflow. Max valid request should be the greatest
1295 640cd7ff 2022-06-22 mark * between the longest and total lines; cap at 10 million.
1296 640cd7ff 2022-06-22 mark */
1297 640cd7ff 2022-06-22 mark if (n >= 9999999)
1298 640cd7ff 2022-06-22 mark n = 9999999;
1299 640cd7ff 2022-06-22 mark else
1300 640cd7ff 2022-06-22 mark n = n * 10 + (c - '0');
1301 640cd7ff 2022-06-22 mark } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1302 640cd7ff 2022-06-22 mark
1303 640cd7ff 2022-06-22 mark /* Massage excessive or inapplicable values at the input handler. */
1304 640cd7ff 2022-06-22 mark view->count = n;
1305 640cd7ff 2022-06-22 mark
1306 640cd7ff 2022-06-22 mark return c;
1307 640cd7ff 2022-06-22 mark }
1308 640cd7ff 2022-06-22 mark
1309 0cf4efb1 2018-09-29 stsp static const struct got_error *
1310 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1311 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1312 e5a0f69f 2018-08-18 stsp {
1313 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1314 669b5ffa 2018-10-07 stsp struct tog_view *v;
1315 1a76625f 2018-10-22 stsp int ch, errcode;
1316 e5a0f69f 2018-08-18 stsp
1317 e5a0f69f 2018-08-18 stsp *new = NULL;
1318 8f4ed634 2020-03-26 stsp
1319 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1320 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1321 640cd7ff 2022-06-22 mark view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1322 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1323 640cd7ff 2022-06-22 mark view->count = 0;
1324 640cd7ff 2022-06-22 mark }
1325 e5a0f69f 2018-08-18 stsp
1326 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1327 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1328 82954512 2020-02-03 stsp if (errcode)
1329 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1330 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1331 3da8ef85 2021-09-21 stsp sched_yield();
1332 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1333 82954512 2020-02-03 stsp if (errcode)
1334 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1335 82954512 2020-02-03 stsp "pthread_mutex_lock");
1336 60493ae3 2019-06-20 stsp view->search_next(view);
1337 60493ae3 2019-06-20 stsp return NULL;
1338 60493ae3 2019-06-20 stsp }
1339 60493ae3 2019-06-20 stsp
1340 a6d37fac 2022-07-03 mark nodelay(view->window, FALSE);
1341 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1342 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1343 1a76625f 2018-10-22 stsp if (errcode)
1344 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1345 a6d37fac 2022-07-03 mark /* If we have an unfinished count, let C-g or backspace abort. */
1346 a6d37fac 2022-07-03 mark if (view->count && --view->count) {
1347 a6d37fac 2022-07-03 mark cbreak();
1348 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1349 640cd7ff 2022-06-22 mark ch = wgetch(view->window);
1350 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1351 a6d37fac 2022-07-03 mark view->count = 0;
1352 a6d37fac 2022-07-03 mark else
1353 a6d37fac 2022-07-03 mark ch = view->ch;
1354 a6d37fac 2022-07-03 mark } else {
1355 a6d37fac 2022-07-03 mark ch = wgetch(view->window);
1356 640cd7ff 2022-06-22 mark if (ch >= '1' && ch <= '9')
1357 640cd7ff 2022-06-22 mark view->ch = ch = get_compound_key(view, ch);
1358 640cd7ff 2022-06-22 mark }
1359 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1360 1a76625f 2018-10-22 stsp if (errcode)
1361 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1362 a6d37fac 2022-07-03 mark nodelay(view->window, TRUE);
1363 25791caa 2018-10-24 stsp
1364 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1365 25791caa 2018-10-24 stsp tog_resizeterm();
1366 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1367 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1368 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1369 25791caa 2018-10-24 stsp err = view_resize(v);
1370 25791caa 2018-10-24 stsp if (err)
1371 25791caa 2018-10-24 stsp return err;
1372 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1373 25791caa 2018-10-24 stsp if (err)
1374 25791caa 2018-10-24 stsp return err;
1375 cdfcfb03 2020-12-06 stsp if (v->child) {
1376 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1377 cdfcfb03 2020-12-06 stsp if (err)
1378 cdfcfb03 2020-12-06 stsp return err;
1379 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1380 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1381 cdfcfb03 2020-12-06 stsp if (err)
1382 cdfcfb03 2020-12-06 stsp return err;
1383 3c1dfe12 2022-07-08 mark if (v->child->resized_x || v->child->resized_y) {
1384 3c1dfe12 2022-07-08 mark err = view_resize_split(v, 0);
1385 3c1dfe12 2022-07-08 mark if (err)
1386 3c1dfe12 2022-07-08 mark return err;
1387 3c1dfe12 2022-07-08 mark }
1388 cdfcfb03 2020-12-06 stsp }
1389 25791caa 2018-10-24 stsp }
1390 25791caa 2018-10-24 stsp }
1391 25791caa 2018-10-24 stsp
1392 e5a0f69f 2018-08-18 stsp switch (ch) {
1393 1e37a5c2 2019-05-12 jcs case '\t':
1394 640cd7ff 2022-06-22 mark view->count = 0;
1395 1e37a5c2 2019-05-12 jcs if (view->child) {
1396 e78dc838 2020-12-04 stsp view->focussed = 0;
1397 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1398 e78dc838 2020-12-04 stsp view->focus_child = 1;
1399 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1400 e78dc838 2020-12-04 stsp view->focussed = 0;
1401 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1402 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1403 9b058f45 2022-06-30 mark if (!view_is_splitscreen(view)) {
1404 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1405 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent,
1406 6fe51fee 2022-07-22 mark 0);
1407 9b058f45 2022-06-30 mark if (err)
1408 9b058f45 2022-06-30 mark return err;
1409 9b058f45 2022-06-30 mark }
1410 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1411 6131ff18 2022-06-20 mark err = view_fullscreen(view->parent);
1412 9b058f45 2022-06-30 mark if (err)
1413 9b058f45 2022-06-30 mark return err;
1414 9b058f45 2022-06-30 mark }
1415 1e37a5c2 2019-05-12 jcs }
1416 1e37a5c2 2019-05-12 jcs break;
1417 1e37a5c2 2019-05-12 jcs case 'q':
1418 9b058f45 2022-06-30 mark if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1419 6fe51fee 2022-07-22 mark if (view->parent->resize) {
1420 9b058f45 2022-06-30 mark /* might need more commits to fill fullscreen */
1421 6fe51fee 2022-07-22 mark err = view->parent->resize(view->parent, 0);
1422 9b058f45 2022-06-30 mark if (err)
1423 9b058f45 2022-06-30 mark break;
1424 9b058f45 2022-06-30 mark }
1425 9b058f45 2022-06-30 mark offset_selection_up(view->parent);
1426 9b058f45 2022-06-30 mark }
1427 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1428 9970f7fc 2020-12-03 stsp view->dying = 1;
1429 1e37a5c2 2019-05-12 jcs break;
1430 1e37a5c2 2019-05-12 jcs case 'Q':
1431 1e37a5c2 2019-05-12 jcs *done = 1;
1432 1e37a5c2 2019-05-12 jcs break;
1433 61417565 2022-06-20 mark case 'F':
1434 640cd7ff 2022-06-22 mark view->count = 0;
1435 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1436 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1437 1e37a5c2 2019-05-12 jcs break;
1438 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1439 e78dc838 2020-12-04 stsp view->focussed = 0;
1440 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1441 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1442 3c1dfe12 2022-07-08 mark } else {
1443 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1444 3c1dfe12 2022-07-08 mark if (!err)
1445 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1446 3c1dfe12 2022-07-08 mark }
1447 1e37a5c2 2019-05-12 jcs if (err)
1448 1e37a5c2 2019-05-12 jcs break;
1449 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1450 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1451 1e37a5c2 2019-05-12 jcs } else {
1452 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1453 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1454 e78dc838 2020-12-04 stsp view->focussed = 1;
1455 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1456 1e37a5c2 2019-05-12 jcs } else {
1457 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1458 9b058f45 2022-06-30 mark if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1459 6131ff18 2022-06-20 mark err = view_resize(view->parent);
1460 3c1dfe12 2022-07-08 mark if (!err)
1461 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 0);
1462 669b5ffa 2018-10-07 stsp }
1463 1e37a5c2 2019-05-12 jcs if (err)
1464 1e37a5c2 2019-05-12 jcs break;
1465 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1466 1e37a5c2 2019-05-12 jcs }
1467 9b058f45 2022-06-30 mark if (err)
1468 9b058f45 2022-06-30 mark break;
1469 6fe51fee 2022-07-22 mark if (view->resize) {
1470 6fe51fee 2022-07-22 mark err = view->resize(view, 0);
1471 9b058f45 2022-06-30 mark if (err)
1472 9b058f45 2022-06-30 mark break;
1473 9b058f45 2022-06-30 mark }
1474 9b058f45 2022-06-30 mark if (view->parent)
1475 9b058f45 2022-06-30 mark err = offset_selection_down(view->parent);
1476 9b058f45 2022-06-30 mark if (!err)
1477 9b058f45 2022-06-30 mark err = offset_selection_down(view);
1478 1e37a5c2 2019-05-12 jcs break;
1479 d2366e29 2022-07-07 mark case 'S':
1480 3c1dfe12 2022-07-08 mark view->count = 0;
1481 d2366e29 2022-07-07 mark err = switch_split(view);
1482 3c1dfe12 2022-07-08 mark break;
1483 3c1dfe12 2022-07-08 mark case '-':
1484 3c1dfe12 2022-07-08 mark err = view_resize_split(view, -1);
1485 d2366e29 2022-07-07 mark break;
1486 3c1dfe12 2022-07-08 mark case '+':
1487 3c1dfe12 2022-07-08 mark err = view_resize_split(view, 1);
1488 3c1dfe12 2022-07-08 mark break;
1489 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1490 60493ae3 2019-06-20 stsp break;
1491 60493ae3 2019-06-20 stsp case '/':
1492 640cd7ff 2022-06-22 mark view->count = 0;
1493 60493ae3 2019-06-20 stsp if (view->search_start)
1494 2b49a8ae 2019-06-22 stsp view_search_start(view);
1495 60493ae3 2019-06-20 stsp else
1496 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1497 1e37a5c2 2019-05-12 jcs break;
1498 b1bf1435 2019-06-21 stsp case 'N':
1499 60493ae3 2019-06-20 stsp case 'n':
1500 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1501 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1502 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1503 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1504 60493ae3 2019-06-20 stsp view->search_next(view);
1505 60493ae3 2019-06-20 stsp } else
1506 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1507 917d79a7 2022-07-01 stsp break;
1508 917d79a7 2022-07-01 stsp case 'A':
1509 917d79a7 2022-07-01 stsp if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1510 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1511 917d79a7 2022-07-01 stsp else
1512 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1513 917d79a7 2022-07-01 stsp TAILQ_FOREACH(v, views, entry) {
1514 917d79a7 2022-07-01 stsp if (v->reset) {
1515 917d79a7 2022-07-01 stsp err = v->reset(v);
1516 917d79a7 2022-07-01 stsp if (err)
1517 917d79a7 2022-07-01 stsp return err;
1518 917d79a7 2022-07-01 stsp }
1519 917d79a7 2022-07-01 stsp if (v->child && v->child->reset) {
1520 917d79a7 2022-07-01 stsp err = v->child->reset(v->child);
1521 917d79a7 2022-07-01 stsp if (err)
1522 917d79a7 2022-07-01 stsp return err;
1523 917d79a7 2022-07-01 stsp }
1524 917d79a7 2022-07-01 stsp }
1525 60493ae3 2019-06-20 stsp break;
1526 1e37a5c2 2019-05-12 jcs default:
1527 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1528 1e37a5c2 2019-05-12 jcs break;
1529 e5a0f69f 2018-08-18 stsp }
1530 e5a0f69f 2018-08-18 stsp
1531 e5a0f69f 2018-08-18 stsp return err;
1532 e5a0f69f 2018-08-18 stsp }
1533 e5a0f69f 2018-08-18 stsp
1534 336075a4 2022-06-25 op static int
1535 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1536 a3404814 2018-09-02 stsp {
1537 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1538 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1539 669b5ffa 2018-10-07 stsp return 0;
1540 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1541 669b5ffa 2018-10-07 stsp return 0;
1542 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1543 a3404814 2018-09-02 stsp return 0;
1544 a3404814 2018-09-02 stsp
1545 669b5ffa 2018-10-07 stsp return view->focussed;
1546 a3404814 2018-09-02 stsp }
1547 a3404814 2018-09-02 stsp
1548 bcbd79e2 2018-08-19 stsp static const struct got_error *
1549 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1550 e5a0f69f 2018-08-18 stsp {
1551 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1552 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1553 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1554 d2366e29 2022-07-07 mark char *mode;
1555 fd823528 2018-10-22 stsp int fast_refresh = 10;
1556 1a76625f 2018-10-22 stsp int done = 0, errcode;
1557 e5a0f69f 2018-08-18 stsp
1558 d2366e29 2022-07-07 mark mode = getenv("TOG_VIEW_SPLIT_MODE");
1559 d2366e29 2022-07-07 mark if (!mode || !(*mode == 'h' || *mode == 'H'))
1560 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_VERT;
1561 d2366e29 2022-07-07 mark else
1562 d2366e29 2022-07-07 mark view->mode = TOG_VIEW_SPLIT_HRZN;
1563 d2366e29 2022-07-07 mark
1564 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1565 1a76625f 2018-10-22 stsp if (errcode)
1566 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1567 1a76625f 2018-10-22 stsp
1568 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1569 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1570 e5a0f69f 2018-08-18 stsp
1571 1004088d 2018-09-29 stsp view->focussed = 1;
1572 878940b7 2018-09-29 stsp err = view->show(view);
1573 0cf4efb1 2018-09-29 stsp if (err)
1574 0cf4efb1 2018-09-29 stsp return err;
1575 0cf4efb1 2018-09-29 stsp update_panels();
1576 0cf4efb1 2018-09-29 stsp doupdate();
1577 5629093a 2022-07-11 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1578 5629093a 2022-07-11 stsp !tog_fatal_signal_received()) {
1579 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1580 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1581 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1582 fd823528 2018-10-22 stsp
1583 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1584 e5a0f69f 2018-08-18 stsp if (err)
1585 e5a0f69f 2018-08-18 stsp break;
1586 9970f7fc 2020-12-03 stsp if (view->dying) {
1587 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1588 669b5ffa 2018-10-07 stsp
1589 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1590 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1591 9970f7fc 2020-12-03 stsp entry);
1592 e78dc838 2020-12-04 stsp else if (view->parent)
1593 669b5ffa 2018-10-07 stsp prev = view->parent;
1594 669b5ffa 2018-10-07 stsp
1595 e78dc838 2020-12-04 stsp if (view->parent) {
1596 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1597 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1598 9b058f45 2022-06-30 mark /* Restore fullscreen line height. */
1599 9b058f45 2022-06-30 mark view->parent->nlines = view->parent->lines;
1600 0dbbbe90 2022-06-17 op err = view_resize(view->parent);
1601 0dbbbe90 2022-06-17 op if (err)
1602 0dbbbe90 2022-06-17 op break;
1603 3c1dfe12 2022-07-08 mark /* Make resized splits persist. */
1604 3c1dfe12 2022-07-08 mark view_transfer_size(view->parent, view);
1605 e78dc838 2020-12-04 stsp } else
1606 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1607 669b5ffa 2018-10-07 stsp
1608 9970f7fc 2020-12-03 stsp err = view_close(view);
1609 fb59748f 2020-12-05 stsp if (err)
1610 e5a0f69f 2018-08-18 stsp goto done;
1611 669b5ffa 2018-10-07 stsp
1612 e78dc838 2020-12-04 stsp view = NULL;
1613 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1614 e78dc838 2020-12-04 stsp if (v->focussed)
1615 e78dc838 2020-12-04 stsp break;
1616 0cf4efb1 2018-09-29 stsp }
1617 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1618 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1619 e78dc838 2020-12-04 stsp if (prev)
1620 e78dc838 2020-12-04 stsp view = prev;
1621 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1622 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1623 e78dc838 2020-12-04 stsp tog_view_list_head);
1624 e78dc838 2020-12-04 stsp }
1625 e78dc838 2020-12-04 stsp if (view) {
1626 e78dc838 2020-12-04 stsp if (view->focus_child) {
1627 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1628 e78dc838 2020-12-04 stsp view = view->child;
1629 e78dc838 2020-12-04 stsp } else
1630 e78dc838 2020-12-04 stsp view->focussed = 1;
1631 e78dc838 2020-12-04 stsp }
1632 e78dc838 2020-12-04 stsp }
1633 e5a0f69f 2018-08-18 stsp }
1634 bcbd79e2 2018-08-19 stsp if (new_view) {
1635 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1636 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1637 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1638 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1639 86c66b02 2018-10-18 stsp continue;
1640 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1641 86c66b02 2018-10-18 stsp err = view_close(v);
1642 86c66b02 2018-10-18 stsp if (err)
1643 86c66b02 2018-10-18 stsp goto done;
1644 86c66b02 2018-10-18 stsp break;
1645 86c66b02 2018-10-18 stsp }
1646 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1647 fed7eaa8 2018-10-24 stsp view = new_view;
1648 0ae84acc 2022-06-15 tracey }
1649 669b5ffa 2018-10-07 stsp if (view) {
1650 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1651 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1652 e78dc838 2020-12-04 stsp view = view->child;
1653 e78dc838 2020-12-04 stsp } else {
1654 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1655 e78dc838 2020-12-04 stsp view = view->parent;
1656 1a76625f 2018-10-22 stsp }
1657 e78dc838 2020-12-04 stsp show_panel(view->panel);
1658 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1659 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1660 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1661 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1662 669b5ffa 2018-10-07 stsp if (err)
1663 1a76625f 2018-10-22 stsp goto done;
1664 669b5ffa 2018-10-07 stsp }
1665 669b5ffa 2018-10-07 stsp err = view->show(view);
1666 0cf4efb1 2018-09-29 stsp if (err)
1667 1a76625f 2018-10-22 stsp goto done;
1668 669b5ffa 2018-10-07 stsp if (view->child) {
1669 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1670 669b5ffa 2018-10-07 stsp if (err)
1671 1a76625f 2018-10-22 stsp goto done;
1672 669b5ffa 2018-10-07 stsp }
1673 1a76625f 2018-10-22 stsp update_panels();
1674 1a76625f 2018-10-22 stsp doupdate();
1675 0cf4efb1 2018-09-29 stsp }
1676 e5a0f69f 2018-08-18 stsp }
1677 e5a0f69f 2018-08-18 stsp done:
1678 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1679 5629093a 2022-07-11 stsp const struct got_error *close_err;
1680 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1681 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1682 5629093a 2022-07-11 stsp close_err = view_close(view);
1683 5629093a 2022-07-11 stsp if (close_err && err == NULL)
1684 5629093a 2022-07-11 stsp err = close_err;
1685 e5a0f69f 2018-08-18 stsp }
1686 1a76625f 2018-10-22 stsp
1687 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1688 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1689 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1690 1a76625f 2018-10-22 stsp
1691 e5a0f69f 2018-08-18 stsp return err;
1692 ea5e7bb5 2018-08-01 stsp }
1693 ea5e7bb5 2018-08-01 stsp
1694 4ed7e80c 2018-05-20 stsp __dead static void
1695 9f7d7167 2018-04-29 stsp usage_log(void)
1696 9f7d7167 2018-04-29 stsp {
1697 80ddbec8 2018-04-29 stsp endwin();
1698 c70c5802 2018-08-01 stsp fprintf(stderr,
1699 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1700 9f7d7167 2018-04-29 stsp getprogname());
1701 9f7d7167 2018-04-29 stsp exit(1);
1702 80ddbec8 2018-04-29 stsp }
1703 80ddbec8 2018-04-29 stsp
1704 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1705 80ddbec8 2018-04-29 stsp static const struct got_error *
1706 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1707 963b370f 2018-05-20 stsp {
1708 00dfcb92 2018-06-11 stsp char *vis = NULL;
1709 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1710 963b370f 2018-05-20 stsp
1711 963b370f 2018-05-20 stsp *ws = NULL;
1712 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1713 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1714 00dfcb92 2018-06-11 stsp int vislen;
1715 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1716 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1717 00dfcb92 2018-06-11 stsp
1718 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1719 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1720 00dfcb92 2018-06-11 stsp if (err)
1721 00dfcb92 2018-06-11 stsp return err;
1722 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1723 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1724 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1725 a7f50699 2018-06-11 stsp goto done;
1726 a7f50699 2018-06-11 stsp }
1727 00dfcb92 2018-06-11 stsp }
1728 963b370f 2018-05-20 stsp
1729 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1730 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1731 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1732 a7f50699 2018-06-11 stsp goto done;
1733 a7f50699 2018-06-11 stsp }
1734 963b370f 2018-05-20 stsp
1735 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1736 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1737 a7f50699 2018-06-11 stsp done:
1738 00dfcb92 2018-06-11 stsp free(vis);
1739 963b370f 2018-05-20 stsp if (err) {
1740 963b370f 2018-05-20 stsp free(*ws);
1741 963b370f 2018-05-20 stsp *ws = NULL;
1742 963b370f 2018-05-20 stsp *wlen = 0;
1743 963b370f 2018-05-20 stsp }
1744 963b370f 2018-05-20 stsp return err;
1745 145b6838 2022-06-16 stsp }
1746 145b6838 2022-06-16 stsp
1747 145b6838 2022-06-16 stsp static const struct got_error *
1748 145b6838 2022-06-16 stsp expand_tab(char **ptr, const char *src)
1749 145b6838 2022-06-16 stsp {
1750 145b6838 2022-06-16 stsp char *dst;
1751 145b6838 2022-06-16 stsp size_t len, n, idx = 0, sz = 0;
1752 145b6838 2022-06-16 stsp
1753 145b6838 2022-06-16 stsp *ptr = NULL;
1754 145b6838 2022-06-16 stsp n = len = strlen(src);
1755 6e1c41ad 2022-06-16 mark dst = malloc(n + 1);
1756 145b6838 2022-06-16 stsp if (dst == NULL)
1757 145b6838 2022-06-16 stsp return got_error_from_errno("malloc");
1758 145b6838 2022-06-16 stsp
1759 145b6838 2022-06-16 stsp while (idx < len && src[idx]) {
1760 145b6838 2022-06-16 stsp const char c = src[idx];
1761 145b6838 2022-06-16 stsp
1762 145b6838 2022-06-16 stsp if (c == '\t') {
1763 145b6838 2022-06-16 stsp size_t nb = TABSIZE - sz % TABSIZE;
1764 367ddf28 2022-06-16 mark char *p;
1765 367ddf28 2022-06-16 mark
1766 367ddf28 2022-06-16 mark p = realloc(dst, n + nb);
1767 6e1c41ad 2022-06-16 mark if (p == NULL) {
1768 6e1c41ad 2022-06-16 mark free(dst);
1769 6e1c41ad 2022-06-16 mark return got_error_from_errno("realloc");
1770 6e1c41ad 2022-06-16 mark
1771 6e1c41ad 2022-06-16 mark }
1772 6e1c41ad 2022-06-16 mark dst = p;
1773 145b6838 2022-06-16 stsp n += nb;
1774 6e1c41ad 2022-06-16 mark memset(dst + sz, ' ', nb);
1775 145b6838 2022-06-16 stsp sz += nb;
1776 145b6838 2022-06-16 stsp } else
1777 145b6838 2022-06-16 stsp dst[sz++] = src[idx];
1778 145b6838 2022-06-16 stsp ++idx;
1779 145b6838 2022-06-16 stsp }
1780 145b6838 2022-06-16 stsp
1781 145b6838 2022-06-16 stsp dst[sz] = '\0';
1782 145b6838 2022-06-16 stsp *ptr = dst;
1783 145b6838 2022-06-16 stsp return NULL;
1784 963b370f 2018-05-20 stsp }
1785 963b370f 2018-05-20 stsp
1786 4e4a9ac8 2022-06-17 op /*
1787 4e4a9ac8 2022-06-17 op * Advance at most n columns from wline starting at offset off.
1788 4e4a9ac8 2022-06-17 op * Return the index to the first character after the span operation.
1789 4e4a9ac8 2022-06-17 op * Return the combined column width of all spanned wide character in
1790 4e4a9ac8 2022-06-17 op * *rcol.
1791 ccda2f4d 2022-06-16 stsp */
1792 4e4a9ac8 2022-06-17 op static int
1793 4e4a9ac8 2022-06-17 op span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1794 4e4a9ac8 2022-06-17 op {
1795 4e4a9ac8 2022-06-17 op int width, i, cols = 0;
1796 ccda2f4d 2022-06-16 stsp
1797 4e4a9ac8 2022-06-17 op if (n == 0) {
1798 4e4a9ac8 2022-06-17 op *rcol = cols;
1799 4e4a9ac8 2022-06-17 op return off;
1800 4e4a9ac8 2022-06-17 op }
1801 ccda2f4d 2022-06-16 stsp
1802 4e4a9ac8 2022-06-17 op for (i = off; wline[i] != L'\0'; ++i) {
1803 4e4a9ac8 2022-06-17 op if (wline[i] == L'\t')
1804 4e4a9ac8 2022-06-17 op width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1805 4e4a9ac8 2022-06-17 op else
1806 4e4a9ac8 2022-06-17 op width = wcwidth(wline[i]);
1807 ccda2f4d 2022-06-16 stsp
1808 4e4a9ac8 2022-06-17 op if (width == -1) {
1809 4e4a9ac8 2022-06-17 op width = 1;
1810 4e4a9ac8 2022-06-17 op wline[i] = L'.';
1811 ccda2f4d 2022-06-16 stsp }
1812 ccda2f4d 2022-06-16 stsp
1813 4e4a9ac8 2022-06-17 op if (cols + width > n)
1814 4e4a9ac8 2022-06-17 op break;
1815 4e4a9ac8 2022-06-17 op cols += width;
1816 ccda2f4d 2022-06-16 stsp }
1817 ccda2f4d 2022-06-16 stsp
1818 4e4a9ac8 2022-06-17 op *rcol = cols;
1819 4e4a9ac8 2022-06-17 op return i;
1820 ccda2f4d 2022-06-16 stsp }
1821 ccda2f4d 2022-06-16 stsp
1822 ccda2f4d 2022-06-16 stsp /*
1823 ccda2f4d 2022-06-16 stsp * Format a line for display, ensuring that it won't overflow a width limit.
1824 ccda2f4d 2022-06-16 stsp * With scrolling, the width returned refers to the scrolled version of the
1825 ccda2f4d 2022-06-16 stsp * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1826 ccda2f4d 2022-06-16 stsp */
1827 ccda2f4d 2022-06-16 stsp static const struct got_error *
1828 ccda2f4d 2022-06-16 stsp format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1829 ccda2f4d 2022-06-16 stsp const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1830 ccda2f4d 2022-06-16 stsp {
1831 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1832 4e4a9ac8 2022-06-17 op int cols;
1833 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1834 145b6838 2022-06-16 stsp char *exstr = NULL;
1835 963b370f 2018-05-20 stsp size_t wlen;
1836 4e4a9ac8 2022-06-17 op int i, scrollx;
1837 963b370f 2018-05-20 stsp
1838 963b370f 2018-05-20 stsp *wlinep = NULL;
1839 b700b5d6 2018-07-10 stsp *widthp = 0;
1840 963b370f 2018-05-20 stsp
1841 145b6838 2022-06-16 stsp if (expand) {
1842 145b6838 2022-06-16 stsp err = expand_tab(&exstr, line);
1843 145b6838 2022-06-16 stsp if (err)
1844 145b6838 2022-06-16 stsp return err;
1845 145b6838 2022-06-16 stsp }
1846 145b6838 2022-06-16 stsp
1847 145b6838 2022-06-16 stsp err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1848 145b6838 2022-06-16 stsp free(exstr);
1849 963b370f 2018-05-20 stsp if (err)
1850 963b370f 2018-05-20 stsp return err;
1851 963b370f 2018-05-20 stsp
1852 4e4a9ac8 2022-06-17 op scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1853 ccda2f4d 2022-06-16 stsp
1854 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1855 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1856 3f670bfb 2020-12-10 stsp wlen--;
1857 3f670bfb 2020-12-10 stsp }
1858 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
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
1863 4e4a9ac8 2022-06-17 op i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1864 4e4a9ac8 2022-06-17 op wline[i] = L'\0';
1865 27a741e5 2019-09-11 stsp
1866 b700b5d6 2018-07-10 stsp if (widthp)
1867 b700b5d6 2018-07-10 stsp *widthp = cols;
1868 ccda2f4d 2022-06-16 stsp if (scrollxp)
1869 ccda2f4d 2022-06-16 stsp *scrollxp = scrollx;
1870 963b370f 2018-05-20 stsp if (err)
1871 963b370f 2018-05-20 stsp free(wline);
1872 963b370f 2018-05-20 stsp else
1873 963b370f 2018-05-20 stsp *wlinep = wline;
1874 963b370f 2018-05-20 stsp return err;
1875 963b370f 2018-05-20 stsp }
1876 963b370f 2018-05-20 stsp
1877 8b473291 2019-02-21 stsp static const struct got_error*
1878 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1879 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1880 8b473291 2019-02-21 stsp {
1881 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1882 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1883 8b473291 2019-02-21 stsp char *s;
1884 8b473291 2019-02-21 stsp const char *name;
1885 8b473291 2019-02-21 stsp
1886 8b473291 2019-02-21 stsp *refs_str = NULL;
1887 8b473291 2019-02-21 stsp
1888 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1889 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1890 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1891 52b5abe1 2019-08-13 stsp int cmp;
1892 52b5abe1 2019-08-13 stsp
1893 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1894 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1895 8b473291 2019-02-21 stsp continue;
1896 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1897 8b473291 2019-02-21 stsp name += 5;
1898 cc488aa7 2022-01-23 stsp if (strncmp(name, "got/", 4) == 0 &&
1899 cc488aa7 2022-01-23 stsp strncmp(name, "got/backup/", 11) != 0)
1900 7143d404 2019-03-12 stsp continue;
1901 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1902 8b473291 2019-02-21 stsp name += 6;
1903 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1904 8b473291 2019-02-21 stsp name += 8;
1905 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1906 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1907 79cc719f 2020-04-24 stsp continue;
1908 79cc719f 2020-04-24 stsp }
1909 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1910 48cae60d 2020-09-22 stsp if (err)
1911 48cae60d 2020-09-22 stsp break;
1912 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1913 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1914 5d844a1e 2019-08-13 stsp if (err) {
1915 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1916 48cae60d 2020-09-22 stsp free(ref_id);
1917 5d844a1e 2019-08-13 stsp break;
1918 48cae60d 2020-09-22 stsp }
1919 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1920 5d844a1e 2019-08-13 stsp err = NULL;
1921 5d844a1e 2019-08-13 stsp tag = NULL;
1922 5d844a1e 2019-08-13 stsp }
1923 52b5abe1 2019-08-13 stsp }
1924 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1925 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1926 48cae60d 2020-09-22 stsp free(ref_id);
1927 52b5abe1 2019-08-13 stsp if (tag)
1928 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1929 52b5abe1 2019-08-13 stsp if (cmp != 0)
1930 52b5abe1 2019-08-13 stsp continue;
1931 8b473291 2019-02-21 stsp s = *refs_str;
1932 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1933 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1934 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1935 8b473291 2019-02-21 stsp free(s);
1936 8b473291 2019-02-21 stsp *refs_str = NULL;
1937 8b473291 2019-02-21 stsp break;
1938 8b473291 2019-02-21 stsp }
1939 8b473291 2019-02-21 stsp free(s);
1940 8b473291 2019-02-21 stsp }
1941 8b473291 2019-02-21 stsp
1942 8b473291 2019-02-21 stsp return err;
1943 8b473291 2019-02-21 stsp }
1944 8b473291 2019-02-21 stsp
1945 963b370f 2018-05-20 stsp static const struct got_error *
1946 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1947 27a741e5 2019-09-11 stsp int col_tab_align)
1948 5813d178 2019-03-09 stsp {
1949 e6b8b890 2020-12-29 naddy char *smallerthan;
1950 5813d178 2019-03-09 stsp
1951 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1952 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1953 5813d178 2019-03-09 stsp author = smallerthan + 1;
1954 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1955 ccda2f4d 2022-06-16 stsp return format_line(wauthor, author_width, NULL, author, 0, limit,
1956 ccda2f4d 2022-06-16 stsp col_tab_align, 0);
1957 5813d178 2019-03-09 stsp }
1958 5813d178 2019-03-09 stsp
1959 5813d178 2019-03-09 stsp static const struct got_error *
1960 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1961 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1962 8fdc79fe 2020-12-01 naddy int author_display_cols)
1963 80ddbec8 2018-04-29 stsp {
1964 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1965 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1966 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1967 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1968 5813d178 2019-03-09 stsp char *author = NULL;
1969 ccda2f4d 2022-06-16 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1970 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1971 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1972 ccda2f4d 2022-06-16 stsp int col, limit, scrollx;
1973 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1974 ccb26ccd 2018-11-05 stsp struct tm tm;
1975 45d799e2 2018-12-23 stsp time_t committer_time;
1976 11b20872 2019-11-08 stsp struct tog_color *tc;
1977 80ddbec8 2018-04-29 stsp
1978 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1979 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1980 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1981 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1982 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1983 b39d25c7 2018-07-10 stsp
1984 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1985 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1986 b39d25c7 2018-07-10 stsp else
1987 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1988 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1989 11b20872 2019-11-08 stsp if (tc)
1990 11b20872 2019-11-08 stsp wattr_on(view->window,
1991 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1992 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1993 11b20872 2019-11-08 stsp if (tc)
1994 11b20872 2019-11-08 stsp wattr_off(view->window,
1995 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1996 27a741e5 2019-09-11 stsp col = limit;
1997 b39d25c7 2018-07-10 stsp if (col > avail)
1998 b39d25c7 2018-07-10 stsp goto done;
1999 6570a66d 2019-11-08 stsp
2000 6570a66d 2019-11-08 stsp if (avail >= 120) {
2001 6570a66d 2019-11-08 stsp char *id_str;
2002 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2003 6570a66d 2019-11-08 stsp if (err)
2004 6570a66d 2019-11-08 stsp goto done;
2005 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2006 11b20872 2019-11-08 stsp if (tc)
2007 11b20872 2019-11-08 stsp wattr_on(view->window,
2008 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2009 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2010 11b20872 2019-11-08 stsp if (tc)
2011 11b20872 2019-11-08 stsp wattr_off(view->window,
2012 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2013 6570a66d 2019-11-08 stsp free(id_str);
2014 6570a66d 2019-11-08 stsp col += 9;
2015 6570a66d 2019-11-08 stsp if (col > avail)
2016 6570a66d 2019-11-08 stsp goto done;
2017 6570a66d 2019-11-08 stsp }
2018 b39d25c7 2018-07-10 stsp
2019 10aab77f 2022-07-19 op if (s->use_committer)
2020 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(commit));
2021 10aab77f 2022-07-19 op else
2022 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(commit));
2023 5813d178 2019-03-09 stsp if (author == NULL) {
2024 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2025 80ddbec8 2018-04-29 stsp goto done;
2026 80ddbec8 2018-04-29 stsp }
2027 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2028 bb737323 2018-05-20 stsp if (err)
2029 bb737323 2018-05-20 stsp goto done;
2030 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2031 11b20872 2019-11-08 stsp if (tc)
2032 11b20872 2019-11-08 stsp wattr_on(view->window,
2033 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2034 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2035 11b20872 2019-11-08 stsp if (tc)
2036 11b20872 2019-11-08 stsp wattr_off(view->window,
2037 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2038 bb737323 2018-05-20 stsp col += author_width;
2039 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2040 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2041 bb737323 2018-05-20 stsp col++;
2042 bb737323 2018-05-20 stsp author_width++;
2043 bb737323 2018-05-20 stsp }
2044 9c2eaf34 2018-05-20 stsp if (col > avail)
2045 9c2eaf34 2018-05-20 stsp goto done;
2046 80ddbec8 2018-04-29 stsp
2047 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2048 5943eee2 2019-08-13 stsp if (err)
2049 6d9fbc00 2018-04-29 stsp goto done;
2050 bb737323 2018-05-20 stsp logmsg = logmsg0;
2051 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2052 bb737323 2018-05-20 stsp logmsg++;
2053 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2054 bb737323 2018-05-20 stsp if (newline)
2055 bb737323 2018-05-20 stsp *newline = '\0';
2056 ccda2f4d 2022-06-16 stsp limit = avail - col;
2057 49b24ee5 2022-07-03 mark if (view->child && !view_is_hsplit_top(view) && limit > 0)
2058 4d1f6af3 2022-06-17 op limit--; /* for the border */
2059 ccda2f4d 2022-06-16 stsp err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2060 ccda2f4d 2022-06-16 stsp limit, col, 1);
2061 29688b02 2022-06-16 stsp if (err)
2062 29688b02 2022-06-16 stsp goto done;
2063 ccda2f4d 2022-06-16 stsp waddwstr(view->window, &wlogmsg[scrollx]);
2064 29688b02 2022-06-16 stsp col += MAX(logmsg_width, 0);
2065 27a741e5 2019-09-11 stsp while (col < avail) {
2066 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2067 bb737323 2018-05-20 stsp col++;
2068 881b2d3e 2018-04-30 stsp }
2069 80ddbec8 2018-04-29 stsp done:
2070 80ddbec8 2018-04-29 stsp free(logmsg0);
2071 bb737323 2018-05-20 stsp free(wlogmsg);
2072 5813d178 2019-03-09 stsp free(author);
2073 bb737323 2018-05-20 stsp free(wauthor);
2074 80ddbec8 2018-04-29 stsp free(line);
2075 80ddbec8 2018-04-29 stsp return err;
2076 80ddbec8 2018-04-29 stsp }
2077 26ed57b2 2018-05-19 stsp
2078 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2079 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2080 899d86c2 2018-05-10 stsp struct got_object_id *id)
2081 80ddbec8 2018-04-29 stsp {
2082 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2083 80ddbec8 2018-04-29 stsp
2084 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2085 80ddbec8 2018-04-29 stsp if (entry == NULL)
2086 899d86c2 2018-05-10 stsp return NULL;
2087 99db9666 2018-05-07 stsp
2088 899d86c2 2018-05-10 stsp entry->id = id;
2089 99db9666 2018-05-07 stsp entry->commit = commit;
2090 899d86c2 2018-05-10 stsp return entry;
2091 99db9666 2018-05-07 stsp }
2092 80ddbec8 2018-04-29 stsp
2093 99db9666 2018-05-07 stsp static void
2094 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2095 99db9666 2018-05-07 stsp {
2096 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2097 99db9666 2018-05-07 stsp
2098 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2099 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2100 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2101 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2102 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2103 99db9666 2018-05-07 stsp free(entry);
2104 99db9666 2018-05-07 stsp }
2105 99db9666 2018-05-07 stsp
2106 99db9666 2018-05-07 stsp static void
2107 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2108 99db9666 2018-05-07 stsp {
2109 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2110 99db9666 2018-05-07 stsp pop_commit(commits);
2111 c4972b91 2018-05-07 stsp }
2112 c4972b91 2018-05-07 stsp
2113 c4972b91 2018-05-07 stsp static const struct got_error *
2114 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2115 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2116 13add988 2019-10-15 stsp {
2117 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2118 13add988 2019-10-15 stsp regmatch_t regmatch;
2119 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2120 13add988 2019-10-15 stsp
2121 13add988 2019-10-15 stsp *have_match = 0;
2122 13add988 2019-10-15 stsp
2123 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2124 13add988 2019-10-15 stsp if (err)
2125 13add988 2019-10-15 stsp return err;
2126 13add988 2019-10-15 stsp
2127 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2128 13add988 2019-10-15 stsp if (err)
2129 13add988 2019-10-15 stsp goto done;
2130 13add988 2019-10-15 stsp
2131 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2132 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2133 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2134 13add988 2019-10-15 stsp &regmatch, 0) == 0 ||
2135 13add988 2019-10-15 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2136 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2137 13add988 2019-10-15 stsp *have_match = 1;
2138 13add988 2019-10-15 stsp done:
2139 13add988 2019-10-15 stsp free(id_str);
2140 13add988 2019-10-15 stsp free(logmsg);
2141 13add988 2019-10-15 stsp return err;
2142 13add988 2019-10-15 stsp }
2143 13add988 2019-10-15 stsp
2144 13add988 2019-10-15 stsp static const struct got_error *
2145 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2146 c4972b91 2018-05-07 stsp {
2147 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2148 9ba79e04 2018-06-11 stsp
2149 1a76625f 2018-10-22 stsp /*
2150 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2151 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2152 1a76625f 2018-10-22 stsp * while updating the display.
2153 1a76625f 2018-10-22 stsp */
2154 4e0d2870 2020-12-07 naddy do {
2155 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2156 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2157 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2158 1a76625f 2018-10-22 stsp int errcode;
2159 899d86c2 2018-05-10 stsp
2160 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2161 4e0d2870 2020-12-07 naddy NULL, NULL);
2162 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2163 ecb28ae0 2018-07-16 stsp break;
2164 899d86c2 2018-05-10 stsp
2165 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2166 9ba79e04 2018-06-11 stsp if (err)
2167 9ba79e04 2018-06-11 stsp break;
2168 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2169 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2170 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2171 9ba79e04 2018-06-11 stsp break;
2172 9ba79e04 2018-06-11 stsp }
2173 93e45b7c 2018-09-24 stsp
2174 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2175 1a76625f 2018-10-22 stsp if (errcode) {
2176 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2177 13add988 2019-10-15 stsp "pthread_mutex_lock");
2178 1a76625f 2018-10-22 stsp break;
2179 1a76625f 2018-10-22 stsp }
2180 1a76625f 2018-10-22 stsp
2181 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2182 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2183 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2184 1a76625f 2018-10-22 stsp
2185 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2186 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2187 7c1452c1 2020-03-26 stsp int have_match;
2188 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2189 7c1452c1 2020-03-26 stsp if (err)
2190 7c1452c1 2020-03-26 stsp break;
2191 7c1452c1 2020-03-26 stsp if (have_match)
2192 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2193 13add988 2019-10-15 stsp }
2194 13add988 2019-10-15 stsp
2195 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2196 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2197 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2198 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2199 7c1452c1 2020-03-26 stsp if (err)
2200 13add988 2019-10-15 stsp break;
2201 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2202 899d86c2 2018-05-10 stsp
2203 9ba79e04 2018-06-11 stsp return err;
2204 0553a4e3 2018-04-30 stsp }
2205 0553a4e3 2018-04-30 stsp
2206 2b779855 2020-12-05 naddy static void
2207 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2208 2b779855 2020-12-05 naddy {
2209 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2210 2b779855 2020-12-05 naddy int ncommits = 0;
2211 2b779855 2020-12-05 naddy
2212 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2213 2b779855 2020-12-05 naddy while (entry) {
2214 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2215 2b779855 2020-12-05 naddy s->selected_entry = entry;
2216 2b779855 2020-12-05 naddy break;
2217 2b779855 2020-12-05 naddy }
2218 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2219 2b779855 2020-12-05 naddy ncommits++;
2220 2b779855 2020-12-05 naddy }
2221 2b779855 2020-12-05 naddy }
2222 2b779855 2020-12-05 naddy
2223 0553a4e3 2018-04-30 stsp static const struct got_error *
2224 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2225 0553a4e3 2018-04-30 stsp {
2226 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2227 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2228 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2229 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2230 60493ae3 2019-06-20 stsp int width;
2231 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2232 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2233 8b473291 2019-02-21 stsp char *refs_str = NULL;
2234 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2235 11b20872 2019-11-08 stsp struct tog_color *tc;
2236 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2237 0553a4e3 2018-04-30 stsp
2238 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2239 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2240 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2241 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2242 1a76625f 2018-10-22 stsp if (err)
2243 ecb28ae0 2018-07-16 stsp return err;
2244 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2245 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2246 d2075bf3 2020-12-25 stsp if (refs) {
2247 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2248 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2249 d2075bf3 2020-12-25 stsp if (err)
2250 d2075bf3 2020-12-25 stsp goto done;
2251 d2075bf3 2020-12-25 stsp }
2252 867c6645 2018-07-10 stsp }
2253 359bfafd 2019-02-22 stsp
2254 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2255 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2256 1a76625f 2018-10-22 stsp
2257 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2258 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2259 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2260 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2261 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2262 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2263 8f4ed634 2020-03-26 stsp goto done;
2264 8f4ed634 2020-03-26 stsp }
2265 8f4ed634 2020-03-26 stsp } else {
2266 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2267 f9686aa5 2020-03-27 stsp
2268 f9686aa5 2020-03-27 stsp if (view->searching) {
2269 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2270 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2271 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2272 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2273 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2274 f9686aa5 2020-03-27 stsp search_str = "searching...";
2275 f9686aa5 2020-03-27 stsp }
2276 f9686aa5 2020-03-27 stsp
2277 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2278 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2279 f9686aa5 2020-03-27 stsp search_str ? search_str :
2280 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2281 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2282 8f4ed634 2020-03-26 stsp goto done;
2283 8f4ed634 2020-03-26 stsp }
2284 8b473291 2019-02-21 stsp }
2285 1a76625f 2018-10-22 stsp
2286 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2287 87411fa9 2022-06-16 stsp if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2288 87411fa9 2022-06-16 stsp "........................................",
2289 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2290 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2291 1a76625f 2018-10-22 stsp header = NULL;
2292 1a76625f 2018-10-22 stsp goto done;
2293 1a76625f 2018-10-22 stsp }
2294 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2295 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2296 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2297 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2298 1a76625f 2018-10-22 stsp header = NULL;
2299 1a76625f 2018-10-22 stsp goto done;
2300 ecb28ae0 2018-07-16 stsp }
2301 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2302 1a76625f 2018-10-22 stsp if (err)
2303 1a76625f 2018-10-22 stsp goto done;
2304 867c6645 2018-07-10 stsp
2305 2814baeb 2018-08-01 stsp werase(view->window);
2306 867c6645 2018-07-10 stsp
2307 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2308 a3404814 2018-09-02 stsp wstandout(view->window);
2309 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2310 11b20872 2019-11-08 stsp if (tc)
2311 11b20872 2019-11-08 stsp wattr_on(view->window,
2312 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2313 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2314 11b20872 2019-11-08 stsp if (tc)
2315 11b20872 2019-11-08 stsp wattr_off(view->window,
2316 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2317 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2318 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2319 1a76625f 2018-10-22 stsp width++;
2320 1a76625f 2018-10-22 stsp }
2321 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2322 a3404814 2018-09-02 stsp wstandend(view->window);
2323 ecb28ae0 2018-07-16 stsp free(wline);
2324 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2325 1a76625f 2018-10-22 stsp goto done;
2326 0553a4e3 2018-04-30 stsp
2327 29688b02 2022-06-16 stsp /* Grow author column size if necessary, and set view->maxx. */
2328 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2329 5813d178 2019-03-09 stsp ncommits = 0;
2330 145b6838 2022-06-16 stsp view->maxx = 0;
2331 5813d178 2019-03-09 stsp while (entry) {
2332 10aab77f 2022-07-19 op struct got_commit_object *c = entry->commit;
2333 145b6838 2022-06-16 stsp char *author, *eol, *msg, *msg0;
2334 29688b02 2022-06-16 stsp wchar_t *wauthor, *wmsg;
2335 5813d178 2019-03-09 stsp int width;
2336 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2337 5813d178 2019-03-09 stsp break;
2338 10aab77f 2022-07-19 op if (s->use_committer)
2339 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_committer(c));
2340 10aab77f 2022-07-19 op else
2341 10aab77f 2022-07-19 op author = strdup(got_object_commit_get_author(c));
2342 5813d178 2019-03-09 stsp if (author == NULL) {
2343 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2344 5813d178 2019-03-09 stsp goto done;
2345 5813d178 2019-03-09 stsp }
2346 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2347 27a741e5 2019-09-11 stsp date_display_cols);
2348 5813d178 2019-03-09 stsp if (author_cols < width)
2349 5813d178 2019-03-09 stsp author_cols = width;
2350 5813d178 2019-03-09 stsp free(wauthor);
2351 5813d178 2019-03-09 stsp free(author);
2352 a310d9c3 2022-07-21 florian if (err)
2353 a310d9c3 2022-07-21 florian goto done;
2354 10aab77f 2022-07-19 op err = got_object_commit_get_logmsg(&msg0, c);
2355 145b6838 2022-06-16 stsp if (err)
2356 145b6838 2022-06-16 stsp goto done;
2357 145b6838 2022-06-16 stsp msg = msg0;
2358 145b6838 2022-06-16 stsp while (*msg == '\n')
2359 145b6838 2022-06-16 stsp ++msg;
2360 145b6838 2022-06-16 stsp if ((eol = strchr(msg, '\n')))
2361 29688b02 2022-06-16 stsp *eol = '\0';
2362 ccda2f4d 2022-06-16 stsp err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2363 29688b02 2022-06-16 stsp date_display_cols + author_cols, 0);
2364 29688b02 2022-06-16 stsp if (err)
2365 29688b02 2022-06-16 stsp goto done;
2366 29688b02 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
2367 145b6838 2022-06-16 stsp free(msg0);
2368 29688b02 2022-06-16 stsp free(wmsg);
2369 7ca04879 2019-10-19 stsp ncommits++;
2370 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2371 5813d178 2019-03-09 stsp }
2372 5813d178 2019-03-09 stsp
2373 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2374 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2375 867c6645 2018-07-10 stsp ncommits = 0;
2376 899d86c2 2018-05-10 stsp while (entry) {
2377 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2378 899d86c2 2018-05-10 stsp break;
2379 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2380 2814baeb 2018-08-01 stsp wstandout(view->window);
2381 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2382 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2383 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2384 2814baeb 2018-08-01 stsp wstandend(view->window);
2385 0553a4e3 2018-04-30 stsp if (err)
2386 60493ae3 2019-06-20 stsp goto done;
2387 0553a4e3 2018-04-30 stsp ncommits++;
2388 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2389 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2390 80ddbec8 2018-04-29 stsp }
2391 80ddbec8 2018-04-29 stsp
2392 9b058f45 2022-06-30 mark view_border(view);
2393 1a76625f 2018-10-22 stsp done:
2394 1a76625f 2018-10-22 stsp free(id_str);
2395 8b473291 2019-02-21 stsp free(refs_str);
2396 1a76625f 2018-10-22 stsp free(ncommits_str);
2397 1a76625f 2018-10-22 stsp free(header);
2398 80ddbec8 2018-04-29 stsp return err;
2399 9f7d7167 2018-04-29 stsp }
2400 07b55e75 2018-05-10 stsp
2401 07b55e75 2018-05-10 stsp static void
2402 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2403 07b55e75 2018-05-10 stsp {
2404 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2405 07b55e75 2018-05-10 stsp int nscrolled = 0;
2406 07b55e75 2018-05-10 stsp
2407 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2408 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2409 07b55e75 2018-05-10 stsp return;
2410 9f7d7167 2018-04-29 stsp
2411 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2412 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2413 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2414 07b55e75 2018-05-10 stsp if (entry) {
2415 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2416 07b55e75 2018-05-10 stsp nscrolled++;
2417 07b55e75 2018-05-10 stsp }
2418 07b55e75 2018-05-10 stsp }
2419 aa075928 2018-05-10 stsp }
2420 aa075928 2018-05-10 stsp
2421 aa075928 2018-05-10 stsp static const struct got_error *
2422 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2423 aa075928 2018-05-10 stsp {
2424 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2425 5e224a3e 2019-02-22 stsp int errcode;
2426 8a42fca8 2019-02-22 stsp
2427 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2428 aa075928 2018-05-10 stsp
2429 5629093a 2022-07-11 stsp while (!ta->log_complete && !tog_thread_error &&
2430 5629093a 2022-07-11 stsp (ta->commits_needed > 0 || ta->load_all)) {
2431 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2432 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2433 7aafa0d1 2019-02-22 stsp if (errcode)
2434 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2435 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2436 7c1452c1 2020-03-26 stsp
2437 7c1452c1 2020-03-26 stsp /*
2438 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2439 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2440 7c1452c1 2020-03-26 stsp */
2441 7c1452c1 2020-03-26 stsp if (!wait)
2442 7c1452c1 2020-03-26 stsp break;
2443 7c1452c1 2020-03-26 stsp
2444 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2445 ffe38506 2020-12-01 naddy show_log_view(view);
2446 7c1452c1 2020-03-26 stsp update_panels();
2447 7c1452c1 2020-03-26 stsp doupdate();
2448 7c1452c1 2020-03-26 stsp
2449 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2450 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2451 82954512 2020-02-03 stsp if (errcode)
2452 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2453 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2454 82954512 2020-02-03 stsp
2455 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2456 ffe38506 2020-12-01 naddy show_log_view(view);
2457 7c1452c1 2020-03-26 stsp update_panels();
2458 7c1452c1 2020-03-26 stsp doupdate();
2459 5e224a3e 2019-02-22 stsp }
2460 5e224a3e 2019-02-22 stsp
2461 5e224a3e 2019-02-22 stsp return NULL;
2462 5e224a3e 2019-02-22 stsp }
2463 5e224a3e 2019-02-22 stsp
2464 5e224a3e 2019-02-22 stsp static const struct got_error *
2465 9b058f45 2022-06-30 mark request_log_commits(struct tog_view *view)
2466 9b058f45 2022-06-30 mark {
2467 9b058f45 2022-06-30 mark struct tog_log_view_state *state = &view->state.log;
2468 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
2469 2525dccb 2022-07-13 mark
2470 2525dccb 2022-07-13 mark if (state->thread_args.log_complete)
2471 2525dccb 2022-07-13 mark return NULL;
2472 9b058f45 2022-06-30 mark
2473 27187d45 2022-07-11 mark state->thread_args.commits_needed += view->nscrolled;
2474 9b058f45 2022-06-30 mark err = trigger_log_thread(view, 1);
2475 9b058f45 2022-06-30 mark view->nscrolled = 0;
2476 9b058f45 2022-06-30 mark
2477 9b058f45 2022-06-30 mark return err;
2478 9b058f45 2022-06-30 mark }
2479 9b058f45 2022-06-30 mark
2480 9b058f45 2022-06-30 mark static const struct got_error *
2481 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2482 5e224a3e 2019-02-22 stsp {
2483 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2484 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2485 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2486 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2487 5e224a3e 2019-02-22 stsp
2488 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2489 5e224a3e 2019-02-22 stsp return NULL;
2490 5e224a3e 2019-02-22 stsp
2491 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2492 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2493 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2494 08ebd0a9 2019-02-22 stsp /*
2495 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2496 08ebd0a9 2019-02-22 stsp */
2497 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2498 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2499 5e224a3e 2019-02-22 stsp if (err)
2500 5e224a3e 2019-02-22 stsp return err;
2501 7aafa0d1 2019-02-22 stsp }
2502 b295e71b 2019-02-22 stsp
2503 7aafa0d1 2019-02-22 stsp do {
2504 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2505 9b058f45 2022-06-30 mark if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2506 88048b54 2019-02-21 stsp break;
2507 88048b54 2019-02-21 stsp
2508 9b058f45 2022-06-30 mark s->last_displayed_entry = pentry ?
2509 9b058f45 2022-06-30 mark pentry : s->last_displayed_entry;;
2510 aa075928 2018-05-10 stsp
2511 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2512 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2513 dd0a52c1 2018-05-20 stsp break;
2514 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2515 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2516 aa075928 2018-05-10 stsp
2517 2525dccb 2022-07-13 mark if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2518 9b058f45 2022-06-30 mark view->nscrolled += nscrolled;
2519 9b058f45 2022-06-30 mark else
2520 9b058f45 2022-06-30 mark view->nscrolled = 0;
2521 9b058f45 2022-06-30 mark
2522 dd0a52c1 2018-05-20 stsp return err;
2523 07b55e75 2018-05-10 stsp }
2524 4a7f7875 2018-05-10 stsp
2525 cd0acaa7 2018-05-20 stsp static const struct got_error *
2526 9b058f45 2022-06-30 mark open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2527 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2528 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2529 cd0acaa7 2018-05-20 stsp {
2530 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2531 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2532 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2533 cd0acaa7 2018-05-20 stsp
2534 9b058f45 2022-06-30 mark diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2535 15a94983 2018-12-23 stsp if (diff_view == NULL)
2536 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2537 ea5e7bb5 2018-08-01 stsp
2538 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2539 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2540 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2541 e5a0f69f 2018-08-18 stsp if (err == NULL)
2542 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2543 cd0acaa7 2018-05-20 stsp return err;
2544 4a7f7875 2018-05-10 stsp }
2545 4a7f7875 2018-05-10 stsp
2546 80ddbec8 2018-04-29 stsp static const struct got_error *
2547 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2548 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2549 9343a5fb 2018-06-23 stsp {
2550 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2551 941e9f74 2019-05-21 stsp
2552 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2553 941e9f74 2019-05-21 stsp if (parent == NULL)
2554 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2555 941e9f74 2019-05-21 stsp
2556 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2557 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2558 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2559 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2560 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2561 941e9f74 2019-05-21 stsp s->tree = subtree;
2562 941e9f74 2019-05-21 stsp s->selected = 0;
2563 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2564 941e9f74 2019-05-21 stsp return NULL;
2565 941e9f74 2019-05-21 stsp }
2566 941e9f74 2019-05-21 stsp
2567 941e9f74 2019-05-21 stsp static const struct got_error *
2568 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2569 a44927cc 2022-04-07 stsp struct got_commit_object *commit, const char *path)
2570 941e9f74 2019-05-21 stsp {
2571 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2572 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2573 941e9f74 2019-05-21 stsp const char *p;
2574 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2575 9343a5fb 2018-06-23 stsp
2576 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2577 941e9f74 2019-05-21 stsp p = path;
2578 941e9f74 2019-05-21 stsp while (*p) {
2579 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2580 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2581 56e0773d 2019-11-28 stsp char *te_name;
2582 33cbf02b 2020-01-12 stsp
2583 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2584 33cbf02b 2020-01-12 stsp p++;
2585 941e9f74 2019-05-21 stsp
2586 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2587 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2588 941e9f74 2019-05-21 stsp if (slash == NULL)
2589 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2590 33cbf02b 2020-01-12 stsp else
2591 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2592 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2593 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2594 56e0773d 2019-11-28 stsp break;
2595 941e9f74 2019-05-21 stsp }
2596 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2597 56e0773d 2019-11-28 stsp if (te == NULL) {
2598 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2599 56e0773d 2019-11-28 stsp free(te_name);
2600 941e9f74 2019-05-21 stsp break;
2601 941e9f74 2019-05-21 stsp }
2602 56e0773d 2019-11-28 stsp free(te_name);
2603 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2604 941e9f74 2019-05-21 stsp
2605 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2606 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2607 b03c880f 2019-05-21 stsp
2608 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2609 941e9f74 2019-05-21 stsp if (slash)
2610 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2611 941e9f74 2019-05-21 stsp else
2612 941e9f74 2019-05-21 stsp subpath = strdup(path);
2613 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2614 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2615 941e9f74 2019-05-21 stsp break;
2616 941e9f74 2019-05-21 stsp }
2617 941e9f74 2019-05-21 stsp
2618 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&tree_id, s->repo, commit,
2619 941e9f74 2019-05-21 stsp subpath);
2620 941e9f74 2019-05-21 stsp if (err)
2621 941e9f74 2019-05-21 stsp break;
2622 941e9f74 2019-05-21 stsp
2623 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2624 941e9f74 2019-05-21 stsp free(tree_id);
2625 941e9f74 2019-05-21 stsp if (err)
2626 941e9f74 2019-05-21 stsp break;
2627 941e9f74 2019-05-21 stsp
2628 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2629 941e9f74 2019-05-21 stsp if (err) {
2630 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2631 941e9f74 2019-05-21 stsp break;
2632 941e9f74 2019-05-21 stsp }
2633 941e9f74 2019-05-21 stsp if (slash == NULL)
2634 941e9f74 2019-05-21 stsp break;
2635 941e9f74 2019-05-21 stsp free(subpath);
2636 941e9f74 2019-05-21 stsp subpath = NULL;
2637 941e9f74 2019-05-21 stsp p = slash;
2638 941e9f74 2019-05-21 stsp }
2639 941e9f74 2019-05-21 stsp
2640 941e9f74 2019-05-21 stsp free(subpath);
2641 1a76625f 2018-10-22 stsp return err;
2642 61266923 2020-01-14 stsp }
2643 61266923 2020-01-14 stsp
2644 61266923 2020-01-14 stsp static const struct got_error *
2645 49b24ee5 2022-07-03 mark browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2646 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2647 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2648 55cccc34 2020-02-20 stsp {
2649 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2650 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2651 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2652 55cccc34 2020-02-20 stsp
2653 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2654 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2655 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2656 55cccc34 2020-02-20 stsp
2657 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2658 bc573f3b 2021-07-10 stsp if (err)
2659 55cccc34 2020-02-20 stsp return err;
2660 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2661 55cccc34 2020-02-20 stsp
2662 55cccc34 2020-02-20 stsp *new_view = tree_view;
2663 55cccc34 2020-02-20 stsp
2664 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2665 55cccc34 2020-02-20 stsp return NULL;
2666 55cccc34 2020-02-20 stsp
2667 a44927cc 2022-04-07 stsp return tree_view_walk_path(s, entry->commit, path);
2668 55cccc34 2020-02-20 stsp }
2669 55cccc34 2020-02-20 stsp
2670 55cccc34 2020-02-20 stsp static const struct got_error *
2671 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2672 61266923 2020-01-14 stsp {
2673 61266923 2020-01-14 stsp sigset_t sigset;
2674 61266923 2020-01-14 stsp int errcode;
2675 61266923 2020-01-14 stsp
2676 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2677 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2678 61266923 2020-01-14 stsp
2679 2497f032 2022-05-31 stsp /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2680 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2681 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2682 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2683 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2684 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGINT) == -1)
2685 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2686 2497f032 2022-05-31 stsp if (sigaddset(&sigset, SIGTERM) == -1)
2687 2497f032 2022-05-31 stsp return got_error_from_errno("sigaddset");
2688 61266923 2020-01-14 stsp
2689 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2690 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2691 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2692 61266923 2020-01-14 stsp
2693 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2694 61266923 2020-01-14 stsp if (errcode)
2695 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2696 61266923 2020-01-14 stsp
2697 61266923 2020-01-14 stsp return NULL;
2698 1a76625f 2018-10-22 stsp }
2699 1a76625f 2018-10-22 stsp
2700 1a76625f 2018-10-22 stsp static void *
2701 1a76625f 2018-10-22 stsp log_thread(void *arg)
2702 1a76625f 2018-10-22 stsp {
2703 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2704 1a76625f 2018-10-22 stsp int errcode = 0;
2705 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2706 1a76625f 2018-10-22 stsp int done = 0;
2707 61266923 2020-01-14 stsp
2708 5629093a 2022-07-11 stsp /*
2709 5629093a 2022-07-11 stsp * Sync startup with main thread such that we begin our
2710 5629093a 2022-07-11 stsp * work once view_input() has released the mutex.
2711 5629093a 2022-07-11 stsp */
2712 5629093a 2022-07-11 stsp errcode = pthread_mutex_lock(&tog_mutex);
2713 5629093a 2022-07-11 stsp if (errcode) {
2714 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_lock");
2715 61266923 2020-01-14 stsp return (void *)err;
2716 5629093a 2022-07-11 stsp }
2717 1a76625f 2018-10-22 stsp
2718 5629093a 2022-07-11 stsp err = block_signals_used_by_main_thread();
2719 5629093a 2022-07-11 stsp if (err) {
2720 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2721 5629093a 2022-07-11 stsp goto done;
2722 5629093a 2022-07-11 stsp }
2723 5629093a 2022-07-11 stsp
2724 2497f032 2022-05-31 stsp while (!done && !err && !tog_fatal_signal_received()) {
2725 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2726 5629093a 2022-07-11 stsp if (errcode) {
2727 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode,
2728 5629093a 2022-07-11 stsp "pthread_mutex_unlock");
2729 5629093a 2022-07-11 stsp goto done;
2730 5629093a 2022-07-11 stsp }
2731 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2732 1a76625f 2018-10-22 stsp if (err) {
2733 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2734 5629093a 2022-07-11 stsp goto done;
2735 1a76625f 2018-10-22 stsp err = NULL;
2736 1a76625f 2018-10-22 stsp done = 1;
2737 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2738 1a76625f 2018-10-22 stsp a->commits_needed--;
2739 1a76625f 2018-10-22 stsp
2740 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2741 3abe8080 2019-04-10 stsp if (errcode) {
2742 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2743 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2744 5629093a 2022-07-11 stsp goto done;
2745 3abe8080 2019-04-10 stsp } else if (*a->quit)
2746 1a76625f 2018-10-22 stsp done = 1;
2747 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2748 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2749 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2750 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2751 1a76625f 2018-10-22 stsp }
2752 1a76625f 2018-10-22 stsp
2753 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2754 7c1452c1 2020-03-26 stsp if (errcode) {
2755 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2756 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2757 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2758 5629093a 2022-07-11 stsp goto done;
2759 7c1452c1 2020-03-26 stsp }
2760 7c1452c1 2020-03-26 stsp
2761 1a76625f 2018-10-22 stsp if (done)
2762 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2763 7c1452c1 2020-03-26 stsp else {
2764 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2765 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2766 7c1452c1 2020-03-26 stsp &tog_mutex);
2767 5629093a 2022-07-11 stsp if (errcode) {
2768 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2769 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2770 5629093a 2022-07-11 stsp pthread_mutex_unlock(&tog_mutex);
2771 5629093a 2022-07-11 stsp goto done;
2772 5629093a 2022-07-11 stsp }
2773 21355643 2020-12-06 stsp if (*a->quit)
2774 21355643 2020-12-06 stsp done = 1;
2775 7c1452c1 2020-03-26 stsp }
2776 1a76625f 2018-10-22 stsp }
2777 1a76625f 2018-10-22 stsp }
2778 3abe8080 2019-04-10 stsp a->log_complete = 1;
2779 5629093a 2022-07-11 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2780 5629093a 2022-07-11 stsp if (errcode)
2781 5629093a 2022-07-11 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2782 5629093a 2022-07-11 stsp done:
2783 5629093a 2022-07-11 stsp if (err) {
2784 5629093a 2022-07-11 stsp tog_thread_error = 1;
2785 5629093a 2022-07-11 stsp pthread_cond_signal(&a->commit_loaded);
2786 5629093a 2022-07-11 stsp }
2787 1a76625f 2018-10-22 stsp return (void *)err;
2788 1a76625f 2018-10-22 stsp }
2789 1a76625f 2018-10-22 stsp
2790 1a76625f 2018-10-22 stsp static const struct got_error *
2791 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2792 1a76625f 2018-10-22 stsp {
2793 5629093a 2022-07-11 stsp const struct got_error *err = NULL, *thread_err = NULL;
2794 1a76625f 2018-10-22 stsp int errcode;
2795 1a76625f 2018-10-22 stsp
2796 1a76625f 2018-10-22 stsp if (s->thread) {
2797 1a76625f 2018-10-22 stsp s->quit = 1;
2798 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2799 1a76625f 2018-10-22 stsp if (errcode)
2800 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2801 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2802 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
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_mutex_unlock");
2806 5629093a 2022-07-11 stsp errcode = pthread_join(s->thread, (void **)&thread_err);
2807 1a76625f 2018-10-22 stsp if (errcode)
2808 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2809 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2810 1a76625f 2018-10-22 stsp if (errcode)
2811 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2812 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2813 1a76625f 2018-10-22 stsp s->thread = NULL;
2814 1a76625f 2018-10-22 stsp }
2815 1a76625f 2018-10-22 stsp
2816 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2817 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2818 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2819 74467cc8 2022-06-15 stsp }
2820 74467cc8 2022-06-15 stsp
2821 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds) {
2822 74467cc8 2022-06-15 stsp const struct got_error *pack_err =
2823 74467cc8 2022-06-15 stsp got_repo_pack_fds_close(s->thread_args.pack_fds);
2824 74467cc8 2022-06-15 stsp if (err == NULL)
2825 74467cc8 2022-06-15 stsp err = pack_err;
2826 74467cc8 2022-06-15 stsp s->thread_args.pack_fds = NULL;
2827 1a76625f 2018-10-22 stsp }
2828 1a76625f 2018-10-22 stsp
2829 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2830 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2831 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2832 1a76625f 2018-10-22 stsp }
2833 1a76625f 2018-10-22 stsp
2834 5629093a 2022-07-11 stsp return err ? err : thread_err;
2835 9343a5fb 2018-06-23 stsp }
2836 9343a5fb 2018-06-23 stsp
2837 9343a5fb 2018-06-23 stsp static const struct got_error *
2838 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2839 1a76625f 2018-10-22 stsp {
2840 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2841 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2842 276b94a1 2020-11-13 naddy int errcode;
2843 1a76625f 2018-10-22 stsp
2844 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2845 276b94a1 2020-11-13 naddy
2846 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2847 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2848 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2849 276b94a1 2020-11-13 naddy
2850 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
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 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2855 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2856 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2857 1a76625f 2018-10-22 stsp free(s->start_id);
2858 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2859 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2860 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2861 1a76625f 2018-10-22 stsp return err;
2862 1a76625f 2018-10-22 stsp }
2863 1a76625f 2018-10-22 stsp
2864 1a76625f 2018-10-22 stsp static const struct got_error *
2865 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2866 60493ae3 2019-06-20 stsp {
2867 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2868 60493ae3 2019-06-20 stsp
2869 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2870 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2871 60493ae3 2019-06-20 stsp return NULL;
2872 60493ae3 2019-06-20 stsp }
2873 60493ae3 2019-06-20 stsp
2874 60493ae3 2019-06-20 stsp static const struct got_error *
2875 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2876 60493ae3 2019-06-20 stsp {
2877 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2878 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2879 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2880 60493ae3 2019-06-20 stsp
2881 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2882 f9686aa5 2020-03-27 stsp show_log_view(view);
2883 f9686aa5 2020-03-27 stsp update_panels();
2884 f9686aa5 2020-03-27 stsp doupdate();
2885 f9686aa5 2020-03-27 stsp
2886 96e2b566 2019-07-08 stsp if (s->search_entry) {
2887 13add988 2019-10-15 stsp int errcode, ch;
2888 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2889 13add988 2019-10-15 stsp if (errcode)
2890 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2891 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2892 13add988 2019-10-15 stsp ch = wgetch(view->window);
2893 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2894 13add988 2019-10-15 stsp if (errcode)
2895 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2896 13add988 2019-10-15 stsp "pthread_mutex_lock");
2897 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2898 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2899 678cbce5 2019-07-28 stsp return NULL;
2900 678cbce5 2019-07-28 stsp }
2901 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2902 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2903 96e2b566 2019-07-08 stsp else
2904 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2905 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2906 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2907 364ac6fd 2022-06-18 stsp int matched_idx = s->matched_entry->idx;
2908 364ac6fd 2022-06-18 stsp int selected_idx = s->selected_entry->idx;
2909 364ac6fd 2022-06-18 stsp
2910 364ac6fd 2022-06-18 stsp /*
2911 f704b35c 2022-06-18 stsp * If the user has moved the cursor after we hit a match,
2912 f704b35c 2022-06-18 stsp * the position from where we should continue searching
2913 f704b35c 2022-06-18 stsp * might have changed.
2914 364ac6fd 2022-06-18 stsp */
2915 4bfe9f0a 2022-06-18 stsp if (view->searching == TOG_SEARCH_FORWARD) {
2916 364ac6fd 2022-06-18 stsp if (matched_idx > selected_idx)
2917 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2918 364ac6fd 2022-06-18 stsp else
2919 364ac6fd 2022-06-18 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2920 4bfe9f0a 2022-06-18 stsp } else {
2921 364ac6fd 2022-06-18 stsp if (matched_idx < selected_idx)
2922 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->selected_entry,
2923 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2924 364ac6fd 2022-06-18 stsp else
2925 364ac6fd 2022-06-18 stsp entry = TAILQ_PREV(s->matched_entry,
2926 364ac6fd 2022-06-18 stsp commit_queue_head, entry);
2927 4bfe9f0a 2022-06-18 stsp }
2928 20be8d96 2019-06-21 stsp } else {
2929 5a5ede53 2021-12-09 stsp entry = s->selected_entry;
2930 20be8d96 2019-06-21 stsp }
2931 60493ae3 2019-06-20 stsp
2932 60493ae3 2019-06-20 stsp while (1) {
2933 13add988 2019-10-15 stsp int have_match = 0;
2934 13add988 2019-10-15 stsp
2935 60493ae3 2019-06-20 stsp if (entry == NULL) {
2936 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2937 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2938 f9967bca 2020-03-27 stsp view->search_next_done =
2939 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2940 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2941 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2942 f801134a 2019-06-25 stsp return NULL;
2943 60493ae3 2019-06-20 stsp }
2944 96e2b566 2019-07-08 stsp /*
2945 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2946 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2947 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2948 96e2b566 2019-07-08 stsp */
2949 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2950 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2951 60493ae3 2019-06-20 stsp }
2952 60493ae3 2019-06-20 stsp
2953 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2954 13add988 2019-10-15 stsp &view->regex);
2955 5943eee2 2019-08-13 stsp if (err)
2956 13add988 2019-10-15 stsp break;
2957 13add988 2019-10-15 stsp if (have_match) {
2958 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2959 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2960 60493ae3 2019-06-20 stsp break;
2961 60493ae3 2019-06-20 stsp }
2962 13add988 2019-10-15 stsp
2963 96e2b566 2019-07-08 stsp s->search_entry = entry;
2964 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2965 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2966 b1bf1435 2019-06-21 stsp else
2967 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2968 60493ae3 2019-06-20 stsp }
2969 60493ae3 2019-06-20 stsp
2970 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2971 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2972 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2973 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2974 60493ae3 2019-06-20 stsp if (err)
2975 60493ae3 2019-06-20 stsp return err;
2976 ead14cbe 2019-06-21 stsp cur++;
2977 ead14cbe 2019-06-21 stsp }
2978 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2979 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2980 60493ae3 2019-06-20 stsp if (err)
2981 60493ae3 2019-06-20 stsp return err;
2982 ead14cbe 2019-06-21 stsp cur--;
2983 60493ae3 2019-06-20 stsp }
2984 60493ae3 2019-06-20 stsp }
2985 60493ae3 2019-06-20 stsp
2986 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2987 96e2b566 2019-07-08 stsp
2988 60493ae3 2019-06-20 stsp return NULL;
2989 60493ae3 2019-06-20 stsp }
2990 60493ae3 2019-06-20 stsp
2991 60493ae3 2019-06-20 stsp static const struct got_error *
2992 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2993 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2994 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2995 80ddbec8 2018-04-29 stsp {
2996 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2997 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2998 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2999 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3000 1a76625f 2018-10-22 stsp int errcode;
3001 80ddbec8 2018-04-29 stsp
3002 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3003 f135c941 2020-02-20 stsp free(s->in_repo_path);
3004 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3005 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3006 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3007 f135c941 2020-02-20 stsp }
3008 ecb28ae0 2018-07-16 stsp
3009 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3010 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
3011 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
3012 78756c87 2020-11-24 stsp
3013 fb2756b9 2018-08-04 stsp s->repo = repo;
3014 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3015 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3016 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3017 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3018 9cd7cbd1 2020-12-07 stsp goto done;
3019 9cd7cbd1 2020-12-07 stsp }
3020 9cd7cbd1 2020-12-07 stsp }
3021 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3022 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3023 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3024 5036bf37 2018-09-24 stsp goto done;
3025 5036bf37 2018-09-24 stsp }
3026 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3027 e5a0f69f 2018-08-18 stsp
3028 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3029 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3030 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3031 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3032 11b20872 2019-11-08 stsp if (err)
3033 11b20872 2019-11-08 stsp goto done;
3034 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3035 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3036 11b20872 2019-11-08 stsp if (err) {
3037 11b20872 2019-11-08 stsp free_colors(&s->colors);
3038 11b20872 2019-11-08 stsp goto done;
3039 11b20872 2019-11-08 stsp }
3040 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3041 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3042 11b20872 2019-11-08 stsp if (err) {
3043 11b20872 2019-11-08 stsp free_colors(&s->colors);
3044 11b20872 2019-11-08 stsp goto done;
3045 11b20872 2019-11-08 stsp }
3046 11b20872 2019-11-08 stsp }
3047 11b20872 2019-11-08 stsp
3048 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3049 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3050 571ccd73 2022-07-19 mark view->resize = resize_log_view;
3051 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3052 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3053 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3054 1a76625f 2018-10-22 stsp
3055 74467cc8 2022-06-15 stsp if (s->thread_args.pack_fds == NULL) {
3056 74467cc8 2022-06-15 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3057 74467cc8 2022-06-15 stsp if (err)
3058 74467cc8 2022-06-15 stsp goto done;
3059 74467cc8 2022-06-15 stsp }
3060 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3061 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3062 0ae84acc 2022-06-15 tracey if (err)
3063 0ae84acc 2022-06-15 tracey goto done;
3064 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3065 b672a97a 2020-01-27 stsp !s->log_branches);
3066 1a76625f 2018-10-22 stsp if (err)
3067 1a76625f 2018-10-22 stsp goto done;
3068 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3069 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3070 c5b78334 2020-01-12 stsp if (err)
3071 c5b78334 2020-01-12 stsp goto done;
3072 1a76625f 2018-10-22 stsp
3073 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3074 1a76625f 2018-10-22 stsp if (errcode) {
3075 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3076 1a76625f 2018-10-22 stsp goto done;
3077 1a76625f 2018-10-22 stsp }
3078 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3079 7c1452c1 2020-03-26 stsp if (errcode) {
3080 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3081 7c1452c1 2020-03-26 stsp goto done;
3082 7c1452c1 2020-03-26 stsp }
3083 1a76625f 2018-10-22 stsp
3084 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3085 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3086 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3087 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3088 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3089 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3090 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3091 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3092 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3093 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3094 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3095 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3096 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3097 ba4f502b 2018-08-04 stsp done:
3098 1a76625f 2018-10-22 stsp if (err)
3099 1a76625f 2018-10-22 stsp close_log_view(view);
3100 ba4f502b 2018-08-04 stsp return err;
3101 ba4f502b 2018-08-04 stsp }
3102 ba4f502b 2018-08-04 stsp
3103 e5a0f69f 2018-08-18 stsp static const struct got_error *
3104 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3105 ba4f502b 2018-08-04 stsp {
3106 f2f6d207 2020-11-24 stsp const struct got_error *err;
3107 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3108 ba4f502b 2018-08-04 stsp
3109 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
3110 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3111 2b380cc8 2018-10-24 stsp &s->thread_args);
3112 2b380cc8 2018-10-24 stsp if (errcode)
3113 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3114 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3115 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3116 f2f6d207 2020-11-24 stsp if (err)
3117 f2f6d207 2020-11-24 stsp return err;
3118 f2f6d207 2020-11-24 stsp }
3119 2b380cc8 2018-10-24 stsp }
3120 2b380cc8 2018-10-24 stsp
3121 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3122 b880cc75 2022-06-30 mark }
3123 b880cc75 2022-06-30 mark
3124 b880cc75 2022-06-30 mark static void
3125 b880cc75 2022-06-30 mark log_move_cursor_up(struct tog_view *view, int page, int home)
3126 b880cc75 2022-06-30 mark {
3127 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3128 b880cc75 2022-06-30 mark
3129 b880cc75 2022-06-30 mark if (s->selected_entry->idx == 0)
3130 b880cc75 2022-06-30 mark view->count = 0;
3131 b880cc75 2022-06-30 mark if (s->first_displayed_entry == NULL)
3132 b880cc75 2022-06-30 mark return;
3133 b880cc75 2022-06-30 mark
3134 b880cc75 2022-06-30 mark if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3135 b880cc75 2022-06-30 mark || home)
3136 b880cc75 2022-06-30 mark s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3137 b880cc75 2022-06-30 mark
3138 b880cc75 2022-06-30 mark if (!page && !home && s->selected > 0)
3139 b880cc75 2022-06-30 mark --s->selected;
3140 b880cc75 2022-06-30 mark else
3141 b880cc75 2022-06-30 mark log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3142 b880cc75 2022-06-30 mark
3143 b880cc75 2022-06-30 mark select_commit(s);
3144 b880cc75 2022-06-30 mark return;
3145 b880cc75 2022-06-30 mark }
3146 b880cc75 2022-06-30 mark
3147 b880cc75 2022-06-30 mark static const struct got_error *
3148 b880cc75 2022-06-30 mark log_move_cursor_down(struct tog_view *view, int page)
3149 b880cc75 2022-06-30 mark {
3150 b880cc75 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
3151 b880cc75 2022-06-30 mark struct commit_queue_entry *first;
3152 b880cc75 2022-06-30 mark const struct got_error *err = NULL;
3153 b880cc75 2022-06-30 mark
3154 b880cc75 2022-06-30 mark first = s->first_displayed_entry;
3155 b880cc75 2022-06-30 mark if (first == NULL) {
3156 b880cc75 2022-06-30 mark view->count = 0;
3157 b880cc75 2022-06-30 mark return NULL;
3158 b880cc75 2022-06-30 mark }
3159 b880cc75 2022-06-30 mark
3160 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3161 b880cc75 2022-06-30 mark s->selected_entry->idx >= s->commits.ncommits - 1)
3162 b880cc75 2022-06-30 mark return NULL;
3163 b880cc75 2022-06-30 mark
3164 b880cc75 2022-06-30 mark if (!page) {
3165 b880cc75 2022-06-30 mark int eos = view->nlines - 2;
3166 b880cc75 2022-06-30 mark
3167 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3168 9b058f45 2022-06-30 mark --eos; /* border consumes the last line */
3169 b880cc75 2022-06-30 mark if (s->selected < MIN(eos, s->commits.ncommits - 1))
3170 b880cc75 2022-06-30 mark ++s->selected;
3171 b880cc75 2022-06-30 mark else
3172 b880cc75 2022-06-30 mark err = log_scroll_down(view, 1);
3173 0dca135e 2022-06-30 mark } else if (s->thread_args.load_all) {
3174 b880cc75 2022-06-30 mark if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3175 b880cc75 2022-06-30 mark s->selected += MIN(s->last_displayed_entry->idx -
3176 b880cc75 2022-06-30 mark s->selected_entry->idx, page + 1);
3177 b880cc75 2022-06-30 mark else
3178 b880cc75 2022-06-30 mark err = log_scroll_down(view, MIN(page,
3179 b880cc75 2022-06-30 mark s->commits.ncommits - s->selected_entry->idx - 1));
3180 b880cc75 2022-06-30 mark s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3181 b880cc75 2022-06-30 mark } else {
3182 b880cc75 2022-06-30 mark err = log_scroll_down(view, page);
3183 b880cc75 2022-06-30 mark if (err)
3184 b880cc75 2022-06-30 mark return err;
3185 b880cc75 2022-06-30 mark if (first == s->first_displayed_entry && s->selected <
3186 b880cc75 2022-06-30 mark MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3187 b880cc75 2022-06-30 mark s->selected = MIN(s->commits.ncommits - 1, page);
3188 b880cc75 2022-06-30 mark }
3189 b880cc75 2022-06-30 mark }
3190 b880cc75 2022-06-30 mark if (err)
3191 b880cc75 2022-06-30 mark return err;
3192 b880cc75 2022-06-30 mark
3193 9b058f45 2022-06-30 mark /*
3194 9b058f45 2022-06-30 mark * We might necessarily overshoot in horizontal
3195 9b058f45 2022-06-30 mark * splits; if so, select the last displayed commit.
3196 9b058f45 2022-06-30 mark */
3197 9b058f45 2022-06-30 mark s->selected = MIN(s->selected,
3198 9b058f45 2022-06-30 mark s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3199 9b058f45 2022-06-30 mark
3200 b880cc75 2022-06-30 mark select_commit(s);
3201 b880cc75 2022-06-30 mark
3202 b880cc75 2022-06-30 mark if (s->thread_args.log_complete &&
3203 b880cc75 2022-06-30 mark s->selected_entry->idx == s->commits.ncommits - 1)
3204 b880cc75 2022-06-30 mark view->count = 0;
3205 b880cc75 2022-06-30 mark
3206 b880cc75 2022-06-30 mark return NULL;
3207 e5a0f69f 2018-08-18 stsp }
3208 04cc582a 2018-08-01 stsp
3209 9b058f45 2022-06-30 mark static void
3210 9b058f45 2022-06-30 mark view_get_split(struct tog_view *view, int *y, int *x)
3211 9b058f45 2022-06-30 mark {
3212 76364b2d 2022-07-02 mark *x = 0;
3213 76364b2d 2022-07-02 mark *y = 0;
3214 76364b2d 2022-07-02 mark
3215 3c1dfe12 2022-07-08 mark if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3216 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_y)
3217 3c1dfe12 2022-07-08 mark *y = view->child->resized_y;
3218 7532ccda 2022-07-11 mark else if (view->resized_y)
3219 7532ccda 2022-07-11 mark *y = view->resized_y;
3220 3c1dfe12 2022-07-08 mark else
3221 3c1dfe12 2022-07-08 mark *y = view_split_begin_y(view->lines);
3222 7532ccda 2022-07-11 mark } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3223 3c1dfe12 2022-07-08 mark if (view->child && view->child->resized_x)
3224 3c1dfe12 2022-07-08 mark *x = view->child->resized_x;
3225 7532ccda 2022-07-11 mark else if (view->resized_x)
3226 7532ccda 2022-07-11 mark *x = view->resized_x;
3227 3c1dfe12 2022-07-08 mark else
3228 3c1dfe12 2022-07-08 mark *x = view_split_begin_x(view->begin_x);
3229 3c1dfe12 2022-07-08 mark }
3230 9b058f45 2022-06-30 mark }
3231 9b058f45 2022-06-30 mark
3232 9b058f45 2022-06-30 mark /* Split view horizontally at y and offset view->state->selected line. */
3233 e5a0f69f 2018-08-18 stsp static const struct got_error *
3234 9b058f45 2022-06-30 mark view_init_hsplit(struct tog_view *view, int y)
3235 9b058f45 2022-06-30 mark {
3236 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
3237 9b058f45 2022-06-30 mark
3238 9b058f45 2022-06-30 mark view->nlines = y;
3239 d2366e29 2022-07-07 mark view->ncols = COLS;
3240 9b058f45 2022-06-30 mark err = view_resize(view);
3241 9b058f45 2022-06-30 mark if (err)
3242 9b058f45 2022-06-30 mark return err;
3243 9b058f45 2022-06-30 mark
3244 9b058f45 2022-06-30 mark err = offset_selection_down(view);
3245 9b058f45 2022-06-30 mark
3246 9b058f45 2022-06-30 mark return err;
3247 9b058f45 2022-06-30 mark }
3248 9b058f45 2022-06-30 mark
3249 9b058f45 2022-06-30 mark static const struct got_error *
3250 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3251 e5a0f69f 2018-08-18 stsp {
3252 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3253 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3254 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3255 136e2bd4 2022-07-23 mark int eos, n, nscroll;
3256 80ddbec8 2018-04-29 stsp
3257 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3258 a6d37fac 2022-07-03 mark if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3259 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3260 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3261 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, s->commits.ncommits);
3262 0dca135e 2022-06-30 mark s->thread_args.load_all = 0;
3263 fb280deb 2021-08-30 stsp }
3264 b880cc75 2022-06-30 mark return err;
3265 528dedf3 2021-08-30 stsp }
3266 0dca135e 2022-06-30 mark
3267 0dca135e 2022-06-30 mark eos = nscroll = view->nlines - 1;
3268 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
3269 0dca135e 2022-06-30 mark --eos; /* border */
3270 0dca135e 2022-06-30 mark
3271 528dedf3 2021-08-30 stsp switch (ch) {
3272 1e37a5c2 2019-05-12 jcs case 'q':
3273 1e37a5c2 2019-05-12 jcs s->quit = 1;
3274 145b6838 2022-06-16 stsp break;
3275 145b6838 2022-06-16 stsp case '0':
3276 145b6838 2022-06-16 stsp view->x = 0;
3277 145b6838 2022-06-16 stsp break;
3278 145b6838 2022-06-16 stsp case '$':
3279 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 2, 0);
3280 640cd7ff 2022-06-22 mark view->count = 0;
3281 145b6838 2022-06-16 stsp break;
3282 145b6838 2022-06-16 stsp case KEY_RIGHT:
3283 145b6838 2022-06-16 stsp case 'l':
3284 145b6838 2022-06-16 stsp if (view->x + view->ncols / 2 < view->maxx)
3285 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
3286 640cd7ff 2022-06-22 mark else
3287 640cd7ff 2022-06-22 mark view->count = 0;
3288 1e37a5c2 2019-05-12 jcs break;
3289 145b6838 2022-06-16 stsp case KEY_LEFT:
3290 145b6838 2022-06-16 stsp case 'h':
3291 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
3292 640cd7ff 2022-06-22 mark if (view->x <= 0)
3293 640cd7ff 2022-06-22 mark view->count = 0;
3294 145b6838 2022-06-16 stsp break;
3295 1e37a5c2 2019-05-12 jcs case 'k':
3296 1e37a5c2 2019-05-12 jcs case KEY_UP:
3297 1e37a5c2 2019-05-12 jcs case '<':
3298 1e37a5c2 2019-05-12 jcs case ',':
3299 02ffd0d5 2021-10-17 stsp case CTRL('p'):
3300 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 0);
3301 912a3f79 2021-08-30 j break;
3302 912a3f79 2021-08-30 j case 'g':
3303 912a3f79 2021-08-30 j case KEY_HOME:
3304 b880cc75 2022-06-30 mark log_move_cursor_up(view, 0, 1);
3305 640cd7ff 2022-06-22 mark view->count = 0;
3306 1e37a5c2 2019-05-12 jcs break;
3307 83cc4199 2022-06-13 stsp case CTRL('u'):
3308 33c3719a 2022-06-15 stsp case 'u':
3309 83cc4199 2022-06-13 stsp nscroll /= 2;
3310 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3311 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3312 a4292ac5 2019-05-12 jcs case CTRL('b'):
3313 61417565 2022-06-20 mark case 'b':
3314 b880cc75 2022-06-30 mark log_move_cursor_up(view, nscroll, 0);
3315 1e37a5c2 2019-05-12 jcs break;
3316 1e37a5c2 2019-05-12 jcs case 'j':
3317 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3318 1e37a5c2 2019-05-12 jcs case '>':
3319 1e37a5c2 2019-05-12 jcs case '.':
3320 02ffd0d5 2021-10-17 stsp case CTRL('n'):
3321 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, 0);
3322 912a3f79 2021-08-30 j break;
3323 10aab77f 2022-07-19 op case '@':
3324 10aab77f 2022-07-19 op s->use_committer = !s->use_committer;
3325 10aab77f 2022-07-19 op break;
3326 912a3f79 2021-08-30 j case 'G':
3327 912a3f79 2021-08-30 j case KEY_END: {
3328 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3329 912a3f79 2021-08-30 j * traverse them all. */
3330 640cd7ff 2022-06-22 mark view->count = 0;
3331 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3332 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3333 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3334 80ddbec8 2018-04-29 stsp }
3335 912a3f79 2021-08-30 j
3336 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3337 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3338 0dca135e 2022-06-30 mark for (n = 0; n < eos; n++) {
3339 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3340 f3bc9f1d 2021-09-05 naddy break;
3341 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3342 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3343 f3bc9f1d 2021-09-05 naddy }
3344 f3bc9f1d 2021-09-05 naddy if (n > 0)
3345 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3346 2b779855 2020-12-05 naddy select_commit(s);
3347 1e37a5c2 2019-05-12 jcs break;
3348 912a3f79 2021-08-30 j }
3349 80b7e8da 2022-06-11 stsp case CTRL('d'):
3350 33c3719a 2022-06-15 stsp case 'd':
3351 83cc4199 2022-06-13 stsp nscroll /= 2;
3352 83cc4199 2022-06-13 stsp /* FALL THROUGH */
3353 83cc4199 2022-06-13 stsp case KEY_NPAGE:
3354 61417565 2022-06-20 mark case CTRL('f'):
3355 48bb96f0 2022-06-20 naddy case 'f':
3356 b880cc75 2022-06-30 mark case ' ':
3357 b880cc75 2022-06-30 mark err = log_move_cursor_down(view, nscroll);
3358 1e37a5c2 2019-05-12 jcs break;
3359 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3360 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3361 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3362 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3363 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3364 2b779855 2020-12-05 naddy select_commit(s);
3365 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3366 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3367 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3368 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3369 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3370 0bf7f153 2020-12-02 naddy }
3371 1e37a5c2 2019-05-12 jcs break;
3372 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3373 49b24ee5 2022-07-03 mark case '\r':
3374 640cd7ff 2022-06-22 mark view->count = 0;
3375 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3376 e5a0f69f 2018-08-18 stsp break;
3377 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3378 1e37a5c2 2019-05-12 jcs break;
3379 5e98fb33 2022-07-22 mark case 'T':
3380 640cd7ff 2022-06-22 mark view->count = 0;
3381 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3382 5036bf37 2018-09-24 stsp break;
3383 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
3384 1e37a5c2 2019-05-12 jcs break;
3385 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3386 21355643 2020-12-06 stsp case CTRL('l'):
3387 21355643 2020-12-06 stsp case 'B':
3388 640cd7ff 2022-06-22 mark view->count = 0;
3389 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3390 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3391 1e37a5c2 2019-05-12 jcs break;
3392 21355643 2020-12-06 stsp err = stop_log_thread(s);
3393 74cfe85e 2020-10-20 stsp if (err)
3394 74cfe85e 2020-10-20 stsp return err;
3395 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3396 21355643 2020-12-06 stsp char *parent_path;
3397 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3398 21355643 2020-12-06 stsp if (err)
3399 21355643 2020-12-06 stsp return err;
3400 21355643 2020-12-06 stsp free(s->in_repo_path);
3401 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3402 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3403 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3404 21355643 2020-12-06 stsp struct got_object_id *start_id;
3405 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3406 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3407 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3408 21355643 2020-12-06 stsp if (err)
3409 21355643 2020-12-06 stsp return err;
3410 21355643 2020-12-06 stsp free(s->start_id);
3411 21355643 2020-12-06 stsp s->start_id = start_id;
3412 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3413 21355643 2020-12-06 stsp } else /* 'B' */
3414 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3415 21355643 2020-12-06 stsp
3416 b0dd8d27 2022-06-16 stsp if (s->thread_args.pack_fds == NULL) {
3417 b0dd8d27 2022-06-16 stsp err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3418 b0dd8d27 2022-06-16 stsp if (err)
3419 b0dd8d27 2022-06-16 stsp return err;
3420 b0dd8d27 2022-06-16 stsp }
3421 74467cc8 2022-06-15 stsp err = got_repo_open(&s->thread_args.repo,
3422 74467cc8 2022-06-15 stsp got_repo_get_path(s->repo), NULL,
3423 74467cc8 2022-06-15 stsp s->thread_args.pack_fds);
3424 74cfe85e 2020-10-20 stsp if (err)
3425 21355643 2020-12-06 stsp return err;
3426 51a10b52 2020-12-26 stsp tog_free_refs();
3427 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, 0);
3428 ca51c541 2020-12-07 stsp if (err)
3429 ca51c541 2020-12-07 stsp return err;
3430 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3431 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3432 d01904d4 2019-06-25 stsp if (err)
3433 d01904d4 2019-06-25 stsp return err;
3434 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3435 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3436 b672a97a 2020-01-27 stsp if (err)
3437 b672a97a 2020-01-27 stsp return err;
3438 21355643 2020-12-06 stsp free_commits(&s->commits);
3439 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3440 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3441 21355643 2020-12-06 stsp s->selected_entry = NULL;
3442 21355643 2020-12-06 stsp s->selected = 0;
3443 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3444 21355643 2020-12-06 stsp s->quit = 0;
3445 9b058f45 2022-06-30 mark s->thread_args.commits_needed = view->lines;
3446 dfee752e 2022-06-18 op s->matched_entry = NULL;
3447 dfee752e 2022-06-18 op s->search_entry = NULL;
3448 01a7bcaf 2022-07-22 mark view->offset = 0;
3449 d01904d4 2019-06-25 stsp break;
3450 5e98fb33 2022-07-22 mark case 'R':
3451 640cd7ff 2022-06-22 mark view->count = 0;
3452 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
3453 6458efa5 2020-11-24 stsp break;
3454 1e37a5c2 2019-05-12 jcs default:
3455 640cd7ff 2022-06-22 mark view->count = 0;
3456 1e37a5c2 2019-05-12 jcs break;
3457 899d86c2 2018-05-10 stsp }
3458 e5a0f69f 2018-08-18 stsp
3459 80ddbec8 2018-04-29 stsp return err;
3460 80ddbec8 2018-04-29 stsp }
3461 80ddbec8 2018-04-29 stsp
3462 4ed7e80c 2018-05-20 stsp static const struct got_error *
3463 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3464 c2db6724 2019-01-04 stsp {
3465 c2db6724 2019-01-04 stsp const struct got_error *error;
3466 c2db6724 2019-01-04 stsp
3467 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3468 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3469 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3470 37c06ea4 2019-07-15 stsp #endif
3471 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3472 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3473 c2db6724 2019-01-04 stsp
3474 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3475 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3476 c2db6724 2019-01-04 stsp
3477 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3478 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3479 c2db6724 2019-01-04 stsp
3480 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3481 c2db6724 2019-01-04 stsp if (error != NULL)
3482 c2db6724 2019-01-04 stsp return error;
3483 c2db6724 2019-01-04 stsp
3484 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3485 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3486 c2db6724 2019-01-04 stsp
3487 c2db6724 2019-01-04 stsp return NULL;
3488 c2db6724 2019-01-04 stsp }
3489 c2db6724 2019-01-04 stsp
3490 a915003a 2019-02-05 stsp static void
3491 a915003a 2019-02-05 stsp init_curses(void)
3492 a915003a 2019-02-05 stsp {
3493 2497f032 2022-05-31 stsp /*
3494 2497f032 2022-05-31 stsp * Override default signal handlers before starting ncurses.
3495 2497f032 2022-05-31 stsp * This should prevent ncurses from installing its own
3496 2497f032 2022-05-31 stsp * broken cleanup() signal handler.
3497 2497f032 2022-05-31 stsp */
3498 2497f032 2022-05-31 stsp signal(SIGWINCH, tog_sigwinch);
3499 2497f032 2022-05-31 stsp signal(SIGPIPE, tog_sigpipe);
3500 2497f032 2022-05-31 stsp signal(SIGCONT, tog_sigcont);
3501 2497f032 2022-05-31 stsp signal(SIGINT, tog_sigint);
3502 2497f032 2022-05-31 stsp signal(SIGTERM, tog_sigterm);
3503 2497f032 2022-05-31 stsp
3504 a915003a 2019-02-05 stsp initscr();
3505 a915003a 2019-02-05 stsp cbreak();
3506 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3507 a915003a 2019-02-05 stsp noecho();
3508 a915003a 2019-02-05 stsp nonl();
3509 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3510 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3511 a915003a 2019-02-05 stsp curs_set(0);
3512 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3513 6d17833f 2019-11-08 stsp start_color();
3514 6d17833f 2019-11-08 stsp use_default_colors();
3515 6d17833f 2019-11-08 stsp }
3516 a915003a 2019-02-05 stsp }
3517 a915003a 2019-02-05 stsp
3518 c2db6724 2019-01-04 stsp static const struct got_error *
3519 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3520 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3521 f135c941 2020-02-20 stsp {
3522 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3523 f135c941 2020-02-20 stsp
3524 f135c941 2020-02-20 stsp if (argc == 0) {
3525 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3526 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3527 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3528 f135c941 2020-02-20 stsp return NULL;
3529 f135c941 2020-02-20 stsp }
3530 f135c941 2020-02-20 stsp
3531 f135c941 2020-02-20 stsp if (worktree) {
3532 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3533 bfd61697 2020-11-14 stsp char *p;
3534 f135c941 2020-02-20 stsp
3535 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3536 f135c941 2020-02-20 stsp if (err)
3537 f135c941 2020-02-20 stsp return err;
3538 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3539 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3540 bfd61697 2020-11-14 stsp p) == -1) {
3541 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3542 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3543 f135c941 2020-02-20 stsp }
3544 f135c941 2020-02-20 stsp free(p);
3545 f135c941 2020-02-20 stsp } else
3546 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3547 f135c941 2020-02-20 stsp
3548 f135c941 2020-02-20 stsp return err;
3549 f135c941 2020-02-20 stsp }
3550 f135c941 2020-02-20 stsp
3551 f135c941 2020-02-20 stsp static const struct got_error *
3552 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3553 9f7d7167 2018-04-29 stsp {
3554 80ddbec8 2018-04-29 stsp const struct got_error *error;
3555 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3556 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3557 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3558 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3559 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3560 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3561 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3562 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3563 04cc582a 2018-08-01 stsp struct tog_view *view;
3564 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
3565 80ddbec8 2018-04-29 stsp
3566 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3567 80ddbec8 2018-04-29 stsp switch (ch) {
3568 b672a97a 2020-01-27 stsp case 'b':
3569 b672a97a 2020-01-27 stsp log_branches = 1;
3570 b672a97a 2020-01-27 stsp break;
3571 80ddbec8 2018-04-29 stsp case 'c':
3572 80ddbec8 2018-04-29 stsp start_commit = optarg;
3573 80ddbec8 2018-04-29 stsp break;
3574 ecb28ae0 2018-07-16 stsp case 'r':
3575 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3576 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3577 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3578 9ba1d308 2019-10-21 stsp optarg);
3579 ecb28ae0 2018-07-16 stsp break;
3580 80ddbec8 2018-04-29 stsp default:
3581 17020d27 2019-03-07 stsp usage_log();
3582 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3583 80ddbec8 2018-04-29 stsp }
3584 80ddbec8 2018-04-29 stsp }
3585 80ddbec8 2018-04-29 stsp
3586 80ddbec8 2018-04-29 stsp argc -= optind;
3587 80ddbec8 2018-04-29 stsp argv += optind;
3588 80ddbec8 2018-04-29 stsp
3589 f135c941 2020-02-20 stsp if (argc > 1)
3590 f135c941 2020-02-20 stsp usage_log();
3591 963f97a1 2019-03-18 stsp
3592 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
3593 0ae84acc 2022-06-15 tracey if (error != NULL)
3594 0ae84acc 2022-06-15 tracey goto done;
3595 0ae84acc 2022-06-15 tracey
3596 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3597 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3598 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3599 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3600 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3601 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3602 c156c7a4 2020-12-18 stsp goto done;
3603 a1fbf39a 2019-08-11 stsp if (worktree)
3604 6962eb72 2020-02-20 stsp repo_path =
3605 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3606 a1fbf39a 2019-08-11 stsp else
3607 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3608 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3609 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3610 c156c7a4 2020-12-18 stsp goto done;
3611 c156c7a4 2020-12-18 stsp }
3612 ecb28ae0 2018-07-16 stsp }
3613 ecb28ae0 2018-07-16 stsp
3614 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3615 80ddbec8 2018-04-29 stsp if (error != NULL)
3616 ecb28ae0 2018-07-16 stsp goto done;
3617 80ddbec8 2018-04-29 stsp
3618 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3619 f135c941 2020-02-20 stsp repo, worktree);
3620 f135c941 2020-02-20 stsp if (error)
3621 f135c941 2020-02-20 stsp goto done;
3622 f135c941 2020-02-20 stsp
3623 f135c941 2020-02-20 stsp init_curses();
3624 f135c941 2020-02-20 stsp
3625 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3626 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3627 c02c541e 2019-03-29 stsp if (error)
3628 c02c541e 2019-03-29 stsp goto done;
3629 c02c541e 2019-03-29 stsp
3630 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3631 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3632 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
3633 87670572 2020-12-26 naddy if (error)
3634 87670572 2020-12-26 naddy goto done;
3635 87670572 2020-12-26 naddy }
3636 51a10b52 2020-12-26 stsp
3637 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3638 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3639 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3640 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3641 d8f38dc4 2020-12-05 stsp if (error)
3642 d8f38dc4 2020-12-05 stsp goto done;
3643 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3644 d8f38dc4 2020-12-05 stsp } else {
3645 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3646 d8f38dc4 2020-12-05 stsp if (error == NULL)
3647 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3648 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3649 d8f38dc4 2020-12-05 stsp goto done;
3650 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3651 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3652 d8f38dc4 2020-12-05 stsp if (error)
3653 d8f38dc4 2020-12-05 stsp goto done;
3654 d8f38dc4 2020-12-05 stsp }
3655 8b473291 2019-02-21 stsp
3656 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3657 04cc582a 2018-08-01 stsp if (view == NULL) {
3658 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3659 04cc582a 2018-08-01 stsp goto done;
3660 04cc582a 2018-08-01 stsp }
3661 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3662 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3663 ba4f502b 2018-08-04 stsp if (error)
3664 ba4f502b 2018-08-04 stsp goto done;
3665 2fc00ff4 2019-08-31 stsp if (worktree) {
3666 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3667 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3668 2fc00ff4 2019-08-31 stsp worktree = NULL;
3669 2fc00ff4 2019-08-31 stsp }
3670 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3671 ecb28ae0 2018-07-16 stsp done:
3672 f135c941 2020-02-20 stsp free(in_repo_path);
3673 ecb28ae0 2018-07-16 stsp free(repo_path);
3674 ecb28ae0 2018-07-16 stsp free(cwd);
3675 899d86c2 2018-05-10 stsp free(start_id);
3676 d8f38dc4 2020-12-05 stsp free(label);
3677 d8f38dc4 2020-12-05 stsp if (ref)
3678 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3679 1d0f4054 2021-06-17 stsp if (repo) {
3680 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3681 1d0f4054 2021-06-17 stsp if (error == NULL)
3682 1d0f4054 2021-06-17 stsp error = close_err;
3683 1d0f4054 2021-06-17 stsp }
3684 ec142235 2019-03-07 stsp if (worktree)
3685 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3686 0ae84acc 2022-06-15 tracey if (pack_fds) {
3687 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
3688 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
3689 0ae84acc 2022-06-15 tracey if (error == NULL)
3690 0ae84acc 2022-06-15 tracey error = pack_err;
3691 0ae84acc 2022-06-15 tracey }
3692 51a10b52 2020-12-26 stsp tog_free_refs();
3693 80ddbec8 2018-04-29 stsp return error;
3694 9f7d7167 2018-04-29 stsp }
3695 9f7d7167 2018-04-29 stsp
3696 4ed7e80c 2018-05-20 stsp __dead static void
3697 9f7d7167 2018-04-29 stsp usage_diff(void)
3698 9f7d7167 2018-04-29 stsp {
3699 80ddbec8 2018-04-29 stsp endwin();
3700 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3701 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3702 9f7d7167 2018-04-29 stsp exit(1);
3703 b304db33 2018-05-20 stsp }
3704 b304db33 2018-05-20 stsp
3705 6d17833f 2019-11-08 stsp static int
3706 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3707 41605754 2020-11-12 stsp regmatch_t *regmatch)
3708 6d17833f 2019-11-08 stsp {
3709 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3710 6d17833f 2019-11-08 stsp }
3711 6d17833f 2019-11-08 stsp
3712 336075a4 2022-06-25 op static struct tog_color *
3713 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3714 6d17833f 2019-11-08 stsp {
3715 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3716 6d17833f 2019-11-08 stsp
3717 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3718 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3719 6d17833f 2019-11-08 stsp return tc;
3720 6d17833f 2019-11-08 stsp }
3721 6d17833f 2019-11-08 stsp
3722 6d17833f 2019-11-08 stsp return NULL;
3723 6d17833f 2019-11-08 stsp }
3724 6d17833f 2019-11-08 stsp
3725 4ed7e80c 2018-05-20 stsp static const struct got_error *
3726 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3727 1853e0f4 2022-06-16 stsp WINDOW *window, int skipcol, regmatch_t *regmatch)
3728 41605754 2020-11-12 stsp {
3729 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3730 44a87665 2022-06-16 stsp char *exstr = NULL;
3731 1853e0f4 2022-06-16 stsp wchar_t *wline = NULL;
3732 1853e0f4 2022-06-16 stsp int rme, rms, n, width, scrollx;
3733 1853e0f4 2022-06-16 stsp int width0 = 0, width1 = 0, width2 = 0;
3734 1853e0f4 2022-06-16 stsp char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3735 41605754 2020-11-12 stsp
3736 41605754 2020-11-12 stsp *wtotal = 0;
3737 1853e0f4 2022-06-16 stsp
3738 145b6838 2022-06-16 stsp rms = regmatch->rm_so;
3739 145b6838 2022-06-16 stsp rme = regmatch->rm_eo;
3740 41605754 2020-11-12 stsp
3741 44a87665 2022-06-16 stsp err = expand_tab(&exstr, line);
3742 44a87665 2022-06-16 stsp if (err)
3743 44a87665 2022-06-16 stsp return err;
3744 44a87665 2022-06-16 stsp
3745 1853e0f4 2022-06-16 stsp /* Split the line into 3 segments, according to match offsets. */
3746 44a87665 2022-06-16 stsp seg0 = strndup(exstr, rms);
3747 44a87665 2022-06-16 stsp if (seg0 == NULL) {
3748 44a87665 2022-06-16 stsp err = got_error_from_errno("strndup");
3749 44a87665 2022-06-16 stsp goto done;
3750 44a87665 2022-06-16 stsp }
3751 44a87665 2022-06-16 stsp seg1 = strndup(exstr + rms, rme - rms);
3752 1853e0f4 2022-06-16 stsp if (seg1 == NULL) {
3753 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3754 1853e0f4 2022-06-16 stsp goto done;
3755 1853e0f4 2022-06-16 stsp }
3756 44a87665 2022-06-16 stsp seg2 = strdup(exstr + rme);
3757 95d136ac 2022-06-16 stsp if (seg2 == NULL) {
3758 1853e0f4 2022-06-16 stsp err = got_error_from_errno("strndup");
3759 1853e0f4 2022-06-16 stsp goto done;
3760 1853e0f4 2022-06-16 stsp }
3761 145b6838 2022-06-16 stsp
3762 145b6838 2022-06-16 stsp /* draw up to matched token if we haven't scrolled past it */
3763 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3764 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3765 1853e0f4 2022-06-16 stsp if (err)
3766 1853e0f4 2022-06-16 stsp goto done;
3767 1853e0f4 2022-06-16 stsp n = MAX(width0 - skipcol, 0);
3768 145b6838 2022-06-16 stsp if (n) {
3769 1853e0f4 2022-06-16 stsp free(wline);
3770 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3771 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3772 1853e0f4 2022-06-16 stsp if (err)
3773 1853e0f4 2022-06-16 stsp goto done;
3774 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3775 1853e0f4 2022-06-16 stsp wlimit -= width;
3776 1853e0f4 2022-06-16 stsp *wtotal += width;
3777 41605754 2020-11-12 stsp }
3778 41605754 2020-11-12 stsp
3779 41605754 2020-11-12 stsp if (wlimit > 0) {
3780 1853e0f4 2022-06-16 stsp int i = 0, w = 0;
3781 1853e0f4 2022-06-16 stsp size_t wlen;
3782 1853e0f4 2022-06-16 stsp
3783 1853e0f4 2022-06-16 stsp free(wline);
3784 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3785 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3786 1853e0f4 2022-06-16 stsp if (err)
3787 1853e0f4 2022-06-16 stsp goto done;
3788 1853e0f4 2022-06-16 stsp wlen = wcslen(wline);
3789 1853e0f4 2022-06-16 stsp while (i < wlen) {
3790 1853e0f4 2022-06-16 stsp width = wcwidth(wline[i]);
3791 1853e0f4 2022-06-16 stsp if (width == -1) {
3792 1853e0f4 2022-06-16 stsp /* should not happen, tabs are expanded */
3793 1853e0f4 2022-06-16 stsp err = got_error(GOT_ERR_RANGE);
3794 1853e0f4 2022-06-16 stsp goto done;
3795 1853e0f4 2022-06-16 stsp }
3796 1853e0f4 2022-06-16 stsp if (width0 + w + width > skipcol)
3797 1853e0f4 2022-06-16 stsp break;
3798 61417565 2022-06-20 mark w += width;
3799 1853e0f4 2022-06-16 stsp i++;
3800 41605754 2020-11-12 stsp }
3801 145b6838 2022-06-16 stsp /* draw (visible part of) matched token (if scrolled into it) */
3802 1853e0f4 2022-06-16 stsp if (width1 - w > 0) {
3803 145b6838 2022-06-16 stsp wattron(window, A_STANDOUT);
3804 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[i]);
3805 145b6838 2022-06-16 stsp wattroff(window, A_STANDOUT);
3806 1853e0f4 2022-06-16 stsp wlimit -= (width1 - w);
3807 1853e0f4 2022-06-16 stsp *wtotal += (width1 - w);
3808 41605754 2020-11-12 stsp }
3809 41605754 2020-11-12 stsp }
3810 41605754 2020-11-12 stsp
3811 1853e0f4 2022-06-16 stsp if (wlimit > 0) { /* draw rest of line */
3812 1853e0f4 2022-06-16 stsp free(wline);
3813 1853e0f4 2022-06-16 stsp if (skipcol > width0 + width1) {
3814 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, &scrollx, seg2,
3815 1853e0f4 2022-06-16 stsp skipcol - (width0 + width1), wlimit,
3816 1853e0f4 2022-06-16 stsp col_tab_align, 1);
3817 1853e0f4 2022-06-16 stsp if (err)
3818 1853e0f4 2022-06-16 stsp goto done;
3819 1853e0f4 2022-06-16 stsp waddwstr(window, &wline[scrollx]);
3820 1853e0f4 2022-06-16 stsp } else {
3821 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width2, NULL, seg2, 0,
3822 1853e0f4 2022-06-16 stsp wlimit, col_tab_align, 1);
3823 1853e0f4 2022-06-16 stsp if (err)
3824 1853e0f4 2022-06-16 stsp goto done;
3825 1853e0f4 2022-06-16 stsp waddwstr(window, wline);
3826 1853e0f4 2022-06-16 stsp }
3827 1853e0f4 2022-06-16 stsp *wtotal += width2;
3828 41605754 2020-11-12 stsp }
3829 1853e0f4 2022-06-16 stsp done:
3830 145b6838 2022-06-16 stsp free(wline);
3831 44a87665 2022-06-16 stsp free(exstr);
3832 1853e0f4 2022-06-16 stsp free(seg0);
3833 1853e0f4 2022-06-16 stsp free(seg1);
3834 1853e0f4 2022-06-16 stsp free(seg2);
3835 1853e0f4 2022-06-16 stsp return err;
3836 41605754 2020-11-12 stsp }
3837 41605754 2020-11-12 stsp
3838 41605754 2020-11-12 stsp static const struct got_error *
3839 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3840 26ed57b2 2018-05-19 stsp {
3841 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3842 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3843 61e69b96 2018-05-20 stsp const struct got_error *err;
3844 fe621944 2020-11-10 stsp int nprinted = 0;
3845 b304db33 2018-05-20 stsp char *line;
3846 826082fe 2020-12-10 stsp size_t linesize = 0;
3847 826082fe 2020-12-10 stsp ssize_t linelen;
3848 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3849 61e69b96 2018-05-20 stsp wchar_t *wline;
3850 e0b650dd 2018-05-20 stsp int width;
3851 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3852 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3853 fe621944 2020-11-10 stsp off_t line_offset;
3854 26ed57b2 2018-05-19 stsp
3855 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3856 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3857 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3858 fe621944 2020-11-10 stsp
3859 f7d12f7e 2018-08-01 stsp werase(view->window);
3860 a3404814 2018-09-02 stsp
3861 a3404814 2018-09-02 stsp if (header) {
3862 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3863 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3864 135a2da0 2020-11-11 stsp header) == -1)
3865 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3866 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3867 ccda2f4d 2022-06-16 stsp 0, 0);
3868 135a2da0 2020-11-11 stsp free(line);
3869 135a2da0 2020-11-11 stsp if (err)
3870 a3404814 2018-09-02 stsp return err;
3871 a3404814 2018-09-02 stsp
3872 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3873 a3404814 2018-09-02 stsp wstandout(view->window);
3874 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3875 e54cc94a 2020-11-11 stsp free(wline);
3876 e54cc94a 2020-11-11 stsp wline = NULL;
3877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3878 a3404814 2018-09-02 stsp wstandend(view->window);
3879 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3880 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3881 26ed57b2 2018-05-19 stsp
3882 a3404814 2018-09-02 stsp if (max_lines <= 1)
3883 a3404814 2018-09-02 stsp return NULL;
3884 a3404814 2018-09-02 stsp max_lines--;
3885 a3404814 2018-09-02 stsp }
3886 a3404814 2018-09-02 stsp
3887 89f1a395 2020-12-01 naddy s->eof = 0;
3888 145b6838 2022-06-16 stsp view->maxx = 0;
3889 826082fe 2020-12-10 stsp line = NULL;
3890 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3891 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3892 826082fe 2020-12-10 stsp if (linelen == -1) {
3893 826082fe 2020-12-10 stsp if (feof(s->f)) {
3894 826082fe 2020-12-10 stsp s->eof = 1;
3895 826082fe 2020-12-10 stsp break;
3896 826082fe 2020-12-10 stsp }
3897 826082fe 2020-12-10 stsp free(line);
3898 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3899 61e69b96 2018-05-20 stsp }
3900 145b6838 2022-06-16 stsp
3901 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
3902 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3903 1853e0f4 2022-06-16 stsp view->x ? 1 : 0);
3904 1853e0f4 2022-06-16 stsp if (err) {
3905 1853e0f4 2022-06-16 stsp free(line);
3906 1853e0f4 2022-06-16 stsp return err;
3907 1853e0f4 2022-06-16 stsp }
3908 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
3909 1853e0f4 2022-06-16 stsp free(wline);
3910 1853e0f4 2022-06-16 stsp wline = NULL;
3911 1853e0f4 2022-06-16 stsp
3912 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3913 f26dddb7 2019-11-08 stsp if (tc)
3914 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3915 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3916 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3917 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3918 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3919 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
3920 41605754 2020-11-12 stsp if (err) {
3921 41605754 2020-11-12 stsp free(line);
3922 41605754 2020-11-12 stsp return err;
3923 41605754 2020-11-12 stsp }
3924 41605754 2020-11-12 stsp } else {
3925 969c159c 2022-06-16 stsp int skip;
3926 969c159c 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
3927 969c159c 2022-06-16 stsp view->x, view->ncols, 0, view->x ? 1 : 0);
3928 969c159c 2022-06-16 stsp if (err) {
3929 969c159c 2022-06-16 stsp free(line);
3930 969c159c 2022-06-16 stsp return err;
3931 969c159c 2022-06-16 stsp }
3932 969c159c 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
3933 969c159c 2022-06-16 stsp free(wline);
3934 41605754 2020-11-12 stsp wline = NULL;
3935 41605754 2020-11-12 stsp }
3936 f26dddb7 2019-11-08 stsp if (tc)
3937 6d17833f 2019-11-08 stsp wattr_off(view->window,
3938 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3939 969c159c 2022-06-16 stsp if (width <= view->ncols - 1)
3940 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3941 fe621944 2020-11-10 stsp nprinted++;
3942 826082fe 2020-12-10 stsp }
3943 826082fe 2020-12-10 stsp free(line);
3944 fe621944 2020-11-10 stsp if (nprinted >= 1)
3945 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3946 89f1a395 2020-12-01 naddy (nprinted - 1);
3947 fe621944 2020-11-10 stsp else
3948 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3949 26ed57b2 2018-05-19 stsp
3950 9b058f45 2022-06-30 mark view_border(view);
3951 c3e9aa98 2019-05-13 jcs
3952 89f1a395 2020-12-01 naddy if (s->eof) {
3953 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3954 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3955 c3e9aa98 2019-05-13 jcs nprinted++;
3956 c3e9aa98 2019-05-13 jcs }
3957 c3e9aa98 2019-05-13 jcs
3958 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3959 ccda2f4d 2022-06-16 stsp view->ncols, 0, 0);
3960 c3e9aa98 2019-05-13 jcs if (err) {
3961 c3e9aa98 2019-05-13 jcs return err;
3962 c3e9aa98 2019-05-13 jcs }
3963 26ed57b2 2018-05-19 stsp
3964 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3965 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3966 e54cc94a 2020-11-11 stsp free(wline);
3967 e54cc94a 2020-11-11 stsp wline = NULL;
3968 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3969 c3e9aa98 2019-05-13 jcs }
3970 c3e9aa98 2019-05-13 jcs
3971 26ed57b2 2018-05-19 stsp return NULL;
3972 abd2672a 2018-12-23 stsp }
3973 abd2672a 2018-12-23 stsp
3974 abd2672a 2018-12-23 stsp static char *
3975 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3976 abd2672a 2018-12-23 stsp {
3977 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3978 09867e48 2019-08-13 stsp char *p, *s;
3979 09867e48 2019-08-13 stsp
3980 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3981 09867e48 2019-08-13 stsp if (tm == NULL)
3982 09867e48 2019-08-13 stsp return NULL;
3983 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3984 09867e48 2019-08-13 stsp if (s == NULL)
3985 09867e48 2019-08-13 stsp return NULL;
3986 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3987 abd2672a 2018-12-23 stsp if (p)
3988 abd2672a 2018-12-23 stsp *p = '\0';
3989 abd2672a 2018-12-23 stsp return s;
3990 9f7d7167 2018-04-29 stsp }
3991 9f7d7167 2018-04-29 stsp
3992 4ed7e80c 2018-05-20 stsp static const struct got_error *
3993 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3994 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3995 0208f208 2020-05-05 stsp {
3996 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3997 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3998 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3999 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4000 0208f208 2020-05-05 stsp
4001 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4002 0208f208 2020-05-05 stsp if (qid != NULL) {
4003 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4004 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4005 d7b5a0e8 2022-04-20 stsp &qid->id);
4006 0208f208 2020-05-05 stsp if (err)
4007 0208f208 2020-05-05 stsp return err;
4008 0208f208 2020-05-05 stsp
4009 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4010 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4011 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4012 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4013 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4014 aa8b5dd0 2021-08-01 stsp }
4015 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4016 0208f208 2020-05-05 stsp
4017 0208f208 2020-05-05 stsp }
4018 0208f208 2020-05-05 stsp
4019 0208f208 2020-05-05 stsp if (tree_id1) {
4020 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4021 0208f208 2020-05-05 stsp if (err)
4022 0208f208 2020-05-05 stsp goto done;
4023 0208f208 2020-05-05 stsp }
4024 0208f208 2020-05-05 stsp
4025 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4026 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4027 0208f208 2020-05-05 stsp if (err)
4028 0208f208 2020-05-05 stsp goto done;
4029 0208f208 2020-05-05 stsp
4030 f9d37699 2022-06-28 stsp err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4031 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4032 0208f208 2020-05-05 stsp done:
4033 0208f208 2020-05-05 stsp if (tree1)
4034 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4035 0208f208 2020-05-05 stsp if (tree2)
4036 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4037 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4038 0208f208 2020-05-05 stsp return err;
4039 0208f208 2020-05-05 stsp }
4040 0208f208 2020-05-05 stsp
4041 0208f208 2020-05-05 stsp static const struct got_error *
4042 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4043 abd2672a 2018-12-23 stsp {
4044 fe621944 2020-11-10 stsp off_t *p;
4045 fe621944 2020-11-10 stsp
4046 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4047 fe621944 2020-11-10 stsp if (p == NULL)
4048 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4049 fe621944 2020-11-10 stsp *line_offsets = p;
4050 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4051 fe621944 2020-11-10 stsp (*nlines)++;
4052 fe621944 2020-11-10 stsp return NULL;
4053 fe621944 2020-11-10 stsp }
4054 fe621944 2020-11-10 stsp
4055 fe621944 2020-11-10 stsp static const struct got_error *
4056 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4057 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4058 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4059 fe621944 2020-11-10 stsp {
4060 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4061 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4062 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4063 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4064 45d799e2 2018-12-23 stsp time_t committer_time;
4065 45d799e2 2018-12-23 stsp const char *author, *committer;
4066 8b473291 2019-02-21 stsp char *refs_str = NULL;
4067 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4068 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4069 fe621944 2020-11-10 stsp off_t outoff = 0;
4070 fe621944 2020-11-10 stsp int n;
4071 abd2672a 2018-12-23 stsp
4072 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4073 0208f208 2020-05-05 stsp
4074 8b473291 2019-02-21 stsp if (refs) {
4075 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4076 8b473291 2019-02-21 stsp if (err)
4077 8b473291 2019-02-21 stsp return err;
4078 8b473291 2019-02-21 stsp }
4079 8b473291 2019-02-21 stsp
4080 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4081 abd2672a 2018-12-23 stsp if (err)
4082 abd2672a 2018-12-23 stsp return err;
4083 abd2672a 2018-12-23 stsp
4084 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4085 15a94983 2018-12-23 stsp if (err) {
4086 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4087 15a94983 2018-12-23 stsp goto done;
4088 15a94983 2018-12-23 stsp }
4089 abd2672a 2018-12-23 stsp
4090 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4091 fe621944 2020-11-10 stsp if (err)
4092 fe621944 2020-11-10 stsp goto done;
4093 fe621944 2020-11-10 stsp
4094 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4095 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4096 fe621944 2020-11-10 stsp if (n < 0) {
4097 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4098 abd2672a 2018-12-23 stsp goto done;
4099 abd2672a 2018-12-23 stsp }
4100 fe621944 2020-11-10 stsp outoff += n;
4101 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4102 fe621944 2020-11-10 stsp if (err)
4103 fe621944 2020-11-10 stsp goto done;
4104 fe621944 2020-11-10 stsp
4105 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4106 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4107 fe621944 2020-11-10 stsp if (n < 0) {
4108 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4109 abd2672a 2018-12-23 stsp goto done;
4110 abd2672a 2018-12-23 stsp }
4111 fe621944 2020-11-10 stsp outoff += n;
4112 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4113 fe621944 2020-11-10 stsp if (err)
4114 fe621944 2020-11-10 stsp goto done;
4115 fe621944 2020-11-10 stsp
4116 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4117 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4118 fe621944 2020-11-10 stsp if (datestr) {
4119 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4120 fe621944 2020-11-10 stsp if (n < 0) {
4121 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4122 fe621944 2020-11-10 stsp goto done;
4123 fe621944 2020-11-10 stsp }
4124 fe621944 2020-11-10 stsp outoff += n;
4125 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4126 fe621944 2020-11-10 stsp if (err)
4127 fe621944 2020-11-10 stsp goto done;
4128 abd2672a 2018-12-23 stsp }
4129 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4130 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4131 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4132 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4133 fe621944 2020-11-10 stsp if (n < 0) {
4134 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4135 fe621944 2020-11-10 stsp goto done;
4136 fe621944 2020-11-10 stsp }
4137 fe621944 2020-11-10 stsp outoff += n;
4138 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4139 fe621944 2020-11-10 stsp if (err)
4140 fe621944 2020-11-10 stsp goto done;
4141 abd2672a 2018-12-23 stsp }
4142 9f98ca05 2021-09-24 stsp if (got_object_commit_get_nparents(commit) > 1) {
4143 9f98ca05 2021-09-24 stsp const struct got_object_id_queue *parent_ids;
4144 9f98ca05 2021-09-24 stsp struct got_object_qid *qid;
4145 9f98ca05 2021-09-24 stsp int pn = 1;
4146 9f98ca05 2021-09-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4147 9f98ca05 2021-09-24 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
4148 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &qid->id);
4149 9f98ca05 2021-09-24 stsp if (err)
4150 9f98ca05 2021-09-24 stsp goto done;
4151 9f98ca05 2021-09-24 stsp n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4152 9f98ca05 2021-09-24 stsp if (n < 0) {
4153 9f98ca05 2021-09-24 stsp err = got_error_from_errno("fprintf");
4154 9f98ca05 2021-09-24 stsp goto done;
4155 9f98ca05 2021-09-24 stsp }
4156 9f98ca05 2021-09-24 stsp outoff += n;
4157 9f98ca05 2021-09-24 stsp err = add_line_offset(line_offsets, nlines, outoff);
4158 9f98ca05 2021-09-24 stsp if (err)
4159 9f98ca05 2021-09-24 stsp goto done;
4160 9f98ca05 2021-09-24 stsp free(id_str);
4161 9f98ca05 2021-09-24 stsp id_str = NULL;
4162 9f98ca05 2021-09-24 stsp }
4163 9f98ca05 2021-09-24 stsp }
4164 9f98ca05 2021-09-24 stsp
4165 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4166 5943eee2 2019-08-13 stsp if (err)
4167 5943eee2 2019-08-13 stsp goto done;
4168 fe621944 2020-11-10 stsp s = logmsg;
4169 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4170 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4171 fe621944 2020-11-10 stsp if (n < 0) {
4172 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4173 fe621944 2020-11-10 stsp goto done;
4174 fe621944 2020-11-10 stsp }
4175 fe621944 2020-11-10 stsp outoff += n;
4176 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4177 fe621944 2020-11-10 stsp if (err)
4178 fe621944 2020-11-10 stsp goto done;
4179 abd2672a 2018-12-23 stsp }
4180 fe621944 2020-11-10 stsp
4181 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4182 0208f208 2020-05-05 stsp if (err)
4183 0208f208 2020-05-05 stsp goto done;
4184 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4185 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4186 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4187 fe621944 2020-11-10 stsp if (n < 0) {
4188 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4189 fe621944 2020-11-10 stsp goto done;
4190 fe621944 2020-11-10 stsp }
4191 fe621944 2020-11-10 stsp outoff += n;
4192 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4193 fe621944 2020-11-10 stsp if (err)
4194 fe621944 2020-11-10 stsp goto done;
4195 0208f208 2020-05-05 stsp free((char *)pe->path);
4196 0208f208 2020-05-05 stsp free(pe->data);
4197 0208f208 2020-05-05 stsp }
4198 fe621944 2020-11-10 stsp
4199 0208f208 2020-05-05 stsp fputc('\n', outfile);
4200 fe621944 2020-11-10 stsp outoff++;
4201 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4202 abd2672a 2018-12-23 stsp done:
4203 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4204 abd2672a 2018-12-23 stsp free(id_str);
4205 5943eee2 2019-08-13 stsp free(logmsg);
4206 8b473291 2019-02-21 stsp free(refs_str);
4207 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4208 7510f233 2020-08-09 stsp if (err) {
4209 ae6a6978 2020-08-09 stsp free(*line_offsets);
4210 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4211 ae6a6978 2020-08-09 stsp *nlines = 0;
4212 7510f233 2020-08-09 stsp }
4213 fe621944 2020-11-10 stsp return err;
4214 abd2672a 2018-12-23 stsp }
4215 abd2672a 2018-12-23 stsp
4216 abd2672a 2018-12-23 stsp static const struct got_error *
4217 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4218 26ed57b2 2018-05-19 stsp {
4219 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4220 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4221 15a94983 2018-12-23 stsp int obj_type;
4222 fe621944 2020-11-10 stsp
4223 fe621944 2020-11-10 stsp free(s->line_offsets);
4224 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4225 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4226 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4227 fe621944 2020-11-10 stsp s->nlines = 0;
4228 26ed57b2 2018-05-19 stsp
4229 511a516b 2018-05-19 stsp f = got_opentemp();
4230 48ae06ee 2018-10-18 stsp if (f == NULL) {
4231 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4232 48ae06ee 2018-10-18 stsp goto done;
4233 48ae06ee 2018-10-18 stsp }
4234 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4235 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4236 fb43ecf1 2019-02-11 stsp goto done;
4237 fb43ecf1 2019-02-11 stsp }
4238 48ae06ee 2018-10-18 stsp s->f = f;
4239 26ed57b2 2018-05-19 stsp
4240 15a94983 2018-12-23 stsp if (s->id1)
4241 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4242 15a94983 2018-12-23 stsp else
4243 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4244 15a94983 2018-12-23 stsp if (err)
4245 15a94983 2018-12-23 stsp goto done;
4246 15a94983 2018-12-23 stsp
4247 15a94983 2018-12-23 stsp switch (obj_type) {
4248 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4249 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4250 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4251 917d79a7 2022-07-01 stsp s->label1, s->label2, tog_diff_algo, s->diff_context,
4252 f9d37699 2022-06-28 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4253 26ed57b2 2018-05-19 stsp break;
4254 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4255 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4256 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4257 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4258 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4259 26ed57b2 2018-05-19 stsp break;
4260 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4261 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4262 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4263 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4264 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4265 abd2672a 2018-12-23 stsp
4266 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4267 abd2672a 2018-12-23 stsp if (err)
4268 3ffacbe1 2020-02-02 tracey goto done;
4269 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4270 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4271 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4272 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4273 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4274 f44b1f58 2020-02-02 tracey if (err)
4275 f44b1f58 2020-02-02 tracey goto done;
4276 f44b1f58 2020-02-02 tracey } else {
4277 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4278 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4279 d7b5a0e8 2022-04-20 stsp if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4280 fe621944 2020-11-10 stsp err = write_commit_info(
4281 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4282 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4283 f44b1f58 2020-02-02 tracey if (err)
4284 f44b1f58 2020-02-02 tracey goto done;
4285 f5404e4e 2020-02-02 tracey break;
4286 15a087fe 2019-02-21 stsp }
4287 abd2672a 2018-12-23 stsp }
4288 abd2672a 2018-12-23 stsp }
4289 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4290 abd2672a 2018-12-23 stsp
4291 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4292 f9d37699 2022-06-28 stsp s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4293 917d79a7 2022-07-01 stsp tog_diff_algo, s->diff_context, s->ignore_whitespace,
4294 4b752015 2022-06-30 stsp s->force_text_diff, s->repo, s->f);
4295 26ed57b2 2018-05-19 stsp break;
4296 abd2672a 2018-12-23 stsp }
4297 26ed57b2 2018-05-19 stsp default:
4298 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4299 48ae06ee 2018-10-18 stsp break;
4300 26ed57b2 2018-05-19 stsp }
4301 f44b1f58 2020-02-02 tracey if (err)
4302 f44b1f58 2020-02-02 tracey goto done;
4303 48ae06ee 2018-10-18 stsp done:
4304 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4305 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4306 48ae06ee 2018-10-18 stsp return err;
4307 48ae06ee 2018-10-18 stsp }
4308 26ed57b2 2018-05-19 stsp
4309 f5215bb9 2019-02-22 stsp static void
4310 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4311 f5215bb9 2019-02-22 stsp {
4312 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4313 f5215bb9 2019-02-22 stsp update_panels();
4314 f5215bb9 2019-02-22 stsp doupdate();
4315 f44b1f58 2020-02-02 tracey }
4316 f44b1f58 2020-02-02 tracey
4317 f44b1f58 2020-02-02 tracey static const struct got_error *
4318 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4319 f44b1f58 2020-02-02 tracey {
4320 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4321 f44b1f58 2020-02-02 tracey
4322 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4323 f44b1f58 2020-02-02 tracey return NULL;
4324 f44b1f58 2020-02-02 tracey }
4325 f44b1f58 2020-02-02 tracey
4326 f44b1f58 2020-02-02 tracey static const struct got_error *
4327 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4328 f44b1f58 2020-02-02 tracey {
4329 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4330 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
4331 f44b1f58 2020-02-02 tracey int lineno;
4332 cb713507 2022-06-16 stsp char *line = NULL;
4333 826082fe 2020-12-10 stsp size_t linesize = 0;
4334 826082fe 2020-12-10 stsp ssize_t linelen;
4335 f44b1f58 2020-02-02 tracey
4336 f44b1f58 2020-02-02 tracey if (!view->searching) {
4337 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4338 f44b1f58 2020-02-02 tracey return NULL;
4339 f44b1f58 2020-02-02 tracey }
4340 f44b1f58 2020-02-02 tracey
4341 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4342 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4343 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4344 f44b1f58 2020-02-02 tracey else
4345 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4346 487cd7d2 2021-12-17 stsp } else
4347 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line;
4348 f44b1f58 2020-02-02 tracey
4349 f44b1f58 2020-02-02 tracey while (1) {
4350 f44b1f58 2020-02-02 tracey off_t offset;
4351 f44b1f58 2020-02-02 tracey
4352 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4353 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4354 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4355 f44b1f58 2020-02-02 tracey break;
4356 f44b1f58 2020-02-02 tracey }
4357 f44b1f58 2020-02-02 tracey
4358 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4359 f44b1f58 2020-02-02 tracey lineno = 1;
4360 f44b1f58 2020-02-02 tracey else
4361 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4362 f44b1f58 2020-02-02 tracey }
4363 f44b1f58 2020-02-02 tracey
4364 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4365 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4366 f44b1f58 2020-02-02 tracey free(line);
4367 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4368 f44b1f58 2020-02-02 tracey }
4369 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4370 cb713507 2022-06-16 stsp if (linelen != -1) {
4371 cb713507 2022-06-16 stsp char *exstr;
4372 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
4373 cb713507 2022-06-16 stsp if (err)
4374 cb713507 2022-06-16 stsp break;
4375 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
4376 cb713507 2022-06-16 stsp &view->regmatch)) {
4377 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4378 cb713507 2022-06-16 stsp s->matched_line = lineno;
4379 cb713507 2022-06-16 stsp free(exstr);
4380 cb713507 2022-06-16 stsp break;
4381 cb713507 2022-06-16 stsp }
4382 cb713507 2022-06-16 stsp free(exstr);
4383 f44b1f58 2020-02-02 tracey }
4384 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4385 f44b1f58 2020-02-02 tracey lineno++;
4386 f44b1f58 2020-02-02 tracey else
4387 f44b1f58 2020-02-02 tracey lineno--;
4388 f44b1f58 2020-02-02 tracey }
4389 826082fe 2020-12-10 stsp free(line);
4390 f44b1f58 2020-02-02 tracey
4391 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4392 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4393 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4394 f44b1f58 2020-02-02 tracey }
4395 f44b1f58 2020-02-02 tracey
4396 145b6838 2022-06-16 stsp return err;
4397 f5215bb9 2019-02-22 stsp }
4398 f5215bb9 2019-02-22 stsp
4399 48ae06ee 2018-10-18 stsp static const struct got_error *
4400 b72706c3 2022-06-01 stsp close_diff_view(struct tog_view *view)
4401 b72706c3 2022-06-01 stsp {
4402 b72706c3 2022-06-01 stsp const struct got_error *err = NULL;
4403 b72706c3 2022-06-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4404 b72706c3 2022-06-01 stsp
4405 b72706c3 2022-06-01 stsp free(s->id1);
4406 b72706c3 2022-06-01 stsp s->id1 = NULL;
4407 b72706c3 2022-06-01 stsp free(s->id2);
4408 b72706c3 2022-06-01 stsp s->id2 = NULL;
4409 b72706c3 2022-06-01 stsp if (s->f && fclose(s->f) == EOF)
4410 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4411 b72706c3 2022-06-01 stsp s->f = NULL;
4412 f9d37699 2022-06-28 stsp if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4413 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4414 b72706c3 2022-06-01 stsp s->f1 = NULL;
4415 f9d37699 2022-06-28 stsp if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4416 b72706c3 2022-06-01 stsp err = got_error_from_errno("fclose");
4417 b72706c3 2022-06-01 stsp s->f2 = NULL;
4418 f9d37699 2022-06-28 stsp if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4419 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4420 f9d37699 2022-06-28 stsp s->fd1 = -1;
4421 f9d37699 2022-06-28 stsp if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4422 f9d37699 2022-06-28 stsp err = got_error_from_errno("close");
4423 f9d37699 2022-06-28 stsp s->fd2 = -1;
4424 b72706c3 2022-06-01 stsp free_colors(&s->colors);
4425 b72706c3 2022-06-01 stsp free(s->line_offsets);
4426 b72706c3 2022-06-01 stsp s->line_offsets = NULL;
4427 b72706c3 2022-06-01 stsp s->nlines = 0;
4428 b72706c3 2022-06-01 stsp return err;
4429 b72706c3 2022-06-01 stsp }
4430 b72706c3 2022-06-01 stsp
4431 b72706c3 2022-06-01 stsp static const struct got_error *
4432 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4433 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4434 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4435 c0f61fa4 2022-07-11 mark struct tog_view *parent_view, struct got_repository *repo)
4436 48ae06ee 2018-10-18 stsp {
4437 48ae06ee 2018-10-18 stsp const struct got_error *err;
4438 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4439 5dc9f4bc 2018-08-04 stsp
4440 b72706c3 2022-06-01 stsp memset(s, 0, sizeof(*s));
4441 f9d37699 2022-06-28 stsp s->fd1 = -1;
4442 f9d37699 2022-06-28 stsp s->fd2 = -1;
4443 b72706c3 2022-06-01 stsp
4444 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4445 15a94983 2018-12-23 stsp int type1, type2;
4446 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4447 15a94983 2018-12-23 stsp if (err)
4448 15a94983 2018-12-23 stsp return err;
4449 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4450 15a94983 2018-12-23 stsp if (err)
4451 15a94983 2018-12-23 stsp return err;
4452 15a94983 2018-12-23 stsp
4453 15a94983 2018-12-23 stsp if (type1 != type2)
4454 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4455 15a94983 2018-12-23 stsp }
4456 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4457 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4458 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4459 f44b1f58 2020-02-02 tracey s->repo = repo;
4460 f44b1f58 2020-02-02 tracey s->id1 = id1;
4461 f44b1f58 2020-02-02 tracey s->id2 = id2;
4462 3dbaef42 2020-11-24 stsp s->label1 = label1;
4463 3dbaef42 2020-11-24 stsp s->label2 = label2;
4464 48ae06ee 2018-10-18 stsp
4465 15a94983 2018-12-23 stsp if (id1) {
4466 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4467 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4468 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4469 48ae06ee 2018-10-18 stsp } else
4470 5465d566 2020-02-01 tracey s->id1 = NULL;
4471 48ae06ee 2018-10-18 stsp
4472 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4473 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4474 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_object_id_dup");
4475 b72706c3 2022-06-01 stsp goto done;
4476 48ae06ee 2018-10-18 stsp }
4477 b72706c3 2022-06-01 stsp
4478 a00719e9 2022-06-17 stsp s->f1 = got_opentemp();
4479 a00719e9 2022-06-17 stsp if (s->f1 == NULL) {
4480 a00719e9 2022-06-17 stsp err = got_error_from_errno("got_opentemp");
4481 a00719e9 2022-06-17 stsp goto done;
4482 a00719e9 2022-06-17 stsp }
4483 a00719e9 2022-06-17 stsp
4484 b72706c3 2022-06-01 stsp s->f2 = got_opentemp();
4485 b72706c3 2022-06-01 stsp if (s->f2 == NULL) {
4486 b72706c3 2022-06-01 stsp err = got_error_from_errno("got_opentemp");
4487 b72706c3 2022-06-01 stsp goto done;
4488 b72706c3 2022-06-01 stsp }
4489 b72706c3 2022-06-01 stsp
4490 f9d37699 2022-06-28 stsp s->fd1 = got_opentempfd();
4491 f9d37699 2022-06-28 stsp if (s->fd1 == -1) {
4492 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4493 f9d37699 2022-06-28 stsp goto done;
4494 f9d37699 2022-06-28 stsp }
4495 f9d37699 2022-06-28 stsp
4496 f9d37699 2022-06-28 stsp s->fd2 = got_opentempfd();
4497 f9d37699 2022-06-28 stsp if (s->fd2 == -1) {
4498 f9d37699 2022-06-28 stsp err = got_error_from_errno("got_opentempfd");
4499 f9d37699 2022-06-28 stsp goto done;
4500 f9d37699 2022-06-28 stsp }
4501 f9d37699 2022-06-28 stsp
4502 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4503 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4504 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4505 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4506 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4507 c0f61fa4 2022-07-11 mark s->parent_view = parent_view;
4508 5465d566 2020-02-01 tracey s->repo = repo;
4509 6d17833f 2019-11-08 stsp
4510 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4511 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4512 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4513 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4514 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4515 6d17833f 2019-11-08 stsp if (err)
4516 b72706c3 2022-06-01 stsp goto done;
4517 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4518 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4519 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4520 b72706c3 2022-06-01 stsp if (err)
4521 b72706c3 2022-06-01 stsp goto done;
4522 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4523 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4524 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4525 b72706c3 2022-06-01 stsp if (err)
4526 b72706c3 2022-06-01 stsp goto done;
4527 6d17833f 2019-11-08 stsp
4528 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4529 8469d821 2022-06-25 stsp "^(commit [0-9a-f]|parent [0-9]|"
4530 8469d821 2022-06-25 stsp "(blob|file|tree|commit) [-+] |"
4531 9f98ca05 2021-09-24 stsp "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4532 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4533 b72706c3 2022-06-01 stsp if (err)
4534 b72706c3 2022-06-01 stsp goto done;
4535 11b20872 2019-11-08 stsp
4536 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4537 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4538 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4539 b72706c3 2022-06-01 stsp if (err)
4540 b72706c3 2022-06-01 stsp goto done;
4541 11b20872 2019-11-08 stsp
4542 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4543 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4544 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4545 b72706c3 2022-06-01 stsp if (err)
4546 b72706c3 2022-06-01 stsp goto done;
4547 6d17833f 2019-11-08 stsp }
4548 5dc9f4bc 2018-08-04 stsp
4549 c0f61fa4 2022-07-11 mark if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4550 c0f61fa4 2022-07-11 mark view_is_splitscreen(view))
4551 c0f61fa4 2022-07-11 mark show_log_view(parent_view); /* draw border */
4552 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4553 f5215bb9 2019-02-22 stsp
4554 5465d566 2020-02-01 tracey err = create_diff(s);
4555 48ae06ee 2018-10-18 stsp
4556 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4557 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4558 917d79a7 2022-07-01 stsp view->reset = reset_diff_view;
4559 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4560 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4561 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4562 b72706c3 2022-06-01 stsp done:
4563 b72706c3 2022-06-01 stsp if (err)
4564 b72706c3 2022-06-01 stsp close_diff_view(view);
4565 e5a0f69f 2018-08-18 stsp return err;
4566 5dc9f4bc 2018-08-04 stsp }
4567 5dc9f4bc 2018-08-04 stsp
4568 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4569 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4570 5dc9f4bc 2018-08-04 stsp {
4571 a3404814 2018-09-02 stsp const struct got_error *err;
4572 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4573 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4574 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4575 a3404814 2018-09-02 stsp
4576 a3404814 2018-09-02 stsp if (s->id1) {
4577 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4578 a3404814 2018-09-02 stsp if (err)
4579 a3404814 2018-09-02 stsp return err;
4580 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4581 3dbaef42 2020-11-24 stsp } else
4582 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4583 3dbaef42 2020-11-24 stsp
4584 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4585 a3404814 2018-09-02 stsp if (err)
4586 a3404814 2018-09-02 stsp return err;
4587 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4588 26ed57b2 2018-05-19 stsp
4589 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4590 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4591 a3404814 2018-09-02 stsp free(id_str1);
4592 a3404814 2018-09-02 stsp free(id_str2);
4593 a3404814 2018-09-02 stsp return err;
4594 a3404814 2018-09-02 stsp }
4595 a3404814 2018-09-02 stsp free(id_str1);
4596 a3404814 2018-09-02 stsp free(id_str2);
4597 a3404814 2018-09-02 stsp
4598 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4599 267bb3b8 2021-08-01 stsp free(header);
4600 267bb3b8 2021-08-01 stsp return err;
4601 15a087fe 2019-02-21 stsp }
4602 15a087fe 2019-02-21 stsp
4603 15a087fe 2019-02-21 stsp static const struct got_error *
4604 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4605 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4606 15a087fe 2019-02-21 stsp {
4607 d7a04538 2019-02-21 stsp const struct got_error *err;
4608 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4609 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4610 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4611 15a087fe 2019-02-21 stsp
4612 15a087fe 2019-02-21 stsp free(s->id2);
4613 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4614 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4615 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4616 15a087fe 2019-02-21 stsp
4617 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4618 d7a04538 2019-02-21 stsp if (err)
4619 d7a04538 2019-02-21 stsp return err;
4620 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4621 15a087fe 2019-02-21 stsp free(s->id1);
4622 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4623 d7b5a0e8 2022-04-20 stsp s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4624 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4625 15a087fe 2019-02-21 stsp return NULL;
4626 0cf4efb1 2018-09-29 stsp }
4627 0cf4efb1 2018-09-29 stsp
4628 0cf4efb1 2018-09-29 stsp static const struct got_error *
4629 917d79a7 2022-07-01 stsp reset_diff_view(struct tog_view *view)
4630 917d79a7 2022-07-01 stsp {
4631 917d79a7 2022-07-01 stsp struct tog_diff_view_state *s = &view->state.diff;
4632 917d79a7 2022-07-01 stsp
4633 917d79a7 2022-07-01 stsp view->count = 0;
4634 917d79a7 2022-07-01 stsp wclear(view->window);
4635 917d79a7 2022-07-01 stsp s->first_displayed_line = 1;
4636 917d79a7 2022-07-01 stsp s->last_displayed_line = view->nlines;
4637 917d79a7 2022-07-01 stsp s->matched_line = 0;
4638 917d79a7 2022-07-01 stsp diff_view_indicate_progress(view);
4639 917d79a7 2022-07-01 stsp return create_diff(s);
4640 917d79a7 2022-07-01 stsp }
4641 917d79a7 2022-07-01 stsp
4642 c0f61fa4 2022-07-11 mark static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4643 c0f61fa4 2022-07-11 mark int, int, int);
4644 c0f61fa4 2022-07-11 mark static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4645 c0f61fa4 2022-07-11 mark int, int);
4646 c0f61fa4 2022-07-11 mark
4647 917d79a7 2022-07-01 stsp static const struct got_error *
4648 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4649 e5a0f69f 2018-08-18 stsp {
4650 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4651 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4652 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4653 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4654 826082fe 2020-12-10 stsp char *line = NULL;
4655 826082fe 2020-12-10 stsp size_t linesize = 0;
4656 826082fe 2020-12-10 stsp ssize_t linelen;
4657 c0f61fa4 2022-07-11 mark int i, nscroll = view->nlines - 1, up = 0;
4658 e5a0f69f 2018-08-18 stsp
4659 e5a0f69f 2018-08-18 stsp switch (ch) {
4660 145b6838 2022-06-16 stsp case '0':
4661 145b6838 2022-06-16 stsp view->x = 0;
4662 145b6838 2022-06-16 stsp break;
4663 145b6838 2022-06-16 stsp case '$':
4664 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
4665 640cd7ff 2022-06-22 mark view->count = 0;
4666 145b6838 2022-06-16 stsp break;
4667 145b6838 2022-06-16 stsp case KEY_RIGHT:
4668 145b6838 2022-06-16 stsp case 'l':
4669 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
4670 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
4671 640cd7ff 2022-06-22 mark else
4672 640cd7ff 2022-06-22 mark view->count = 0;
4673 145b6838 2022-06-16 stsp break;
4674 145b6838 2022-06-16 stsp case KEY_LEFT:
4675 145b6838 2022-06-16 stsp case 'h':
4676 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
4677 640cd7ff 2022-06-22 mark if (view->x <= 0)
4678 640cd7ff 2022-06-22 mark view->count = 0;
4679 145b6838 2022-06-16 stsp break;
4680 64453f7e 2020-11-21 stsp case 'a':
4681 3dbaef42 2020-11-24 stsp case 'w':
4682 3dbaef42 2020-11-24 stsp if (ch == 'a')
4683 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4684 3dbaef42 2020-11-24 stsp if (ch == 'w')
4685 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4686 917d79a7 2022-07-01 stsp err = reset_diff_view(view);
4687 912a3f79 2021-08-30 j break;
4688 912a3f79 2021-08-30 j case 'g':
4689 912a3f79 2021-08-30 j case KEY_HOME:
4690 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4691 640cd7ff 2022-06-22 mark view->count = 0;
4692 64453f7e 2020-11-21 stsp break;
4693 912a3f79 2021-08-30 j case 'G':
4694 912a3f79 2021-08-30 j case KEY_END:
4695 640cd7ff 2022-06-22 mark view->count = 0;
4696 912a3f79 2021-08-30 j if (s->eof)
4697 912a3f79 2021-08-30 j break;
4698 912a3f79 2021-08-30 j
4699 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4700 912a3f79 2021-08-30 j s->eof = 1;
4701 912a3f79 2021-08-30 j break;
4702 1e37a5c2 2019-05-12 jcs case 'k':
4703 1e37a5c2 2019-05-12 jcs case KEY_UP:
4704 02ffd0d5 2021-10-17 stsp case CTRL('p'):
4705 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4706 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4707 640cd7ff 2022-06-22 mark else
4708 640cd7ff 2022-06-22 mark view->count = 0;
4709 1e37a5c2 2019-05-12 jcs break;
4710 83cc4199 2022-06-13 stsp case CTRL('u'):
4711 33c3719a 2022-06-15 stsp case 'u':
4712 83cc4199 2022-06-13 stsp nscroll /= 2;
4713 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4714 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4715 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4716 61417565 2022-06-20 mark case 'b':
4717 640cd7ff 2022-06-22 mark if (s->first_displayed_line == 1) {
4718 640cd7ff 2022-06-22 mark view->count = 0;
4719 26ed57b2 2018-05-19 stsp break;
4720 640cd7ff 2022-06-22 mark }
4721 1e37a5c2 2019-05-12 jcs i = 0;
4722 83cc4199 2022-06-13 stsp while (i++ < nscroll && s->first_displayed_line > 1)
4723 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4724 1e37a5c2 2019-05-12 jcs break;
4725 1e37a5c2 2019-05-12 jcs case 'j':
4726 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4727 02ffd0d5 2021-10-17 stsp case CTRL('n'):
4728 1e37a5c2 2019-05-12 jcs if (!s->eof)
4729 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4730 640cd7ff 2022-06-22 mark else
4731 640cd7ff 2022-06-22 mark view->count = 0;
4732 1e37a5c2 2019-05-12 jcs break;
4733 83cc4199 2022-06-13 stsp case CTRL('d'):
4734 33c3719a 2022-06-15 stsp case 'd':
4735 83cc4199 2022-06-13 stsp nscroll /= 2;
4736 83cc4199 2022-06-13 stsp /* FALL THROUGH */
4737 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4738 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4739 61417565 2022-06-20 mark case 'f':
4740 1e37a5c2 2019-05-12 jcs case ' ':
4741 640cd7ff 2022-06-22 mark if (s->eof) {
4742 640cd7ff 2022-06-22 mark view->count = 0;
4743 1e37a5c2 2019-05-12 jcs break;
4744 640cd7ff 2022-06-22 mark }
4745 1e37a5c2 2019-05-12 jcs i = 0;
4746 83cc4199 2022-06-13 stsp while (!s->eof && i++ < nscroll) {
4747 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4748 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4749 826082fe 2020-12-10 stsp if (linelen == -1) {
4750 826082fe 2020-12-10 stsp if (feof(s->f)) {
4751 826082fe 2020-12-10 stsp s->eof = 1;
4752 826082fe 2020-12-10 stsp } else
4753 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4754 34bc9ec9 2019-02-22 stsp break;
4755 826082fe 2020-12-10 stsp }
4756 1e37a5c2 2019-05-12 jcs }
4757 826082fe 2020-12-10 stsp free(line);
4758 1e37a5c2 2019-05-12 jcs break;
4759 1e37a5c2 2019-05-12 jcs case '[':
4760 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4761 1e37a5c2 2019-05-12 jcs s->diff_context--;
4762 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4763 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4764 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4765 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4766 27829c9e 2020-11-21 stsp s->nlines) {
4767 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4768 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4769 27829c9e 2020-11-21 stsp }
4770 640cd7ff 2022-06-22 mark } else
4771 640cd7ff 2022-06-22 mark view->count = 0;
4772 1e37a5c2 2019-05-12 jcs break;
4773 1e37a5c2 2019-05-12 jcs case ']':
4774 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4775 1e37a5c2 2019-05-12 jcs s->diff_context++;
4776 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4777 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4778 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4779 640cd7ff 2022-06-22 mark } else
4780 640cd7ff 2022-06-22 mark view->count = 0;
4781 1e37a5c2 2019-05-12 jcs break;
4782 1e37a5c2 2019-05-12 jcs case '<':
4783 1e37a5c2 2019-05-12 jcs case ',':
4784 2b3e6702 2022-07-20 mark case 'K':
4785 c0f61fa4 2022-07-11 mark up = 1;
4786 c0f61fa4 2022-07-11 mark /* FALL THROUGH */
4787 c0f61fa4 2022-07-11 mark case '>':
4788 c0f61fa4 2022-07-11 mark case '.':
4789 2b3e6702 2022-07-20 mark case 'J':
4790 c0f61fa4 2022-07-11 mark if (s->parent_view == NULL) {
4791 640cd7ff 2022-06-22 mark view->count = 0;
4792 48ae06ee 2018-10-18 stsp break;
4793 640cd7ff 2022-06-22 mark }
4794 c0f61fa4 2022-07-11 mark s->parent_view->count = view->count;
4795 6524637e 2019-02-21 stsp
4796 c0f61fa4 2022-07-11 mark if (s->parent_view->type == TOG_VIEW_LOG) {
4797 c0f61fa4 2022-07-11 mark ls = &s->parent_view->state.log;
4798 c0f61fa4 2022-07-11 mark old_selected_entry = ls->selected_entry;
4799 15a087fe 2019-02-21 stsp
4800 c0f61fa4 2022-07-11 mark err = input_log_view(NULL, s->parent_view,
4801 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4802 c0f61fa4 2022-07-11 mark if (err)
4803 c0f61fa4 2022-07-11 mark break;
4804 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4805 15a087fe 2019-02-21 stsp
4806 c0f61fa4 2022-07-11 mark if (old_selected_entry == ls->selected_entry)
4807 c0f61fa4 2022-07-11 mark break;
4808 15a087fe 2019-02-21 stsp
4809 c0f61fa4 2022-07-11 mark err = set_selected_commit(s, ls->selected_entry);
4810 c0f61fa4 2022-07-11 mark if (err)
4811 c0f61fa4 2022-07-11 mark break;
4812 c0f61fa4 2022-07-11 mark } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4813 c0f61fa4 2022-07-11 mark struct tog_blame_view_state *bs;
4814 c0f61fa4 2022-07-11 mark struct got_object_id *id, *prev_id;
4815 5e224a3e 2019-02-22 stsp
4816 c0f61fa4 2022-07-11 mark bs = &s->parent_view->state.blame;
4817 c0f61fa4 2022-07-11 mark prev_id = get_annotation_for_line(bs->blame.lines,
4818 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->last_diffed_line);
4819 c0f61fa4 2022-07-11 mark
4820 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view,
4821 c0f61fa4 2022-07-11 mark up ? KEY_UP : KEY_DOWN);
4822 c0f61fa4 2022-07-11 mark if (err)
4823 c0f61fa4 2022-07-11 mark break;
4824 c0f61fa4 2022-07-11 mark view->count = s->parent_view->count;
4825 5a8b5076 2020-12-05 stsp
4826 c0f61fa4 2022-07-11 mark if (prev_id == NULL)
4827 c0f61fa4 2022-07-11 mark break;
4828 c0f61fa4 2022-07-11 mark id = get_selected_commit_id(bs->blame.lines,
4829 c0f61fa4 2022-07-11 mark bs->blame.nlines, bs->first_displayed_line,
4830 c0f61fa4 2022-07-11 mark bs->selected_line);
4831 c0f61fa4 2022-07-11 mark if (id == NULL)
4832 c0f61fa4 2022-07-11 mark break;
4833 15a087fe 2019-02-21 stsp
4834 c0f61fa4 2022-07-11 mark if (!got_object_id_cmp(prev_id, id))
4835 c0f61fa4 2022-07-11 mark break;
4836 15a087fe 2019-02-21 stsp
4837 c0f61fa4 2022-07-11 mark err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4838 c0f61fa4 2022-07-11 mark if (err)
4839 c0f61fa4 2022-07-11 mark break;
4840 c0f61fa4 2022-07-11 mark }
4841 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4842 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4843 67b5eae1 2021-12-27 naddy s->matched_line = 0;
4844 145b6838 2022-06-16 stsp view->x = 0;
4845 1e37a5c2 2019-05-12 jcs
4846 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4847 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4848 1e37a5c2 2019-05-12 jcs break;
4849 1e37a5c2 2019-05-12 jcs default:
4850 640cd7ff 2022-06-22 mark view->count = 0;
4851 1e37a5c2 2019-05-12 jcs break;
4852 26ed57b2 2018-05-19 stsp }
4853 e5a0f69f 2018-08-18 stsp
4854 bcbd79e2 2018-08-19 stsp return err;
4855 26ed57b2 2018-05-19 stsp }
4856 26ed57b2 2018-05-19 stsp
4857 4ed7e80c 2018-05-20 stsp static const struct got_error *
4858 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4859 9f7d7167 2018-04-29 stsp {
4860 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4861 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4862 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4863 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4864 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4865 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4866 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4867 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4868 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4869 3dbaef42 2020-11-24 stsp const char *errstr;
4870 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4871 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
4872 70ac5f84 2019-03-28 stsp
4873 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4874 26ed57b2 2018-05-19 stsp switch (ch) {
4875 64453f7e 2020-11-21 stsp case 'a':
4876 64453f7e 2020-11-21 stsp force_text_diff = 1;
4877 3dbaef42 2020-11-24 stsp break;
4878 3dbaef42 2020-11-24 stsp case 'C':
4879 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4880 3dbaef42 2020-11-24 stsp &errstr);
4881 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4882 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4883 5a20d08d 2022-02-09 op errstr, errstr);
4884 64453f7e 2020-11-21 stsp break;
4885 09b5bff8 2020-02-23 naddy case 'r':
4886 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4887 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4888 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4889 09b5bff8 2020-02-23 naddy optarg);
4890 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4891 09b5bff8 2020-02-23 naddy break;
4892 3dbaef42 2020-11-24 stsp case 'w':
4893 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4894 3dbaef42 2020-11-24 stsp break;
4895 26ed57b2 2018-05-19 stsp default:
4896 17020d27 2019-03-07 stsp usage_diff();
4897 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4898 26ed57b2 2018-05-19 stsp }
4899 26ed57b2 2018-05-19 stsp }
4900 26ed57b2 2018-05-19 stsp
4901 26ed57b2 2018-05-19 stsp argc -= optind;
4902 26ed57b2 2018-05-19 stsp argv += optind;
4903 26ed57b2 2018-05-19 stsp
4904 26ed57b2 2018-05-19 stsp if (argc == 0) {
4905 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4906 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4907 15a94983 2018-12-23 stsp id_str1 = argv[0];
4908 15a94983 2018-12-23 stsp id_str2 = argv[1];
4909 26ed57b2 2018-05-19 stsp } else
4910 26ed57b2 2018-05-19 stsp usage_diff();
4911 eb6600df 2019-01-04 stsp
4912 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
4913 0ae84acc 2022-06-15 tracey if (error)
4914 0ae84acc 2022-06-15 tracey goto done;
4915 0ae84acc 2022-06-15 tracey
4916 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4917 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4918 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4919 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4920 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4921 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4922 c156c7a4 2020-12-18 stsp goto done;
4923 a273ac94 2020-02-23 naddy if (worktree)
4924 a273ac94 2020-02-23 naddy repo_path =
4925 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4926 a273ac94 2020-02-23 naddy else
4927 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4928 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4929 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4930 c156c7a4 2020-12-18 stsp goto done;
4931 c156c7a4 2020-12-18 stsp }
4932 a273ac94 2020-02-23 naddy }
4933 a273ac94 2020-02-23 naddy
4934 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4935 eb6600df 2019-01-04 stsp if (error)
4936 eb6600df 2019-01-04 stsp goto done;
4937 26ed57b2 2018-05-19 stsp
4938 a273ac94 2020-02-23 naddy init_curses();
4939 a273ac94 2020-02-23 naddy
4940 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4941 51a10b52 2020-12-26 stsp if (error)
4942 51a10b52 2020-12-26 stsp goto done;
4943 51a10b52 2020-12-26 stsp
4944 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
4945 26ed57b2 2018-05-19 stsp if (error)
4946 26ed57b2 2018-05-19 stsp goto done;
4947 26ed57b2 2018-05-19 stsp
4948 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4949 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4950 26ed57b2 2018-05-19 stsp if (error)
4951 26ed57b2 2018-05-19 stsp goto done;
4952 26ed57b2 2018-05-19 stsp
4953 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4954 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4955 26ed57b2 2018-05-19 stsp if (error)
4956 26ed57b2 2018-05-19 stsp goto done;
4957 26ed57b2 2018-05-19 stsp
4958 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4959 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4960 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4961 ea5e7bb5 2018-08-01 stsp goto done;
4962 ea5e7bb5 2018-08-01 stsp }
4963 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4964 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4965 5dc9f4bc 2018-08-04 stsp if (error)
4966 5dc9f4bc 2018-08-04 stsp goto done;
4967 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4968 26ed57b2 2018-05-19 stsp done:
4969 3dbaef42 2020-11-24 stsp free(label1);
4970 3dbaef42 2020-11-24 stsp free(label2);
4971 c02c541e 2019-03-29 stsp free(repo_path);
4972 a273ac94 2020-02-23 naddy free(cwd);
4973 1d0f4054 2021-06-17 stsp if (repo) {
4974 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4975 1d0f4054 2021-06-17 stsp if (error == NULL)
4976 1d0f4054 2021-06-17 stsp error = close_err;
4977 1d0f4054 2021-06-17 stsp }
4978 a273ac94 2020-02-23 naddy if (worktree)
4979 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4980 0ae84acc 2022-06-15 tracey if (pack_fds) {
4981 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
4982 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
4983 0ae84acc 2022-06-15 tracey if (error == NULL)
4984 0ae84acc 2022-06-15 tracey error = pack_err;
4985 0ae84acc 2022-06-15 tracey }
4986 51a10b52 2020-12-26 stsp tog_free_refs();
4987 26ed57b2 2018-05-19 stsp return error;
4988 9f7d7167 2018-04-29 stsp }
4989 9f7d7167 2018-04-29 stsp
4990 4ed7e80c 2018-05-20 stsp __dead static void
4991 9f7d7167 2018-04-29 stsp usage_blame(void)
4992 9f7d7167 2018-04-29 stsp {
4993 80ddbec8 2018-04-29 stsp endwin();
4994 87411fa9 2022-06-16 stsp fprintf(stderr,
4995 87411fa9 2022-06-16 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4996 9f7d7167 2018-04-29 stsp getprogname());
4997 9f7d7167 2018-04-29 stsp exit(1);
4998 9f7d7167 2018-04-29 stsp }
4999 84451b3e 2018-07-10 stsp
5000 84451b3e 2018-07-10 stsp struct tog_blame_line {
5001 84451b3e 2018-07-10 stsp int annotated;
5002 84451b3e 2018-07-10 stsp struct got_object_id *id;
5003 84451b3e 2018-07-10 stsp };
5004 9f7d7167 2018-04-29 stsp
5005 4ed7e80c 2018-05-20 stsp static const struct got_error *
5006 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5007 84451b3e 2018-07-10 stsp {
5008 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5009 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5010 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5011 84451b3e 2018-07-10 stsp const struct got_error *err;
5012 4cb9d869 2022-06-16 stsp int lineno = 0, nprinted = 0;
5013 826082fe 2020-12-10 stsp char *line = NULL;
5014 826082fe 2020-12-10 stsp size_t linesize = 0;
5015 826082fe 2020-12-10 stsp ssize_t linelen;
5016 84451b3e 2018-07-10 stsp wchar_t *wline;
5017 27a741e5 2019-09-11 stsp int width;
5018 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5019 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5020 ab089a2a 2018-07-12 stsp char *id_str;
5021 11b20872 2019-11-08 stsp struct tog_color *tc;
5022 ab089a2a 2018-07-12 stsp
5023 d7b5a0e8 2022-04-20 stsp err = got_object_id_str(&id_str, &s->blamed_commit->id);
5024 ab089a2a 2018-07-12 stsp if (err)
5025 ab089a2a 2018-07-12 stsp return err;
5026 84451b3e 2018-07-10 stsp
5027 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5028 f7d12f7e 2018-08-01 stsp werase(view->window);
5029 84451b3e 2018-07-10 stsp
5030 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5031 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5032 ab089a2a 2018-07-12 stsp free(id_str);
5033 ab089a2a 2018-07-12 stsp return err;
5034 ab089a2a 2018-07-12 stsp }
5035 ab089a2a 2018-07-12 stsp
5036 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5037 ab089a2a 2018-07-12 stsp free(line);
5038 2550e4c3 2018-07-13 stsp line = NULL;
5039 1cae65b4 2019-09-22 stsp if (err)
5040 1cae65b4 2019-09-22 stsp return err;
5041 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5042 a3404814 2018-09-02 stsp wstandout(view->window);
5043 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5044 11b20872 2019-11-08 stsp if (tc)
5045 11b20872 2019-11-08 stsp wattr_on(view->window,
5046 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5047 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5048 11b20872 2019-11-08 stsp if (tc)
5049 11b20872 2019-11-08 stsp wattr_off(view->window,
5050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5051 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5052 a3404814 2018-09-02 stsp wstandend(view->window);
5053 2550e4c3 2018-07-13 stsp free(wline);
5054 2550e4c3 2018-07-13 stsp wline = NULL;
5055 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5056 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5057 ab089a2a 2018-07-12 stsp
5058 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5059 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5060 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5061 ab089a2a 2018-07-12 stsp free(id_str);
5062 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5063 ab089a2a 2018-07-12 stsp }
5064 ab089a2a 2018-07-12 stsp free(id_str);
5065 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5066 3f60a8ef 2018-07-10 stsp free(line);
5067 2550e4c3 2018-07-13 stsp line = NULL;
5068 3f60a8ef 2018-07-10 stsp if (err)
5069 3f60a8ef 2018-07-10 stsp return err;
5070 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5071 2550e4c3 2018-07-13 stsp free(wline);
5072 2550e4c3 2018-07-13 stsp wline = NULL;
5073 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5074 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5075 3f60a8ef 2018-07-10 stsp
5076 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5077 145b6838 2022-06-16 stsp view->maxx = 0;
5078 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5079 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5080 826082fe 2020-12-10 stsp if (linelen == -1) {
5081 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5082 826082fe 2020-12-10 stsp s->eof = 1;
5083 826082fe 2020-12-10 stsp break;
5084 826082fe 2020-12-10 stsp }
5085 84451b3e 2018-07-10 stsp free(line);
5086 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5087 84451b3e 2018-07-10 stsp }
5088 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5089 826082fe 2020-12-10 stsp continue;
5090 1853e0f4 2022-06-16 stsp
5091 1853e0f4 2022-06-16 stsp /* Set view->maxx based on full line length. */
5092 1853e0f4 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5093 1853e0f4 2022-06-16 stsp if (err) {
5094 1853e0f4 2022-06-16 stsp free(line);
5095 1853e0f4 2022-06-16 stsp return err;
5096 1853e0f4 2022-06-16 stsp }
5097 1853e0f4 2022-06-16 stsp free(wline);
5098 1853e0f4 2022-06-16 stsp wline = NULL;
5099 1853e0f4 2022-06-16 stsp view->maxx = MAX(view->maxx, width);
5100 84451b3e 2018-07-10 stsp
5101 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5102 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5103 b700b5d6 2018-07-10 stsp
5104 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5105 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5106 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5107 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5108 c0f61fa4 2022-07-11 mark !(nprinted == s->selected_line - 1)) {
5109 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5110 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5111 8d0fe45a 2019-08-12 stsp char *id_str;
5112 87411fa9 2022-06-16 stsp err = got_object_id_str(&id_str,
5113 87411fa9 2022-06-16 stsp blame_line->id);
5114 8d0fe45a 2019-08-12 stsp if (err) {
5115 8d0fe45a 2019-08-12 stsp free(line);
5116 8d0fe45a 2019-08-12 stsp return err;
5117 8d0fe45a 2019-08-12 stsp }
5118 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5119 11b20872 2019-11-08 stsp if (tc)
5120 11b20872 2019-11-08 stsp wattr_on(view->window,
5121 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5122 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5123 11b20872 2019-11-08 stsp if (tc)
5124 11b20872 2019-11-08 stsp wattr_off(view->window,
5125 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5126 8d0fe45a 2019-08-12 stsp free(id_str);
5127 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5128 8d0fe45a 2019-08-12 stsp } else {
5129 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5130 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5131 84451b3e 2018-07-10 stsp }
5132 ee41ec32 2018-07-10 stsp } else {
5133 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5134 ee41ec32 2018-07-10 stsp prev_id = NULL;
5135 ee41ec32 2018-07-10 stsp }
5136 84451b3e 2018-07-10 stsp
5137 c0f61fa4 2022-07-11 mark if (nprinted == s->selected_line - 1)
5138 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5139 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5140 27a741e5 2019-09-11 stsp
5141 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5142 41605754 2020-11-12 stsp width = 9;
5143 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5144 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5145 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5146 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5147 145b6838 2022-06-16 stsp view->window, view->x, regmatch);
5148 41605754 2020-11-12 stsp if (err) {
5149 41605754 2020-11-12 stsp free(line);
5150 41605754 2020-11-12 stsp return err;
5151 41605754 2020-11-12 stsp }
5152 41605754 2020-11-12 stsp width += 9;
5153 41605754 2020-11-12 stsp } else {
5154 4cb9d869 2022-06-16 stsp int skip;
5155 4cb9d869 2022-06-16 stsp err = format_line(&wline, &width, &skip, line,
5156 4cb9d869 2022-06-16 stsp view->x, view->ncols - 9, 9, 1);
5157 4cb9d869 2022-06-16 stsp if (err) {
5158 4cb9d869 2022-06-16 stsp free(line);
5159 4cb9d869 2022-06-16 stsp return err;
5160 145b6838 2022-06-16 stsp }
5161 4cb9d869 2022-06-16 stsp waddwstr(view->window, &wline[skip]);
5162 a75b3e2d 2022-06-16 stsp width += 9;
5163 41605754 2020-11-12 stsp free(wline);
5164 41605754 2020-11-12 stsp wline = NULL;
5165 41605754 2020-11-12 stsp }
5166 41605754 2020-11-12 stsp
5167 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5168 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5169 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5170 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5171 84451b3e 2018-07-10 stsp }
5172 826082fe 2020-12-10 stsp free(line);
5173 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5174 84451b3e 2018-07-10 stsp
5175 9b058f45 2022-06-30 mark view_border(view);
5176 84451b3e 2018-07-10 stsp
5177 84451b3e 2018-07-10 stsp return NULL;
5178 84451b3e 2018-07-10 stsp }
5179 84451b3e 2018-07-10 stsp
5180 84451b3e 2018-07-10 stsp static const struct got_error *
5181 392891ce 2022-04-07 stsp blame_cb(void *arg, int nlines, int lineno,
5182 392891ce 2022-04-07 stsp struct got_commit_object *commit, struct got_object_id *id)
5183 84451b3e 2018-07-10 stsp {
5184 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5185 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5186 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5187 1a76625f 2018-10-22 stsp int errcode;
5188 84451b3e 2018-07-10 stsp
5189 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5190 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5191 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5192 84451b3e 2018-07-10 stsp
5193 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5194 1a76625f 2018-10-22 stsp if (errcode)
5195 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5196 84451b3e 2018-07-10 stsp
5197 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5198 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5199 d68a0a7d 2018-07-10 stsp goto done;
5200 d68a0a7d 2018-07-10 stsp }
5201 d68a0a7d 2018-07-10 stsp
5202 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5203 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5204 d68a0a7d 2018-07-10 stsp
5205 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5206 d68a0a7d 2018-07-10 stsp if (line->annotated)
5207 d68a0a7d 2018-07-10 stsp goto done;
5208 d68a0a7d 2018-07-10 stsp
5209 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5210 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5211 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5212 84451b3e 2018-07-10 stsp goto done;
5213 84451b3e 2018-07-10 stsp }
5214 84451b3e 2018-07-10 stsp line->annotated = 1;
5215 84451b3e 2018-07-10 stsp done:
5216 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5217 1a76625f 2018-10-22 stsp if (errcode)
5218 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5219 84451b3e 2018-07-10 stsp return err;
5220 84451b3e 2018-07-10 stsp }
5221 84451b3e 2018-07-10 stsp
5222 84451b3e 2018-07-10 stsp static void *
5223 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5224 84451b3e 2018-07-10 stsp {
5225 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5226 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5227 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5228 e6e73e55 2022-06-30 tracey int errcode, fd1 = -1, fd2 = -1;
5229 e6e73e55 2022-06-30 tracey FILE *f1 = NULL, *f2 = NULL;
5230 1b484788 2022-06-28 tracey
5231 e6e73e55 2022-06-30 tracey fd1 = got_opentempfd();
5232 e6e73e55 2022-06-30 tracey if (fd1 == -1)
5233 1b484788 2022-06-28 tracey return (void *)got_error_from_errno("got_opentempfd");
5234 e6e73e55 2022-06-30 tracey
5235 e6e73e55 2022-06-30 tracey fd2 = got_opentempfd();
5236 e6e73e55 2022-06-30 tracey if (fd2 == -1) {
5237 e6e73e55 2022-06-30 tracey err = got_error_from_errno("got_opentempfd");
5238 e6e73e55 2022-06-30 tracey goto done;
5239 e6e73e55 2022-06-30 tracey }
5240 18430de3 2018-07-10 stsp
5241 e6e73e55 2022-06-30 tracey f1 = got_opentemp();
5242 e6e73e55 2022-06-30 tracey if (f1 == NULL) {
5243 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5244 e6e73e55 2022-06-30 tracey goto done;
5245 e6e73e55 2022-06-30 tracey }
5246 e6e73e55 2022-06-30 tracey f2 = got_opentemp();
5247 e6e73e55 2022-06-30 tracey if (f2 == NULL) {
5248 e6e73e55 2022-06-30 tracey err = (void *)got_error_from_errno("got_opentemp");
5249 e6e73e55 2022-06-30 tracey goto done;
5250 e6e73e55 2022-06-30 tracey }
5251 e6e73e55 2022-06-30 tracey
5252 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5253 61266923 2020-01-14 stsp if (err)
5254 e6e73e55 2022-06-30 tracey goto done;
5255 61266923 2020-01-14 stsp
5256 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5257 917d79a7 2022-07-01 stsp tog_diff_algo, blame_cb, ta->cb_args,
5258 4b752015 2022-06-30 stsp ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5259 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5260 fc06ba56 2019-08-22 stsp err = NULL;
5261 18430de3 2018-07-10 stsp
5262 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5263 e6e73e55 2022-06-30 tracey if (errcode) {
5264 e6e73e55 2022-06-30 tracey err = got_error_set_errno(errcode, "pthread_mutex_lock");
5265 e6e73e55 2022-06-30 tracey goto done;
5266 e6e73e55 2022-06-30 tracey }
5267 18430de3 2018-07-10 stsp
5268 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5269 1d0f4054 2021-06-17 stsp if (err == NULL)
5270 1d0f4054 2021-06-17 stsp err = close_err;
5271 c9beca56 2018-07-22 stsp ta->repo = NULL;
5272 c9beca56 2018-07-22 stsp *ta->complete = 1;
5273 18430de3 2018-07-10 stsp
5274 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5275 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5276 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5277 18430de3 2018-07-10 stsp
5278 e6e73e55 2022-06-30 tracey done:
5279 e6e73e55 2022-06-30 tracey if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5280 1b484788 2022-06-28 tracey err = got_error_from_errno("close");
5281 e6e73e55 2022-06-30 tracey if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5282 e6e73e55 2022-06-30 tracey err = got_error_from_errno("close");
5283 e6e73e55 2022-06-30 tracey if (f1 && fclose(f1) == EOF && err == NULL)
5284 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5285 e6e73e55 2022-06-30 tracey if (f2 && fclose(f2) == EOF && err == NULL)
5286 e6e73e55 2022-06-30 tracey err = got_error_from_errno("fclose");
5287 1b484788 2022-06-28 tracey
5288 18430de3 2018-07-10 stsp return (void *)err;
5289 84451b3e 2018-07-10 stsp }
5290 84451b3e 2018-07-10 stsp
5291 245d91c1 2018-07-12 stsp static struct got_object_id *
5292 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5293 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5294 245d91c1 2018-07-12 stsp {
5295 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5296 8d0fe45a 2019-08-12 stsp
5297 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5298 8d0fe45a 2019-08-12 stsp return NULL;
5299 b880a918 2018-07-10 stsp
5300 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5301 c0f61fa4 2022-07-11 mark if (!line->annotated)
5302 c0f61fa4 2022-07-11 mark return NULL;
5303 c0f61fa4 2022-07-11 mark
5304 c0f61fa4 2022-07-11 mark return line->id;
5305 c0f61fa4 2022-07-11 mark }
5306 c0f61fa4 2022-07-11 mark
5307 c0f61fa4 2022-07-11 mark static struct got_object_id *
5308 c0f61fa4 2022-07-11 mark get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5309 c0f61fa4 2022-07-11 mark int lineno)
5310 c0f61fa4 2022-07-11 mark {
5311 c0f61fa4 2022-07-11 mark struct tog_blame_line *line;
5312 c0f61fa4 2022-07-11 mark
5313 c0f61fa4 2022-07-11 mark if (nlines <= 0 || lineno >= nlines)
5314 c0f61fa4 2022-07-11 mark return NULL;
5315 c0f61fa4 2022-07-11 mark
5316 c0f61fa4 2022-07-11 mark line = &lines[lineno - 1];
5317 245d91c1 2018-07-12 stsp if (!line->annotated)
5318 245d91c1 2018-07-12 stsp return NULL;
5319 245d91c1 2018-07-12 stsp
5320 245d91c1 2018-07-12 stsp return line->id;
5321 b880a918 2018-07-10 stsp }
5322 245d91c1 2018-07-12 stsp
5323 b880a918 2018-07-10 stsp static const struct got_error *
5324 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5325 a70480e0 2018-06-23 stsp {
5326 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5327 245d91c1 2018-07-12 stsp int i;
5328 245d91c1 2018-07-12 stsp
5329 245d91c1 2018-07-12 stsp if (blame->thread) {
5330 1a76625f 2018-10-22 stsp int errcode;
5331 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5332 1a76625f 2018-10-22 stsp if (errcode)
5333 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5334 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5335 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5336 1a76625f 2018-10-22 stsp if (errcode)
5337 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5338 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5339 1a76625f 2018-10-22 stsp if (errcode)
5340 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5341 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5342 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5343 245d91c1 2018-07-12 stsp err = NULL;
5344 245d91c1 2018-07-12 stsp blame->thread = NULL;
5345 245d91c1 2018-07-12 stsp }
5346 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5347 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5348 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5349 1d0f4054 2021-06-17 stsp if (err == NULL)
5350 1d0f4054 2021-06-17 stsp err = close_err;
5351 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5352 245d91c1 2018-07-12 stsp }
5353 245d91c1 2018-07-12 stsp if (blame->f) {
5354 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5355 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5356 245d91c1 2018-07-12 stsp blame->f = NULL;
5357 245d91c1 2018-07-12 stsp }
5358 57670559 2018-12-23 stsp if (blame->lines) {
5359 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5360 57670559 2018-12-23 stsp free(blame->lines[i].id);
5361 57670559 2018-12-23 stsp free(blame->lines);
5362 57670559 2018-12-23 stsp blame->lines = NULL;
5363 57670559 2018-12-23 stsp }
5364 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5365 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5366 0ae84acc 2022-06-15 tracey if (blame->pack_fds) {
5367 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
5368 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(blame->pack_fds);
5369 0ae84acc 2022-06-15 tracey if (err == NULL)
5370 0ae84acc 2022-06-15 tracey err = pack_err;
5371 8b195234 2022-06-15 stsp blame->pack_fds = NULL;
5372 0ae84acc 2022-06-15 tracey }
5373 245d91c1 2018-07-12 stsp return err;
5374 245d91c1 2018-07-12 stsp }
5375 245d91c1 2018-07-12 stsp
5376 245d91c1 2018-07-12 stsp static const struct got_error *
5377 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5378 fc06ba56 2019-08-22 stsp {
5379 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5380 fc06ba56 2019-08-22 stsp int *done = arg;
5381 fc06ba56 2019-08-22 stsp int errcode;
5382 fc06ba56 2019-08-22 stsp
5383 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5384 fc06ba56 2019-08-22 stsp if (errcode)
5385 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5386 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5387 fc06ba56 2019-08-22 stsp
5388 fc06ba56 2019-08-22 stsp if (*done)
5389 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5390 fc06ba56 2019-08-22 stsp
5391 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5392 fc06ba56 2019-08-22 stsp if (errcode)
5393 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5394 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5395 fc06ba56 2019-08-22 stsp
5396 fc06ba56 2019-08-22 stsp return err;
5397 fc06ba56 2019-08-22 stsp }
5398 fc06ba56 2019-08-22 stsp
5399 fc06ba56 2019-08-22 stsp static const struct got_error *
5400 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5401 245d91c1 2018-07-12 stsp {
5402 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5403 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5404 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5405 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
5406 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5407 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5408 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5409 eb81bc23 2022-06-28 tracey int obj_type, fd = -1;
5410 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
5411 a70480e0 2018-06-23 stsp
5412 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&commit, s->repo,
5413 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id);
5414 27d434c2 2018-09-15 stsp if (err)
5415 15a94983 2018-12-23 stsp return err;
5416 eb81bc23 2022-06-28 tracey
5417 eb81bc23 2022-06-28 tracey fd = got_opentempfd();
5418 eb81bc23 2022-06-28 tracey if (fd == -1) {
5419 eb81bc23 2022-06-28 tracey err = got_error_from_errno("got_opentempfd");
5420 eb81bc23 2022-06-28 tracey goto done;
5421 eb81bc23 2022-06-28 tracey }
5422 a44927cc 2022-04-07 stsp
5423 a44927cc 2022-04-07 stsp err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5424 a44927cc 2022-04-07 stsp if (err)
5425 a44927cc 2022-04-07 stsp goto done;
5426 27d434c2 2018-09-15 stsp
5427 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5428 84451b3e 2018-07-10 stsp if (err)
5429 84451b3e 2018-07-10 stsp goto done;
5430 27d434c2 2018-09-15 stsp
5431 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5432 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5433 84451b3e 2018-07-10 stsp goto done;
5434 84451b3e 2018-07-10 stsp }
5435 a70480e0 2018-06-23 stsp
5436 eb81bc23 2022-06-28 tracey err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5437 a70480e0 2018-06-23 stsp if (err)
5438 a70480e0 2018-06-23 stsp goto done;
5439 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5440 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5441 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5442 84451b3e 2018-07-10 stsp goto done;
5443 84451b3e 2018-07-10 stsp }
5444 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5445 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5446 1fddf795 2021-01-20 stsp if (err)
5447 1fddf795 2021-01-20 stsp goto done;
5448 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5449 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5450 84451b3e 2018-07-10 stsp goto done;
5451 1fddf795 2021-01-20 stsp }
5452 b02560ec 2019-08-19 stsp
5453 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5454 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5455 b02560ec 2019-08-19 stsp blame->nlines--;
5456 a70480e0 2018-06-23 stsp
5457 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5458 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5459 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5460 84451b3e 2018-07-10 stsp goto done;
5461 84451b3e 2018-07-10 stsp }
5462 a70480e0 2018-06-23 stsp
5463 0ae84acc 2022-06-15 tracey err = got_repo_pack_fds_open(&pack_fds);
5464 bd24772e 2018-07-11 stsp if (err)
5465 bd24772e 2018-07-11 stsp goto done;
5466 0ae84acc 2022-06-15 tracey err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5467 0ae84acc 2022-06-15 tracey pack_fds);
5468 0ae84acc 2022-06-15 tracey if (err)
5469 0ae84acc 2022-06-15 tracey goto done;
5470 bd24772e 2018-07-11 stsp
5471 0ae84acc 2022-06-15 tracey blame->pack_fds = pack_fds;
5472 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5473 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5474 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5475 d7b5a0e8 2022-04-20 stsp blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5476 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5477 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5478 245d91c1 2018-07-12 stsp goto done;
5479 245d91c1 2018-07-12 stsp }
5480 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5481 245d91c1 2018-07-12 stsp
5482 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5483 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5484 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5485 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5486 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5487 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5488 a5388363 2020-12-01 naddy s->blame_complete = 0;
5489 f5a09613 2020-12-13 naddy
5490 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5491 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5492 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5493 f5a09613 2020-12-13 naddy s->selected_line = 1;
5494 f5a09613 2020-12-13 naddy }
5495 67b5eae1 2021-12-27 naddy s->matched_line = 0;
5496 245d91c1 2018-07-12 stsp
5497 245d91c1 2018-07-12 stsp done:
5498 a44927cc 2022-04-07 stsp if (commit)
5499 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
5500 eb81bc23 2022-06-28 tracey if (fd != -1 && close(fd) == -1 && err == NULL)
5501 eb81bc23 2022-06-28 tracey err = got_error_from_errno("close");
5502 245d91c1 2018-07-12 stsp if (blob)
5503 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5504 27d434c2 2018-09-15 stsp free(obj_id);
5505 245d91c1 2018-07-12 stsp if (err)
5506 245d91c1 2018-07-12 stsp stop_blame(blame);
5507 245d91c1 2018-07-12 stsp return err;
5508 245d91c1 2018-07-12 stsp }
5509 245d91c1 2018-07-12 stsp
5510 245d91c1 2018-07-12 stsp static const struct got_error *
5511 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5512 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5513 245d91c1 2018-07-12 stsp {
5514 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5515 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5516 dbc6a6b6 2018-07-12 stsp
5517 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5518 245d91c1 2018-07-12 stsp
5519 c4843652 2019-08-12 stsp s->path = strdup(path);
5520 c4843652 2019-08-12 stsp if (s->path == NULL)
5521 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5522 c4843652 2019-08-12 stsp
5523 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5524 c4843652 2019-08-12 stsp if (err) {
5525 c4843652 2019-08-12 stsp free(s->path);
5526 7cbe629d 2018-08-04 stsp return err;
5527 c4843652 2019-08-12 stsp }
5528 245d91c1 2018-07-12 stsp
5529 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5530 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5531 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5532 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5533 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5534 fb2756b9 2018-08-04 stsp s->repo = repo;
5535 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5536 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5537 7cbe629d 2018-08-04 stsp
5538 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5539 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5540 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5541 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5542 11b20872 2019-11-08 stsp if (err)
5543 11b20872 2019-11-08 stsp return err;
5544 11b20872 2019-11-08 stsp }
5545 11b20872 2019-11-08 stsp
5546 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5547 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5548 917d79a7 2022-07-01 stsp view->reset = reset_blame_view;
5549 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5550 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5551 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5552 e5a0f69f 2018-08-18 stsp
5553 a5388363 2020-12-01 naddy return run_blame(view);
5554 7cbe629d 2018-08-04 stsp }
5555 7cbe629d 2018-08-04 stsp
5556 e5a0f69f 2018-08-18 stsp static const struct got_error *
5557 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5558 7cbe629d 2018-08-04 stsp {
5559 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5560 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5561 7cbe629d 2018-08-04 stsp
5562 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5563 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5564 e5a0f69f 2018-08-18 stsp
5565 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5566 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5567 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5568 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5569 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5570 7cbe629d 2018-08-04 stsp }
5571 e5a0f69f 2018-08-18 stsp
5572 e5a0f69f 2018-08-18 stsp free(s->path);
5573 11b20872 2019-11-08 stsp free_colors(&s->colors);
5574 e5a0f69f 2018-08-18 stsp return err;
5575 7cbe629d 2018-08-04 stsp }
5576 7cbe629d 2018-08-04 stsp
5577 7cbe629d 2018-08-04 stsp static const struct got_error *
5578 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5579 6c4c42e0 2019-06-24 stsp {
5580 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5581 6c4c42e0 2019-06-24 stsp
5582 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5583 6c4c42e0 2019-06-24 stsp return NULL;
5584 6c4c42e0 2019-06-24 stsp }
5585 6c4c42e0 2019-06-24 stsp
5586 6c4c42e0 2019-06-24 stsp static const struct got_error *
5587 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5588 6c4c42e0 2019-06-24 stsp {
5589 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5590 145b6838 2022-06-16 stsp const struct got_error *err = NULL;
5591 6c4c42e0 2019-06-24 stsp int lineno;
5592 cb713507 2022-06-16 stsp char *line = NULL;
5593 826082fe 2020-12-10 stsp size_t linesize = 0;
5594 826082fe 2020-12-10 stsp ssize_t linelen;
5595 6c4c42e0 2019-06-24 stsp
5596 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5597 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5598 6c4c42e0 2019-06-24 stsp return NULL;
5599 6c4c42e0 2019-06-24 stsp }
5600 6c4c42e0 2019-06-24 stsp
5601 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5602 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5603 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5604 6c4c42e0 2019-06-24 stsp else
5605 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5606 487cd7d2 2021-12-17 stsp } else
5607 487cd7d2 2021-12-17 stsp lineno = s->first_displayed_line - 1 + s->selected_line;
5608 6c4c42e0 2019-06-24 stsp
5609 6c4c42e0 2019-06-24 stsp while (1) {
5610 6c4c42e0 2019-06-24 stsp off_t offset;
5611 6c4c42e0 2019-06-24 stsp
5612 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5613 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5614 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5615 6c4c42e0 2019-06-24 stsp break;
5616 6c4c42e0 2019-06-24 stsp }
5617 2246482e 2019-06-25 stsp
5618 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5619 6c4c42e0 2019-06-24 stsp lineno = 1;
5620 6c4c42e0 2019-06-24 stsp else
5621 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5622 6c4c42e0 2019-06-24 stsp }
5623 6c4c42e0 2019-06-24 stsp
5624 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5625 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5626 6c4c42e0 2019-06-24 stsp free(line);
5627 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5628 6c4c42e0 2019-06-24 stsp }
5629 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5630 cb713507 2022-06-16 stsp if (linelen != -1) {
5631 cb713507 2022-06-16 stsp char *exstr;
5632 cb713507 2022-06-16 stsp err = expand_tab(&exstr, line);
5633 cb713507 2022-06-16 stsp if (err)
5634 cb713507 2022-06-16 stsp break;
5635 cb713507 2022-06-16 stsp if (match_line(exstr, &view->regex, 1,
5636 cb713507 2022-06-16 stsp &view->regmatch)) {
5637 cb713507 2022-06-16 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5638 cb713507 2022-06-16 stsp s->matched_line = lineno;
5639 cb713507 2022-06-16 stsp free(exstr);
5640 cb713507 2022-06-16 stsp break;
5641 cb713507 2022-06-16 stsp }
5642 cb713507 2022-06-16 stsp free(exstr);
5643 6c4c42e0 2019-06-24 stsp }
5644 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5645 6c4c42e0 2019-06-24 stsp lineno++;
5646 6c4c42e0 2019-06-24 stsp else
5647 6c4c42e0 2019-06-24 stsp lineno--;
5648 6c4c42e0 2019-06-24 stsp }
5649 826082fe 2020-12-10 stsp free(line);
5650 6c4c42e0 2019-06-24 stsp
5651 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5652 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5653 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5654 6c4c42e0 2019-06-24 stsp }
5655 6c4c42e0 2019-06-24 stsp
5656 145b6838 2022-06-16 stsp return err;
5657 6c4c42e0 2019-06-24 stsp }
5658 6c4c42e0 2019-06-24 stsp
5659 6c4c42e0 2019-06-24 stsp static const struct got_error *
5660 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5661 7cbe629d 2018-08-04 stsp {
5662 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5663 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5664 2b380cc8 2018-10-24 stsp int errcode;
5665 2b380cc8 2018-10-24 stsp
5666 1fddf795 2021-01-20 stsp if (s->blame.thread == NULL && !s->blame_complete) {
5667 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5668 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5669 2b380cc8 2018-10-24 stsp if (errcode)
5670 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5671 51fe7530 2019-08-19 stsp
5672 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5673 2b380cc8 2018-10-24 stsp }
5674 e5a0f69f 2018-08-18 stsp
5675 51fe7530 2019-08-19 stsp if (s->blame_complete)
5676 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5677 51fe7530 2019-08-19 stsp
5678 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5679 e5a0f69f 2018-08-18 stsp
5680 9b058f45 2022-06-30 mark view_border(view);
5681 e5a0f69f 2018-08-18 stsp return err;
5682 e5a0f69f 2018-08-18 stsp }
5683 e5a0f69f 2018-08-18 stsp
5684 e5a0f69f 2018-08-18 stsp static const struct got_error *
5685 05f04cdf 2022-07-20 mark log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5686 05f04cdf 2022-07-20 mark struct got_repository *repo, struct got_object_id *id)
5687 05f04cdf 2022-07-20 mark {
5688 05f04cdf 2022-07-20 mark struct tog_view *log_view;
5689 05f04cdf 2022-07-20 mark const struct got_error *err = NULL;
5690 05f04cdf 2022-07-20 mark
5691 05f04cdf 2022-07-20 mark *new_view = NULL;
5692 05f04cdf 2022-07-20 mark
5693 05f04cdf 2022-07-20 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5694 05f04cdf 2022-07-20 mark if (log_view == NULL)
5695 05f04cdf 2022-07-20 mark return got_error_from_errno("view_open");
5696 05f04cdf 2022-07-20 mark
5697 05f04cdf 2022-07-20 mark err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5698 05f04cdf 2022-07-20 mark if (err)
5699 05f04cdf 2022-07-20 mark view_close(log_view);
5700 05f04cdf 2022-07-20 mark else
5701 05f04cdf 2022-07-20 mark *new_view = log_view;
5702 05f04cdf 2022-07-20 mark
5703 05f04cdf 2022-07-20 mark return err;
5704 05f04cdf 2022-07-20 mark }
5705 05f04cdf 2022-07-20 mark
5706 05f04cdf 2022-07-20 mark static const struct got_error *
5707 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5708 e5a0f69f 2018-08-18 stsp {
5709 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5710 136e2bd4 2022-07-23 mark struct tog_view *diff_view;
5711 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5712 9b058f45 2022-06-30 mark int eos, nscroll, begin_y = 0, begin_x = 0;
5713 9b058f45 2022-06-30 mark
5714 9b058f45 2022-06-30 mark eos = nscroll = view->nlines - 2;
5715 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
5716 9b058f45 2022-06-30 mark --eos; /* border */
5717 7cbe629d 2018-08-04 stsp
5718 e5a0f69f 2018-08-18 stsp switch (ch) {
5719 145b6838 2022-06-16 stsp case '0':
5720 145b6838 2022-06-16 stsp view->x = 0;
5721 145b6838 2022-06-16 stsp break;
5722 145b6838 2022-06-16 stsp case '$':
5723 145b6838 2022-06-16 stsp view->x = MAX(view->maxx - view->ncols / 3, 0);
5724 640cd7ff 2022-06-22 mark view->count = 0;
5725 145b6838 2022-06-16 stsp break;
5726 145b6838 2022-06-16 stsp case KEY_RIGHT:
5727 145b6838 2022-06-16 stsp case 'l':
5728 145b6838 2022-06-16 stsp if (view->x + view->ncols / 3 < view->maxx)
5729 145b6838 2022-06-16 stsp view->x += 2; /* move two columns right */
5730 640cd7ff 2022-06-22 mark else
5731 640cd7ff 2022-06-22 mark view->count = 0;
5732 145b6838 2022-06-16 stsp break;
5733 145b6838 2022-06-16 stsp case KEY_LEFT:
5734 145b6838 2022-06-16 stsp case 'h':
5735 145b6838 2022-06-16 stsp view->x -= MIN(view->x, 2); /* move two columns back */
5736 640cd7ff 2022-06-22 mark if (view->x <= 0)
5737 640cd7ff 2022-06-22 mark view->count = 0;
5738 145b6838 2022-06-16 stsp break;
5739 1e37a5c2 2019-05-12 jcs case 'q':
5740 1e37a5c2 2019-05-12 jcs s->done = 1;
5741 4deef56f 2021-09-02 naddy break;
5742 4deef56f 2021-09-02 naddy case 'g':
5743 4deef56f 2021-09-02 naddy case KEY_HOME:
5744 4deef56f 2021-09-02 naddy s->selected_line = 1;
5745 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5746 640cd7ff 2022-06-22 mark view->count = 0;
5747 4deef56f 2021-09-02 naddy break;
5748 4deef56f 2021-09-02 naddy case 'G':
5749 4deef56f 2021-09-02 naddy case KEY_END:
5750 9b058f45 2022-06-30 mark if (s->blame.nlines < eos) {
5751 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5752 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5753 4deef56f 2021-09-02 naddy } else {
5754 9b058f45 2022-06-30 mark s->selected_line = eos;
5755 9b058f45 2022-06-30 mark s->first_displayed_line = s->blame.nlines - (eos - 1);
5756 4deef56f 2021-09-02 naddy }
5757 640cd7ff 2022-06-22 mark view->count = 0;
5758 1e37a5c2 2019-05-12 jcs break;
5759 1e37a5c2 2019-05-12 jcs case 'k':
5760 1e37a5c2 2019-05-12 jcs case KEY_UP:
5761 02ffd0d5 2021-10-17 stsp case CTRL('p'):
5762 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5763 1e37a5c2 2019-05-12 jcs s->selected_line--;
5764 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5765 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5766 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5767 640cd7ff 2022-06-22 mark else
5768 640cd7ff 2022-06-22 mark view->count = 0;
5769 1e37a5c2 2019-05-12 jcs break;
5770 83cc4199 2022-06-13 stsp case CTRL('u'):
5771 33c3719a 2022-06-15 stsp case 'u':
5772 83cc4199 2022-06-13 stsp nscroll /= 2;
5773 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5774 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5775 ea025d1d 2020-02-22 naddy case CTRL('b'):
5776 61417565 2022-06-20 mark case 'b':
5777 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5778 640cd7ff 2022-06-22 mark if (view->count > 1)
5779 640cd7ff 2022-06-22 mark nscroll += nscroll;
5780 83cc4199 2022-06-13 stsp s->selected_line = MAX(1, s->selected_line - nscroll);
5781 640cd7ff 2022-06-22 mark view->count = 0;
5782 e5a0f69f 2018-08-18 stsp break;
5783 1e37a5c2 2019-05-12 jcs }
5784 83cc4199 2022-06-13 stsp if (s->first_displayed_line > nscroll)
5785 83cc4199 2022-06-13 stsp s->first_displayed_line -= nscroll;
5786 1e37a5c2 2019-05-12 jcs else
5787 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5788 1e37a5c2 2019-05-12 jcs break;
5789 1e37a5c2 2019-05-12 jcs case 'j':
5790 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5791 02ffd0d5 2021-10-17 stsp case CTRL('n'):
5792 9b058f45 2022-06-30 mark if (s->selected_line < eos && s->first_displayed_line +
5793 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5794 1e37a5c2 2019-05-12 jcs s->selected_line++;
5795 9b058f45 2022-06-30 mark else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5796 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5797 640cd7ff 2022-06-22 mark else
5798 640cd7ff 2022-06-22 mark view->count = 0;
5799 1e37a5c2 2019-05-12 jcs break;
5800 61417565 2022-06-20 mark case 'c':
5801 1e37a5c2 2019-05-12 jcs case 'p': {
5802 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5803 640cd7ff 2022-06-22 mark
5804 640cd7ff 2022-06-22 mark view->count = 0;
5805 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5806 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5807 1e37a5c2 2019-05-12 jcs if (id == NULL)
5808 e5a0f69f 2018-08-18 stsp break;
5809 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5810 a44927cc 2022-04-07 stsp struct got_commit_object *commit, *pcommit;
5811 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5812 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5813 1e37a5c2 2019-05-12 jcs int obj_type;
5814 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5815 1e37a5c2 2019-05-12 jcs s->repo, id);
5816 e5a0f69f 2018-08-18 stsp if (err)
5817 e5a0f69f 2018-08-18 stsp break;
5818 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5819 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5820 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5821 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5822 e5a0f69f 2018-08-18 stsp break;
5823 e5a0f69f 2018-08-18 stsp }
5824 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5825 a44927cc 2022-04-07 stsp err = got_object_open_as_commit(&pcommit,
5826 d7b5a0e8 2022-04-20 stsp s->repo, &pid->id);
5827 a44927cc 2022-04-07 stsp if (err)
5828 a44927cc 2022-04-07 stsp break;
5829 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5830 a44927cc 2022-04-07 stsp pcommit, s->path);
5831 a44927cc 2022-04-07 stsp got_object_commit_close(pcommit);
5832 e5a0f69f 2018-08-18 stsp if (err) {
5833 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5834 1e37a5c2 2019-05-12 jcs err = NULL;
5835 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5836 e5a0f69f 2018-08-18 stsp break;
5837 e5a0f69f 2018-08-18 stsp }
5838 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5839 1e37a5c2 2019-05-12 jcs blob_id);
5840 1e37a5c2 2019-05-12 jcs free(blob_id);
5841 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5842 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5843 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5844 e5a0f69f 2018-08-18 stsp break;
5845 1e37a5c2 2019-05-12 jcs }
5846 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5847 d7b5a0e8 2022-04-20 stsp &pid->id);
5848 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5849 1e37a5c2 2019-05-12 jcs } else {
5850 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5851 d7b5a0e8 2022-04-20 stsp &s->blamed_commit->id) == 0)
5852 1e37a5c2 2019-05-12 jcs break;
5853 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5854 1e37a5c2 2019-05-12 jcs id);
5855 1e37a5c2 2019-05-12 jcs }
5856 1e37a5c2 2019-05-12 jcs if (err)
5857 e5a0f69f 2018-08-18 stsp break;
5858 1e37a5c2 2019-05-12 jcs s->done = 1;
5859 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5860 1e37a5c2 2019-05-12 jcs s->done = 0;
5861 1e37a5c2 2019-05-12 jcs if (thread_err)
5862 1e37a5c2 2019-05-12 jcs break;
5863 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5864 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5865 a5388363 2020-12-01 naddy err = run_blame(view);
5866 1e37a5c2 2019-05-12 jcs if (err)
5867 1e37a5c2 2019-05-12 jcs break;
5868 1e37a5c2 2019-05-12 jcs break;
5869 1e37a5c2 2019-05-12 jcs }
5870 61417565 2022-06-20 mark case 'C': {
5871 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5872 640cd7ff 2022-06-22 mark
5873 640cd7ff 2022-06-22 mark view->count = 0;
5874 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5875 d7b5a0e8 2022-04-20 stsp if (!got_object_id_cmp(&first->id, s->commit_id))
5876 1e37a5c2 2019-05-12 jcs break;
5877 1e37a5c2 2019-05-12 jcs s->done = 1;
5878 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5879 1e37a5c2 2019-05-12 jcs s->done = 0;
5880 1e37a5c2 2019-05-12 jcs if (thread_err)
5881 1e37a5c2 2019-05-12 jcs break;
5882 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5883 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5884 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5885 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5886 a5388363 2020-12-01 naddy err = run_blame(view);
5887 1e37a5c2 2019-05-12 jcs if (err)
5888 1e37a5c2 2019-05-12 jcs break;
5889 1e37a5c2 2019-05-12 jcs break;
5890 1e37a5c2 2019-05-12 jcs }
5891 136e2bd4 2022-07-23 mark case 'L':
5892 05f04cdf 2022-07-20 mark view->count = 0;
5893 136e2bd4 2022-07-23 mark s->id_to_log = get_selected_commit_id(s->blame.lines,
5894 136e2bd4 2022-07-23 mark s->blame.nlines, s->first_displayed_line, s->selected_line);
5895 136e2bd4 2022-07-23 mark if (s->id_to_log)
5896 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
5897 05f04cdf 2022-07-20 mark break;
5898 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5899 1e37a5c2 2019-05-12 jcs case '\r': {
5900 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5901 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5902 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5903 640cd7ff 2022-06-22 mark
5904 640cd7ff 2022-06-22 mark view->count = 0;
5905 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5906 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5907 1e37a5c2 2019-05-12 jcs if (id == NULL)
5908 1e37a5c2 2019-05-12 jcs break;
5909 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5910 1e37a5c2 2019-05-12 jcs if (err)
5911 1e37a5c2 2019-05-12 jcs break;
5912 9b058f45 2022-06-30 mark pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5913 c0f61fa4 2022-07-11 mark if (*new_view) {
5914 c0f61fa4 2022-07-11 mark /* traversed from diff view, release diff resources */
5915 c0f61fa4 2022-07-11 mark err = close_diff_view(*new_view);
5916 c0f61fa4 2022-07-11 mark if (err)
5917 c0f61fa4 2022-07-11 mark break;
5918 c0f61fa4 2022-07-11 mark diff_view = *new_view;
5919 c0f61fa4 2022-07-11 mark } else {
5920 c0f61fa4 2022-07-11 mark if (view_is_parent_view(view))
5921 c0f61fa4 2022-07-11 mark view_get_split(view, &begin_y, &begin_x);
5922 9b058f45 2022-06-30 mark
5923 c0f61fa4 2022-07-11 mark diff_view = view_open(0, 0, begin_y, begin_x,
5924 c0f61fa4 2022-07-11 mark TOG_VIEW_DIFF);
5925 c0f61fa4 2022-07-11 mark if (diff_view == NULL) {
5926 c0f61fa4 2022-07-11 mark got_object_commit_close(commit);
5927 c0f61fa4 2022-07-11 mark err = got_error_from_errno("view_open");
5928 c0f61fa4 2022-07-11 mark break;
5929 c0f61fa4 2022-07-11 mark }
5930 15a94983 2018-12-23 stsp }
5931 d7b5a0e8 2022-04-20 stsp err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5932 c0f61fa4 2022-07-11 mark id, NULL, NULL, 3, 0, 0, view, s->repo);
5933 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5934 1e37a5c2 2019-05-12 jcs if (err) {
5935 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5936 1e37a5c2 2019-05-12 jcs break;
5937 1e37a5c2 2019-05-12 jcs }
5938 c0f61fa4 2022-07-11 mark s->last_diffed_line = s->first_displayed_line - 1 +
5939 c0f61fa4 2022-07-11 mark s->selected_line;
5940 c0f61fa4 2022-07-11 mark if (*new_view)
5941 c0f61fa4 2022-07-11 mark break; /* still open from active diff view */
5942 49b24ee5 2022-07-03 mark if (view_is_parent_view(view) &&
5943 49b24ee5 2022-07-03 mark view->mode == TOG_VIEW_SPLIT_HRZN) {
5944 9b058f45 2022-06-30 mark err = view_init_hsplit(view, begin_y);
5945 9b058f45 2022-06-30 mark if (err)
5946 9b058f45 2022-06-30 mark break;
5947 9b058f45 2022-06-30 mark }
5948 9b058f45 2022-06-30 mark
5949 e78dc838 2020-12-04 stsp view->focussed = 0;
5950 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5951 9b058f45 2022-06-30 mark diff_view->mode = view->mode;
5952 9b058f45 2022-06-30 mark diff_view->nlines = view->lines - begin_y;
5953 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5954 3c1dfe12 2022-07-08 mark view_transfer_size(diff_view, view);
5955 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5956 1e37a5c2 2019-05-12 jcs if (err)
5957 34bc9ec9 2019-02-22 stsp break;
5958 0dbbbe90 2022-06-17 op err = view_set_child(view, diff_view);
5959 0dbbbe90 2022-06-17 op if (err)
5960 0dbbbe90 2022-06-17 op break;
5961 e78dc838 2020-12-04 stsp view->focus_child = 1;
5962 1e37a5c2 2019-05-12 jcs } else
5963 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5964 1e37a5c2 2019-05-12 jcs if (err)
5965 e5a0f69f 2018-08-18 stsp break;
5966 1e37a5c2 2019-05-12 jcs break;
5967 1e37a5c2 2019-05-12 jcs }
5968 83cc4199 2022-06-13 stsp case CTRL('d'):
5969 33c3719a 2022-06-15 stsp case 'd':
5970 83cc4199 2022-06-13 stsp nscroll /= 2;
5971 83cc4199 2022-06-13 stsp /* FALL THROUGH */
5972 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5973 ea025d1d 2020-02-22 naddy case CTRL('f'):
5974 61417565 2022-06-20 mark case 'f':
5975 1e37a5c2 2019-05-12 jcs case ' ':
5976 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5977 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5978 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5979 640cd7ff 2022-06-22 mark view->count = 0;
5980 e5a0f69f 2018-08-18 stsp break;
5981 1e37a5c2 2019-05-12 jcs }
5982 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5983 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5984 83cc4199 2022-06-13 stsp s->selected_line +=
5985 83cc4199 2022-06-13 stsp MIN(nscroll, s->last_displayed_line -
5986 83cc4199 2022-06-13 stsp s->first_displayed_line - s->selected_line + 1);
5987 1e37a5c2 2019-05-12 jcs }
5988 83cc4199 2022-06-13 stsp if (s->last_displayed_line + nscroll <= s->blame.nlines)
5989 83cc4199 2022-06-13 stsp s->first_displayed_line += nscroll;
5990 1e37a5c2 2019-05-12 jcs else
5991 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5992 83cc4199 2022-06-13 stsp s->blame.nlines - (view->nlines - 3);
5993 1e37a5c2 2019-05-12 jcs break;
5994 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5995 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5996 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5997 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5998 1e37a5c2 2019-05-12 jcs }
5999 1e37a5c2 2019-05-12 jcs break;
6000 1e37a5c2 2019-05-12 jcs default:
6001 640cd7ff 2022-06-22 mark view->count = 0;
6002 1e37a5c2 2019-05-12 jcs break;
6003 a70480e0 2018-06-23 stsp }
6004 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6005 917d79a7 2022-07-01 stsp }
6006 917d79a7 2022-07-01 stsp
6007 917d79a7 2022-07-01 stsp static const struct got_error *
6008 917d79a7 2022-07-01 stsp reset_blame_view(struct tog_view *view)
6009 917d79a7 2022-07-01 stsp {
6010 917d79a7 2022-07-01 stsp const struct got_error *err;
6011 917d79a7 2022-07-01 stsp struct tog_blame_view_state *s = &view->state.blame;
6012 917d79a7 2022-07-01 stsp
6013 917d79a7 2022-07-01 stsp view->count = 0;
6014 917d79a7 2022-07-01 stsp s->done = 1;
6015 917d79a7 2022-07-01 stsp err = stop_blame(&s->blame);
6016 917d79a7 2022-07-01 stsp s->done = 0;
6017 917d79a7 2022-07-01 stsp if (err)
6018 917d79a7 2022-07-01 stsp return err;
6019 917d79a7 2022-07-01 stsp return run_blame(view);
6020 a70480e0 2018-06-23 stsp }
6021 a70480e0 2018-06-23 stsp
6022 a70480e0 2018-06-23 stsp static const struct got_error *
6023 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6024 9f7d7167 2018-04-29 stsp {
6025 a70480e0 2018-06-23 stsp const struct got_error *error;
6026 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6027 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6028 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6029 0587e10c 2020-07-23 stsp char *link_target = NULL;
6030 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6031 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6032 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6033 a70480e0 2018-06-23 stsp int ch;
6034 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6035 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6036 a70480e0 2018-06-23 stsp
6037 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6038 a70480e0 2018-06-23 stsp switch (ch) {
6039 a70480e0 2018-06-23 stsp case 'c':
6040 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6041 a70480e0 2018-06-23 stsp break;
6042 69069811 2018-08-02 stsp case 'r':
6043 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6044 69069811 2018-08-02 stsp if (repo_path == NULL)
6045 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6046 9ba1d308 2019-10-21 stsp optarg);
6047 69069811 2018-08-02 stsp break;
6048 a70480e0 2018-06-23 stsp default:
6049 17020d27 2019-03-07 stsp usage_blame();
6050 a70480e0 2018-06-23 stsp /* NOTREACHED */
6051 a70480e0 2018-06-23 stsp }
6052 a70480e0 2018-06-23 stsp }
6053 a70480e0 2018-06-23 stsp
6054 a70480e0 2018-06-23 stsp argc -= optind;
6055 a70480e0 2018-06-23 stsp argv += optind;
6056 a70480e0 2018-06-23 stsp
6057 f135c941 2020-02-20 stsp if (argc != 1)
6058 a70480e0 2018-06-23 stsp usage_blame();
6059 6962eb72 2020-02-20 stsp
6060 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6061 0ae84acc 2022-06-15 tracey if (error != NULL)
6062 0ae84acc 2022-06-15 tracey goto done;
6063 0ae84acc 2022-06-15 tracey
6064 69069811 2018-08-02 stsp if (repo_path == NULL) {
6065 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6066 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6067 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6068 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6069 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6070 c156c7a4 2020-12-18 stsp goto done;
6071 f135c941 2020-02-20 stsp if (worktree)
6072 eb41ed75 2019-02-05 stsp repo_path =
6073 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6074 f135c941 2020-02-20 stsp else
6075 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6076 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6077 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6078 c156c7a4 2020-12-18 stsp goto done;
6079 c156c7a4 2020-12-18 stsp }
6080 f135c941 2020-02-20 stsp }
6081 a915003a 2019-02-05 stsp
6082 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6083 c02c541e 2019-03-29 stsp if (error != NULL)
6084 8e94dd5b 2019-01-04 stsp goto done;
6085 69069811 2018-08-02 stsp
6086 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6087 f135c941 2020-02-20 stsp worktree);
6088 c02c541e 2019-03-29 stsp if (error)
6089 92205607 2019-01-04 stsp goto done;
6090 69069811 2018-08-02 stsp
6091 f135c941 2020-02-20 stsp init_curses();
6092 f135c941 2020-02-20 stsp
6093 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6094 51a10b52 2020-12-26 stsp if (error)
6095 51a10b52 2020-12-26 stsp goto done;
6096 51a10b52 2020-12-26 stsp
6097 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6098 eb41ed75 2019-02-05 stsp if (error)
6099 69069811 2018-08-02 stsp goto done;
6100 a70480e0 2018-06-23 stsp
6101 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6102 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6103 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6104 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6105 a70480e0 2018-06-23 stsp if (error != NULL)
6106 66b4983c 2018-06-23 stsp goto done;
6107 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6108 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6109 a70480e0 2018-06-23 stsp } else {
6110 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6111 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6112 a70480e0 2018-06-23 stsp }
6113 a19e88aa 2018-06-23 stsp if (error != NULL)
6114 8b473291 2019-02-21 stsp goto done;
6115 8b473291 2019-02-21 stsp
6116 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6117 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6118 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6119 e1cd8fed 2018-08-01 stsp goto done;
6120 e1cd8fed 2018-08-01 stsp }
6121 0587e10c 2020-07-23 stsp
6122 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6123 a44927cc 2022-04-07 stsp if (error)
6124 a44927cc 2022-04-07 stsp goto done;
6125 a44927cc 2022-04-07 stsp
6126 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6127 a44927cc 2022-04-07 stsp commit, repo);
6128 7cbe629d 2018-08-04 stsp if (error)
6129 7cbe629d 2018-08-04 stsp goto done;
6130 0587e10c 2020-07-23 stsp
6131 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6132 78756c87 2020-11-24 stsp commit_id, repo);
6133 0587e10c 2020-07-23 stsp if (error)
6134 0587e10c 2020-07-23 stsp goto done;
6135 12314ad4 2019-08-31 stsp if (worktree) {
6136 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6137 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6138 12314ad4 2019-08-31 stsp worktree = NULL;
6139 12314ad4 2019-08-31 stsp }
6140 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6141 a70480e0 2018-06-23 stsp done:
6142 69069811 2018-08-02 stsp free(repo_path);
6143 f135c941 2020-02-20 stsp free(in_repo_path);
6144 0587e10c 2020-07-23 stsp free(link_target);
6145 69069811 2018-08-02 stsp free(cwd);
6146 a70480e0 2018-06-23 stsp free(commit_id);
6147 a44927cc 2022-04-07 stsp if (commit)
6148 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
6149 eb41ed75 2019-02-05 stsp if (worktree)
6150 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6151 1d0f4054 2021-06-17 stsp if (repo) {
6152 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6153 1d0f4054 2021-06-17 stsp if (error == NULL)
6154 1d0f4054 2021-06-17 stsp error = close_err;
6155 1d0f4054 2021-06-17 stsp }
6156 0ae84acc 2022-06-15 tracey if (pack_fds) {
6157 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
6158 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
6159 0ae84acc 2022-06-15 tracey if (error == NULL)
6160 0ae84acc 2022-06-15 tracey error = pack_err;
6161 0ae84acc 2022-06-15 tracey }
6162 51a10b52 2020-12-26 stsp tog_free_refs();
6163 a70480e0 2018-06-23 stsp return error;
6164 ffd1d5e5 2018-06-23 stsp }
6165 ffd1d5e5 2018-06-23 stsp
6166 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6167 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6168 ffd1d5e5 2018-06-23 stsp {
6169 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6170 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6171 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6172 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6173 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6174 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6175 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6176 ffd1d5e5 2018-06-23 stsp
6177 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6178 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
6179 9b058f45 2022-06-30 mark --limit; /* border */
6180 ffd1d5e5 2018-06-23 stsp
6181 f7d12f7e 2018-08-01 stsp werase(view->window);
6182 ffd1d5e5 2018-06-23 stsp
6183 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6184 ffd1d5e5 2018-06-23 stsp return NULL;
6185 ffd1d5e5 2018-06-23 stsp
6186 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6187 ccda2f4d 2022-06-16 stsp 0, 0);
6188 ffd1d5e5 2018-06-23 stsp if (err)
6189 ffd1d5e5 2018-06-23 stsp return err;
6190 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6191 a3404814 2018-09-02 stsp wstandout(view->window);
6192 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6193 11b20872 2019-11-08 stsp if (tc)
6194 11b20872 2019-11-08 stsp wattr_on(view->window,
6195 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6196 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6197 11b20872 2019-11-08 stsp if (tc)
6198 11b20872 2019-11-08 stsp wattr_off(view->window,
6199 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6200 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6201 a3404814 2018-09-02 stsp wstandend(view->window);
6202 2550e4c3 2018-07-13 stsp free(wline);
6203 2550e4c3 2018-07-13 stsp wline = NULL;
6204 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6205 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6206 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6207 ffd1d5e5 2018-06-23 stsp return NULL;
6208 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6209 ccda2f4d 2022-06-16 stsp 0, 0);
6210 ce52c690 2018-06-23 stsp if (err)
6211 ce52c690 2018-06-23 stsp return err;
6212 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6213 2550e4c3 2018-07-13 stsp free(wline);
6214 2550e4c3 2018-07-13 stsp wline = NULL;
6215 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6216 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6217 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6218 ffd1d5e5 2018-06-23 stsp return NULL;
6219 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6220 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6221 a1eca9bb 2018-06-23 stsp return NULL;
6222 ffd1d5e5 2018-06-23 stsp
6223 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6224 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6225 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6226 0cf4efb1 2018-09-29 stsp if (view->focussed)
6227 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6228 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6229 ffd1d5e5 2018-06-23 stsp }
6230 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6231 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6232 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6233 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6234 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6235 ffd1d5e5 2018-06-23 stsp return NULL;
6236 ffd1d5e5 2018-06-23 stsp n = 1;
6237 ffd1d5e5 2018-06-23 stsp } else {
6238 ffd1d5e5 2018-06-23 stsp n = 0;
6239 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6240 ffd1d5e5 2018-06-23 stsp }
6241 ffd1d5e5 2018-06-23 stsp
6242 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6243 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6244 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6245 848d6979 2019-08-12 stsp const char *modestr = "";
6246 56e0773d 2019-11-28 stsp mode_t mode;
6247 1d13200f 2018-07-12 stsp
6248 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6249 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6250 56e0773d 2019-11-28 stsp
6251 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6252 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6253 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6254 1d13200f 2018-07-12 stsp if (err)
6255 638f9024 2019-05-13 stsp return got_error_from_errno(
6256 230a42bd 2019-05-11 jcs "got_object_id_str");
6257 1d13200f 2018-07-12 stsp }
6258 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6259 63c5ca5d 2019-08-24 stsp modestr = "$";
6260 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6261 0d6c6ee3 2020-05-20 stsp int i;
6262 0d6c6ee3 2020-05-20 stsp
6263 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6264 d86d3b18 2020-12-01 naddy te, s->repo);
6265 0d6c6ee3 2020-05-20 stsp if (err) {
6266 0d6c6ee3 2020-05-20 stsp free(id_str);
6267 0d6c6ee3 2020-05-20 stsp return err;
6268 0d6c6ee3 2020-05-20 stsp }
6269 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6270 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6271 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6272 0d6c6ee3 2020-05-20 stsp }
6273 848d6979 2019-08-12 stsp modestr = "@";
6274 0d6c6ee3 2020-05-20 stsp }
6275 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6276 848d6979 2019-08-12 stsp modestr = "/";
6277 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6278 848d6979 2019-08-12 stsp modestr = "*";
6279 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6280 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6281 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6282 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6283 1d13200f 2018-07-12 stsp free(id_str);
6284 0d6c6ee3 2020-05-20 stsp free(link_target);
6285 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6286 1d13200f 2018-07-12 stsp }
6287 1d13200f 2018-07-12 stsp free(id_str);
6288 0d6c6ee3 2020-05-20 stsp free(link_target);
6289 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6290 ccda2f4d 2022-06-16 stsp 0, 0);
6291 ffd1d5e5 2018-06-23 stsp if (err) {
6292 ffd1d5e5 2018-06-23 stsp free(line);
6293 ffd1d5e5 2018-06-23 stsp break;
6294 ffd1d5e5 2018-06-23 stsp }
6295 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6296 0cf4efb1 2018-09-29 stsp if (view->focussed)
6297 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6298 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6299 ffd1d5e5 2018-06-23 stsp }
6300 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6301 f26dddb7 2019-11-08 stsp if (tc)
6302 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6303 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6304 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6305 f26dddb7 2019-11-08 stsp if (tc)
6306 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6307 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6308 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6309 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6310 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6311 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6312 ffd1d5e5 2018-06-23 stsp free(line);
6313 2550e4c3 2018-07-13 stsp free(wline);
6314 2550e4c3 2018-07-13 stsp wline = NULL;
6315 ffd1d5e5 2018-06-23 stsp n++;
6316 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6317 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6318 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6319 ffd1d5e5 2018-06-23 stsp break;
6320 ffd1d5e5 2018-06-23 stsp }
6321 ffd1d5e5 2018-06-23 stsp
6322 ffd1d5e5 2018-06-23 stsp return err;
6323 ffd1d5e5 2018-06-23 stsp }
6324 ffd1d5e5 2018-06-23 stsp
6325 ffd1d5e5 2018-06-23 stsp static void
6326 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6327 ffd1d5e5 2018-06-23 stsp {
6328 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6329 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6330 fa86c4bf 2020-11-29 stsp int i = 0;
6331 ffd1d5e5 2018-06-23 stsp
6332 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6333 ffd1d5e5 2018-06-23 stsp return;
6334 ffd1d5e5 2018-06-23 stsp
6335 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6336 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6337 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6338 fa86c4bf 2020-11-29 stsp if (!isroot)
6339 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6340 fa86c4bf 2020-11-29 stsp break;
6341 fa86c4bf 2020-11-29 stsp }
6342 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6343 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6344 ffd1d5e5 2018-06-23 stsp }
6345 ffd1d5e5 2018-06-23 stsp }
6346 ffd1d5e5 2018-06-23 stsp
6347 9b058f45 2022-06-30 mark static const struct got_error *
6348 9b058f45 2022-06-30 mark tree_scroll_down(struct tog_view *view, int maxscroll)
6349 ffd1d5e5 2018-06-23 stsp {
6350 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
6351 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6352 ffd1d5e5 2018-06-23 stsp int n = 0;
6353 ffd1d5e5 2018-06-23 stsp
6354 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6355 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6356 694d3271 2020-12-01 naddy s->first_displayed_entry);
6357 694d3271 2020-12-01 naddy else
6358 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6359 56e0773d 2019-11-28 stsp
6360 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6361 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
6362 9b058f45 2022-06-30 mark if (last)
6363 9b058f45 2022-06-30 mark last = got_tree_entry_get_next(s->tree, last);
6364 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6365 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6366 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6367 768394f3 2019-01-24 stsp }
6368 ffd1d5e5 2018-06-23 stsp }
6369 9b058f45 2022-06-30 mark
6370 9b058f45 2022-06-30 mark return NULL;
6371 ffd1d5e5 2018-06-23 stsp }
6372 ffd1d5e5 2018-06-23 stsp
6373 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6374 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6375 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6376 ffd1d5e5 2018-06-23 stsp {
6377 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6378 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6379 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6380 ffd1d5e5 2018-06-23 stsp
6381 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6382 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6383 56e0773d 2019-11-28 stsp + 1 /* slash */;
6384 ce52c690 2018-06-23 stsp if (te)
6385 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6386 ce52c690 2018-06-23 stsp
6387 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6388 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6389 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6390 ffd1d5e5 2018-06-23 stsp
6391 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6392 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6393 d9765a41 2018-06-23 stsp while (pt) {
6394 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6395 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6396 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6397 cb2ebc8a 2018-06-23 stsp goto done;
6398 cb2ebc8a 2018-06-23 stsp }
6399 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", 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 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6404 ffd1d5e5 2018-06-23 stsp }
6405 ce52c690 2018-06-23 stsp if (te) {
6406 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6407 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6408 ce52c690 2018-06-23 stsp goto done;
6409 ce52c690 2018-06-23 stsp }
6410 cb2ebc8a 2018-06-23 stsp }
6411 ce52c690 2018-06-23 stsp done:
6412 ce52c690 2018-06-23 stsp if (err) {
6413 ce52c690 2018-06-23 stsp free(*path);
6414 ce52c690 2018-06-23 stsp *path = NULL;
6415 ce52c690 2018-06-23 stsp }
6416 ce52c690 2018-06-23 stsp return err;
6417 ce52c690 2018-06-23 stsp }
6418 ce52c690 2018-06-23 stsp
6419 ce52c690 2018-06-23 stsp static const struct got_error *
6420 9b058f45 2022-06-30 mark blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6421 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6422 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6423 ce52c690 2018-06-23 stsp {
6424 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6425 ce52c690 2018-06-23 stsp char *path;
6426 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6427 a0de39f3 2019-08-09 stsp
6428 a0de39f3 2019-08-09 stsp *new_view = NULL;
6429 69efd4c4 2018-07-18 stsp
6430 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6431 ce52c690 2018-06-23 stsp if (err)
6432 ce52c690 2018-06-23 stsp return err;
6433 ffd1d5e5 2018-06-23 stsp
6434 9b058f45 2022-06-30 mark blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6435 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6436 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6437 83ce39e3 2019-08-12 stsp goto done;
6438 83ce39e3 2019-08-12 stsp }
6439 cdf1ee82 2018-08-01 stsp
6440 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6441 e5a0f69f 2018-08-18 stsp if (err) {
6442 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6443 fc06ba56 2019-08-22 stsp err = NULL;
6444 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6445 e5a0f69f 2018-08-18 stsp } else
6446 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6447 83ce39e3 2019-08-12 stsp done:
6448 83ce39e3 2019-08-12 stsp free(path);
6449 69efd4c4 2018-07-18 stsp return err;
6450 69efd4c4 2018-07-18 stsp }
6451 69efd4c4 2018-07-18 stsp
6452 69efd4c4 2018-07-18 stsp static const struct got_error *
6453 49b24ee5 2022-07-03 mark log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6454 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6455 69efd4c4 2018-07-18 stsp {
6456 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6457 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6458 69efd4c4 2018-07-18 stsp char *path;
6459 a0de39f3 2019-08-09 stsp
6460 a0de39f3 2019-08-09 stsp *new_view = NULL;
6461 69efd4c4 2018-07-18 stsp
6462 49b24ee5 2022-07-03 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6463 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6464 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6465 e5a0f69f 2018-08-18 stsp
6466 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6467 69efd4c4 2018-07-18 stsp if (err)
6468 69efd4c4 2018-07-18 stsp return err;
6469 69efd4c4 2018-07-18 stsp
6470 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6471 4e97c21c 2020-12-06 stsp path, 0);
6472 ba4f502b 2018-08-04 stsp if (err)
6473 e5a0f69f 2018-08-18 stsp view_close(log_view);
6474 e5a0f69f 2018-08-18 stsp else
6475 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6476 cb2ebc8a 2018-06-23 stsp free(path);
6477 cb2ebc8a 2018-06-23 stsp return err;
6478 ffd1d5e5 2018-06-23 stsp }
6479 ffd1d5e5 2018-06-23 stsp
6480 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6481 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6482 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6483 ffd1d5e5 2018-06-23 stsp {
6484 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6485 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6486 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6487 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6488 ffd1d5e5 2018-06-23 stsp
6489 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6490 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6491 bc573f3b 2021-07-10 stsp
6492 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6493 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6494 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6495 ffd1d5e5 2018-06-23 stsp
6496 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6497 bc573f3b 2021-07-10 stsp if (err)
6498 bc573f3b 2021-07-10 stsp goto done;
6499 bc573f3b 2021-07-10 stsp
6500 bc573f3b 2021-07-10 stsp /*
6501 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6502 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6503 bc573f3b 2021-07-10 stsp * closed on demand.
6504 bc573f3b 2021-07-10 stsp */
6505 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6506 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6507 bc573f3b 2021-07-10 stsp if (err)
6508 bc573f3b 2021-07-10 stsp goto done;
6509 bc573f3b 2021-07-10 stsp s->tree = s->root;
6510 bc573f3b 2021-07-10 stsp
6511 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6512 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6513 ffd1d5e5 2018-06-23 stsp goto done;
6514 ffd1d5e5 2018-06-23 stsp
6515 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6516 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6517 ffd1d5e5 2018-06-23 stsp goto done;
6518 ffd1d5e5 2018-06-23 stsp }
6519 ffd1d5e5 2018-06-23 stsp
6520 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6521 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6522 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6523 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6524 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6525 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6526 9cd7cbd1 2020-12-07 stsp goto done;
6527 9cd7cbd1 2020-12-07 stsp }
6528 9cd7cbd1 2020-12-07 stsp }
6529 fb2756b9 2018-08-04 stsp s->repo = repo;
6530 c0b01bdb 2019-11-08 stsp
6531 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6532 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6533 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6534 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6535 c0b01bdb 2019-11-08 stsp if (err)
6536 c0b01bdb 2019-11-08 stsp goto done;
6537 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6538 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6539 bc573f3b 2021-07-10 stsp if (err)
6540 c0b01bdb 2019-11-08 stsp goto done;
6541 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6542 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6543 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6544 bc573f3b 2021-07-10 stsp if (err)
6545 c0b01bdb 2019-11-08 stsp goto done;
6546 e5a0f69f 2018-08-18 stsp
6547 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6548 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6549 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6550 bc573f3b 2021-07-10 stsp if (err)
6551 11b20872 2019-11-08 stsp goto done;
6552 11b20872 2019-11-08 stsp
6553 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6554 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6555 bc573f3b 2021-07-10 stsp if (err)
6556 c0b01bdb 2019-11-08 stsp goto done;
6557 c0b01bdb 2019-11-08 stsp }
6558 c0b01bdb 2019-11-08 stsp
6559 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6560 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6561 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6562 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6563 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6564 ad80ab7b 2018-08-04 stsp done:
6565 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6566 bc573f3b 2021-07-10 stsp if (commit)
6567 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6568 bc573f3b 2021-07-10 stsp if (err)
6569 bc573f3b 2021-07-10 stsp close_tree_view(view);
6570 ad80ab7b 2018-08-04 stsp return err;
6571 ad80ab7b 2018-08-04 stsp }
6572 ad80ab7b 2018-08-04 stsp
6573 e5a0f69f 2018-08-18 stsp static const struct got_error *
6574 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6575 ad80ab7b 2018-08-04 stsp {
6576 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6577 ad80ab7b 2018-08-04 stsp
6578 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6579 fb2756b9 2018-08-04 stsp free(s->tree_label);
6580 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6581 6484ec90 2018-09-29 stsp free(s->commit_id);
6582 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6583 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6584 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6585 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6586 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6587 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6588 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6589 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6590 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6591 ad80ab7b 2018-08-04 stsp free(parent);
6592 ad80ab7b 2018-08-04 stsp
6593 ad80ab7b 2018-08-04 stsp }
6594 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6595 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6596 bc573f3b 2021-07-10 stsp if (s->root)
6597 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6598 7c32bd05 2019-06-22 stsp return NULL;
6599 7c32bd05 2019-06-22 stsp }
6600 7c32bd05 2019-06-22 stsp
6601 7c32bd05 2019-06-22 stsp static const struct got_error *
6602 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6603 7c32bd05 2019-06-22 stsp {
6604 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6605 7c32bd05 2019-06-22 stsp
6606 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6607 7c32bd05 2019-06-22 stsp return NULL;
6608 7c32bd05 2019-06-22 stsp }
6609 7c32bd05 2019-06-22 stsp
6610 7c32bd05 2019-06-22 stsp static int
6611 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6612 7c32bd05 2019-06-22 stsp {
6613 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6614 7c32bd05 2019-06-22 stsp
6615 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6616 56e0773d 2019-11-28 stsp 0) == 0;
6617 7c32bd05 2019-06-22 stsp }
6618 7c32bd05 2019-06-22 stsp
6619 7c32bd05 2019-06-22 stsp static const struct got_error *
6620 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6621 7c32bd05 2019-06-22 stsp {
6622 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6623 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6624 7c32bd05 2019-06-22 stsp
6625 7c32bd05 2019-06-22 stsp if (!view->searching) {
6626 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6627 7c32bd05 2019-06-22 stsp return NULL;
6628 7c32bd05 2019-06-22 stsp }
6629 7c32bd05 2019-06-22 stsp
6630 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6631 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6632 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6633 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6634 56e0773d 2019-11-28 stsp s->selected_entry);
6635 7c32bd05 2019-06-22 stsp else
6636 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6637 56e0773d 2019-11-28 stsp } else {
6638 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6639 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6640 56e0773d 2019-11-28 stsp else
6641 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6642 56e0773d 2019-11-28 stsp s->selected_entry);
6643 7c32bd05 2019-06-22 stsp }
6644 7c32bd05 2019-06-22 stsp } else {
6645 487cd7d2 2021-12-17 stsp if (s->selected_entry)
6646 487cd7d2 2021-12-17 stsp te = s->selected_entry;
6647 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
6648 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6649 56e0773d 2019-11-28 stsp else
6650 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6651 7c32bd05 2019-06-22 stsp }
6652 7c32bd05 2019-06-22 stsp
6653 7c32bd05 2019-06-22 stsp while (1) {
6654 56e0773d 2019-11-28 stsp if (te == NULL) {
6655 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6656 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6657 ac66afb8 2019-06-24 stsp return NULL;
6658 ac66afb8 2019-06-24 stsp }
6659 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6660 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6661 56e0773d 2019-11-28 stsp else
6662 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6663 7c32bd05 2019-06-22 stsp }
6664 7c32bd05 2019-06-22 stsp
6665 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6666 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6667 56e0773d 2019-11-28 stsp s->matched_entry = te;
6668 7c32bd05 2019-06-22 stsp break;
6669 7c32bd05 2019-06-22 stsp }
6670 7c32bd05 2019-06-22 stsp
6671 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6672 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6673 56e0773d 2019-11-28 stsp else
6674 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6675 7c32bd05 2019-06-22 stsp }
6676 e5a0f69f 2018-08-18 stsp
6677 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6678 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6679 7c32bd05 2019-06-22 stsp s->selected = 0;
6680 7c32bd05 2019-06-22 stsp }
6681 7c32bd05 2019-06-22 stsp
6682 e5a0f69f 2018-08-18 stsp return NULL;
6683 ad80ab7b 2018-08-04 stsp }
6684 ad80ab7b 2018-08-04 stsp
6685 ad80ab7b 2018-08-04 stsp static const struct got_error *
6686 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6687 ad80ab7b 2018-08-04 stsp {
6688 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6689 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6690 e5a0f69f 2018-08-18 stsp char *parent_path;
6691 ad80ab7b 2018-08-04 stsp
6692 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6693 e5a0f69f 2018-08-18 stsp if (err)
6694 e5a0f69f 2018-08-18 stsp return err;
6695 ffd1d5e5 2018-06-23 stsp
6696 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6697 e5a0f69f 2018-08-18 stsp free(parent_path);
6698 669b5ffa 2018-10-07 stsp
6699 9b058f45 2022-06-30 mark view_border(view);
6700 e5a0f69f 2018-08-18 stsp return err;
6701 e5a0f69f 2018-08-18 stsp }
6702 ce52c690 2018-06-23 stsp
6703 e5a0f69f 2018-08-18 stsp static const struct got_error *
6704 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6705 e5a0f69f 2018-08-18 stsp {
6706 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6707 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6708 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6709 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 3;
6710 ffd1d5e5 2018-06-23 stsp
6711 e5a0f69f 2018-08-18 stsp switch (ch) {
6712 1e37a5c2 2019-05-12 jcs case 'i':
6713 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6714 640cd7ff 2022-06-22 mark view->count = 0;
6715 1e37a5c2 2019-05-12 jcs break;
6716 5e98fb33 2022-07-22 mark case 'L':
6717 640cd7ff 2022-06-22 mark view->count = 0;
6718 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6719 ffd1d5e5 2018-06-23 stsp break;
6720 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
6721 152c1c93 2020-11-29 stsp break;
6722 5e98fb33 2022-07-22 mark case 'R':
6723 640cd7ff 2022-06-22 mark view->count = 0;
6724 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_REF);
6725 e4526bf5 2021-09-03 naddy break;
6726 e4526bf5 2021-09-03 naddy case 'g':
6727 e4526bf5 2021-09-03 naddy case KEY_HOME:
6728 e4526bf5 2021-09-03 naddy s->selected = 0;
6729 640cd7ff 2022-06-22 mark view->count = 0;
6730 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6731 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6732 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6733 e4526bf5 2021-09-03 naddy else
6734 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6735 1e37a5c2 2019-05-12 jcs break;
6736 e4526bf5 2021-09-03 naddy case 'G':
6737 9b058f45 2022-06-30 mark case KEY_END: {
6738 9b058f45 2022-06-30 mark int eos = view->nlines - 3;
6739 9b058f45 2022-06-30 mark
6740 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
6741 9b058f45 2022-06-30 mark --eos; /* border */
6742 e4526bf5 2021-09-03 naddy s->selected = 0;
6743 640cd7ff 2022-06-22 mark view->count = 0;
6744 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6745 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
6746 e4526bf5 2021-09-03 naddy if (te == NULL) {
6747 ad055527 2022-07-16 mark if (s->tree != s->root) {
6748 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6749 e4526bf5 2021-09-03 naddy n++;
6750 e4526bf5 2021-09-03 naddy }
6751 e4526bf5 2021-09-03 naddy break;
6752 e4526bf5 2021-09-03 naddy }
6753 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6754 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6755 e4526bf5 2021-09-03 naddy }
6756 e4526bf5 2021-09-03 naddy if (n > 0)
6757 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6758 e4526bf5 2021-09-03 naddy break;
6759 9b058f45 2022-06-30 mark }
6760 1e37a5c2 2019-05-12 jcs case 'k':
6761 1e37a5c2 2019-05-12 jcs case KEY_UP:
6762 02ffd0d5 2021-10-17 stsp case CTRL('p'):
6763 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6764 1e37a5c2 2019-05-12 jcs s->selected--;
6765 fa86c4bf 2020-11-29 stsp break;
6766 1e37a5c2 2019-05-12 jcs }
6767 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6768 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6769 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6770 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6771 640cd7ff 2022-06-22 mark view->count = 0;
6772 1e37a5c2 2019-05-12 jcs break;
6773 83cc4199 2022-06-13 stsp case CTRL('u'):
6774 33c3719a 2022-06-15 stsp case 'u':
6775 83cc4199 2022-06-13 stsp nscroll /= 2;
6776 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6777 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6778 ea025d1d 2020-02-22 naddy case CTRL('b'):
6779 61417565 2022-06-20 mark case 'b':
6780 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6781 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6782 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6783 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6784 fa86c4bf 2020-11-29 stsp } else {
6785 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6786 83cc4199 2022-06-13 stsp s->selected -= MIN(s->selected, nscroll);
6787 fa86c4bf 2020-11-29 stsp }
6788 83cc4199 2022-06-13 stsp tree_scroll_up(s, MAX(0, nscroll));
6789 640cd7ff 2022-06-22 mark if (s->selected_entry == NULL ||
6790 640cd7ff 2022-06-22 mark (s->tree == s->root && s->selected_entry ==
6791 640cd7ff 2022-06-22 mark got_object_tree_get_first_entry(s->tree)))
6792 640cd7ff 2022-06-22 mark view->count = 0;
6793 1e37a5c2 2019-05-12 jcs break;
6794 1e37a5c2 2019-05-12 jcs case 'j':
6795 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6796 02ffd0d5 2021-10-17 stsp case CTRL('n'):
6797 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6798 1e37a5c2 2019-05-12 jcs s->selected++;
6799 1e37a5c2 2019-05-12 jcs break;
6800 1e37a5c2 2019-05-12 jcs }
6801 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6802 640cd7ff 2022-06-22 mark == NULL) {
6803 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6804 640cd7ff 2022-06-22 mark view->count = 0;
6805 1e37a5c2 2019-05-12 jcs break;
6806 640cd7ff 2022-06-22 mark }
6807 9b058f45 2022-06-30 mark tree_scroll_down(view, 1);
6808 1e37a5c2 2019-05-12 jcs break;
6809 83cc4199 2022-06-13 stsp case CTRL('d'):
6810 33c3719a 2022-06-15 stsp case 'd':
6811 83cc4199 2022-06-13 stsp nscroll /= 2;
6812 83cc4199 2022-06-13 stsp /* FALL THROUGH */
6813 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6814 ea025d1d 2020-02-22 naddy case CTRL('f'):
6815 61417565 2022-06-20 mark case 'f':
6816 48bb96f0 2022-06-20 naddy case ' ':
6817 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6818 1e37a5c2 2019-05-12 jcs == NULL) {
6819 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6820 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6821 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
6822 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
6823 640cd7ff 2022-06-22 mark else
6824 640cd7ff 2022-06-22 mark view->count = 0;
6825 1e37a5c2 2019-05-12 jcs break;
6826 1e37a5c2 2019-05-12 jcs }
6827 9b058f45 2022-06-30 mark tree_scroll_down(view, nscroll);
6828 1e37a5c2 2019-05-12 jcs break;
6829 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6830 1e37a5c2 2019-05-12 jcs case '\r':
6831 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6832 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6833 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6834 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6835 640cd7ff 2022-06-22 mark if (s->tree == s->root) {
6836 640cd7ff 2022-06-22 mark view->count = 0;
6837 1e37a5c2 2019-05-12 jcs break;
6838 640cd7ff 2022-06-22 mark }
6839 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6840 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6841 1e37a5c2 2019-05-12 jcs entry);
6842 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6843 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6844 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6845 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6846 1e37a5c2 2019-05-12 jcs s->selected_entry =
6847 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6848 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6849 9b058f45 2022-06-30 mark if (s->selected > view->nlines - 3) {
6850 9b058f45 2022-06-30 mark err = offset_selection_down(view);
6851 9b058f45 2022-06-30 mark if (err)
6852 9b058f45 2022-06-30 mark break;
6853 9b058f45 2022-06-30 mark }
6854 1e37a5c2 2019-05-12 jcs free(parent);
6855 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6856 56e0773d 2019-11-28 stsp s->selected_entry))) {
6857 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6858 640cd7ff 2022-06-22 mark view->count = 0;
6859 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6860 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6861 1e37a5c2 2019-05-12 jcs if (err)
6862 1e37a5c2 2019-05-12 jcs break;
6863 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6864 941e9f74 2019-05-21 stsp if (err) {
6865 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6866 1e37a5c2 2019-05-12 jcs break;
6867 1e37a5c2 2019-05-12 jcs }
6868 136e2bd4 2022-07-23 mark } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6869 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6870 1e37a5c2 2019-05-12 jcs break;
6871 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6872 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6873 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6874 640cd7ff 2022-06-22 mark view->count = 0;
6875 1e37a5c2 2019-05-12 jcs break;
6876 1e37a5c2 2019-05-12 jcs default:
6877 640cd7ff 2022-06-22 mark view->count = 0;
6878 1e37a5c2 2019-05-12 jcs break;
6879 ffd1d5e5 2018-06-23 stsp }
6880 e5a0f69f 2018-08-18 stsp
6881 ffd1d5e5 2018-06-23 stsp return err;
6882 9f7d7167 2018-04-29 stsp }
6883 9f7d7167 2018-04-29 stsp
6884 ffd1d5e5 2018-06-23 stsp __dead static void
6885 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6886 ffd1d5e5 2018-06-23 stsp {
6887 ffd1d5e5 2018-06-23 stsp endwin();
6888 87411fa9 2022-06-16 stsp fprintf(stderr,
6889 87411fa9 2022-06-16 stsp "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6890 ffd1d5e5 2018-06-23 stsp getprogname());
6891 ffd1d5e5 2018-06-23 stsp exit(1);
6892 ffd1d5e5 2018-06-23 stsp }
6893 ffd1d5e5 2018-06-23 stsp
6894 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6895 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6896 ffd1d5e5 2018-06-23 stsp {
6897 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6898 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6899 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6900 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6901 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6902 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
6903 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6904 4e97c21c 2020-12-06 stsp char *label = NULL;
6905 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6906 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6907 ffd1d5e5 2018-06-23 stsp int ch;
6908 5221c383 2018-08-01 stsp struct tog_view *view;
6909 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
6910 70ac5f84 2019-03-28 stsp
6911 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6912 ffd1d5e5 2018-06-23 stsp switch (ch) {
6913 ffd1d5e5 2018-06-23 stsp case 'c':
6914 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6915 74283ab8 2020-02-07 stsp break;
6916 74283ab8 2020-02-07 stsp case 'r':
6917 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6918 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6919 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6920 74283ab8 2020-02-07 stsp optarg);
6921 ffd1d5e5 2018-06-23 stsp break;
6922 ffd1d5e5 2018-06-23 stsp default:
6923 e99e2d15 2020-11-24 naddy usage_tree();
6924 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6925 ffd1d5e5 2018-06-23 stsp }
6926 ffd1d5e5 2018-06-23 stsp }
6927 ffd1d5e5 2018-06-23 stsp
6928 ffd1d5e5 2018-06-23 stsp argc -= optind;
6929 ffd1d5e5 2018-06-23 stsp argv += optind;
6930 ffd1d5e5 2018-06-23 stsp
6931 55cccc34 2020-02-20 stsp if (argc > 1)
6932 e99e2d15 2020-11-24 naddy usage_tree();
6933 0ae84acc 2022-06-15 tracey
6934 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
6935 0ae84acc 2022-06-15 tracey if (error != NULL)
6936 0ae84acc 2022-06-15 tracey goto done;
6937 74283ab8 2020-02-07 stsp
6938 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6939 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6940 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6941 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6942 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6943 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6944 c156c7a4 2020-12-18 stsp goto done;
6945 55cccc34 2020-02-20 stsp if (worktree)
6946 52185f70 2019-02-05 stsp repo_path =
6947 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6948 55cccc34 2020-02-20 stsp else
6949 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6950 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6951 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6952 c156c7a4 2020-12-18 stsp goto done;
6953 c156c7a4 2020-12-18 stsp }
6954 55cccc34 2020-02-20 stsp }
6955 a915003a 2019-02-05 stsp
6956 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6957 c02c541e 2019-03-29 stsp if (error != NULL)
6958 52185f70 2019-02-05 stsp goto done;
6959 d188b9a6 2019-01-04 stsp
6960 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6961 55cccc34 2020-02-20 stsp repo, worktree);
6962 55cccc34 2020-02-20 stsp if (error)
6963 55cccc34 2020-02-20 stsp goto done;
6964 55cccc34 2020-02-20 stsp
6965 55cccc34 2020-02-20 stsp init_curses();
6966 55cccc34 2020-02-20 stsp
6967 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6968 c02c541e 2019-03-29 stsp if (error)
6969 52185f70 2019-02-05 stsp goto done;
6970 ffd1d5e5 2018-06-23 stsp
6971 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
6972 51a10b52 2020-12-26 stsp if (error)
6973 51a10b52 2020-12-26 stsp goto done;
6974 51a10b52 2020-12-26 stsp
6975 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6976 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6977 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6978 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6979 4e97c21c 2020-12-06 stsp if (error)
6980 4e97c21c 2020-12-06 stsp goto done;
6981 4e97c21c 2020-12-06 stsp head_ref_name = label;
6982 4e97c21c 2020-12-06 stsp } else {
6983 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6984 4e97c21c 2020-12-06 stsp if (error == NULL)
6985 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6986 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6987 4e97c21c 2020-12-06 stsp goto done;
6988 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6989 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6990 4e97c21c 2020-12-06 stsp if (error)
6991 4e97c21c 2020-12-06 stsp goto done;
6992 4e97c21c 2020-12-06 stsp }
6993 ffd1d5e5 2018-06-23 stsp
6994 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6995 a44927cc 2022-04-07 stsp if (error)
6996 a44927cc 2022-04-07 stsp goto done;
6997 a44927cc 2022-04-07 stsp
6998 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6999 5221c383 2018-08-01 stsp if (view == NULL) {
7000 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7001 5221c383 2018-08-01 stsp goto done;
7002 5221c383 2018-08-01 stsp }
7003 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7004 ad80ab7b 2018-08-04 stsp if (error)
7005 ad80ab7b 2018-08-04 stsp goto done;
7006 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7007 a44927cc 2022-04-07 stsp error = tree_view_walk_path(&view->state.tree, commit,
7008 d91faf3b 2020-12-01 naddy in_repo_path);
7009 55cccc34 2020-02-20 stsp if (error)
7010 55cccc34 2020-02-20 stsp goto done;
7011 55cccc34 2020-02-20 stsp }
7012 55cccc34 2020-02-20 stsp
7013 55cccc34 2020-02-20 stsp if (worktree) {
7014 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7015 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7016 55cccc34 2020-02-20 stsp worktree = NULL;
7017 55cccc34 2020-02-20 stsp }
7018 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7019 ffd1d5e5 2018-06-23 stsp done:
7020 52185f70 2019-02-05 stsp free(repo_path);
7021 e4a0e26d 2020-02-20 stsp free(cwd);
7022 ffd1d5e5 2018-06-23 stsp free(commit_id);
7023 4e97c21c 2020-12-06 stsp free(label);
7024 486cd271 2020-12-06 stsp if (ref)
7025 486cd271 2020-12-06 stsp got_ref_close(ref);
7026 1d0f4054 2021-06-17 stsp if (repo) {
7027 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7028 1d0f4054 2021-06-17 stsp if (error == NULL)
7029 1d0f4054 2021-06-17 stsp error = close_err;
7030 1d0f4054 2021-06-17 stsp }
7031 0ae84acc 2022-06-15 tracey if (pack_fds) {
7032 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7033 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7034 0ae84acc 2022-06-15 tracey if (error == NULL)
7035 0ae84acc 2022-06-15 tracey error = pack_err;
7036 0ae84acc 2022-06-15 tracey }
7037 51a10b52 2020-12-26 stsp tog_free_refs();
7038 ffd1d5e5 2018-06-23 stsp return error;
7039 6458efa5 2020-11-24 stsp }
7040 6458efa5 2020-11-24 stsp
7041 6458efa5 2020-11-24 stsp static const struct got_error *
7042 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7043 6458efa5 2020-11-24 stsp {
7044 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7045 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7046 6458efa5 2020-11-24 stsp
7047 6458efa5 2020-11-24 stsp s->nrefs = 0;
7048 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7049 cc488aa7 2022-01-23 stsp if (strncmp(got_ref_get_name(sre->ref),
7050 cc488aa7 2022-01-23 stsp "refs/got/", 9) == 0 &&
7051 cc488aa7 2022-01-23 stsp strncmp(got_ref_get_name(sre->ref),
7052 cc488aa7 2022-01-23 stsp "refs/got/backup/", 16) != 0)
7053 6458efa5 2020-11-24 stsp continue;
7054 6458efa5 2020-11-24 stsp
7055 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7056 6458efa5 2020-11-24 stsp if (re == NULL)
7057 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7058 6458efa5 2020-11-24 stsp
7059 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7060 8924d611 2020-12-26 stsp if (re->ref == NULL)
7061 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7062 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7063 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7064 6458efa5 2020-11-24 stsp }
7065 6458efa5 2020-11-24 stsp
7066 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7067 6458efa5 2020-11-24 stsp return NULL;
7068 6458efa5 2020-11-24 stsp }
7069 6458efa5 2020-11-24 stsp
7070 336075a4 2022-06-25 op static void
7071 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7072 6458efa5 2020-11-24 stsp {
7073 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7074 6458efa5 2020-11-24 stsp
7075 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7076 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7077 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7078 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7079 6458efa5 2020-11-24 stsp free(re);
7080 6458efa5 2020-11-24 stsp }
7081 6458efa5 2020-11-24 stsp }
7082 6458efa5 2020-11-24 stsp
7083 6458efa5 2020-11-24 stsp static const struct got_error *
7084 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7085 6458efa5 2020-11-24 stsp {
7086 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7087 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7088 6458efa5 2020-11-24 stsp
7089 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7090 6458efa5 2020-11-24 stsp s->repo = repo;
7091 6458efa5 2020-11-24 stsp
7092 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7093 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7094 6458efa5 2020-11-24 stsp
7095 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7096 6458efa5 2020-11-24 stsp if (err)
7097 6458efa5 2020-11-24 stsp return err;
7098 34ba6917 2020-11-30 stsp
7099 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7100 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7101 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7102 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7103 6458efa5 2020-11-24 stsp if (err)
7104 6458efa5 2020-11-24 stsp goto done;
7105 6458efa5 2020-11-24 stsp
7106 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7107 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7108 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7109 6458efa5 2020-11-24 stsp if (err)
7110 6458efa5 2020-11-24 stsp goto done;
7111 6458efa5 2020-11-24 stsp
7112 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7113 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7114 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7115 cc488aa7 2022-01-23 stsp if (err)
7116 cc488aa7 2022-01-23 stsp goto done;
7117 cc488aa7 2022-01-23 stsp
7118 cc488aa7 2022-01-23 stsp err = add_color(&s->colors, "^refs/got/backup/",
7119 cc488aa7 2022-01-23 stsp TOG_COLOR_REFS_BACKUP,
7120 cc488aa7 2022-01-23 stsp get_color_value("TOG_COLOR_REFS_BACKUP"));
7121 6458efa5 2020-11-24 stsp if (err)
7122 6458efa5 2020-11-24 stsp goto done;
7123 6458efa5 2020-11-24 stsp }
7124 6458efa5 2020-11-24 stsp
7125 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7126 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7127 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7128 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7129 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7130 6458efa5 2020-11-24 stsp done:
7131 6458efa5 2020-11-24 stsp if (err)
7132 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7133 6458efa5 2020-11-24 stsp return err;
7134 6458efa5 2020-11-24 stsp }
7135 6458efa5 2020-11-24 stsp
7136 6458efa5 2020-11-24 stsp static const struct got_error *
7137 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7138 6458efa5 2020-11-24 stsp {
7139 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7140 6458efa5 2020-11-24 stsp
7141 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7142 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7143 6458efa5 2020-11-24 stsp
7144 6458efa5 2020-11-24 stsp return NULL;
7145 9f7d7167 2018-04-29 stsp }
7146 ce5b7c56 2019-07-09 stsp
7147 6458efa5 2020-11-24 stsp static const struct got_error *
7148 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7149 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7150 6458efa5 2020-11-24 stsp {
7151 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7152 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7153 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7154 6458efa5 2020-11-24 stsp int obj_type;
7155 6458efa5 2020-11-24 stsp
7156 c42c9805 2020-11-24 stsp *commit_id = NULL;
7157 6458efa5 2020-11-24 stsp
7158 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7159 6458efa5 2020-11-24 stsp if (err)
7160 6458efa5 2020-11-24 stsp return err;
7161 6458efa5 2020-11-24 stsp
7162 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7163 6458efa5 2020-11-24 stsp if (err)
7164 6458efa5 2020-11-24 stsp goto done;
7165 6458efa5 2020-11-24 stsp
7166 6458efa5 2020-11-24 stsp switch (obj_type) {
7167 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7168 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7169 6458efa5 2020-11-24 stsp break;
7170 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7171 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7172 6458efa5 2020-11-24 stsp if (err)
7173 6458efa5 2020-11-24 stsp goto done;
7174 c42c9805 2020-11-24 stsp free(obj_id);
7175 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7176 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7177 c42c9805 2020-11-24 stsp if (err)
7178 6458efa5 2020-11-24 stsp goto done;
7179 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7180 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7181 c42c9805 2020-11-24 stsp goto done;
7182 c42c9805 2020-11-24 stsp }
7183 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7184 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7185 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7186 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7187 c42c9805 2020-11-24 stsp goto done;
7188 c42c9805 2020-11-24 stsp }
7189 6458efa5 2020-11-24 stsp break;
7190 6458efa5 2020-11-24 stsp default:
7191 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7192 c42c9805 2020-11-24 stsp break;
7193 6458efa5 2020-11-24 stsp }
7194 6458efa5 2020-11-24 stsp
7195 c42c9805 2020-11-24 stsp done:
7196 c42c9805 2020-11-24 stsp if (tag)
7197 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7198 c42c9805 2020-11-24 stsp if (err) {
7199 c42c9805 2020-11-24 stsp free(*commit_id);
7200 c42c9805 2020-11-24 stsp *commit_id = NULL;
7201 c42c9805 2020-11-24 stsp }
7202 c42c9805 2020-11-24 stsp return err;
7203 c42c9805 2020-11-24 stsp }
7204 c42c9805 2020-11-24 stsp
7205 c42c9805 2020-11-24 stsp static const struct got_error *
7206 9b058f45 2022-06-30 mark log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7207 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7208 c42c9805 2020-11-24 stsp {
7209 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7210 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7211 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7212 c42c9805 2020-11-24 stsp
7213 c42c9805 2020-11-24 stsp *new_view = NULL;
7214 c42c9805 2020-11-24 stsp
7215 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7216 c42c9805 2020-11-24 stsp if (err) {
7217 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7218 c42c9805 2020-11-24 stsp return err;
7219 c42c9805 2020-11-24 stsp else
7220 c42c9805 2020-11-24 stsp return NULL;
7221 c42c9805 2020-11-24 stsp }
7222 c42c9805 2020-11-24 stsp
7223 9b058f45 2022-06-30 mark log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7224 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7225 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7226 6458efa5 2020-11-24 stsp goto done;
7227 6458efa5 2020-11-24 stsp }
7228 6458efa5 2020-11-24 stsp
7229 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7230 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7231 6458efa5 2020-11-24 stsp done:
7232 6458efa5 2020-11-24 stsp if (err)
7233 6458efa5 2020-11-24 stsp view_close(log_view);
7234 6458efa5 2020-11-24 stsp else
7235 6458efa5 2020-11-24 stsp *new_view = log_view;
7236 c42c9805 2020-11-24 stsp free(commit_id);
7237 6458efa5 2020-11-24 stsp return err;
7238 6458efa5 2020-11-24 stsp }
7239 6458efa5 2020-11-24 stsp
7240 ce5b7c56 2019-07-09 stsp static void
7241 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7242 6458efa5 2020-11-24 stsp {
7243 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7244 34ba6917 2020-11-30 stsp int i = 0;
7245 6458efa5 2020-11-24 stsp
7246 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7247 6458efa5 2020-11-24 stsp return;
7248 6458efa5 2020-11-24 stsp
7249 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7250 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7251 34ba6917 2020-11-30 stsp if (re == NULL)
7252 34ba6917 2020-11-30 stsp break;
7253 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7254 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7255 6458efa5 2020-11-24 stsp }
7256 6458efa5 2020-11-24 stsp }
7257 6458efa5 2020-11-24 stsp
7258 9b058f45 2022-06-30 mark static const struct got_error *
7259 9b058f45 2022-06-30 mark ref_scroll_down(struct tog_view *view, int maxscroll)
7260 6458efa5 2020-11-24 stsp {
7261 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7262 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7263 6458efa5 2020-11-24 stsp int n = 0;
7264 6458efa5 2020-11-24 stsp
7265 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7266 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7267 6458efa5 2020-11-24 stsp else
7268 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7269 6458efa5 2020-11-24 stsp
7270 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7271 9b058f45 2022-06-30 mark while (next && n++ < maxscroll) {
7272 9b058f45 2022-06-30 mark if (last)
7273 9b058f45 2022-06-30 mark last = TAILQ_NEXT(last, entry);
7274 9b058f45 2022-06-30 mark if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7275 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7276 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7277 6458efa5 2020-11-24 stsp }
7278 6458efa5 2020-11-24 stsp }
7279 9b058f45 2022-06-30 mark
7280 9b058f45 2022-06-30 mark return NULL;
7281 6458efa5 2020-11-24 stsp }
7282 6458efa5 2020-11-24 stsp
7283 6458efa5 2020-11-24 stsp static const struct got_error *
7284 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7285 6458efa5 2020-11-24 stsp {
7286 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7287 6458efa5 2020-11-24 stsp
7288 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7289 6458efa5 2020-11-24 stsp return NULL;
7290 6458efa5 2020-11-24 stsp }
7291 6458efa5 2020-11-24 stsp
7292 6458efa5 2020-11-24 stsp static int
7293 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7294 6458efa5 2020-11-24 stsp {
7295 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7296 6458efa5 2020-11-24 stsp
7297 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7298 6458efa5 2020-11-24 stsp 0) == 0;
7299 6458efa5 2020-11-24 stsp }
7300 6458efa5 2020-11-24 stsp
7301 6458efa5 2020-11-24 stsp static const struct got_error *
7302 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7303 6458efa5 2020-11-24 stsp {
7304 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7305 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7306 6458efa5 2020-11-24 stsp
7307 6458efa5 2020-11-24 stsp if (!view->searching) {
7308 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7309 6458efa5 2020-11-24 stsp return NULL;
7310 6458efa5 2020-11-24 stsp }
7311 6458efa5 2020-11-24 stsp
7312 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7313 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7314 6458efa5 2020-11-24 stsp if (s->selected_entry)
7315 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7316 6458efa5 2020-11-24 stsp else
7317 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7318 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7319 6458efa5 2020-11-24 stsp } else {
7320 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7321 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7322 6458efa5 2020-11-24 stsp else
7323 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7324 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7325 6458efa5 2020-11-24 stsp }
7326 6458efa5 2020-11-24 stsp } else {
7327 487cd7d2 2021-12-17 stsp if (s->selected_entry)
7328 487cd7d2 2021-12-17 stsp re = s->selected_entry;
7329 487cd7d2 2021-12-17 stsp else if (view->searching == TOG_SEARCH_FORWARD)
7330 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7331 6458efa5 2020-11-24 stsp else
7332 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7333 6458efa5 2020-11-24 stsp }
7334 6458efa5 2020-11-24 stsp
7335 6458efa5 2020-11-24 stsp while (1) {
7336 6458efa5 2020-11-24 stsp if (re == NULL) {
7337 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7338 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7339 6458efa5 2020-11-24 stsp return NULL;
7340 6458efa5 2020-11-24 stsp }
7341 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7342 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7343 6458efa5 2020-11-24 stsp else
7344 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7345 6458efa5 2020-11-24 stsp }
7346 6458efa5 2020-11-24 stsp
7347 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7348 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7349 6458efa5 2020-11-24 stsp s->matched_entry = re;
7350 6458efa5 2020-11-24 stsp break;
7351 6458efa5 2020-11-24 stsp }
7352 6458efa5 2020-11-24 stsp
7353 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7354 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7355 6458efa5 2020-11-24 stsp else
7356 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7357 6458efa5 2020-11-24 stsp }
7358 6458efa5 2020-11-24 stsp
7359 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7360 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7361 6458efa5 2020-11-24 stsp s->selected = 0;
7362 6458efa5 2020-11-24 stsp }
7363 6458efa5 2020-11-24 stsp
7364 6458efa5 2020-11-24 stsp return NULL;
7365 6458efa5 2020-11-24 stsp }
7366 6458efa5 2020-11-24 stsp
7367 6458efa5 2020-11-24 stsp static const struct got_error *
7368 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7369 6458efa5 2020-11-24 stsp {
7370 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7371 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7372 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7373 6458efa5 2020-11-24 stsp char *line = NULL;
7374 6458efa5 2020-11-24 stsp wchar_t *wline;
7375 6458efa5 2020-11-24 stsp struct tog_color *tc;
7376 6458efa5 2020-11-24 stsp int width, n;
7377 6458efa5 2020-11-24 stsp int limit = view->nlines;
7378 6458efa5 2020-11-24 stsp
7379 6458efa5 2020-11-24 stsp werase(view->window);
7380 6458efa5 2020-11-24 stsp
7381 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7382 49b24ee5 2022-07-03 mark if (view_is_hsplit_top(view))
7383 9b058f45 2022-06-30 mark --limit; /* border */
7384 6458efa5 2020-11-24 stsp
7385 6458efa5 2020-11-24 stsp if (limit == 0)
7386 6458efa5 2020-11-24 stsp return NULL;
7387 6458efa5 2020-11-24 stsp
7388 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7389 6458efa5 2020-11-24 stsp
7390 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7391 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7392 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7393 6458efa5 2020-11-24 stsp
7394 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7395 6458efa5 2020-11-24 stsp if (err) {
7396 6458efa5 2020-11-24 stsp free(line);
7397 6458efa5 2020-11-24 stsp return err;
7398 6458efa5 2020-11-24 stsp }
7399 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7400 6458efa5 2020-11-24 stsp wstandout(view->window);
7401 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7402 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7403 6458efa5 2020-11-24 stsp wstandend(view->window);
7404 6458efa5 2020-11-24 stsp free(wline);
7405 6458efa5 2020-11-24 stsp wline = NULL;
7406 6458efa5 2020-11-24 stsp free(line);
7407 6458efa5 2020-11-24 stsp line = NULL;
7408 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7409 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7410 6458efa5 2020-11-24 stsp if (--limit <= 0)
7411 6458efa5 2020-11-24 stsp return NULL;
7412 6458efa5 2020-11-24 stsp
7413 6458efa5 2020-11-24 stsp n = 0;
7414 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7415 6458efa5 2020-11-24 stsp char *line = NULL;
7416 b4996bee 2022-06-16 stsp char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7417 6458efa5 2020-11-24 stsp
7418 b4996bee 2022-06-16 stsp if (s->show_date) {
7419 b4996bee 2022-06-16 stsp struct got_commit_object *ci;
7420 b4996bee 2022-06-16 stsp struct got_tag_object *tag;
7421 b4996bee 2022-06-16 stsp struct got_object_id *id;
7422 b4996bee 2022-06-16 stsp struct tm tm;
7423 b4996bee 2022-06-16 stsp time_t t;
7424 b4996bee 2022-06-16 stsp
7425 b4996bee 2022-06-16 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7426 b4996bee 2022-06-16 stsp if (err)
7427 b4996bee 2022-06-16 stsp return err;
7428 b4996bee 2022-06-16 stsp err = got_object_open_as_tag(&tag, s->repo, id);
7429 b4996bee 2022-06-16 stsp if (err) {
7430 b4996bee 2022-06-16 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
7431 b4996bee 2022-06-16 stsp free(id);
7432 b4996bee 2022-06-16 stsp return err;
7433 b4996bee 2022-06-16 stsp }
7434 b4996bee 2022-06-16 stsp err = got_object_open_as_commit(&ci, s->repo,
7435 b4996bee 2022-06-16 stsp id);
7436 b4996bee 2022-06-16 stsp if (err) {
7437 b4996bee 2022-06-16 stsp free(id);
7438 b4996bee 2022-06-16 stsp return err;
7439 b4996bee 2022-06-16 stsp }
7440 b4996bee 2022-06-16 stsp t = got_object_commit_get_committer_time(ci);
7441 b4996bee 2022-06-16 stsp got_object_commit_close(ci);
7442 b4996bee 2022-06-16 stsp } else {
7443 b4996bee 2022-06-16 stsp t = got_object_tag_get_tagger_time(tag);
7444 b4996bee 2022-06-16 stsp got_object_tag_close(tag);
7445 b4996bee 2022-06-16 stsp }
7446 b4996bee 2022-06-16 stsp free(id);
7447 b4996bee 2022-06-16 stsp if (gmtime_r(&t, &tm) == NULL)
7448 b4996bee 2022-06-16 stsp return got_error_from_errno("gmtime_r");
7449 b4996bee 2022-06-16 stsp if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7450 b4996bee 2022-06-16 stsp return got_error(GOT_ERR_NO_SPACE);
7451 b4996bee 2022-06-16 stsp }
7452 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7453 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s -> %s", s->show_date ?
7454 b4996bee 2022-06-16 stsp ymd : "", got_ref_get_name(re->ref),
7455 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7456 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7457 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7458 6458efa5 2020-11-24 stsp struct got_object_id *id;
7459 6458efa5 2020-11-24 stsp char *id_str;
7460 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7461 6458efa5 2020-11-24 stsp if (err)
7462 6458efa5 2020-11-24 stsp return err;
7463 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7464 6458efa5 2020-11-24 stsp if (err) {
7465 6458efa5 2020-11-24 stsp free(id);
7466 6458efa5 2020-11-24 stsp return err;
7467 6458efa5 2020-11-24 stsp }
7468 b4996bee 2022-06-16 stsp if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7469 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7470 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7471 6458efa5 2020-11-24 stsp free(id);
7472 6458efa5 2020-11-24 stsp free(id_str);
7473 6458efa5 2020-11-24 stsp return err;
7474 6458efa5 2020-11-24 stsp }
7475 6458efa5 2020-11-24 stsp free(id);
7476 6458efa5 2020-11-24 stsp free(id_str);
7477 b4996bee 2022-06-16 stsp } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7478 b4996bee 2022-06-16 stsp got_ref_get_name(re->ref)) == -1)
7479 b4996bee 2022-06-16 stsp return got_error_from_errno("asprintf");
7480 6458efa5 2020-11-24 stsp
7481 ccda2f4d 2022-06-16 stsp err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7482 ccda2f4d 2022-06-16 stsp 0, 0);
7483 6458efa5 2020-11-24 stsp if (err) {
7484 6458efa5 2020-11-24 stsp free(line);
7485 6458efa5 2020-11-24 stsp return err;
7486 6458efa5 2020-11-24 stsp }
7487 6458efa5 2020-11-24 stsp if (n == s->selected) {
7488 6458efa5 2020-11-24 stsp if (view->focussed)
7489 6458efa5 2020-11-24 stsp wstandout(view->window);
7490 6458efa5 2020-11-24 stsp s->selected_entry = re;
7491 6458efa5 2020-11-24 stsp }
7492 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7493 6458efa5 2020-11-24 stsp if (tc)
7494 6458efa5 2020-11-24 stsp wattr_on(view->window,
7495 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7496 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7497 6458efa5 2020-11-24 stsp if (tc)
7498 6458efa5 2020-11-24 stsp wattr_off(view->window,
7499 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7500 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7501 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7502 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7503 6458efa5 2020-11-24 stsp wstandend(view->window);
7504 6458efa5 2020-11-24 stsp free(line);
7505 6458efa5 2020-11-24 stsp free(wline);
7506 6458efa5 2020-11-24 stsp wline = NULL;
7507 6458efa5 2020-11-24 stsp n++;
7508 6458efa5 2020-11-24 stsp s->ndisplayed++;
7509 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7510 6458efa5 2020-11-24 stsp
7511 6458efa5 2020-11-24 stsp limit--;
7512 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7513 6458efa5 2020-11-24 stsp }
7514 6458efa5 2020-11-24 stsp
7515 9b058f45 2022-06-30 mark view_border(view);
7516 6458efa5 2020-11-24 stsp return err;
7517 6458efa5 2020-11-24 stsp }
7518 6458efa5 2020-11-24 stsp
7519 6458efa5 2020-11-24 stsp static const struct got_error *
7520 49b24ee5 2022-07-03 mark browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7521 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7522 c42c9805 2020-11-24 stsp {
7523 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7524 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7525 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7526 c42c9805 2020-11-24 stsp
7527 c42c9805 2020-11-24 stsp *new_view = NULL;
7528 c42c9805 2020-11-24 stsp
7529 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7530 c42c9805 2020-11-24 stsp if (err) {
7531 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7532 c42c9805 2020-11-24 stsp return err;
7533 c42c9805 2020-11-24 stsp else
7534 c42c9805 2020-11-24 stsp return NULL;
7535 c42c9805 2020-11-24 stsp }
7536 c42c9805 2020-11-24 stsp
7537 c42c9805 2020-11-24 stsp
7538 49b24ee5 2022-07-03 mark tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7539 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7540 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7541 c42c9805 2020-11-24 stsp goto done;
7542 c42c9805 2020-11-24 stsp }
7543 c42c9805 2020-11-24 stsp
7544 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7545 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7546 c42c9805 2020-11-24 stsp if (err)
7547 c42c9805 2020-11-24 stsp goto done;
7548 c42c9805 2020-11-24 stsp
7549 c42c9805 2020-11-24 stsp *new_view = tree_view;
7550 c42c9805 2020-11-24 stsp done:
7551 c42c9805 2020-11-24 stsp free(commit_id);
7552 c42c9805 2020-11-24 stsp return err;
7553 c42c9805 2020-11-24 stsp }
7554 c42c9805 2020-11-24 stsp static const struct got_error *
7555 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7556 6458efa5 2020-11-24 stsp {
7557 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7558 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7559 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7560 136e2bd4 2022-07-23 mark int n, nscroll = view->nlines - 1;
7561 6458efa5 2020-11-24 stsp
7562 6458efa5 2020-11-24 stsp switch (ch) {
7563 6458efa5 2020-11-24 stsp case 'i':
7564 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7565 640cd7ff 2022-06-22 mark view->count = 0;
7566 7f66531d 2021-11-16 stsp break;
7567 b4996bee 2022-06-16 stsp case 'm':
7568 b4996bee 2022-06-16 stsp s->show_date = !s->show_date;
7569 640cd7ff 2022-06-22 mark view->count = 0;
7570 b4996bee 2022-06-16 stsp break;
7571 07a065fe 2021-11-20 stsp case 'o':
7572 7f66531d 2021-11-16 stsp s->sort_by_date = !s->sort_by_date;
7573 640cd7ff 2022-06-22 mark view->count = 0;
7574 50617b77 2021-11-20 stsp err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7575 50617b77 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending :
7576 cc488aa7 2022-01-23 stsp tog_ref_cmp_by_name, s->repo);
7577 50617b77 2021-11-20 stsp if (err)
7578 50617b77 2021-11-20 stsp break;
7579 50617b77 2021-11-20 stsp got_reflist_object_id_map_free(tog_refs_idmap);
7580 50617b77 2021-11-20 stsp err = got_reflist_object_id_map_create(&tog_refs_idmap,
7581 50617b77 2021-11-20 stsp &tog_refs, s->repo);
7582 7f66531d 2021-11-16 stsp if (err)
7583 7f66531d 2021-11-16 stsp break;
7584 7f66531d 2021-11-16 stsp ref_view_free_refs(s);
7585 7f66531d 2021-11-16 stsp err = ref_view_load_refs(s);
7586 6458efa5 2020-11-24 stsp break;
7587 6458efa5 2020-11-24 stsp case KEY_ENTER:
7588 6458efa5 2020-11-24 stsp case '\r':
7589 640cd7ff 2022-06-22 mark view->count = 0;
7590 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7591 9b058f45 2022-06-30 mark break;
7592 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_LOG);
7593 6458efa5 2020-11-24 stsp break;
7594 5e98fb33 2022-07-22 mark case 'T':
7595 640cd7ff 2022-06-22 mark view->count = 0;
7596 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7597 c42c9805 2020-11-24 stsp break;
7598 136e2bd4 2022-07-23 mark err = view_request_new(new_view, view, TOG_VIEW_TREE);
7599 c42c9805 2020-11-24 stsp break;
7600 e4526bf5 2021-09-03 naddy case 'g':
7601 e4526bf5 2021-09-03 naddy case KEY_HOME:
7602 e4526bf5 2021-09-03 naddy s->selected = 0;
7603 640cd7ff 2022-06-22 mark view->count = 0;
7604 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7605 e4526bf5 2021-09-03 naddy break;
7606 e4526bf5 2021-09-03 naddy case 'G':
7607 9b058f45 2022-06-30 mark case KEY_END: {
7608 9b058f45 2022-06-30 mark int eos = view->nlines - 1;
7609 9b058f45 2022-06-30 mark
7610 9b058f45 2022-06-30 mark if (view->mode == TOG_VIEW_SPLIT_HRZN)
7611 9b058f45 2022-06-30 mark --eos; /* border */
7612 e4526bf5 2021-09-03 naddy s->selected = 0;
7613 640cd7ff 2022-06-22 mark view->count = 0;
7614 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7615 9b058f45 2022-06-30 mark for (n = 0; n < eos; n++) {
7616 e4526bf5 2021-09-03 naddy if (re == NULL)
7617 e4526bf5 2021-09-03 naddy break;
7618 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7619 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7620 e4526bf5 2021-09-03 naddy }
7621 e4526bf5 2021-09-03 naddy if (n > 0)
7622 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7623 e4526bf5 2021-09-03 naddy break;
7624 9b058f45 2022-06-30 mark }
7625 6458efa5 2020-11-24 stsp case 'k':
7626 6458efa5 2020-11-24 stsp case KEY_UP:
7627 02ffd0d5 2021-10-17 stsp case CTRL('p'):
7628 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7629 6458efa5 2020-11-24 stsp s->selected--;
7630 6458efa5 2020-11-24 stsp break;
7631 34ba6917 2020-11-30 stsp }
7632 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7633 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7634 640cd7ff 2022-06-22 mark view->count = 0;
7635 6458efa5 2020-11-24 stsp break;
7636 83cc4199 2022-06-13 stsp case CTRL('u'):
7637 33c3719a 2022-06-15 stsp case 'u':
7638 83cc4199 2022-06-13 stsp nscroll /= 2;
7639 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7640 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7641 6458efa5 2020-11-24 stsp case CTRL('b'):
7642 61417565 2022-06-20 mark case 'b':
7643 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7644 83cc4199 2022-06-13 stsp s->selected -= MIN(nscroll, s->selected);
7645 83cc4199 2022-06-13 stsp ref_scroll_up(s, MAX(0, nscroll));
7646 640cd7ff 2022-06-22 mark if (s->selected_entry == TAILQ_FIRST(&s->refs))
7647 640cd7ff 2022-06-22 mark view->count = 0;
7648 6458efa5 2020-11-24 stsp break;
7649 6458efa5 2020-11-24 stsp case 'j':
7650 6458efa5 2020-11-24 stsp case KEY_DOWN:
7651 02ffd0d5 2021-10-17 stsp case CTRL('n'):
7652 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7653 6458efa5 2020-11-24 stsp s->selected++;
7654 6458efa5 2020-11-24 stsp break;
7655 6458efa5 2020-11-24 stsp }
7656 640cd7ff 2022-06-22 mark if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7657 6458efa5 2020-11-24 stsp /* can't scroll any further */
7658 640cd7ff 2022-06-22 mark view->count = 0;
7659 6458efa5 2020-11-24 stsp break;
7660 640cd7ff 2022-06-22 mark }
7661 9b058f45 2022-06-30 mark ref_scroll_down(view, 1);
7662 6458efa5 2020-11-24 stsp break;
7663 83cc4199 2022-06-13 stsp case CTRL('d'):
7664 33c3719a 2022-06-15 stsp case 'd':
7665 83cc4199 2022-06-13 stsp nscroll /= 2;
7666 83cc4199 2022-06-13 stsp /* FALL THROUGH */
7667 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7668 6458efa5 2020-11-24 stsp case CTRL('f'):
7669 61417565 2022-06-20 mark case 'f':
7670 48bb96f0 2022-06-20 naddy case ' ':
7671 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7672 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7673 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7674 83cc4199 2022-06-13 stsp s->selected += MIN(nscroll,
7675 83cc4199 2022-06-13 stsp s->ndisplayed - s->selected - 1);
7676 640cd7ff 2022-06-22 mark if (view->count > 1 && s->selected < s->ndisplayed - 1)
7677 640cd7ff 2022-06-22 mark s->selected += s->ndisplayed - s->selected - 1;
7678 640cd7ff 2022-06-22 mark view->count = 0;
7679 6458efa5 2020-11-24 stsp break;
7680 6458efa5 2020-11-24 stsp }
7681 9b058f45 2022-06-30 mark ref_scroll_down(view, nscroll);
7682 6458efa5 2020-11-24 stsp break;
7683 6458efa5 2020-11-24 stsp case CTRL('l'):
7684 640cd7ff 2022-06-22 mark view->count = 0;
7685 8924d611 2020-12-26 stsp tog_free_refs();
7686 7f66531d 2021-11-16 stsp err = tog_load_refs(s->repo, s->sort_by_date);
7687 8924d611 2020-12-26 stsp if (err)
7688 8924d611 2020-12-26 stsp break;
7689 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7690 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7691 6458efa5 2020-11-24 stsp break;
7692 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7693 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7694 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7695 6458efa5 2020-11-24 stsp break;
7696 6458efa5 2020-11-24 stsp default:
7697 640cd7ff 2022-06-22 mark view->count = 0;
7698 6458efa5 2020-11-24 stsp break;
7699 6458efa5 2020-11-24 stsp }
7700 6458efa5 2020-11-24 stsp
7701 6458efa5 2020-11-24 stsp return err;
7702 6458efa5 2020-11-24 stsp }
7703 6458efa5 2020-11-24 stsp
7704 6458efa5 2020-11-24 stsp __dead static void
7705 6458efa5 2020-11-24 stsp usage_ref(void)
7706 6458efa5 2020-11-24 stsp {
7707 6458efa5 2020-11-24 stsp endwin();
7708 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7709 6458efa5 2020-11-24 stsp getprogname());
7710 6458efa5 2020-11-24 stsp exit(1);
7711 6458efa5 2020-11-24 stsp }
7712 6458efa5 2020-11-24 stsp
7713 6458efa5 2020-11-24 stsp static const struct got_error *
7714 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7715 6458efa5 2020-11-24 stsp {
7716 6458efa5 2020-11-24 stsp const struct got_error *error;
7717 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7718 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7719 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7720 6458efa5 2020-11-24 stsp int ch;
7721 6458efa5 2020-11-24 stsp struct tog_view *view;
7722 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
7723 6458efa5 2020-11-24 stsp
7724 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7725 6458efa5 2020-11-24 stsp switch (ch) {
7726 6458efa5 2020-11-24 stsp case 'r':
7727 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7728 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7729 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7730 6458efa5 2020-11-24 stsp optarg);
7731 6458efa5 2020-11-24 stsp break;
7732 6458efa5 2020-11-24 stsp default:
7733 e99e2d15 2020-11-24 naddy usage_ref();
7734 6458efa5 2020-11-24 stsp /* NOTREACHED */
7735 6458efa5 2020-11-24 stsp }
7736 6458efa5 2020-11-24 stsp }
7737 6458efa5 2020-11-24 stsp
7738 6458efa5 2020-11-24 stsp argc -= optind;
7739 6458efa5 2020-11-24 stsp argv += optind;
7740 6458efa5 2020-11-24 stsp
7741 6458efa5 2020-11-24 stsp if (argc > 1)
7742 e99e2d15 2020-11-24 naddy usage_ref();
7743 0ae84acc 2022-06-15 tracey
7744 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
7745 0ae84acc 2022-06-15 tracey if (error != NULL)
7746 0ae84acc 2022-06-15 tracey goto done;
7747 6458efa5 2020-11-24 stsp
7748 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7749 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7750 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7751 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7752 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7753 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7754 c156c7a4 2020-12-18 stsp goto done;
7755 6458efa5 2020-11-24 stsp if (worktree)
7756 6458efa5 2020-11-24 stsp repo_path =
7757 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7758 6458efa5 2020-11-24 stsp else
7759 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7760 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7761 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7762 c156c7a4 2020-12-18 stsp goto done;
7763 c156c7a4 2020-12-18 stsp }
7764 6458efa5 2020-11-24 stsp }
7765 6458efa5 2020-11-24 stsp
7766 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7767 6458efa5 2020-11-24 stsp if (error != NULL)
7768 6458efa5 2020-11-24 stsp goto done;
7769 6458efa5 2020-11-24 stsp
7770 6458efa5 2020-11-24 stsp init_curses();
7771 6458efa5 2020-11-24 stsp
7772 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7773 51a10b52 2020-12-26 stsp if (error)
7774 51a10b52 2020-12-26 stsp goto done;
7775 51a10b52 2020-12-26 stsp
7776 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
7777 6458efa5 2020-11-24 stsp if (error)
7778 6458efa5 2020-11-24 stsp goto done;
7779 6458efa5 2020-11-24 stsp
7780 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7781 6458efa5 2020-11-24 stsp if (view == NULL) {
7782 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7783 6458efa5 2020-11-24 stsp goto done;
7784 6458efa5 2020-11-24 stsp }
7785 6458efa5 2020-11-24 stsp
7786 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7787 6458efa5 2020-11-24 stsp if (error)
7788 6458efa5 2020-11-24 stsp goto done;
7789 6458efa5 2020-11-24 stsp
7790 6458efa5 2020-11-24 stsp if (worktree) {
7791 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7792 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7793 6458efa5 2020-11-24 stsp worktree = NULL;
7794 6458efa5 2020-11-24 stsp }
7795 6458efa5 2020-11-24 stsp error = view_loop(view);
7796 6458efa5 2020-11-24 stsp done:
7797 6458efa5 2020-11-24 stsp free(repo_path);
7798 6458efa5 2020-11-24 stsp free(cwd);
7799 1d0f4054 2021-06-17 stsp if (repo) {
7800 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7801 1d0f4054 2021-06-17 stsp if (close_err)
7802 1d0f4054 2021-06-17 stsp error = close_err;
7803 1d0f4054 2021-06-17 stsp }
7804 0ae84acc 2022-06-15 tracey if (pack_fds) {
7805 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
7806 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
7807 0ae84acc 2022-06-15 tracey if (error == NULL)
7808 0ae84acc 2022-06-15 tracey error = pack_err;
7809 0ae84acc 2022-06-15 tracey }
7810 51a10b52 2020-12-26 stsp tog_free_refs();
7811 6458efa5 2020-11-24 stsp return error;
7812 6458efa5 2020-11-24 stsp }
7813 6458efa5 2020-11-24 stsp
7814 136e2bd4 2022-07-23 mark static const struct got_error *
7815 136e2bd4 2022-07-23 mark view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7816 136e2bd4 2022-07-23 mark enum tog_view_type request, int y, int x)
7817 136e2bd4 2022-07-23 mark {
7818 136e2bd4 2022-07-23 mark const struct got_error *err = NULL;
7819 136e2bd4 2022-07-23 mark
7820 136e2bd4 2022-07-23 mark *new_view = NULL;
7821 136e2bd4 2022-07-23 mark
7822 136e2bd4 2022-07-23 mark switch (request) {
7823 136e2bd4 2022-07-23 mark case TOG_VIEW_DIFF:
7824 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG) {
7825 136e2bd4 2022-07-23 mark struct tog_log_view_state *s = &view->state.log;
7826 136e2bd4 2022-07-23 mark
7827 136e2bd4 2022-07-23 mark err = open_diff_view_for_commit(new_view, y, x,
7828 136e2bd4 2022-07-23 mark s->selected_entry->commit, s->selected_entry->id,
7829 136e2bd4 2022-07-23 mark view, s->repo);
7830 136e2bd4 2022-07-23 mark } else
7831 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7832 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7833 136e2bd4 2022-07-23 mark break;
7834 136e2bd4 2022-07-23 mark case TOG_VIEW_BLAME:
7835 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_TREE) {
7836 136e2bd4 2022-07-23 mark struct tog_tree_view_state *s = &view->state.tree;
7837 136e2bd4 2022-07-23 mark
7838 136e2bd4 2022-07-23 mark err = blame_tree_entry(new_view, y, x,
7839 136e2bd4 2022-07-23 mark s->selected_entry, &s->parents, s->commit_id,
7840 136e2bd4 2022-07-23 mark s->repo);
7841 136e2bd4 2022-07-23 mark } else
7842 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7843 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7844 136e2bd4 2022-07-23 mark break;
7845 136e2bd4 2022-07-23 mark case TOG_VIEW_LOG:
7846 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_BLAME)
7847 136e2bd4 2022-07-23 mark err = log_annotated_line(new_view, y, x,
7848 136e2bd4 2022-07-23 mark view->state.blame.repo, view->state.blame.id_to_log);
7849 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
7850 136e2bd4 2022-07-23 mark err = log_selected_tree_entry(new_view, y, x,
7851 136e2bd4 2022-07-23 mark &view->state.tree);
7852 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
7853 136e2bd4 2022-07-23 mark err = log_ref_entry(new_view, y, x,
7854 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
7855 136e2bd4 2022-07-23 mark view->state.ref.repo);
7856 136e2bd4 2022-07-23 mark else
7857 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7858 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7859 136e2bd4 2022-07-23 mark break;
7860 136e2bd4 2022-07-23 mark case TOG_VIEW_TREE:
7861 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
7862 136e2bd4 2022-07-23 mark err = browse_commit_tree(new_view, y, x,
7863 136e2bd4 2022-07-23 mark view->state.log.selected_entry,
7864 136e2bd4 2022-07-23 mark view->state.log.in_repo_path,
7865 136e2bd4 2022-07-23 mark view->state.log.head_ref_name,
7866 136e2bd4 2022-07-23 mark view->state.log.repo);
7867 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_REF)
7868 136e2bd4 2022-07-23 mark err = browse_ref_tree(new_view, y, x,
7869 136e2bd4 2022-07-23 mark view->state.ref.selected_entry,
7870 136e2bd4 2022-07-23 mark view->state.ref.repo);
7871 136e2bd4 2022-07-23 mark else
7872 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL,
7873 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7874 136e2bd4 2022-07-23 mark break;
7875 136e2bd4 2022-07-23 mark case TOG_VIEW_REF:
7876 136e2bd4 2022-07-23 mark *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7877 136e2bd4 2022-07-23 mark if (*new_view == NULL)
7878 136e2bd4 2022-07-23 mark return got_error_from_errno("view_open");
7879 136e2bd4 2022-07-23 mark if (view->type == TOG_VIEW_LOG)
7880 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.log.repo);
7881 136e2bd4 2022-07-23 mark else if (view->type == TOG_VIEW_TREE)
7882 136e2bd4 2022-07-23 mark err = open_ref_view(*new_view, view->state.tree.repo);
7883 136e2bd4 2022-07-23 mark else
7884 136e2bd4 2022-07-23 mark err = got_error_msg(GOT_ERR_NOT_IMPL,
7885 136e2bd4 2022-07-23 mark "parent/child view pair not supported");
7886 136e2bd4 2022-07-23 mark if (err)
7887 136e2bd4 2022-07-23 mark view_close(*new_view);
7888 136e2bd4 2022-07-23 mark break;
7889 136e2bd4 2022-07-23 mark default:
7890 136e2bd4 2022-07-23 mark return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7891 136e2bd4 2022-07-23 mark }
7892 136e2bd4 2022-07-23 mark
7893 136e2bd4 2022-07-23 mark return err;
7894 136e2bd4 2022-07-23 mark }
7895 136e2bd4 2022-07-23 mark
7896 9b058f45 2022-06-30 mark /*
7897 9b058f45 2022-06-30 mark * If view was scrolled down to move the selected line into view when opening a
7898 9b058f45 2022-06-30 mark * horizontal split, scroll back up when closing the split/toggling fullscreen.
7899 9b058f45 2022-06-30 mark */
7900 6458efa5 2020-11-24 stsp static void
7901 9b058f45 2022-06-30 mark offset_selection_up(struct tog_view *view)
7902 9b058f45 2022-06-30 mark {
7903 9b058f45 2022-06-30 mark switch (view->type) {
7904 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7905 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7906 9b058f45 2022-06-30 mark if (s->first_displayed_line == 1) {
7907 9b058f45 2022-06-30 mark s->selected_line = MAX(s->selected_line - view->offset,
7908 9b058f45 2022-06-30 mark 1);
7909 9b058f45 2022-06-30 mark break;
7910 9b058f45 2022-06-30 mark }
7911 9b058f45 2022-06-30 mark if (s->first_displayed_line > view->offset)
7912 9b058f45 2022-06-30 mark s->first_displayed_line -= view->offset;
7913 9b058f45 2022-06-30 mark else
7914 9b058f45 2022-06-30 mark s->first_displayed_line = 1;
7915 9b058f45 2022-06-30 mark s->selected_line += view->offset;
7916 9b058f45 2022-06-30 mark break;
7917 9b058f45 2022-06-30 mark }
7918 9b058f45 2022-06-30 mark case TOG_VIEW_LOG:
7919 9b058f45 2022-06-30 mark log_scroll_up(&view->state.log, view->offset);
7920 9b058f45 2022-06-30 mark view->state.log.selected += view->offset;
7921 9b058f45 2022-06-30 mark break;
7922 9b058f45 2022-06-30 mark case TOG_VIEW_REF:
7923 9b058f45 2022-06-30 mark ref_scroll_up(&view->state.ref, view->offset);
7924 9b058f45 2022-06-30 mark view->state.ref.selected += view->offset;
7925 9b058f45 2022-06-30 mark break;
7926 9b058f45 2022-06-30 mark case TOG_VIEW_TREE:
7927 9b058f45 2022-06-30 mark tree_scroll_up(&view->state.tree, view->offset);
7928 9b058f45 2022-06-30 mark view->state.tree.selected += view->offset;
7929 9b058f45 2022-06-30 mark break;
7930 9b058f45 2022-06-30 mark default:
7931 9b058f45 2022-06-30 mark break;
7932 9b058f45 2022-06-30 mark }
7933 9b058f45 2022-06-30 mark
7934 9b058f45 2022-06-30 mark view->offset = 0;
7935 9b058f45 2022-06-30 mark }
7936 9b058f45 2022-06-30 mark
7937 9b058f45 2022-06-30 mark /*
7938 9b058f45 2022-06-30 mark * If the selected line is in the section of screen covered by the bottom split,
7939 9b058f45 2022-06-30 mark * scroll down offset lines to move it into view and index its new position.
7940 9b058f45 2022-06-30 mark */
7941 9b058f45 2022-06-30 mark static const struct got_error *
7942 9b058f45 2022-06-30 mark offset_selection_down(struct tog_view *view)
7943 9b058f45 2022-06-30 mark {
7944 9b058f45 2022-06-30 mark const struct got_error *err = NULL;
7945 9b058f45 2022-06-30 mark const struct got_error *(*scrolld)(struct tog_view *, int);
7946 9b058f45 2022-06-30 mark int *selected = NULL;
7947 9b058f45 2022-06-30 mark int header, offset;
7948 9b058f45 2022-06-30 mark
7949 9b058f45 2022-06-30 mark switch (view->type) {
7950 9b058f45 2022-06-30 mark case TOG_VIEW_BLAME: {
7951 9b058f45 2022-06-30 mark struct tog_blame_view_state *s = &view->state.blame;
7952 9b058f45 2022-06-30 mark header = 3;
7953 9b058f45 2022-06-30 mark scrolld = NULL;
7954 9b058f45 2022-06-30 mark if (s->selected_line > view->nlines - header) {
7955 9b058f45 2022-06-30 mark offset = abs(view->nlines - s->selected_line - header);
7956 9b058f45 2022-06-30 mark s->first_displayed_line += offset;
7957 9b058f45 2022-06-30 mark s->selected_line -= offset;
7958 9b058f45 2022-06-30 mark view->offset = offset;
7959 9b058f45 2022-06-30 mark }
7960 9b058f45 2022-06-30 mark break;
7961 9b058f45 2022-06-30 mark }
7962 9b058f45 2022-06-30 mark case TOG_VIEW_LOG: {
7963 9b058f45 2022-06-30 mark struct tog_log_view_state *s = &view->state.log;
7964 9b058f45 2022-06-30 mark scrolld = &log_scroll_down;
7965 d2366e29 2022-07-07 mark header = view_is_parent_view(view) ? 3 : 2;
7966 9b058f45 2022-06-30 mark selected = &s->selected;
7967 9b058f45 2022-06-30 mark break;
7968 9b058f45 2022-06-30 mark }
7969 9b058f45 2022-06-30 mark case TOG_VIEW_REF: {
7970 9b058f45 2022-06-30 mark struct tog_ref_view_state *s = &view->state.ref;
7971 9b058f45 2022-06-30 mark scrolld = &ref_scroll_down;
7972 9b058f45 2022-06-30 mark header = 3;
7973 9b058f45 2022-06-30 mark selected = &s->selected;
7974 9b058f45 2022-06-30 mark break;
7975 9b058f45 2022-06-30 mark }
7976 9b058f45 2022-06-30 mark case TOG_VIEW_TREE: {
7977 9b058f45 2022-06-30 mark struct tog_tree_view_state *s = &view->state.tree;
7978 9b058f45 2022-06-30 mark scrolld = &tree_scroll_down;
7979 9b058f45 2022-06-30 mark header = 5;
7980 9b058f45 2022-06-30 mark selected = &s->selected;
7981 9b058f45 2022-06-30 mark break;
7982 9b058f45 2022-06-30 mark }
7983 9b058f45 2022-06-30 mark default:
7984 9b058f45 2022-06-30 mark selected = NULL;
7985 9b058f45 2022-06-30 mark scrolld = NULL;
7986 9b058f45 2022-06-30 mark header = 0;
7987 9b058f45 2022-06-30 mark break;
7988 9b058f45 2022-06-30 mark }
7989 9b058f45 2022-06-30 mark
7990 9b058f45 2022-06-30 mark if (selected && *selected > view->nlines - header) {
7991 9b058f45 2022-06-30 mark offset = abs(view->nlines - *selected - header);
7992 9b058f45 2022-06-30 mark view->offset = offset;
7993 9b058f45 2022-06-30 mark if (scrolld && offset) {
7994 9b058f45 2022-06-30 mark err = scrolld(view, offset);
7995 9b058f45 2022-06-30 mark *selected -= offset;
7996 9b058f45 2022-06-30 mark }
7997 9b058f45 2022-06-30 mark }
7998 9b058f45 2022-06-30 mark
7999 9b058f45 2022-06-30 mark return err;
8000 9b058f45 2022-06-30 mark }
8001 9b058f45 2022-06-30 mark
8002 9b058f45 2022-06-30 mark static void
8003 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8004 ce5b7c56 2019-07-09 stsp {
8005 6059809a 2020-12-17 stsp size_t i;
8006 9f7d7167 2018-04-29 stsp
8007 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8008 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8009 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = &tog_commands[i];
8010 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8011 ce5b7c56 2019-07-09 stsp }
8012 6879ba42 2020-10-01 naddy fputc('\n', fp);
8013 ce5b7c56 2019-07-09 stsp }
8014 ce5b7c56 2019-07-09 stsp
8015 4ed7e80c 2018-05-20 stsp __dead static void
8016 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8017 9f7d7167 2018-04-29 stsp {
8018 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8019 6879ba42 2020-10-01 naddy
8020 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8021 6879ba42 2020-10-01 naddy getprogname());
8022 6879ba42 2020-10-01 naddy if (hflag) {
8023 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8024 6879ba42 2020-10-01 naddy list_commands(fp);
8025 ee85c5e8 2020-02-29 stsp }
8026 6879ba42 2020-10-01 naddy exit(status);
8027 9f7d7167 2018-04-29 stsp }
8028 9f7d7167 2018-04-29 stsp
8029 c2301be8 2018-04-30 stsp static char **
8030 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8031 c2301be8 2018-04-30 stsp {
8032 ee85c5e8 2020-02-29 stsp va_list ap;
8033 c2301be8 2018-04-30 stsp char **argv;
8034 ee85c5e8 2020-02-29 stsp int i;
8035 c2301be8 2018-04-30 stsp
8036 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8037 ee85c5e8 2020-02-29 stsp
8038 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8039 c2301be8 2018-04-30 stsp if (argv == NULL)
8040 c2301be8 2018-04-30 stsp err(1, "calloc");
8041 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8042 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8043 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8044 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8045 c2301be8 2018-04-30 stsp }
8046 c2301be8 2018-04-30 stsp
8047 ee85c5e8 2020-02-29 stsp va_end(ap);
8048 c2301be8 2018-04-30 stsp return argv;
8049 ee85c5e8 2020-02-29 stsp }
8050 ee85c5e8 2020-02-29 stsp
8051 ee85c5e8 2020-02-29 stsp /*
8052 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8053 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8054 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8055 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8056 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8057 ee85c5e8 2020-02-29 stsp */
8058 ee85c5e8 2020-02-29 stsp static const struct got_error *
8059 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8060 ee85c5e8 2020-02-29 stsp {
8061 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8062 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8063 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8064 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8065 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8066 a44927cc 2022-04-07 stsp struct got_commit_object *commit = NULL;
8067 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8068 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8069 0ae84acc 2022-06-15 tracey int *pack_fds = NULL;
8070 84de9106 2020-12-26 stsp
8071 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8072 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8073 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8074 ee85c5e8 2020-02-29 stsp
8075 0ae84acc 2022-06-15 tracey error = got_repo_pack_fds_open(&pack_fds);
8076 0ae84acc 2022-06-15 tracey if (error != NULL)
8077 0ae84acc 2022-06-15 tracey goto done;
8078 0ae84acc 2022-06-15 tracey
8079 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8080 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8081 ee85c5e8 2020-02-29 stsp goto done;
8082 ee85c5e8 2020-02-29 stsp
8083 ee85c5e8 2020-02-29 stsp if (worktree)
8084 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8085 ee85c5e8 2020-02-29 stsp else
8086 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8087 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8088 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8089 ee85c5e8 2020-02-29 stsp goto done;
8090 ee85c5e8 2020-02-29 stsp }
8091 ee85c5e8 2020-02-29 stsp
8092 0ae84acc 2022-06-15 tracey error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8093 ee85c5e8 2020-02-29 stsp if (error != NULL)
8094 ee85c5e8 2020-02-29 stsp goto done;
8095 ee85c5e8 2020-02-29 stsp
8096 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8097 ee85c5e8 2020-02-29 stsp repo, worktree);
8098 ee85c5e8 2020-02-29 stsp if (error)
8099 ee85c5e8 2020-02-29 stsp goto done;
8100 ee85c5e8 2020-02-29 stsp
8101 7f66531d 2021-11-16 stsp error = tog_load_refs(repo, 0);
8102 84de9106 2020-12-26 stsp if (error)
8103 84de9106 2020-12-26 stsp goto done;
8104 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8105 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8106 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8107 ee85c5e8 2020-02-29 stsp if (error)
8108 ee85c5e8 2020-02-29 stsp goto done;
8109 ee85c5e8 2020-02-29 stsp
8110 ee85c5e8 2020-02-29 stsp if (worktree) {
8111 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8112 ee85c5e8 2020-02-29 stsp worktree = NULL;
8113 ee85c5e8 2020-02-29 stsp }
8114 ee85c5e8 2020-02-29 stsp
8115 a44927cc 2022-04-07 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8116 a44927cc 2022-04-07 stsp if (error)
8117 a44927cc 2022-04-07 stsp goto done;
8118 a44927cc 2022-04-07 stsp
8119 a44927cc 2022-04-07 stsp error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8120 ee85c5e8 2020-02-29 stsp if (error) {
8121 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8122 ee85c5e8 2020-02-29 stsp goto done;
8123 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8124 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8125 6879ba42 2020-10-01 naddy usage(1, 1);
8126 ee85c5e8 2020-02-29 stsp /* not reached */
8127 ee85c5e8 2020-02-29 stsp }
8128 ee85c5e8 2020-02-29 stsp
8129 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8130 1d0f4054 2021-06-17 stsp if (error == NULL)
8131 1d0f4054 2021-06-17 stsp error = close_err;
8132 ee85c5e8 2020-02-29 stsp repo = NULL;
8133 ee85c5e8 2020-02-29 stsp
8134 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8135 ee85c5e8 2020-02-29 stsp if (error)
8136 ee85c5e8 2020-02-29 stsp goto done;
8137 ee85c5e8 2020-02-29 stsp
8138 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8139 ee85c5e8 2020-02-29 stsp argc = 4;
8140 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8141 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8142 ee85c5e8 2020-02-29 stsp done:
8143 1d0f4054 2021-06-17 stsp if (repo) {
8144 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8145 1d0f4054 2021-06-17 stsp if (error == NULL)
8146 1d0f4054 2021-06-17 stsp error = close_err;
8147 1d0f4054 2021-06-17 stsp }
8148 a44927cc 2022-04-07 stsp if (commit)
8149 a44927cc 2022-04-07 stsp got_object_commit_close(commit);
8150 ee85c5e8 2020-02-29 stsp if (worktree)
8151 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8152 0ae84acc 2022-06-15 tracey if (pack_fds) {
8153 0ae84acc 2022-06-15 tracey const struct got_error *pack_err =
8154 0ae84acc 2022-06-15 tracey got_repo_pack_fds_close(pack_fds);
8155 0ae84acc 2022-06-15 tracey if (error == NULL)
8156 0ae84acc 2022-06-15 tracey error = pack_err;
8157 0ae84acc 2022-06-15 tracey }
8158 ee85c5e8 2020-02-29 stsp free(id);
8159 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8160 ee85c5e8 2020-02-29 stsp free(commit_id);
8161 ee85c5e8 2020-02-29 stsp free(cwd);
8162 ee85c5e8 2020-02-29 stsp free(repo_path);
8163 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8164 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8165 ee85c5e8 2020-02-29 stsp int i;
8166 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8167 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8168 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8169 ee85c5e8 2020-02-29 stsp }
8170 87670572 2020-12-26 naddy tog_free_refs();
8171 ee85c5e8 2020-02-29 stsp return error;
8172 c2301be8 2018-04-30 stsp }
8173 c2301be8 2018-04-30 stsp
8174 9f7d7167 2018-04-29 stsp int
8175 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8176 9f7d7167 2018-04-29 stsp {
8177 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8178 3e166534 2022-02-16 naddy const struct tog_cmd *cmd = NULL;
8179 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8180 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8181 3e166534 2022-02-16 naddy static const struct option longopts[] = {
8182 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8183 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8184 83cd27f8 2020-01-13 stsp };
8185 917d79a7 2022-07-01 stsp char *diff_algo_str = NULL;
8186 9f7d7167 2018-04-29 stsp
8187 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8188 9f7d7167 2018-04-29 stsp
8189 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8190 9f7d7167 2018-04-29 stsp switch (ch) {
8191 9f7d7167 2018-04-29 stsp case 'h':
8192 9f7d7167 2018-04-29 stsp hflag = 1;
8193 9f7d7167 2018-04-29 stsp break;
8194 53ccebc2 2019-07-30 stsp case 'V':
8195 53ccebc2 2019-07-30 stsp Vflag = 1;
8196 53ccebc2 2019-07-30 stsp break;
8197 9f7d7167 2018-04-29 stsp default:
8198 6879ba42 2020-10-01 naddy usage(hflag, 1);
8199 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8200 9f7d7167 2018-04-29 stsp }
8201 9f7d7167 2018-04-29 stsp }
8202 9f7d7167 2018-04-29 stsp
8203 9f7d7167 2018-04-29 stsp argc -= optind;
8204 9f7d7167 2018-04-29 stsp argv += optind;
8205 9814e6a3 2020-09-27 naddy optind = 1;
8206 c2301be8 2018-04-30 stsp optreset = 1;
8207 9f7d7167 2018-04-29 stsp
8208 53ccebc2 2019-07-30 stsp if (Vflag) {
8209 53ccebc2 2019-07-30 stsp got_version_print_str();
8210 6879ba42 2020-10-01 naddy return 0;
8211 53ccebc2 2019-07-30 stsp }
8212 4010e238 2020-12-04 stsp
8213 4010e238 2020-12-04 stsp #ifndef PROFILE
8214 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8215 4010e238 2020-12-04 stsp NULL) == -1)
8216 4010e238 2020-12-04 stsp err(1, "pledge");
8217 4010e238 2020-12-04 stsp #endif
8218 53ccebc2 2019-07-30 stsp
8219 c2301be8 2018-04-30 stsp if (argc == 0) {
8220 f29d3e89 2018-06-23 stsp if (hflag)
8221 6879ba42 2020-10-01 naddy usage(hflag, 0);
8222 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8223 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8224 c2301be8 2018-04-30 stsp argc = 1;
8225 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8226 c2301be8 2018-04-30 stsp } else {
8227 6059809a 2020-12-17 stsp size_t i;
8228 9f7d7167 2018-04-29 stsp
8229 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8230 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8231 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8232 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8233 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8234 9f7d7167 2018-04-29 stsp break;
8235 9f7d7167 2018-04-29 stsp }
8236 9f7d7167 2018-04-29 stsp }
8237 ee85c5e8 2020-02-29 stsp }
8238 3642c4c6 2019-07-09 stsp
8239 917d79a7 2022-07-01 stsp diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8240 917d79a7 2022-07-01 stsp if (diff_algo_str) {
8241 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "patience") == 0)
8242 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8243 917d79a7 2022-07-01 stsp if (strcasecmp(diff_algo_str, "myers") == 0)
8244 917d79a7 2022-07-01 stsp tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8245 917d79a7 2022-07-01 stsp }
8246 917d79a7 2022-07-01 stsp
8247 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8248 ee85c5e8 2020-02-29 stsp if (argc != 1)
8249 6879ba42 2020-10-01 naddy usage(0, 1);
8250 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8251 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8252 ee85c5e8 2020-02-29 stsp } else {
8253 ee85c5e8 2020-02-29 stsp if (hflag)
8254 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8255 ee85c5e8 2020-02-29 stsp else
8256 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8257 9f7d7167 2018-04-29 stsp }
8258 9f7d7167 2018-04-29 stsp
8259 9f7d7167 2018-04-29 stsp endwin();
8260 b46c1e04 2020-09-20 naddy putchar('\n');
8261 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8262 a2f4a359 2020-02-28 stsp int i;
8263 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8264 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8265 a2f4a359 2020-02-28 stsp free(cmd_argv);
8266 a2f4a359 2020-02-28 stsp }
8267 a2f4a359 2020-02-28 stsp
8268 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8269 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8270 9f7d7167 2018-04-29 stsp return 0;
8271 9f7d7167 2018-04-29 stsp }